Docsity

NQC Programming - Embedded Intelligent Robotics - Lecture Slides, Slides of Robotics

Partial preview of the text

Download NQC Programming - Embedded Intelligent Robotics - Lecture Slides and more Slides Robotics in PDF only on Docsity!

2. Textual user

interface NQC (Not

quite C)

 C-like programs translated into CRX-bytecode

 Composed of:

  1. Global variables
  2. Task blocks
  3. Inline functions
  4. subroutines

1. You’ll be programming your robot in NQC (Not **Quite C).

  1. You’ll use the BricxCC (Bricx Control Centre)** **as your development environment.
  2. When you first fire up BricxCC (by double** clicking on the BricxCC icon) it looks for the **RCX brick.
  3. It will find it, because you will have switched it** on (using the red On-Off button) and placed it with its infra-red windows facing the IR tower and a few centimetres away from it. NQC Programming

You’ll get this. You should also get another window

If you don’t, press F9 once or twice until you do

Copy and paste the text file ‘trusty’ from your email

And save it locally as trusty (a .nqc file)

1. You will get helpful error messages below the **program

  1. If you can’t sort out the errors by yourself, call** Renzo, the GTA If your program won’t compile…

Tasks

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

1. name : any legal identifier.

  1. 1 task - named "main" - started when program is run.
  2. Maximum number of tasks on RCX: 10
  3. The body of a task consists of a list of statements.
  4. Tasks started and stopped: start and stop statements 6. StopAllTasks stops all currently running tasks.

In robotC #pragma config(Motor, motorA, RightMotor, tmotorNormal, PIDControl, ) #pragma config(Motor, motorB, LeftMotor, tmotorNormal, PIDControl, ) //!!Code automatically generated by 'ROBOTC' configuration wizard !!// void rightTurn(int turnTime) { motor[RightMotor] = - 100; motor[LeftMotor] = 100; wait10Msec(turnTime); } void leftTurn(int turnTime) { motor[RightMotor] = 100; motor[LeftMotor] = - 100; wait10Msec(turnTime); } sub turn_around() { OnRev(OUT_C); Wait(400); OnFwd( OUT_A + OUT_C ); } This in NQC

In robotC #pragma config(Motor, motorA, RightMotor, tmotorNormal, PIDControl, ) #pragma config(Motor, motorB, LeftMotor, tmotorNormal, PIDControl, ) //!!Code automatically generated by 'ROBOTC' configuration wizard !!// void rightTurn(int turnTime) { motor[RightMotor] = - 100; motor[LeftMotor] = 100; wait10Msec(turnTime); } void leftTurn(int turnTime) { motor[RightMotor] = 100; motor[LeftMotor] = - 100; wait10Msec(turnTime); } task main() { motor[RightMotor] = 100; motor[LeftMotor] = 100; wait10Msec(100); rightTurn(20); motor[RightMotor] = 100; motor[LeftMotor] = 100; wait10Msec(100); leftTurn(20); motor[RightMotor] = 100; motor[LeftMotor] = 100; wait10Msec(100); StopAllTasks(); }

Subroutines sub turn_around() { OnRev(OUT_C); Wait(400); OnFwd(OUT_A+OUT_C); } task main() { OnFwd(OUT_A+OUT_C); Wait(100); turn_around(); Wait(200); turn_around(); Wait(100); turn_around(); Off(OUT_A+OUT_C);}c

Subroutines

1. Subroutines allow a single copy of some code

to be shared between several different callers

(space efficient).

2. Restrictions:

  1. First of all, subroutines cannot use any arguments.
  2. A subroutine cannot call another subroutine.
  3. Maximum number of subroutines: 8 for the RCX
  4. If calling from multiple tasks: no local variables or perform calculations that require temporary variables (this restriction is lifted for the Scout and RCX2).

Control structures

  • If-statements if ( condition ) consequence if ( condition ) consequence else alternative
  • While-statements while ( condition ) body
  • Repeat-statements repeat ( expression ) body
  • Switch-statement switch ( expression ) body
  • Until-macro

    define until (c) while (! (c ))

Inline function, call by reference void turn_around(int turntime) { OnRev(OUT_C); Wait(turntime); OnFwd(OUT_A+OUT_C); } task main() { OnFwd(OUT_A+OUT_C); Wait(100); turn_around(200); Wait(200); turn_around(50); Wait(100); turn_around(300); Off(OUT_A+OUT_C); } task main() { int count=0; while (count<=5) { PlaySound(SOUND_CLICK) ; Wait(count*20); increment(count); } } void increment(int& n) { n++; }