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.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

1chung 5 th Annual Robofest 2004 Informational Meeting Mar. 20, 2004 Chan Jin Chung Ali Khazaal Jason Lo Computer Science Lawrence Technological University.
Robofest 2005 Introduction to Programming RIS 2.0 RCX Code.
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Jason Howard. Agenda I. How to download robotc II. What is tele-op used for? III. How to build a basic tele-op program IV. Getting the robot to drive.
Team 5220 Roboknights. Outline  Getting Started  Hardware Setup/Wiring  Software Setup/Pragmas  Programming with RobotC  Grammar/Syntax  Basic Statements.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Automation and Robotics
Using the NXT Light Sensor. 2 Connect One Light Sensor – 1 From My Files use Left / Right NXT buttons and get to View menu and push Orange button. From.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Alternative Programming Languages Myles McNally LMICSE Workshop November , 2004 University of Mississippi.
Motor control drive in circles Pragmas configure motors Turning right in function of time Turning left in function of time Main program starts from here.
LEGO Robotics Lecture 1: Getting acquainted with your robotic environment.
CC467 Biologically inspired robotics Introduction to Lego Mindstorms Owen Holland Computer Science University of Essex.
NQC: Not Quite C (last updated 9/14/05 2:24pm) Each NQC program has a task main () The program is contained within curly braces { ….. } task main() { SetSensor(SENSOR_1,
RobotC Programming for LEGO Mindstorms NXT Carnegie Mellon Dacta Lego Timothy Friez Miha Štajdohar SOURCES:
Not Quite C: A CS 0 Option LMICSE Workshop June , 2005 Alma College.
LEGO Mindstorms RIS 2.0 Programming: NQC Code B.A. Juliano and R.S. Renner September 2004 California State University, Chico Intelligent Systems Laboratory.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Using Variables Variables form a very important aspect of every programming language. Variables are memory locations in which we can store a value. We.
Inline Functions Sometimes you need the same piece of code at multiple places in your task. Inline functions are copied at each place they are used. In.
Tasks An NQC program consists of at most 10 tasks. Each task has a name. One task must have the name main, and this task will be executed. The other tasks.
Repeating Blocks of Code (updated 9/20/05 7:35pm) Reference NQC Tutorial pp 9-12.
Preprocessor Directives (last modified 9/19/05 2:47pm) Statements beginning with # are directives to the preprocessor. –They DO NOT end with ; –Before.
RobotC For Beginners Tyler Lutz and Keaton Bonds DRSS Enterprise.
LEGO Mindstorms NXT Programming We will be using the Common Palette for our Robots This is how you download your program onto the brick Drag and drop a.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Testbed: Exercises.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Programming 101 The Common Palette Content provided by Connor Statham (6 th Grade Student) Formatting by Shannon Sieber.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
- 1 -  P. Marwedel, Univ. Dortmund, Informatik 12, 2005/6 Universität Dortmund Embedded System Hardware Embedded system hardware is frequently used in.
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’
Mindscript Presented by Dean Thomas Matthew Horoszowski.
NQC Brief Introduction – Part 3 David Schilling. NQC – Where is my data? Programs have 32 global variables Tasks have 16 local variables These are always.
Motors and Sound Troubleshooting Tips. © H-CCS Problem 1 Why can’t I download my program to the RCX?
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Conditions and loops Day 4 Computer Programming through Robotics CPST 410 Summer 2009.
Lego MindStorm An Introduction to Blocks. Blocks Blocks are used to give instructions to your robot. There are many types of blocks You can use the blocks.
Castor Bot. Now, we will begin creating a robot Log onto your computer On your screen, click on the website labeled “castor bot” Your building instructions.
NQC / BricxCC Brief Introduction David Schilling.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.
RobotC Remote Control. Learning Objectives: Focusing on Virtual World with Physical Examples Understand Real-Time Joystick Mapping Understand how to use.
Programming - Motion Intro to Robotics. Motors and Sensors Setup The first thing we need to do is tell ROBOTC that we have motors on our robot. Choose.
Controlling Program Flow with Decision Structures.
Lesson 1: Motors and Sound Programming Solutions.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Automation and Robotics.  First you select the platform type so that you can use Natural Language PLTW.
Programming VEX Cortex Robotics with ROBOTC
Casne.ncl.ac.uk Taking care of the CrumbleBot Please do NOT stress the robot's motors 1.Do NOT push the robot 2.Do NOT hold the.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Making Interactive Programs with Visual Basic .NET
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Robotics Programming Wall Follow Line tracking for a set amount of time Line tracking for a distance.
ROBOTC for CORTEX Teacher Training © 2011 Project Lead The Way, Inc. Automation and Robotics VEX.
Automation and Robotics
Remote Control For this activity we will use the Squarebot
Getting started with LEGO EV3 Mindstorms software
LEGO Mindstorms Robot and Java
NQC Program Structure 3 types of code blocks with their own features and limitations 1. Tasks 2. Subroutines 3. Inline Functions.
ITM 352 Functions.
Presentation transcript:

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  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). 2.You’ll use the BricxCC (Bricx Control Centre) as your development environment. 3.When you first fire up BricxCC (by double clicking on the BricxCC icon) it looks for the RCX brick. 4.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

Click on RCX2, then OK 2 1

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

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

File, New gets you a programming window

Copy and paste the text file ‘trusty’ from your

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

Click on Compile to compile, and on Download to put it into the RCX. 1 2

1.You will get helpful error messages below the program 2.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. 2.1 task - named "main" - started when program is run. 3.Maximum number of tasks on RCX: 10 4.The body of a task consists of a list of statements. 5.Tasks started and stopped: start and stop statements 6.StopAllTasks 6.StopAllTasks stops all currently running tasks.

NQC/RCX basics: Motors 1.Motor connections A, B, and C are known as OUT_A, OUT_B, and OUT_C 2.You set the direction of a motor by OnFwd (OUT_A) or OnRev(OUT_B) 3.You set the power of a motor by SetPower (OUT_A, x) where x runs from 0 to 7 4.You turn a motor off by Off(OUT_B)

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); OUT_AOUT_C 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(); }

