This code demonstrates how to do some basic control of a lego nxt robot via a bluetooth connection. It uses the NKH Mindsqualls libraries available for free download and used to comunicate with a lego nxt brick. Search Mindsqalls in Google for the original download.
I was asked by a friend who just got his first Lego Mindstorms kit to produce a basic program in C# to show how to use C# and the Mindsqalls libraries. So here it is for anyone else who is having any problems.
Using the Code
My code doesn't improve the NKH Mindsqalls classes but my Remote Control Panel and associated functions could be re-used in other programs.
This program is a basic application which has only two main parts:
MainForm - The main Windows form
- Remote Control Panel - A user control I made that deals with running the robots motors
So how does it work. When the application starts, it runs
MainForm has 5 controls:
- 1. Remote Control Panel - The user control that deals with running the motors
- 2. Status Label - Tells the user what the program is doing
- 3 - 5. Sensor status labels - The statuses of Sensors 1 to 3 are displayed in these labels.
MainForm runs, it loops through all the available com ports it can find trying to connect to an nxt. If no connection can be made, it tells the user and then exits as without a connection, the program is rendered useless and may as well exit. If a connection is made, the program sets up sensors 1 and 2 as touch sensors and sensor 3 as an ultrasonic sensor. For reasons unknown to me, NKH Mindsqualls libraries provide no function for determining what type of sensor is connected to any given port nor do the libraries support the standard colour sensor. For this reason, the type of sensors set up are hard coded and sensor 4 is left off for the obvious reason that many people only have the standard kit of which the fourth sensor (colour sensor) is not supported.
If all that is completed successfully, the application enables the Remote Control Panel and tells the user via the status label that an nxt is now connected. (To disconnect, the user must close the application.) The application will also warn the user every 30 seconds if their robot has low battery as bluetooth uses quite a lot of power.
The Remote Control Panel has two parts:
- Running Controls
The setup can be changed at any time while the user is connected to their robot. The setup works on the idea that every time users wants to run a motor, they click the relevant button, the application checks the current settings against the last settings, if they are the same the motor(s) run(s) immediately; however, if they are different then all the motors are reset to the current settings and then the motors are run. In this way, the user can change quickly and easily which motors are drive motors and which (if any) is the auxiliary motor along with settings like drive power and turn ratio.
The running controls are separated into two parts:
- Drive Motors (2 or 3)
- Auxiliary Motor (1 or none) - Can also be set as steering
There have to be at least two drive motors, but there can be three. If there are only two drive motors, the code makes use of the Mindsqualls
MotorPair class. This pairs two motors and guarantees (if not debugging while running the application) that the two motors will run in perfect sync with each other. If there are three drive motors, then the code manually tries to run them synchronously but unison is not guaranteed.
There does not have to be an auxiliary motor and if all three motors are set as drive motors, there isn't an auxiliary motor. If there is a spare motor, it is set as the auxiliary motor and has its own separate set of controls. However, the auxiliary motor can also be set to be used as steering so when the user wants to turn left or right, the auxiliary motor is used like a steering wheel.
For any robot programmer it's fairly basic, but if you are new to robot programming don't be put-off by it looking complicated. Most of it is the same code but with an option changed each time the code repeats. Have fun!
Points of Interest
I found it annoying that there was no way to detect what type of sensor was actually connected to any given port and even more frustrating that the standard colour sensor isn't supported. Hopefully this will be fixed in the future :/.
- 28th August, 2010: Initial post