Introduction to Programming LEGO ® MINDSTORMS ® NXT Robots

Document Sample
Introduction to Programming LEGO ® MINDSTORMS ® NXT Robots Powered By Docstoc
					  Introduction to Programming
LEGO® MINDSTORMS® NXT Robots
   with Next Byte Codes (NBC)




                                1
   MINDSTORMS® NXT Hardware
• NXT brick
   – 32-bit ARM7 microcontroller (RISC)
        • 256 Kbytes FLASH, 64 Kbytes RAM
   – 8-bit AVR microcontroller
        • 4 Kbytes FLASH, 512 Byte RAM
   –   USB 2.0 and Bluetooth support
   –   4 input ports and 3 output pors
   –   100 x 64 pixel LCD graphical display
   –   NXT Buttons
        • Orange button : On/Enter /Run
        • Light grey arrows: Used for moving left and right in the NXT menu
        • Dark grey button: Clear/Go back
   – Loudspeaker
                                                                              2
   MINDSTORMS® NXT Hardware
• NXT brick




                              3
   MINDSTORMS® NXT Hardware
• Sensors
  – Ultrasonic sensor

  – Touch sensor

  – Sound sensor

  – Light sensor

• Motors
  – Three motors with built-in rotation sensors


                                                  4
Programming MINDSTORMS® NXT
• Many options
  –   NXT-G
  –   NXC
  –   NBC
  –   etc.
• We will use NBC for our project
• Programs to download and install on computer
  – MINDSTORMS NXT Driver v1.02, from
    http://mindstorms.lego.com/support/updates/
  – BricxCC, from http://bricxcc.sourceforge.net/
  – Debugger?
                                                    5
          Next Byte Codes (NBC)
• Developed by John Hansen
• Assembly-like language
• 3-address style
   – Syntax: op destination, sourec1, source2
   – Example: add a, b, c a  b + c
• Extensive utilization of built-in “macros”
   – I/O
   – Motor control
   – Sensor data processing
• We will learn programming in NBC by examples

                                                 6
            NBC Instruction Set
•   Arithmetic
•   Logic
•   Comparison
•   Data manipulation
•   Control flow
•   … etc.



                                  7
 Commonly used NBC Instructions
• Arithmetic/logic
   – op destination, source1, source2
   – add, sub, neg, mul, div, mod, and, or, xor, not
• Comparison
   – cmp condition, destination, source1, source2
     // destination  (source1 condition source2)
   – tst condition, destination, source // cmp to 0
   – Conditions: LT, GT, LTEQ, GTEQ, EQ, NEQ
   – Example: cmp EQ, XEqY, x, y
• Data manipulation: mov, set
   – mov destination, source // destination  source
   – set destination, literal // destination  literal


                                                         8
 Commonly used NBC Instructions
• Control flow
  – Unconditional branch: jmp destination
  – Conditional branch
     • brcmp condition, destination, source1, source2
       // jump to destination if (source1 condition source2)
     • brtst condition, destination, source // brcmp to 0
     • Conditions: LT, GT, LTEQ, GTEQ, EQ, NEQ
     • Example: cmp GT, XBigger, x, y
                    jump to XBigger if x > y

                                                               9
          Other NBC Instructions
•   System I/O
•   System call
•   Timing
•   Scheduling
•   Array manipulation
•   String manipulation



                                   10
                      NBC Data
• Literal: just number, no # needed
  – 41 is a numerical decimal constant
  – 0x41 is a numerical hexadecimal constant (=6510)
• Data type and size
  –   byte: 8 bits unsigned; sbyte: 8 bits signed
  –   word: 16 bits unsigned; sword: 16 bits signed
  –   long: 32 bits unsigned; slong: 32 bits signed
  –   mutex: mutual exclusion, for thread synchronization
• Comments
  – Java styles
  – ; followed by comments
                                                            11
          NBC Program Structure
• Preprocessor + data segment + code
• Preprocessor (optional)
   – Define constants and macros
   – Include (import) files
• Data segment: variable declaration
   – Syntax: var_name type_name optional_initialization
   – Inside dseg segment - dseg ends block
• Code: multithreading by nature
   – Threads: thread name
               // code here
              endt
   – Must have : thread main
   – Subroutines
                                                          12
   NBC Program Structure Example
#define TEN 10            // define constant TEN to be 10       Preprocessor

dseg segment
 aaa byte                 // aaa is a byte
 bbb byte 1               // bbb is a byte w/ initial value 1
                                                                Data
 ccc word                 // ccc is a word
dseg ends

thread main
 set aaa, TEN           // aaa is now equal to 10
 mul bbb, 20, 5         // bbb is now equal to 100
 mov ccc, bbb           // ccc is now equal to 100
 div ccc, ccc, aaa      // ccc is now equal to 10               Code
 add ccc, ccc, 5        // ccc is now equal to 15
 NumOut(10, LCD_LINE7, ccc) // display ccc
 wait 2000              // wait so you get to see it!
 exit
endt                                                                      13
                NBC Macros
• LCD display
  – TextOut(x, y, string)
  – NumOut(x, y, number)
  – …etc.
• Motor control
• Sensor control
• … etc.


                             14
                 Motor Control
•   Move forward: OnFwd(outputs, power)
•   Move backward: OnRev(outputs, power)
•   Off(outputs)
•   Parameters
    – outputs constants:
       • OUT_A, OUT_B, OUT_C, OUT_AB, OUT_AC, OUT_BC,
         OUT_ABC
    – power is the percentage of motor power

                                                        15
                    Sensor Control
• Set sensor
   –   SetSensorTouch(port)
   –   SetSensorSound(port)
   –   SetSensorLight(port)
   –   SetSensorUltrasonic(port)
   –   port: one of IN_1, IN_2, IN_3, IN_4
• ReadSensor(port, value)
   – Sensor type       value
     Touch             1: touched; 0: not touched
     Sound             volume
     Light             light intensity
     Ultrasonic        distance

                                                    16
               References
• [1] LEGO Mindstorms
  (http://mindstorms.lego.com/)
      Official product homepage

 [2] NXT Byte Codes home page
 (http://bricxcc.sourceforge.net/nbc/




                                        17