Top Banner
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
23

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’

Jan 03, 2016

Download

Documents

Mercy Atkins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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’

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 2: 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’

NXC uses BricxCC

Reasonably stable development environmentProvides support for all LEGO robotic platformsFree!

Page 3: 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’

Programming with NXC

Assuming you are comfortable with C…

How to use the motorsHow to read the sensorsThreading model

Page 4: 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’

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 5: 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’

NXC program skeleton

#include "NXCDefs.h"

task main() { }

Page 6: 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’

NXC Sensors

#include "NXCDefs.h"

task main() { SetSensorType( IN_1,

SENSOR_TYPE_TOUCH ); SetSensorMode( IN_1,

SENSOR_MODE_BOOL ); }

Page 7: 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’

NXC Sensors

#include "NXCDefs.h"

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

Page 8: 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’

NXC Sensors

To read a sensor use

x = Sensor( IN_1 );

Page 9: 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’

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 10: 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’

NXC Motors

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

Page 11: 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’

NXC Motors

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

Page 12: 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’

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 13: 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’

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 14: 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’

Displaying Text

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

Page 15: 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’

Displaying Text

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

Page 16: 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’

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 17: 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’

Text Example

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

// characters are 6 pixels wide

Page 18: 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’

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 19: 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’

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 20: 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’

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 21: 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’

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 22: 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’

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

Page 23: 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’

NXC Help

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