programming lego minsdstormes nxt with nxc -...

35
Programming Lego Minsdstormes NXT with NXC Theoretic & Practical Work

Upload: hoangdung

Post on 18-Oct-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Programming Lego Minsdstormes

NXT with NXC

Theoretic & Practical Work

Page 2: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Lego Mindstorms RCX   The first retail version

of Lego Mindstorms was released in 1998 and marketed commercially as the Robotics Invention System (RIS).

http://en.wikipedia.org

Page 3: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Lego Mindstorms NXT In August 2006, LEGO released a new

Mindstorms kit called the NXT http://en.wikipedia.org

Page 4: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Lego Mindstorms

NXT

RCX

Page 5: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Curiosity

  In 2006, nearly 8,846 teams and 90,000 children participated in the event, with the topic Nano Quest

  In 2007, approximately 106,000 children and 10,600 teams participated worldwide

Page 6: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Programming languages

NXT-G Retail

NXT-G Educational

RoboLab 2.9

NBC NXC

Graphic Graphic Graphic Assembly C-like

RobotC NI LabVIEW Toolkit

leJOS NXJ

pbLua LEJOS OSEK

C Graphic Java Lua ANSI C

Page 7: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Building a robot

Page 8: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

Not eXactly C (NXC)

Page 9: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Variable Declaration

int x; // declare x bool y, z; // declare y and z long a = 1, b; // declare a and b, initialize a to 1

x = 2; // set x to 2 y = 7; // set y to 7 x += y; // x is 9

Page 10: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Arrays

  Declare a array

  To initialize local arrays or arrays with multiple dimensions it is necessary to use the ArrayInit function

int my_array []; // declare an array with 0 elements bool my_array [][]; // declare a 2-dimensional array

int myArray [][]; int myVector []; ArrayInit (myVector, 0, 10); // 10 zeros in myVector ArrayInit (myArray, myVector, 10); // 10 vectors myArray

Page 11: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Arrays

  Return the length of the specified array

  Copy a subset of the source array starting at the specified index and containing the specified number of elements into the destination array

x = ArrayLen (myArray); //return the length of the array

ArraySubset (myArray, srcArray, 2, 5); //copy 5 elements, // beginning in 2

Page 12: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Structs

struct person { string name; int age; car vehicle; };

person myPerson; myPerson.age = 40;

  Like C

Page 13: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Control Structures

  The goto statement forces a program to jump to the specified location (I do not recommend is a bad idea)

  until will continue looping until the condition becomes true

my_loop: x++; // something // …

goto my_loop;

until (SENSOR_1 == 1 ); // Wait for touch sensor pressed Off (OUT_AC); // and stop motors.

