Click here to Skip to main content
15,884,099 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 36.4K   1.4K   14   5
Demonstration of how to do basic robot remote control via bluetooth using NKH Mindsqualls libraries.

Introduction

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.

Background

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 :/.

History

  • 28th August, 2010: Initial post

License

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


Written By
Student
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 
I have seen your program and really liked what I found, is there anyway I could do this in just visual basic? I got the Bluetooth figured out, but I cannot figure out how to get the motors initialized to do left\right\forward\backward commands and the brick to communicate this. I haven't found any code for this in just the basic visual basic coding, and if it is not possible to use the NKH Mindsqualls Libraries in that coding then how would I use what I already have and combine it with C#. I am only a beginner! Please help and here is my code I have already implemented:
' Program Name: Robotic Control NXT ver. 1.0
' Author Name: Abraham Zurita and Matthew Scholle
' Purpose: The purpose of this program is to make it simple to execute forward, backward, left, and right
' commands on the Lego Mindstorms Robot NXT 2.0 while connected with the bluetooth.
Option Strict Off
Option Explicit On
Public Class frmRobotControlPanel
Dim Connected As Boolean
Public intStpForward As Integer
Public intStpBackward As Integer
Public intStpLeft As Integer
Public intStpRight As Integer
Private Sub frmRobotControlPanel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' The picDeviceStatusNL becomes visable because the bluetooth is not autodetected and notifies the user
' that they need to connect the bluetooth in order to execute commands.
Me.picDeviceStatusNL.Visible = True
' The following buttons; forward, backward, left, and right are disabled until the user connects the bluetooth with the robot.
Me.btnForward.Enabled = False
Me.btnBackward.Enabled = False
Me.btnLeft.Enabled = False
Me.btnRight.Enabled = False
Me.btnDisconnectBT.Enabled = False

End Sub
Private Sub btnDisconnectBT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnectBT.Click
' Close the PC Serial COM Port for Bluetooth connection.
Dim strBTDisconnectNotify As String = "You have disconnected successfully!"
Dim strMessageBoxTitle As String = "Message Notification"
NXTPort.Close()
Connected = False
txtComPort.Enabled = True
Me.btnForward.Enabled = False
Me.btnBackward.Enabled = False
Me.btnLeft.Enabled = False
Me.btnRight.Enabled = False
Me.btnDisconnectBT.Enabled = False
Me.picDeviceStatusNL.Visible = False
Me.picDeviceStatusOn.Visible = False
Me.btnDisconnectBT.Enabled = False
Me.btnConnectBT.Enabled = True
Me.picDeviceStatusOff.Visible = True
MessageBox.Show(strBTDisconnectNotify, strMessageBoxTitle)
End Sub
Private Sub btnConnectBT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectBT.Click
' Open\Intialize PC Serial COM Port for Bluetooth connection.
Dim strBluetoothOfflineError As String = "Bluetooth not detected! Either you entered the wrong COM connection or the device is not plugged in."
Dim strBluetoothOnlineNotify As String = "Bluetooth Detected!."
Dim strMotorandBrickNotify As String = "A,B, and C Motors and NXT Brick have been Intialized!."
Dim strErrorMessageBoxTitle As String = "Error"
Dim strMessageBoxTitle As String = "Success!"
If Connected = False Then
Try
With NXTPort
.PortName = txtComPort.Text
.BaudRate = 96000
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.ReadTimeout = 300
.WriteTimeout = 300
End With
Connected = True
NXTPort.Open()
' Bluetooth has Connected
txtComPort.Enabled = False
Me.picDeviceStatusNL.Visible = False
Me.picDeviceStatusOff.Visible = False
Me.picDeviceStatusOn.Visible = True
Me.btnConnectBT.Enabled = False
Me.btnDisconnectBT.Enabled = True
MessageBox.Show(strBluetoothOnlineNotify, strMessageBoxTitle)
' Enable left, right, forward, backward buttons
Me.btnForward.Enabled = True
Me.btnBackward.Enabled = True
Me.btnLeft.Enabled = True
Me.btnRight.Enabled = True
' Intialize NXT Brick and A,B, and C Motors procedure
MessageBox.Show(strMotorandBrickNotify, strMessageBoxTitle)
' Exception catching reason why the connection failed
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
' Bluetooth not Connected
Me.picDeviceStatusNL.Visible = False
Me.picDeviceStatusOn.Visible = False
Me.picDeviceStatusOff.Visible = True
Me.btnForward.Enabled = False
Me.btnBackward.Enabled = False
Me.btnLeft.Enabled = False
Me.btnRight.Enabled = False
Me.btnDisconnectBT.Enabled = False
MessageBox.Show(strBluetoothOfflineError, strErrorMessageBoxTitle)
End Try
Else
' Close the PC Serial COM Port for Bluetooth connection if not detected.
NXTPort.Close()
Connected = False
txtComPort.Enabled = True
End If
End Sub
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
' This menu item when clicked will open a form talking about the program version and description.
End Sub

Private Sub RegisterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisterToolStripMenuItem.Click
'This menu item when clicked will open another other form which is to register this software product.
Dim Register As VariantType
Register = Shell("Register.exe", vbNormalFocus)
End Sub
Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
' This menu item when clicked will open a help file that explains about the Robot Control Panel Program
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
' Application will terminate and close
Me.Close()
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
' This will clear the Statistics Panel Movement Counters

' StpLeft Counter is reset back to 0 and label is cleared.
intStpLeft = intStpLeft * 0
Me.lblStpLeft.Text = intStpLeft.ToString("G")
Me.lblStpLeft.Text = ""
' StpRight Counter is reset back to 0 and label is cleared.
intStpRight = intStpRight * 0
Me.lblStpRight.Text = intStpRight.ToString("G")
Me.lblStpRight.Text = ""
' StpBackward Counter is reset back to 0 and label is cleared.
intStpBackward = intStpBackward * 0
Me.lblStpBackward.Text = intStpBackward.ToString("G")
Me.lblStpBackward.Text = ""
' StpForward Counter is reset back to 0 and label is cleared.
intStpForward = intStpForward * 0
Me.lblStpForward.Text = intStpForward.ToString("G")
Me.lblStpForward.Text = ""
End Sub
Private Sub btnForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click
' When this Forward button is clicked the code will execute the robot to move forward and
' will add a counter to the StpForward variable.
Me.lblStpForward.Visible = True
intStpForward = intStpForward + 1
Me.lblStpForward.Text = intStpForward.ToString("G")
End Sub
Private Sub btnBackward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackward.Click
' When this Backward button is clicked the code will execute the robot to move backward and
' will add a counter to the StpBackward variable.
Me.lblStpBackward.Visible = True
intStpBackward = intStpBackward + 1
Me.lblStpBackward.Text = intStpBackward.ToString("G")
End Sub
Private Sub btnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click
' When this Left button is clicked the code will execute the robot to move left and
' will add a counter to the StpLeft variable.
Me.lblStpLeft.Visible = True
intStpLeft = intStpLeft + 1
Me.lblStpLeft.Text = intStpLeft.ToString("G")
End Sub
Private Sub btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRight.Click
' When this Right button is clicked the code will execute the robot to move right and
' will add a counter to the StpRight variable.
Me.lblStpRight.Visible = True
intStpRight = intStpRight + 1
Me.lblStpRight.Text = intStpRight.ToString("G")
End Sub
End Class



-- Modified Wednesday, April 13, 2011 1:37 AM
AnswerRe: Basic Robot Control in VB.Net Programming code Pin
Ed Nutting13-Apr-11 6:39
Ed Nutting13-Apr-11 6:39 
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.