NQC/RCX basics: Subroutines 1.There are several ways of avoiding having to write out long strings of repetitive chunks of code. 2.One is to use subroutines. 3.The format of a subroutine is: sub subroutinename ( ) {code goes here} 4.And you call it in the main body of your code by subroutinename ( ); 5.(Semicolons are used as separators for expressions etc.)

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

NQC/RCX basics: Control structures 1.while (true) {stuff} will execute stuff forever. 2.if (condition) do_something_simple will do_something_simple if the condition is true 3.if (condition) do_something_simple else do_something_else will do_something_else if the condition is not true

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++; } task main() { int count=0; while (count<=5) { PlaySound(SOUND_CLICK) ; Wait(count*20); increment(count); } void increment(int& n) { n++; }

(Inline) Functions void name( argument_list ) { // body of the function } Functions cannot return a value; void is related to C Argument list: empty, or ≥ 1 argument definitions. Arguments: type followed by its name. All values are 16 bit signed integers. 4 different argument classes4 different argument classes: TypeMeaningRestriction intPass by valueNone int&Pass by referenceOnly variables may be used const intPass by valueOnly constant may be used const int&Pass by referenceFunction cannot modify argument

Playing preprogrammed sounds & tones task music() { while (true) { PlayTone(262,40); Wait(50); PlayTone(294,40); Wait(50); PlayTone(330,40); Wait(50); PlayTone(294,40); Wait(50); } } task main() { start music; while(true) { OnFwd(OUT_A+OUT_C); Wait(300); OnRev(OUT_A+OUT_C); Wait(300); } } task main() { PlaySound(0); Wait(100); PlaySound(1); Wait(100); PlaySound(2); Wait(100); PlaySound(3); Wait(100); PlaySound(4); Wait(100); PlaySound(5); Wait(100); } task main() { PlaySound(0); Wait(100); PlaySound(1); Wait(100); PlaySound(2); Wait(100); PlaySound(3); Wait(100); PlaySound(4); Wait(100); PlaySound(5); Wait(100); }

Variables All variables of the same type: 16 bit signed integers. Declarations: int variable[=initial value] [, variable [=initial value]] ; Examples: int x ; // declare x int y, z ; // declare y and z int a =1, b ; // declare a and b, initialize a to 1 Used within tasks, functions, subroutines. –Global variables: declared at the program scope; Used within tasks, functions, subroutines. Max: 32 –Local variables: within tasks, functions, and sometimes within subroutines. Max: RCX, –Local variables may be declared in a compound statement, following a {

All variables are integers. You have to declare them, otherwise the compiler will moan. int thing; at the beginning of your program will let you use the integer variable thing anywhere. If you declare it within some curly brackets (as part of an expression or subroutine etc.) it won’t work if you try to use it outside the brackets (and the compiler will complain). NQC/RCX basics: Variables

You can give constants meaningful names instead of numbers by defining them at the start of your program: #define MONKEY_SCORE 25 means you can write MONKEY_SCORE in your code instead of just 25, and so you will be able to distinguish it from all the other constants that might be 25 – for example, SLAMMER_RECORD NQC/RCX basics: Constants

Arrays Arrays exist only for RCX2

Assignments Syntax: Variable operator expression Operators: = Set variable to expression += Add expression to variable -= Subtract expression from variable *= Multiple variable by expression /= Divide variable by expression &= Bitwise AND expression into variable |= Bitwise OR expression into variable ||= Set variable to absolute value of expression +-= Set variable to sign (-1,+1,0) of expression

Built-in API SetPower(outputs, power) Function 1.Sets the power level of the specified outputs. 2.Power should result in a value between 0 and 7. 3.OUT_LOW, OUT_HALF, OUT_FULL may also be used. Examples: SetPower( OUT_A, OUT _FULL) ; // A full power SetPower( OUT_B, x ); OnFwd(outputs) Function 1.Set outputs to forward direction and turn them on. 2.Outputs is one or more of OUT_A, OUT_B, and OUT_C added together. Example: OnFwd (OUT_A );

SENSORS

Sensor Types Old RCX NXT brick

Sensor Modes

Sensor Type/Mode Combinations

Setting Sensor Type and Mode SetSensor(sensor, configuration) –Set the type and mode to the specified configuration (constant containing both type and mode info). Example: SetSensor (SENSOR_1, SENSOR_TOUCH) ; SetSensorType(sensor, type) –Set type (one of the predefined sensor type constants). –Example: SetSensorType(SENSOR_1, SENSOR_TYPE _TOUCH ); SetSensorMode(sensor, mode) –Set mode (one of the predefined sensor mode constants) Optional slope parameter for Boolean conversion.

Reading out sensors, Wait SensorValue(n) –Returns the processed sensor reading for sensor n, where n is 0, 1, or 2. This is the same value that is returned by the sensor names (e.g. SENSOR_1). –Example: x = SensorValue(0); // readsensor_1 Wait(time) Make a task sleep for specified amount of time (in 1/100 s). Argument may be an expression or a constant. Wait(100); // wait 1 second Wait(Random (100)); // wait random time up to 1 second Wait(time) Make a task sleep for specified amount of time (in 1/100 s). Argument may be an expression or a constant. Wait(100); // wait 1 second Wait(Random (100)); // wait random time up to 1 second For more information refer to the NQC programmers manual

Example of motor control in NQC // speed.nqc -- sets motor power, goes forward, waits, // goes backwards task main( ) { SetPower(OUT_A+OUT_C,2); OnFwd(OUT_A+OUT_C); Wait(400); OnRev(OUT_A+OUT_C); Wait(400); Off(OUT_A+OUT_C); } 1.sets motor power, 2.goes forward, waits, 3.goes backwards

Robots moves in a Spiral // spiral.nqc -- Uses repeat & variables to make robot // move in a spiral #define TURN_TIME 100 int move_time; // define a variable task main() { move_time = 20; // set the initial value repeat(50) { OnFwd OnFwd(OUT_A+OUT_C); use the variable for sleeping Wait(move_time); // use the variable for sleeping OnRev OnRev(OUT_C); Wait(TURN_TIME); increase the variable move_time += 5; // increase the variable } Off(OUT_A+OUT_C); }

touch sensors Use of touch sensors // Use of touch sensors task main() { SetSensor(SENSOR_1,SENSOR_TOUCH); OnFwd(OUT_A+OUT_C); while (true) { if (SENSOR_1 == 1) { OnRev(OUT_A+OUT_C); Wait(30); OnFwd(OUT_A); Wait(30); OnFwd(OUT_A+OUT_C); } SENSOR_1 Declare that SENSOR_1 is touch sensor Go forward with speed OUT_A + OUT_C Go backward with speed OUT_A + OUT_C

Use of light sensor // Use of a light sensor to make robot go forward until // it "sees" black, then turn until it's over white #define THRESHOLD 37 task main() { SetSensor(SENSOR_2,SENSOR_LIGHT); OnFwd(OUT_A+OUT_C); while (true) { if (SENSOR_2 < THRESHOLD) { OnRev(OUT_C); Wait(10); until (SENSOR_2 >= THRESHOLD); OnFwd(OUT_A+OUT_C); } } }

NQC/RCX basics: Sensors 1.Sensor connections 1, 2, and 3 are known as SENSOR_1, SENSOR_2, and SENSOR_3 2.You have to tell each sensor connection what sort of sensor is plugged into it. 3.For the light sensor, it’s just a matter of: 4.SetSensor (SENSOR_2, SENSOR_LIGHT)

NQC/RCX basics: Tasks 1.NQC can run several tasks at the same time. (Unfortunately you have to be careful to ensure that tasks do not conflict, especially over resources.) 2.The format of a task is: task taskname( ) {task instructions etc go here} 3.Every program must contain a task called main: task ( ) main {stuff}

Tasking task main() { SetSensor(SENSOR_1,SENSOR_TOUCH); start check_sensors; start move_square; } task move_square() { while (true) { OnFwd(OUT_A+OUT_C); Wait(100); OnRev(OUT_C); Wait(85); } } task check_sensors() { while (true) { if (SENSOR_1 == 1) { stop move_square; OnRev(OUT_A+OUT_C); Wait(50); OnFwd(OUT_A); Wait(85); start move_square; } } } Starts two tasks described below

Macros #define turn_right(s,t) SetPower(OUT_A+OUT_C,s);OnFwd(OUT_A);OnRev(OUT_C);Wait(t); #define turn_left(s,t) SetPower(OUT_A+OUT_C,s);OnRev(OUT_A);OnFwd(OUT_C);Wait(t); #define forwards(s,t) SetPower(OUT_A+OUT_C,s);OnFwd(OUT_A+OUT_C);Wait(t); #define backwards(s,t) SetPower(OUT_A+OUT_C,s);OnRev(OUT_A+OUT_C);Wait(t); task main() { forwards(1,200); turn_left(7,85); forwards(4,100); backwards(1,200); forwards(7,100); turn_right(4,85); forwards(1,200); Off(OUT_A+OUT_C);} #define turn_right(s,t) SetPower(OUT_A+OUT_C,s);OnFwd(OUT_A);OnRev(OUT_C);Wait(t); #define turn_left(s,t) SetPower(OUT_A+OUT_C,s);OnRev(OUT_A);OnFwd(OUT_C);Wait(t); #define forwards(s,t) SetPower(OUT_A+OUT_C,s);OnFwd(OUT_A+OUT_C);Wait(t); #define backwards(s,t) SetPower(OUT_A+OUT_C,s);OnRev(OUT_A+OUT_C);Wait(t); task main() { forwards(1,200); turn_left(7,85); forwards(4,100); backwards(1,200); forwards(7,100); turn_right(4,85); forwards(1,200); Off(OUT_A+OUT_C);}

It’s straight from the Knudsen book. We’ll skip through some basics, and then go through the program. EXAMPLE: The trusty.nqc program

int state; #define BOTH_ON 3 #define LEFT_ON 1 #define RIGHT_ON 2 #define BOTH_OFF 0 #define INDETERMINATE 255 #define DARK2 35 #define LIGHT2 40 #define DARK3 40 #define LIGHT3 45 #define POWER 4 trusty.nqc

int state; THIS IS THE DECLARATION OF THE VARIABLE WE WILL USE TO REPRESENT ALL THE DIFFERENT MEANINGFUL COMBINATIONS OF SENSOR STATES trusty.nqc

#define BOTH_ON 3 #define LEFT_ON 1 #define RIGHT_ON 2 #define BOTH_OFF 0 #define INDETERMINATE 255 THESE ARE THE VALUES THE VARIABLE state CAN TAKE. BOTH_ON MEANS BOTH SENSORS ON THE TRACK, ETC. INDETERMINATE MEANS AT LEAST ONE SENSOR IS ON THE BORDERLINE trusty.nqc

#define DARK2 35 #define LIGHT2 40 #define DARK3 40 #define LIGHT3 45 THESE ARE THE SENSOR INPUT VALUES FOR DECIDING WHETHER A SENSOR IS ON OR OFF THE LINE, OR ON THE BORDERLINE trusty.nqc

Sensor tip 1.If the sensor port is correctly configured, you can read the value of a sensor directly from the LCD display on the RCX. 2.Press VIEW. 1.You will see a little ^ pointing to input 1. 2.The number on the LCD will be the current input of the sensor connected to input 1. 3.Press VIEW again. 1.The ^ will move round one place to input 2. 4.Keep on pressing and it will go 3, A, B, C, and then out of VIEW mode.

#define POWER 4 THIS SETS THE POWER LEVEL THAT WILL BE USED FOR MOTORS THAT ARE ON. trusty.nqc

task main() { initialize(); while (true) { if (state == BOTH_ON) OnFwd(OUT_A + OUT_C); else if (state == LEFT_ON) { Off(OUT_A); OnFwd(OUT_C); } else if (state == RIGHT_ON) { Off(OUT_C); OnFwd(OUT_A); } trusty.nqc

task main() { initialize(); while (true) { CODE HERE } THIS STARTS THE MAIN TASK, INITIALIZES EVERYTHING USING THE SUBROUTINE INITIALIZE, AND EXECUTES THE CODE IN THE MIDDLE trusty.nqc

if (state = = BOTH_ON) OnFwd(OUT_A + OUT_C); else if (state = = LEFT_ON) { Off(OUT_A); OnFwd(OUT_C); } else if (state = = RIGHT_ON) { Off(OUT_C); OnFwd(OUT_A); THIS GOES FORWARD FOR BOTH_ON, TURNS LEFT FOR LEFT_ON, AND TURNS RIGHT FOR RIGHT_ON trusty.nqc

sub initialize() sub initialize() { SetSensor (SENSOR_2, SENSOR_LIGHT); SetSensor (SENSOR_3, SENSOR_LIGHT); SetPower (OUT_A + OUT_C, POWER); OnFwd(OUT_A + OUT_C); start watcher; } THIS IS THE SUBROUTINE initialize. IT SETS THE SENSOR INPUTS CORRECTLY, STARTS THE ROBOT GOING FORWARD, AND STARTS THE TASK watcher. trusty.nqc

task watcher() { while (true) { if (SENSOR_2 < DARK2) { if (SENSOR_3 < DARK3) state = BOTH_ON; else if (SENSOR_3 > LIGHT3) state = LEFT_ON; else state = INDETERMINATE; } else if (SENSOR_2 > LIGHT2) { if (SENSOR_3 < DARK3) state = RIGHT_ON; else if (SENSOR_3 > LIGHT3) state = BOTH_OFF; else state = INDETERMINATE; } else state = INDETERMINATE; } trusty.nqc

task watcher() { while (true) { ONCE STARTED, WATCHER KEEPS ON RUNNING FOR EVER trusty.nqc

if (SENSOR_2 < DARK2) { if (SENSOR_3 < DARK3) state = BOTH_ON; else if (SENSOR_3 > LIGHT3) state = LEFT_ON; else state = INDETERMINATE; IF BOTH SENSORS ARE BELOW THEIR INDIVIDUAL DARK THRESHOLD, state IS SET TO BOTH_ON. IF THE SENSOR ON THE LEFT IS DARK, AND THE SENSOR ON THE RIGHT IS ABOVE ITS LIGHT THRESHOLD, state IS SET TO LEFT_ON. IF THE SENSOR ON THE LEFT IS DARK, AND THE SENSOR ON THE RIGHT IS BETWEEN ITS LIGHT AND DARK THRESHOLD, state IS SET TO INDETERMINATE trusty.nqc

else if (SENSOR_2 > LIGHT2) { if (SENSOR_3 < DARK3) state = RIGHT_ON; else if (SENSOR_3 > LIGHT3) state = BOTH_OFF; else state = INDETERMINATE; THIS SORTS OUT RIGHT_ON AND BOTH_OFF, AND ANOTHER KIND OF INDETERMINATE trusty.nqc

else state = INDETERMINATE; AND THIS CATCHES THE CASE WHERE BOTH SENSORS ARE BETWEEN THRESHOLDS THAT’S ALL YOU NEED – OFF YOU GO… trusty.nqc

Books: Core Lego Mindstorms Programming by Brian Bagnold Good intro to Mindstorms and to LeJos Web: Too many sites to list. Just Google any mixture of mindstorms, lego, robot, sensors, java, nqc, rcx. Lots of good stuff, and most sites have links to lots of others. How can I find out more?

sources Owen Holland Peter Marwedel