Page 14: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Control Structures if (x==1){ y = 1; z = 2; } ------------------||-------------||--------- while(x < 10){ x = x+1; y = y*2; } ------------------||-------------||--------- do{ // body }while (condition); ------------------||-------------||--------- for(stmt1 ; condition ; stmt2){ // body }

repeat (expression){ // body } ------------------||-------------||---------

switch(x){ case 1: // do something when X is 1 break; case 2: // do something else when x is 2 break; default: // do this when x is not 1 or 2 break; }

Page 15: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Functions

  This allows the callee to modify the value and have those changes visible in the caller

  It is also passed by reference, but with the restriction that the callee is not allowed to modify the value

ᗛ  In general this is the most efficient way to pass arguments in NXC

void foo (int &x){ x = 2; }

void foo (int bar, const int baz){ // do something here... }

Page 16: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Tasks

  A function call is a statement of the form

  Tasks may be started or stopped with the following statements

  You can adjust the priority of a task using the priority statement

name (arguments);

start task_name; stop task_name;

priority task_name, new_priority;

Page 17: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - Tasks

  The NXT supports multi-threading   A task in NXC directly corresponds to an NXT thread   Tasks are defined using the task keyword using the

following syntax

  A program must always have at least one task - named “main”

  The maximum number of tasks is 256

task name() { // the task's code is placed here }

Page 18: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC - API

  The NXC API defines a set of constants, functions, values, and macros that provide access to various capabilities of the NXT such as:   sensors   outputs   communication

  Typically it takes some action or configures some parameter

Page 19: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – API (Timing Functions)

Wait (1000); // Make a task sleep for specified amount of time // wait 1 second

Wait (Random(1000)); // wait random time up to 1 second

x = FirstTick(); // the time that the program began running

SleepNow(); // turn off the NXT immediately

x = CurrentTick(); // the current system timing value // (called a "tick") in milliseconds

Page 20: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – API (Program Control Functions)

Stop (x == 24); // stop the running program if ( x==24 ) is true

StartTask (sound); // start the specified task (sound task in this case)

StopTask (sound); // stop the sound task

ExitTo (nextTask); // immediately exit the current task and start // executing the specified task

StopAllTasks(); // stop the program

Page 21: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – API (Program Control Functions)

Follows (main);

Precedes (moving, drawing, playing); // the tasks will all execute // simultaneously unless other dependencies // prevent them from doing so

  Schedule the specified tasks for execution once the current task has completed executing

  Schedule this task to follow the specified tasks so that it will execute once any of the specified tasks has completed executing

Page 22: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – API (Program Control Functions)

Acquire (motorMutex); // make sure we have exclusive access // to the motors

Release (motorMutex); // release mutex for other tasks

  This function is used to ensure that the current task has exclusive access to a shared resource, such as the display or a motor. After the current task has finished using the shared resource the program should call Release to allow other tasks to acquire the mutex

  Release should always be called after a matching call to Acquire and as soon as possible after a shared resource is no longer needed

  These mutex-type variables are called semaphores

Page 23: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Input Module (Sensors)

SetSensorLight (port); //for exemple IN_3 SetSensorSound (port); //for exemple IN_2 SetSensorTouch (port); //for exemple IN_1 SetSensorLowspeed(port); //for exemple IN_4

  Light sensor → can measure the amount of reflected light or ambient light in a particular direction

  Sound sensor→ detects sounds and play sounds   Touch sensor → indicates that this is a touch sensor   Ultrasonic sensor → works as a sonar, it sends a burst

of ultrasonic waves and measures the time needed for the waves to be reflected back by the object in sight

Page 24: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Sensor Information

SetSensor (S1, SENSOR_TOUCH); x = Sensor(S1); // read sensor 1

/*for the ultrasonic sensor:*/ SetSensorLowspeed (S4); x = SensorUS (S4); // read sensor 4

x = SensorRaw (S1); //Return the raw value of a sensor on port n x = SensorNormalized (S1); //Return the normalized value of a sensor on port n x = SensorScaled (S1); //Return the scaled value of a sensor on port n

Page 25: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Convenience Calls

OnFwd(OUT_A, 75);

Off(OUT_A); // turn off output A

Coast(OUT_A); // coast output A Float(OUT_A); // float output A

  Turn the specified outputs off

  Turn off the specified outputs, making them coast to a stop

  Set outputs to forward direction and turn them on

  Set outputs to reverse direction and turn them on OnRev(OUT_A, 75);

Page 26: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Convenience Calls

RotateMotor(OUT_A, 75, 45); // forward 45 degrees RotateMotor(OUT_A, -75, 45); // reverse 45 degrees

OnFwdSync(OUT_AB, 75, -100); // spin right

OnRevSync(OUT_AB, 75, -100); // spin left

  Run the specified outputs forward with regulated synchronization using the specified turn ratio ( between [-100; 0[ → left, between ]0, 100] → right )

  The same for the output reverse

  Run the specified outputs forward for the specified number of degrees

Page 27: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXT / NXC - advices

  The NXC filenames, should be maximum 15 characters   Example:   follow_line_HARD_final.nxc   appears in NXT screen: follow_line_HAR

  Before starting plan the main program, you should do tests with the engine speed and time to determine the angle   Example: (using ruler and square determine the angle)

OnRev(OUT_A, 30); Wait(300); Off(OUT_AC); OnFwd(OUT_C, 30); Wait (50);

Page 28: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Example 1

#define TURN_TIME 360 // waiting time int move_time; // define a variable task main() { move_time = 200; // set the initial value repeat(5) // repeat five times { OnFwd(OUT_AC, 75); Wait(move_time); // use the variable for sleeping OnRev(OUT_C, 75); Wait(TURN_TIME); move_time += 200; // increase the variable } Off(OUT_AC); }

  Driving in a spiral

Page 29: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Example 2

#define THRESHOLD 40 #define MIC SENSOR_2 task main() { SetSensorSound(IN_2); while(true){ until(MIC > THRESHOLD); OnFwd(OUT_AC, 75); Wait(300); until(MIC > THRESHOLD); Off(OUT_AC); Wait(300); } }

  Waits for a loud sound, and drives the robot until another sound is detected

Page 30: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Example 3

task main(){ SetSensorLight(IN_3); while(true){ if (Sensor(IN_3) <= 40) { OnFwd(OUT_AC, 30); Wait(2000); } else Off(OUT_AC); } }//end Task main

task main(){ SetSensorLight(IN_3); while(true){ if (Sensor(IN_3) <= 40) { OnFwdSync(OUT_AC, 30, 0); Wait(2000); } else Off(OUT_AC); } }//end Task main

  Where is the difference?

Page 31: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Example 4

#define NEAR 25 //cm mutex block;

task verify_obstacle(){ while(true){ OnFwd(OUT_AC,20); while(SensorUS(IN_4)>NEAR); Off(OUT_AC); OnRev(OUT_AC,20); Wait(400); OnFwd(OUT_A,20); Wait(400); } }//end Task verify_obstacle

task verify_touch(){ while (true) if (SENSOR_1 == 1) OnFwd(OUT_AC, 55); Wait(100); }//end Task verify_touch

task main(){ Precedes(verify_touch, verify_obstacle ); SetSensorLowspeed(IN_4); SetSensorTouch(IN_1); }//end Task main

  React to any obstacle in front or behind

Page 32: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

NXC – Example 5

  Follow a Line

Page 33: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

#define THRESHOLD 50 // < 50 é preto #define NEAR 15 // cm mutex block; int conta=0;

sub turn_R(){ Acquire(block); do{ OnRev(OUT_A, 30); Wait(300); Off(OUT_AC); OnFwd(OUT_C, 30); Wait (50); conta=0; }while(Sensor(IN_3) > THRESHOLD); Release(block); }//end Sub turn_R

sub turn_L(){ Acquire(block); OnFwd(OUT_A, 30); OnRev(OUT_C, 15); Wait(400); Off(OUT_AC);

if (Sensor(IN_3) > THRESHOLD){ Wait (1000); conta++; }

if (conta==5){ Wait(3000); turn_R(); } Release(block); }//end Sub turn_L

task move_forward(){ while(true){ while (Sensor(IN_3) <= THRESHOLD) { Acquire(block); OnFwdSync(OUT_AC, 30, 0); //specify the 'turnpct' steering percentual (from -100 to 100) pagina 32 || [0 ; 100] desloca-se para a direita [-100, 0]eskerda Wait(5); Off(OUT_AC); conta=0; Release(block); if (Sensor(IN_3) > THRESHOLD) break; } //while

turn_L(); //corrige a tragectoria }// end while }//end Task move_forward

task main(){ Precedes(move_forward); SetSensorLight(IN_3); SetSensorLowspeed(IN_2); }//end Task main

Page 34: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

  http://en.wikipedia.org/wiki/Lego_Mindstorms_NXT#Programming

  http://www.teamhassenplug.org/NXT/NXTSoftware.html   http://bricxcc.sourceforge.net/nbc/   http://www.botmag.com/articles/10-31-07_NXT.shtml   http://forums.nxtasy.org/index.php?

&&CODE=autologin&fromreg=1

Bibliography

Page 35: Programming Lego Minsdstormes NXT with NXC - Eklablogekladata.com/TReVjWVth4CSTzUPVugpT6rHAUg.pdf · Lego Mindstorms RCX The first retail version of Lego Mindstorms was released in

by Hernani Costa

Thanks for your attencion

[email protected]