45
Using the LEGO NXT March 2007 David Schilling Gus Jansson

Using the LEGO NXT March 2007 David Schilling Gus Jansson

Embed Size (px)

Citation preview

Page 1: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Using the LEGO NXT

March 2007 David Schilling Gus Jansson

Page 2: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Overview

NXT HardwareProgramming LanguagesUsing the NXTDemo Robots

Page 3: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXT – What you see

4 Inputs (digital & analog)3 Outputs (support for encoders)Screen (100x64 pixels)4 ButtonsSoundUSB and Bluetooth

Page 4: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Processors

Main processor: Atmel 32-bit ARM processor,

AT91SAM7S256 256 KB FLASH 64 KB RAM 48 MHz

Co-processor: Atmel 8-bit AVR processor, ATmega48 4 KB FLASH 512 Byte RAM 8 MHz

Page 5: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Bluetooth and USB communication

Bluetooth wireless communication CSR BlueCoreTM 4 v2.0 +EDR System Supports the Serial Port Profile (SPP) Internal 47 KByte RAM External 8 MBit FLASH 26 MHz

USB 2.0 Full speed com port (12 Mbit/s)

Page 6: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Graphics, Sound, etc

Display 100 x 64 pixel LCD black & white graphical displayLoudspeaker Sound output channel with 8-bit resolution Sample rate of 2-16 KHz

4 Buttons

Page 7: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Misc.

3 output ports 6-wire interface supporting input from encoders4 input ports 6-wire interface supporting I2C as well as analog 1 high speed port, IEC 61158 Type 4/EN

50170 compliant

JTag debugging pins if you want themLEGO has released technical specifications for all this

Page 8: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Online Documentation

http://mindstorms.lego.com/ - click on “NXTreme” button for information and downloads for advanced usersOpen source firmware!!!SDK – Software Developer KitHDK – Hardware Developer KitBDK – Bluetooth Developer Kit

Page 9: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Motors

Biggest improvement over RCX – rotation counters built in360 counts per rotationThey are relative, not absolute positionsMaximum speed is approx. 200 RPMMotors are much stronger than the other, older LEGO motors

Page 10: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Sensors

Digital ultrasonic sensor (3cm-100cm)Light sensor – can control LEDSound sensor – DB and DBATouch sensor - binary

Page 11: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Conversion cable

LEGO has a conversion cable that allows you to use the sensors and motors of the RCX on the NXTAnd vice versa, but you can’t use the ultrasonic sensor on the RCX, and the motors won’t have encoder support

Page 12: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Wires

The LEGO NXT wires have odd ends (like the DEC RJ-12 connector, but backwards)Wiring diagrams:

Analog input

Page 13: Using the LEGO NXT March 2007 David Schilling Gus Jansson

3rd Party Sensors

Prices from $30 to $50 (so far)HiTechnic (www.HiTechnic.com) Packaging like LEGO sensors Authorized by LEGO

MindSensors (www.MindSensors.com) Circuit boards with connectors

Page 14: Using the LEGO NXT March 2007 David Schilling Gus Jansson

HiTechnic SensorsColor SensorCompass SensorAcceleration SensorMore coming… Prototype board Gyro sensor (analog, not I2C digital) IR Link (to talk to RCX and other LEGO IR

devices) IR Seeker (for robo soccer, for example) No-contact touch sensor (electrostatic sensor) Motor Multiplexor Sensor Multiplexor

Page 15: Using the LEGO NXT March 2007 David Schilling Gus Jansson

MindSensors Sensors

Acceleration SensorRCX to NXT communications linkPS2 controller interfaceMotor MultiplexorMagnetic CompassPneumatic Pressure Sensor

Page 16: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Bluetooth

One way to download programsCan send files from one NXT to anotherAllows up to four NXTs to talk with each other One master, three slaves

Can use a phone as a remote control for your NXTOther devices may be supported

Page 17: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Programming Languages

NXT-G codeNXC (and NBC)Robot CpbLuaJava and others

Page 18: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXT-G code

LEGO’s development environmentGraphical programmingBased on National Instruments’ LabViewGreat for quick programsWonderful for kids to use … but a real pain for old geezers like me who grew up with text programming languages

Page 19: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC (and NBC)

NXC (Not eXactly C) is a language similar to NQC, which was the most popular way to program the RCXBuilt on NBC, the Next Byte Code ‘assembler’Uses the built in firmware, and p-code system

Page 20: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC uses BricxCC

Reasonably stable development environmentProvides support for all LEGO robotic platformsFree!

Page 21: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Robot C

Carnegie Mellon’s programming language for various robotic platforms (including VEX, RCX, as well as NXT)Requires new firmwareNot free (free trial, $30 download)

