Today, as I was on duty for tech support, I had lots of time to try something new. Being equipped with a Samsung Galaxy S i9001 smartphone, I told myself that it must be possible to connect the thing to a LEGO Mindstorms NXT programmable brick via a Bluetooth Serial Port Profile (SPP) connection. As I'm not really deep into Android programming yet, I wanted to use MIT's AppInventor (download local component for Windows here) to create a control application for the phone. I intended to let my app read the orientation sensor's values and use them to set the output power for some motors accordingly.
First, I paired the NXT with my phone using the Bluetooth system settings panel. This step is neccessary, as pairing new devices is impossible from inside an app. The NXT got two motors connected to ports B and C.
Second, I created my app's screen in the AppInventor. To control the NXT, I had to add a BluetoothClient and an NxtDrive. The NxtDrive uses the BluetoothClient to communicate with the NXT, so the NxtDrive must be told which instance of BluetoothClient to use within the screen designer! Then there's the OrientationSensor the measure how your phone is tilted at the moment.
I also added three labels to display the orientation values and a list picker. The list picker is used to connect to a specfic Bluetooth device via SPP. As I said, the device must be paired outside the app. The SPP connection itself is created from within the app.
So here's a screenshot of my very simple screen layout:
NXT control screen layout |
NxtDrive property rollout: Note that the value of "BluetoothClient" is set to "BluetoothClient1" |
Somehow it's neccessary to save the result of the call to BluetoothClient1.Connect in a variable, so that's what I did here, but I don't use the result anywhere.
Finally, the orientation values have to be read continuously and the motor power has to be set accordingly. I just use the "roll" readings and adjust for signedness, as I don't know whether MoveForwardIndefinitely can take any negative values for its power setting. So, if the "roll" value is negative, I tell the NXT motors to rotate backward, otherwise, they'll have to move forward (whatever that means...). I also update the labels to show the orientation values.
Here's what it looks like:
Last, but not least, here's a video showing you how to operate the app:
can you send me this app?
AntwortenLöschenSure! Just write me an email to mobicebear@googlemail.com and I'll mail it back to you.
AntwortenLöschenHey.
AntwortenLöschenAt first i have to say that the app is really cool.But what is the definition of roll? It goes forard as long as you nick the smartphone on the left or on the right?
And i also have a question about the Bluetooth cilent:
I download the application on m phone and add bluetooth and then???
I wasn't able to find out how it works. My email adress is phiiilll64@gmail.com
Thank you :)
ei friend i have a question, where is the bt_con_ok... or what is that? i cant see it in the designer
AntwortenLöschenbt_con_ok is a global variable. Please take a look at the third screenshot to see how it is defined ("def bt_con_ok as false"). Are you using AppInventor 2? This is for the older version which is completedly different in some aspects.
AntwortenLöschenyeah friend! i am using the lastest version of app inventor! that's why i have some complications.... i am doing this app 'couse this is soo interesting... and i'd like it to present in my school.... need your help..
AntwortenLöschendoes this work with EV3?
AntwortenLöschenWell, no. However, you can still use the Bluetooth Client control to connect to an EV3, as demonstrated here (didn't test it myself, though):
Löschenhttp://tinyurl.com/le868rq