Saturday, April 7, 2012

First Attempts with Lego Mindstorms NTX 2.0

I knew that Lego have done some robotics kits. You would program a big brick, connect motors and sensors and start the fun. Well, buying the kit at Brazil is pretty expensive: US$ 1000 by today's exchange rate. I found that if I import directly from Amazon  I could get it for US$ 600, import taxes included !




Okay, here is my first robot. It's the basic starter model. I program it to avoid bumping into objects.




My second Robot was the ColorSorter.  That one also comes with the kit.




The kit comes with a NXT-G, a graphical programing environment based on LabView from National Instruments. When I said Graphical, I mean you have to drag blocks and make connection to program.  See how hard is only to implement the modulo function :


It is not only hard to people used to program by "typing code", but it also generates big and sluggish programs. And program size really matters as memory is scarce.  The alternative I found was Bricxcc, a Windows IDE for NXC ( Not eXactly C) , a C like language to programming the NXT2.0.

Here is the code for the Color Sorter Robot above. It is a direct translation  of the NXT-G code that comes with the kit. I haven't done any improvements to make the robot smatter.

/*
  Lego color sorter
  Attempt to convert from NXT-G to NXC
*/

#define COLOR_SENSOR SENSOR_3
#define TOUCH_SENSOR1 SENSOR_2
#define TOUCH_SENSOR2 SENSOR_1


#define PRESSED 1
#define NOT_PRESSED 0
#define UNTIL_BUMPED(x)  until((x) == PRESSED);until((x) == NOT_PRESSED);

enum color_t {
  BLUE = INPUT_BLUECOLOR,
  RED = INPUT_REDCOLOR,
  YELLOW = INPUT_YELLOWCOLOR,
  GREEN = INPUT_GREENCOLOR
};


sub initializeSensors(){
    //Color Sensor
    SetSensorColorFull(IN_3);
    //Touch sensor 1
    SetSensorTouch(IN_2);
    //Touch sensor 2
    SetSensorTouch(IN_1);
}


task main(){
     initializeSensors();
     
     OnFwd(OUT_C,50);
     until(TOUCH_SENSOR1 == PRESSED);
     RotateMotor(OUT_C,50,55);
     while(true){
         color_t color  = COLOR_SENSOR;
         
         switch(color){
                       case BLUE:
                            //PlaySound wait for completion
                            OnFwd(OUT_B,50);
                            UNTIL_BUMPED(TOUCH_SENSOR2);
                            RotateMotor(OUT_B,50,0.8*360);
                            Wait(MS_300);
                       break;
                       
                       case RED:
                            //PlaySound
                            RotateMotor(OUT_C,40,90);
                            OnFwd(OUT_B,50);
                            UNTIL_BUMPED(TOUCH_SENSOR2);
                            RotateMotor(OUT_B,50,0.8*360);
                            RotateMotor(OUT_C,40,270);
                       break;
                       
                       case YELLOW:
                            //PlaySound
                            RotateMotor(OUT_C,40,180);
                            OnFwd(OUT_B,50);
                            UNTIL_BUMPED(TOUCH_SENSOR2);
                            RotateMotor(OUT_B,50,0.8*360);
                            RotateMotor(OUT_C,40,180);
                       break;
                            
                       case GREEN:
                            //PlaySound
                            RotateMotor(OUT_C,40,270);
                            OnFwd(OUT_B,50);
                            UNTIL_BUMPED(TOUCH_SENSOR2);
                            RotateMotor(OUT_B,50,0.8*360);
                            RotateMotor(OUT_C,40,90);
                       break;
                       
                       default:
                           //Play Sound - try again
                            OnFwd(OUT_B,50);
                            UNTIL_BUMPED(TOUCH_SENSOR2);
                            RotateMotor(OUT_B,50,0.8*360);

         }
     }
}