310 likes | 336 Vues
Programming Your Robot (in C). Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03. 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
E N D
Programming Your Robot (in C) Terry Grant, NASA, Ames Research Center 1/23/03 1/30/03
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
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
Robotics H/W & S/W Architecture 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 Interactive C v. 4.10 * Editor * Debug Interpreter * Loader Other Apps Desktop Operating System Desktop Hardware IR for RCX* Serial Data Interface Charger (HB only) Lego Mechanical Lego Motors & Sensors
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
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
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
General Syntax • declaring: output type Function(inputs e.g.int x, int y) {block ofstatements} • 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.
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.
IC4 voidmain() { 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 Simple Code Example
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
Notation of IC 4 IC notation is the same for RCX & HB if ("condition") { "statements" } else { "statements" } while ("condition") { "statements" }
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 Notation of IC4 -2
Notation of IC4 - 3 Starting and ending parallel tasks: pid = start_process(taskname()); kill_process(pid);
Inputs for RCX - light(y) for y = 1,2, or 3 - light_passive(y) - digital(y) or touch(y) Notation of IC4 - 4
IC Outputs Motor 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 Notation of IC4 - 5
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.
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. Starting Light 4’ x 4’ barrier Bot 1 Bot 2
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.
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
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();
Simple Sumo code – cont’d motor(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); }
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.
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
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
Line Following Experiments • Simple, one sensor • Line turns to the right • Check sensor responses first • Use touch sensor to start & stop
Checking sensor first while(digital(1)==0){ // check sensor until switch is hit printf("%d", light(2)); //moveon and off line here sleep(.3); } while(digital(1)==1); // wait here to release switch
Follow line until touch sensor hit // follow line to the right motor(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