Download
nxc and nbc n.
Skip this Video
Loading SlideShow in 5 Seconds..
NXC (and NBC) PowerPoint Presentation
Download Presentation
NXC (and NBC)

NXC (and NBC)

67 Vues Download Presentation
Télécharger la présentation

NXC (and NBC)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

  2. NXC uses BricxCC • Reasonably stable development environment • Provides support for all LEGO robotic platforms • Free!

  3. Programming with NXC Assuming you are comfortable with C… • How to use the motors • How to read the sensors • Threading model

  4. NXC Programs • NXC is based on C • Restrictions 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’

  5. NXC program skeleton #include "NXCDefs.h" task main() { }

  6. NXC Sensors #include "NXCDefs.h" task main() { SetSensorType( IN_1, SENSOR_TYPE_TOUCH ); SetSensorMode( IN_1, SENSOR_MODE_BOOL ); }

  7. NXC Sensors #include "NXCDefs.h" task main() { SetSensor( IN_1, SENSOR_TOUCH ); }

  8. NXC Sensors • To read a sensor use x = Sensor( IN_1 );

  9. 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 ) ) ; } }

  10. NXC Motors • Simple motor commands are available • OnFwd(), OnRev(), Off(), Float()

  11. NXC Motors • To use the built-in rotation sensors, you need to use the new motor commands • Easiest to use is RotateMotor()

  12. 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)

  13. 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 );

  14. Displaying Text • The LCD display is useful for • Debugging • Setting parameters • Runtime messages • Graphics • Games • … and more

  15. Displaying Text • TextOut( x, y, text ); • NumOut( x, y, number );

  16. LCD display • The origin is the bottom left • So TextOut( 0,0, “hi” ) will display in the bottom left corner • Use the LCD_LINEn macros if you like to make it easier

  17. Text Example y = 42; TextOut(0, LCD_LINE5, "Answer:" ); NumOut( 8*6, LCD_LINE5, y ); // characters are 6 pixels wide

  18. Graphics • There are also commands to draw lines, circles, rectangles, and set points • You can display bitmaps from a file • RIC files – contain drawing commands • One 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

  19. Tasks and Subroutines • Multiple tasks are possible, but don’t work like you might expect • Scheduling 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

  20. Tasks task 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 }

  21. Subroutines • Essentially a task that can be called • It suspends the calling task until it returns • Don’t use task keyword to define these • Can pass in parameters or return a value

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

  23. NXC Help • Preliminary help file is a PDF • There are many samples and a tutorial online