Click here to Skip to main content
15,942,934 members
Articles / Web Development / ASP.NET

Basic Robot Control using NKH Mindsqualls Libraries

Rate me:
Please Sign up or sign in to vote.
4.69/5 (6 votes)
28 Aug 2010CPOL4 min read 37K   1.4K   14   5
Demonstration of how to do basic robot remote control via bluetooth using NKH Mindsqualls libraries.


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:

  1. MainForm - The main Windows form
  2. 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. 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.

When 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:

  1. Setup
  2. 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:

  1. Drive Motors (2 or 3)
  2. 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! :D

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionMindSqualls library is updated Pin
Mindsqualls21-Jul-11 0:33
Mindsqualls21-Jul-11 0:33 
QuestionBasic Robot Control in VB.Net Programming code [modified] Pin
RoboTechNerd12-Apr-11 19:15
RoboTechNerd12-Apr-11 19:15 
AnswerRe: Basic Robot Control in VB.Net Programming code Pin
Ed Nutting13-Apr-11 6:39
Ed Nutting13-Apr-11 6:39 
Hi, I can try and help though I have no real knowledge of VB so excuse me if i getthings wrong. My first question is can you actually reference the NKH Mindsqualls library? If you can't then check what version of the /Net framework you are using, it should be 3.5. If you can reference the library then what you need to do should be something like the following:

1) Create an NxtBrick object (I'll use the name TheNxt). In C# this would be done like so:

TheNxt = new NxtBrick(PortName);

This block of code was put in a while loop that looped through all possible port names until it got a connection. After that you initialise the motors like so:

TheNxt.MotorA = new NxtMotor(false);

Where the false says if the drive direction is reversed and MotorA refers to Motor Port A on the next, there are also MotorB and MotorC options for which you do the same.

To run motors together to create unified forward drive you need to use a motor sync, created like so:

TheNxt.MotorA = new NxtMotor(currentState.DriveReversed);
TheNxt.MotorB = new NxtMotor(currentState.DriveReversed);
NxtMotorSync = new NxtMotorSync(GlobalVariables.TheNxt.MotorA, GlobalVariables.TheNxt.MotorB);

To run the motors use one of the following:

TheNxt.MotorA.Run(100, 0);

Where 100 refers the power used and 0 refers to number of degrees to turn through. (0 means unlimited rotations so it will only stop where you call TheNxt.MotorA.Idle() or .Brake() depending on the desired affect.)

You can also do:

MotorPair.Run(100 0, 0);

Wher the extra 0 refers to the turn ratio between the two motors that can range from -100 to 100. Play around with it to see the affects.

I would not know how to translate this to VB but if you can reference the NKH Mindsqualls libraries then you should be fine.

Hope this helps and good luck,

GeneralI can not see the code... Pin
theo927311-Oct-10 3:01
theo927311-Oct-10 3:01 
GeneralMy vote of 1 Pin
phi101030-Aug-10 22:35
phi101030-Aug-10 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.