Page 22: Using the LEGO NXT March 2007 David Schilling Gus Jansson

pbLua

New firmware, and languageSmall portable language for embedded systemsBy Ralph Hemple, the author of pbForth for the RCXCompiles on brick!Use any terminal device to talk to it

Page 23: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Other languages

Java and several other languages are reportedly in the works as wellMS Robotic Studio also supports the NXT Download a small client program to

NXT Actual control is on a PC

Page 24: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Programming with NXC

Assuming you are comfortable with C…

How to use the motorsHow to read the sensorsThreading model

Page 25: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Programs

NXC is based on CRestrictions because it’s based on the built-in P-Code system There’s no stack Limited memory Limited number of tasks (256) And subroutines, which are also

‘tasks’

Page 26: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC program skeleton

#include "NXCDefs.h"

task main() { }

Page 27: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Sensors

#include "NXCDefs.h"

task main() { SetSensorType( IN_1,

SENSOR_TYPE_TOUCH ); SetSensorMode( IN_1,

SENSOR_MODE_BOOL ); }

Page 28: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Sensors

#include "NXCDefs.h"

task main() { SetSensor( IN_1, SENSOR_TOUCH ); }

Page 29: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Sensors

To read a sensor use

x = Sensor( IN_1 );

Page 30: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Sample program#include "NXCDefs.h“task main() { SetSensor( IN_1, SENSOR_TOUCH ); while( true ) { if( Sensor( IN_1 ) ) { PlayToneEx(440, 100, 3, false); TextOut( 0, LCD_LINE1, "TOUCHING!"); while( Sensor( IN_1 ) ) ; } TextOut( 0, LCD_LINE1, "---------"); while( !Sensor( IN_1 ) ) ; }}

Page 31: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Motors

Simple motor commands are available OnFwd(), OnRev(), Off(), Float()

Page 32: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Motors

To use the built-in rotation sensors, you need to use the new motor commandsEasiest to use is RotateMotor()

Page 33: Using the LEGO NXT March 2007 David Schilling Gus Jansson

RotateMotor

RotateMotor( port, speed, angle ); port is OUT_A, OUT_B, OUT_C, or

combinations such as OUT_AB, OUT_ABC

speed is a number -100 to 100 angle is the number of degrees you

want the motor to turn (positive or negative)

Page 34: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Mimicking a Servo Motor

Read the current motor angle Current =

MotorRotationCount( OUT_A );

Calculate how far you need to turn Delta = Target – Current;

Turn that amount RotateMotor( OUT_A, speed, Delta );

Page 35: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Displaying Text

The LCD display is useful for Debugging Setting parameters Runtime messages Graphics Games … and more

Page 36: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Displaying Text

TextOut( x, y, text );NumOut( x, y, number );

Page 37: Using the LEGO NXT March 2007 David Schilling Gus Jansson

LCD display

The origin is the bottom leftSo TextOut( 0,0, “hi” ) will display in the bottom left cornerUse the LCD_LINEn macros if you like to make it easier

Page 38: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Text Example

y = 42;TextOut(0, LCD_LINE5, "Answer:" );NumOut( 8*6, LCD_LINE5, y );

// characters are 6 pixels wide

Page 39: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Graphics

There are also commands to draw lines, circles, rectangles, and set pointsYou can display bitmaps from a fileRIC files – contain drawing commandsOne problem is there isn’t an easy way to clear areas of the screen It’s easy to clear the whole screen You can display a 1x1 blank bitmap

Page 40: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Tasks and Subroutines

Multiple tasks are possible, but don’t work like you might expectScheduling is different – tasks start when a ‘dependant’ task finishes There is no easy way of stopping a

task

Use Precedes() or Follows() in a task to define task dependencies

Page 41: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Taskstask FooTask() { // will start executing when main() finishes}

task main() { // program starts here Precedes( FooTask );}

task BarTask() { Follows( main ); // will also start executing when main()

finishes}

Page 42: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Subroutines

Essentially a task that can be calledIt suspends the calling task until it returnsDon’t use task keyword to define theseCan pass in parameters or return a value

Page 43: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Subroutine Examplevoid TestSub( int x, int y, short i ) { x = y + i; }task main() { TestSub( 1, 2, 3 ); }

Page 44: Using the LEGO NXT March 2007 David Schilling Gus Jansson

NXC Help

Preliminary help file is a PDFThere are many samples and a tutorial online

Page 45: Using the LEGO NXT March 2007 David Schilling Gus Jansson

Demo Robots

Dog6-legged walkerCraneCrate Dump truckOthers?