programming your robot (in c) terry grant, nasa, ames research center 1/23/03 1/30/03
TRANSCRIPT
![Page 1: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/1.jpg)
Programming Your Robot (in C)
Terry Grant,NASA, Ames Research Center
1/23/03
1/30/03
![Page 2: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/2.jpg)
Outline
• 1/23– Robotics Hardware & Software Architecture– Programming in C Introduction
• 1/30– Review: Robot Project Requirements &
Example – Simple Sumo Contest - Simple line follow– Teacher as Coach– Wrap-up
![Page 3: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/3.jpg)
Robot Building & Coding• Completed LEGO robot from MLCAD
– Ref: http://www.lm-software.com/mlcad/
– Art of LEGO Design
– http://handyboard.com/techdocs/artoflego.pdf
• Pictures and Code from the Workshop– http://robotics.nasa.gov/edu/BBworkshop03
• IC4 Environment downloads:– http://www.botball.org/about_botball/ic4.html
• Hands-on Challenges Ref:– http://robotics.nasa.gov/students/challenge.htm
![Page 4: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/4.jpg)
Robotics H/W & S/W Architecture
Interactive C v. 4.10* Editor* Debug Interpreter* LoaderOther Apps
Desktop Operating System
Desktop Hardware
Bot Multi-tasking S/W Components Real-Time Operating System * P-code interpreter * Input/Output Drivers - Clock * Load/Run modes
Handy Board or RCX H/W*Central Processor* Random Access Memory* Special I/O Circuits* Battery & Power Conditioner
Serial Data Interface
Charger (HB only)
Lego Motors& Sensors
LegoMechanical
IR for RCX*
![Page 5: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/5.jpg)
Robot Project Requirements
• Hardware configuration and general environmental constraints
• Operator Requirements
• Controller requirements
All Three Elements are needed and should be written down for a common team understanding
![Page 6: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/6.jpg)
Team Strategy & Plans
• Translating a Challenge into Requirements– Robot physical capabilities– Robot behavior (high level code)– Operator – robot interaction
• Assigning tasks and milestones
• Writing a total schedule (initial and revised)– Plan to test capabilities & behavior– Plan for full robot tests & re-planning– Plan for team coordination meetings
![Page 7: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/7.jpg)
Programming in C - Introduction
• IC4 provides an editing, compiling, and downloading environment for either RCX or Handy Board.
• Follows C syntax (grammar)• Uses functions < xyz() > declared and called• Many functions for Input/Output are preloaded in a
library• Good tutorial examples provided with the application • Multi-tasking capability in O.S.
– allows sampling & holding multiple conditions in parallel:
position, direction, and other sensors
![Page 8: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/8.jpg)
General Syntax
• declaring:
output type Function(inputs e.g. int x, int y) {block of statements}
• calling: Function(x, y);
• types: int x, y, z;float a, b, c;
all variables must have a declared type.– global types are defined at the top, outside of a
function, and usable by all functions.
![Page 9: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/9.jpg)
Simple Example
Make a Robot Go Forward and Return– H/W & Environment:
Build a bot with the RCX, wired to motors such that forward power moves wheels forward, and put on a demonstration table with enough flat surface
– Operator:Write the code, load the RCX, and initiate the execution
(running) of the code
– The controller:Turn on the motors forward, wait 2 seconds, reverse the motors,
wait 2 seconds, then stop.
![Page 10: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/10.jpg)
Simple Code ExampleIC4void main(){ fd(A); fd(C); sleep(2.0); bk(A); bk(C); sleep(2.0); off(A); off(C);}
• Open Interactive C to view the actual environment & write code
![Page 11: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/11.jpg)
More Basics• Three modes: off, standby, run• Use of ‘view’ button function w/o running a program• Use of ‘Interaction’ window in IC4
– battery_volts() to check battery– Test new functions for I/O,
• Check list of library functions, global variables• Download firmware• Upload Arrays for spread-sheet analysis• Edit aids
– Auto-indentation– Parenthesis matching– Syntax checking (on download)
• Use of ‘save as’ to file new, or trial code
![Page 12: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/12.jpg)
Notation of IC 4IC notation is the same for
RCX & HBif ("condition"){ "statements"}else{ "statements"}
while ("condition")
{
"statements"
}
![Page 13: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/13.jpg)
Notation of IC4 -2
Defining a function or task:xxx “name”(){ "statements"}xxx = ‘void’ if no return variables = ‘int’ if integer return variables = ‘float’ if floating point return variables
![Page 14: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/14.jpg)
Notation of IC4 - 3
Starting and ending parallel tasks:
pid = start_process(taskname());
kill_process(pid);
![Page 15: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/15.jpg)
Notation of IC4 - 4
Inputs for RCX
- light(y) for y = 1,2, or 3
- light_passive(y)
- digital(y) or touch(y)
![Page 16: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/16.jpg)
Notation of IC4 - 5
IC OutputsMotor outputs, ports 1 to 3 (or A to C)
To use port 1:
fd(1); forward, positive voltage
bk(1); backward, negative voltage
Motor(1, x); x = -100 to 100
off(1); leave port ‘open’
brake(1); for the RCX only, to brake the motor
![Page 17: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/17.jpg)
Notation of IC4 - 6
To display on Controller LCD e.g.
printf(“Hello\n”);
printf(“X= %d\n”, x); /* x is an integer */
printf(“X= %f\n”, y); /* y is floating point */
printf(“%d -%d\n”, a, b); /* a & b are integers */
In the RCX only five characters total can be displayed,
and “\n” is not needed.
![Page 18: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/18.jpg)
Sumo Example
![Page 19: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/19.jpg)
Sumo Requirements• Robots start facing each other at the edge of a central ring.• Robots must start when a button is pushed or the light
comes on.• Robots must stop after T (5-15) seconds.• The first robot to touch the barrier (or cross line) loses.
Bot 1 Bot 24’ x 4’ barrier
Starting Light
![Page 20: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/20.jpg)
Light Sensor
• Sensor includes a LED source: red & near IR.
• Photodetector responds strongly to near IR as well as red. [lower = more]
• Response changes according to ambient light & Battery voltage.
• Mounting assembly attaches to front bumper facing down as shown in the cover picture.
![Page 21: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/21.jpg)
Simple Sumo Behavior
• Display program title
• Wait for prgm_button push, then beep
• Wait 3 seconds to start
• Go straight forward – while T not exceeded,
• Stop quickly and turn if line is sensed
• Back away & turn if bumped
– When T exceeded brake to a stop
![Page 22: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/22.jpg)
Simple Sumo code// LEGO-based Sumo #6 for widetrack bot tlg 1/20/03
// assumes front bumper on port 3, light sensor on 2, motors on A & C
// start 3 seconds after prgm button push
#define TURN_TIME 0.45
#define THRES 750 /* assumes nominal white is ~ 720 */
void main()
{
long time;
printf("SUMo6");
while(!prgm_button());
beep();
sleep(3.); // wait 3 seconds
time =mseconds();
//beep();
![Page 23: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/23.jpg)
Simple Sumo code – cont’dmotor(A,30); motor(C,30);// start straight ahead while(15000L > (mseconds()-time)){ //run time if(light(2)>THRES){ //wait for edge brake(A);brake(C);sleep(.05); //quick stop motor(C,-45);off(A); //turn sleep(TURN_TIME); motor(A,30); motor(C,30); sleep(.2); } if(digital(3)){//back away and turn if bumped motor(A,-30);motor(C,-30);sleep(.2); brake(A); sleep(TURN_TIME); motor(A,30); motor(C,30); sleep(.2); } } brake(A); brake(C);}
![Page 24: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/24.jpg)
Light Trigger Calibration• Hardware & Environment
– L1 is the remote trigger light.
– L2 is the room lighting.
– Pd photodetector has a wide field of view.
• The Controller display helps the operator measure both the dark and light response.
• The controller [RCX code] sets the “light vs. dark” threshold and waits for the threshold to be exceeded to trigger the action.
![Page 25: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/25.jpg)
Sumo - Sensor Test Project
• To support a robot sumo contest with a light start, design a robust light trigger for a “sumo wrestling” action which runs the motors for 5 seconds after a light is turned on.– Discuss all requirements (total group)– Write a code design for each Bot. – Write and debug the code
• Participate in a Bot Sumo contest• Compare trigger and behavior designs and results
![Page 26: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/26.jpg)
Sumo - Sensor Test Behavior e.g.• Display program title [for a few seconds]• Repeat sequence while program is running• While prgm_button is not pushed,
– Display sensor level and– Prompt for prgm_button push– While view_button is pushed,
display and increment the trigger threshold
• When prgm_button is pushed,– Display sensor level – Wait for sensor level to cross the trigger threshold, then go forward,
etc as original sumo - measuring run time
• When T is exceeded: stop, – display “done” for a few seconds
• Repeat
![Page 27: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/27.jpg)
Line Following Experiments
![Page 28: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/28.jpg)
Line Following Experiments
• Simple, one sensor
• Line turns to the right
• Check sensor responses first
• Use touch sensor to start & stop
![Page 29: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/29.jpg)
Checking sensor firstwhile(digital(1)==0){ // check sensor until switch is hit
printf("%d", light(2)); //move on and off line here
sleep(.3);
}
while(digital(1)==1); // wait here to release switch
![Page 30: Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03](https://reader035.vdocuments.mx/reader035/viewer/2022070306/5519057055034626428b4682/html5/thumbnails/30.jpg)
Follow line until touch sensor hit// follow line to the rightmotor(A,30); motor(C,30); // start going straight while (digital(1)==0) { // until switch is hit
if (light(2) < THRESHOLD) { // if brighter than line motor(C,-30);off(A); // turn right
while (light(2) < THRESHOLD); // wait until >= motor(A,30); motor(C,30); // go straight
} }
ao(); // turn off motors when done