Domo arigato, Mr. Legoboto NXT#, turn the motor on (LEGO MINDSTORMS NXT)

We found that LEGO did a great job in delivering the MINDSTORMS NXT products (note that this link might point to the next version of MINDSTORMS products in the future and not to NXT anymore).

It is a great for learning purposes and can actually be used with Microsoft Robotics Studio. However, you might want to have a more basic way of controlling the robot, but there is no native C# interface available right now.

Bram Fokke did a great job in actually writing a C# based interface for controlling MindStorms NXT named NXT#. Even though I would not agree with every little implementation detail, he deserves a lot of respect and credit for that.
However, there could be more sample code.

The API offers a LOT more than what I will discuss here. I only deal with the basic features: How to control the motors and maybe later how to read the sensors. So I recommend you check it out yourself.

There are a lot of issues when controlling the robot. So expect funny behavior. Like for example when sending two identical commands to the robot in some time delay so that the first command actually executed already, it might just refuse to execute the second one. Also some other commands resulted in a "non"-movement, even so they should have done something. So – please feel free to experiment, and please add own exception-handling code. After all this is about experimenting and learning.

But how did we get into LEGO MINDSTORMS NXT in the first place? Well, partially it was also a collateral from dealing with RFID. RFID is all about sensors and reacting to it. So, we wanted to look around a little and ended up with a toy that proved to be a lot more than just a toy.

In fact, if we are dealing with hands-on-people, we like to use this product sometimes to do customer demos with it, since it makes some business-integration or business process scenarios suddenly so much more interesting, when you actually see something happening in the physical world.

This first session will only deal with controlling the motor.

Preparations: You have to pair your LEGO MINDSTORMS NXT brick with your bluetooth interface. You have to assign an outgoing com-port to your robot. Then you need to remember the actual name of the COM-port and put it in the code. The code-sample has a COM-port "COM4" but it could be entirely different and easily be a number of COM10 or higher.
The LEGO MINDSTORMS NXT brick has to be turned on before you connect. And finally - be sure you have enough batteries. One set will probably last only a day or so when you play around for a longer while.

Here comes the sample code:

using System;

using System.Collections.Generic;

using System.Text;

using Bram.NxtSharp;

 

namespace LegoNxtConsoleSample

{

    class Program

    {

        const string c_BluetoothComPort = "COM4";

 

        static void MotorDemo()

        {

            NxtBrick brick;

            brick = new NxtBrick();

            brick.COMPortName = c_BluetoothComPort;

            brick.Connect();

 

 

            //select MotorB and turn 180 degrees

            NxtMotor motor1;

            motor1 = new NxtMotor();

            //unfortunately this is necessary, should be wrapped by the API

            //e.g. by "AttachMotor"

            motor1.Brick = brick;

            //that will actually reattach motor to different port

            //if port was already set

            brick.MotorB = motor1;

            //brick.MotorB=motor1 wraps

            //brick.AttachMotor(NxtMotorPort.PortB, motor1);

            Console.WriteLine("turn motor B 180 degrees backwards");

            motor1.Turn(-50, 180);

            System.Threading.Thread.Sleep(2000);

            //will not stop the motor apruptly

            Console.WriteLine("coast motor B");

            motor1.Coast();

            System.Threading.Thread.Sleep(2000);

            Console.WriteLine("turn motor B 180 degrees");

            //turns the other direction and let the other motor start after that

            motor1.Turn(75, 180);

 

 

            //attach MotorC in a different way and turn

            NxtMotor motor2 = new NxtMotor();

            motor2.Brick = brick;

            //will also use "AttachMotor" internally

            motor2.Port = NxtMotorPort.PortC;

            Console.WriteLine("turn motor C until break or coast is sent");

            motor2.Turn(60, 0);

            System.Threading.Thread.Sleep(2000);

            Console.WriteLine("brake motor C");

            motor2.Brake();

 

            //break MotorB also if still running

            Console.WriteLine("brake motor B if still running");

            motor1.Brake();

 

 

            //use SetOutputState for motor C

            Console.WriteLine("turn motor C with granular control, " +

                               "flip first (should change direction)");

            motor2.Flip = !motor2.Flip;

            motor2.SetOutputState(50, NxtMotorMode.MotorOn | NxtMotorMode.Regulated,

            NxtMotorRegulationMode.MotorSpeed, 0, NxtMotorRunState.Running, 1000);

            System.Threading.Thread.Sleep(2000);

            Console.WriteLine("brake motor C");

            motor2.Brake();

 

 

            //iterate through Motorports, not depending on connection state

            foreach (NxtMotorPort motorPort in brick.MotorPorts())

            {

                motor1 = brick.GetMotor(motorPort);//returns null when motor not set

                if (motor1 != null)

                {

                    Console.WriteLine(motorPort.ToString() + " is connected");

                }

                else

                {

                    Console.WriteLine(motorPort.ToString() + " is not connected");

                }

            }

 

 

            //only motors where the brick and port were set are shown here.

            foreach (NxtMotor motor in brick.ConnectedMotors())

            {

                Console.WriteLine("reset motorposition for motor with port " + motor.Port.ToString());

                motor.ResetPosition(false);

            }

 

 

            brick.Disconnect();

        }

 

        static void Main(string[] args)

        {

            MotorDemo();

        }

    }

}


Posted Apr 19 2007, 12:53 AM by Andreas Erben

Comments

Andreas Erben's posts wrote Ohayo gozaimashita, Legoboto-san, NXT: come to your sensors.
on 04-19-2007 1:36
In the previous post I offered some sample code for motor movement of the LEGO MINDSTORMS NXT product
Andreas Erben's posts wrote Legoboto: hajimemashou, LEGO MINDSTORMS NXT, sensor eventing
on 04-19-2007 1:50
In the previous posts I offered some sample code for motor movement and dealing with sensors of the LEGO
developers.de is a .Net Community Blog powered by daenet GmbH.