370 likes | 527 Views
Interface Electronics. One PWM Source Per Motor (Using a Dual 4-Input Multiplexer). Vcc (5V). REV - PWM. PWM. FWD - PWM. DIR. R1 1k Ω. R2 1k Ω. http://www.neufeld.newton.ks.us/electronics/?p=32. Microcontroller Restrictions.
E N D
One PWM Source Per Motor(Using a Dual 4-Input Multiplexer) Vcc (5V) REV - PWM PWM FWD - PWM DIR R11kΩ R21kΩ http://www.neufeld.newton.ks.us/electronics/?p=32
Microcontroller Restrictions 2x16 LCD is tied to 7 pins of a specific PORT. Which PORT to choose? OC2A OC1A OC1A OC2B
+5V 47K Vsensor Rphoto Photocell Voltage Divider Circuit
Actual Differential Photocell Sensor Schematic +5V 47K Rphoto1 Vout Rphoto2 Vout = ?
Limiting Current Draw to an LED Vcc R2 R1 To Microcontroller
Phototransistor +5V 47K Vsensor Phototransistor
Reflectance Sensor Interface Diagram +5V +5V 220 to 470 47K R1 Vsensor Phototransistor LED1 Reflective/Transmissive Opto-Sensor
10 Meg - + - + +5 100k Alternatives • Photodiode • Phototransistor
Sensor Interpretation World Modeling Planning Execution The Old Approach:World Modeling & Planning • Given a control task: • collect and interpret data from multiple sensors, • fuse data into a consistent world model, {Sensor Fusion} • formulate a plan to achieve some goal, and • execute the plan by sending a series of commands to the actuators. Sensors Actuators
World Modeling - Continued • Advantages • Guaranteed Solution (or lack of) • Optimized Solution • Disadvantages • Optimized Solution • Expensive – Requires significant computation, storage, power • Modeling Difficulties - Sensors are subject to noise, conflicts, errors • Time - World changes before executing plan • Applications • Well suited to static environments • Example: Industrial Robots – Pick ‘N’ Place • Would this approach work for a mobile robot?
Sensors Software Behavior Actuators The New Approach: Behavior-Based Programming(also called Reaction Based Programming) • Based on work of Rodney Brooks and the Mobile Robot Group at the MIT AI Lab [1986] • A.K.A. Behavior Control, Subsumption Architecture Basic Subsumption Architecture • Given a control task, decompose control system into a network of task-achieving behaviors where: • sensors trigger behaviors, • behaviors run in parallel and are prioritized, • higher lever behaviors may temporarily suppress or subsume lower level ones, and • behaviors are ‘fused’ to produce overall program ‘behavior.’ {Behavior Fusion} Subsumption Diagrams • Simple depiction of the subsumption architecture
Subsumption - Continued • Advantages • Computationally Efficient • No world model – decreased storage requirements • Behaviors are like simple reflexes – less computation • Extendable • Simply add new behaviors to achieve higher levels of competence or more interesting behavior • Add new behaviors without modifying or affecting old • Robust • Can deal in real-time with a changing world. There is no predefined model or plan to update – just have to react consistently to the same sensor input • Limitations of sensors and sensor data are concealed by making local decisions rather than global ones • Scalable • If we improve the control program by adding new behaviors, the program does not necessarily run slower • Scales easily to multiple parallel processors • Emergence of Complex Behaviors • Surprisingly, layers of relatively simple behaviors acting together often produce seemingly complex behavior overall • Disadvantage(s) – Sub-Optimal Solutions
Stop Button Reset Photocells Dance S Forward Motors S BehaviorsA Subsumption Diagram
Stop Button Reset Photocells Dance S Forward Motors S BehaviorsA Subsumption Diagram Action Sequence Priorities
Execution of a Behavior CHECK SENSOR Trigger No Yes Set Command = Action
Behavior Based ProgrammingUsing ATMEL’s Microcontroller • BEHAVIORS • Default action is REST/OFF • If triggered, set to next action and set time to completion • Upon meeting time to completion, set next action and reset time to completion. • When all actions are completed, reset action to REST/OFF. • ARBITRATE • Call each behavior • Determine highest triggered behavior • Pass highest action to the motor controller. • The ONLY place in your code that sets the motors! • If no behaviors are triggered, set the motors to the default (typically FORWARD). • MOTOR_CONTROLLER • Sets left and right motors to appropriate action (i.e., FORWARD, REVERSE…) • Calls the motor() library function. • Must add lower level routines: • Setup Interrupts to detect changes in state • Create a Timer Routine (e.g., second() ) • Use the motor() Routine from the 1st lab
Stop Button Reset Photocells Dance S Forward Motors S Behaviors (An Example)
Behavior 1: reset • Whenever the stop button is pressed, stop the robot and return to program start. • The code would like something like: Stop Button reset Motors #define GO 0 #define STOP 1 //Global Variables intreset_action; reset_action = GO; //This will be in the Main routine void reset () { if ( stop_button() ) //Could be set via an interrupt { reset_action = STOP; } }
Stop Button Reset Photocells Dance S Forward Motors S Behavior 2: dance · Dance when the light goes down! What actions must Dance perform?
Photocells Dance Forward Motors S Default Forward Dance Reverse Turn Left Dance Reverse Turn Right State (Behavior) Actions Behavior 2: dance · Dance when the light goes down! What actions must Dance perform? Use a State Transition Diagram! Start Photocell < 75 75 <=Photocell < 150 Return Return How would you add Reset?
Dance Behavior CHECK Photocell Backup (5s) < 150 No Turn (10s) Yes Set Command = Backup Timer Set Command = Turn Random Timer Set Command = Rest
Behavior 2: Dance Code #define REST 0 #define STOP 1 #define FORWARD 2 #define REVERSE 3 #define TURN_RIGHT 4 #define TURN_LEFT 5 #define OFF 6 //Port Assignments int PHOTO_PORT = 3; //Global Variables int dance_action; float dance_timer; int dance_photo; //Set Times #define REVERSE_DELAY 1.3 dance_action = REST; //This will be in the Main routine void dance() { if (dance_action != REST) { dance_photo = adc_data; if (dance_photo < 150) { dance_action = REVERSE; dance_timer = seconds() + REVERSE_DELAY; } } . . .
Behavior 2: Dance Code (continued) void dance() { if (dance_action == REST) { dance_photo = analog(PHOTO_PORT); if (dance_photo < 150) { dance_action = REVERSE; dance_timer = seconds() + REVERSE_DELAY; } } else if (dance_action == REVERSE) { if (seconds() > dance_timer) { if (dance_photo < 75) dance_action = TURN_LEFT; else dance_action = TURN_RIGHT; dance_timer = seconds() + (float)random(128)/100 } } else if (dance_action == TURN_LEFT || dance_action == TURN_RIGHT) { if (seconds() > dance_timer) dance_action = REST } } Note: There are no delays in this;
Stop Button Reset Photocells Dance S Forward Motors S Behaviors How do you “subsume” behaviors?
Arbitrate Routine • To implement the suppressor node we need an arbitration mechanism. • When activated, a behavior should suppress or subsume all lower level behaviors. • The Dance behavior (reversing and then turning for a while) will temporarily control the motors as long as no higher priority behavior if activated (e.g., rest). • When the dance terminates, any lower level behaviors resume (e.g. going forward). • void main () • { • int motor_cmd; • int old_motor_cmd; • int flag; • while(TRUE) • { • //Wait here for the start button to be pressed. • ? • //Initialize the behaviors • dance_action = REST; • reset_action = REST; • //Initialize local variables • old_motor_cmd = -1; • flag = TRUE; • while(flag) //Arbitrate • . • . • .
Arbitrate Routine (Continued) while(flag) //Arbitrate { //Check Behaviors dance(); reset_robot(); //Select highest priority action if (reset_robot != OFF) { motor_cmd = reset_action; flag = FALSE; } else if (dance_action != REST) motor_cmd = dance_action; else //Set default motion motor_cmd = FORWARD; //Execute action (only if it is a change) if (motor_cmd != old_motor_cmd) { motorControl(motor_cmd); old_motor_cmd = motor_cmd; } }
Adding a New Behavior: Escape • When a bumper is struck, backup ¼ of the length of the robot and then turn 45º away from the object struck (e.g., turn right if the left bumper was struck). • What’s Priority Order? • Escape • Reset • Dance
Adding a New Behavior: Escape • When a bumper is struck, backup ¼ of the length of the robot and then turn 45º away from the object struck (e.g., turn right if the left bumper was struck). Stop Button Reset Bumper Escape S Photocells Dance S Forward Motors S
Arbitrate Subroutine while(flag) //Arbitrate { //Check Behaviors dance(); reset(); escape(); //Select highest priority action if (reset_robot != OFF) { motor_cmd = reset_action; flag = FALSE; } else if (escape_action != REST) motor_cmd = dance_action; else if (dance_action != REST) motor_cmd = dance_action; else //Set default motion motor_cmd = FORWARD; //Execute action (only if it is a change) if (motor_cmd != old_motor_cmd) { motorControl(motor_cmd); old_motor_cmd = motor_cmd; } } Note: Only the Arbitrate Routine sends commands to the motors!
C Program Structure(Your code should follow this order) • Title • Team # • Team Member Names • Global Variables • Subroutines & Functions • ISR • Sensors • Actuators (i.e., Motor Controller) • Calculations • Behaviors (Processes) • Main /Arbitration Routine • Note: • Include Team # & Title in subject line of email • Send to TA and myself • Each deviation will cost you 5%!
Standardize Control //Motor Commands #define REST0 #define FORWARD 1 /* Setup Motor Commands */ #define REVERSE 2 #define TURN_LEFT 3 #define TURN_RIGHT 4 #define STOP 5 //PhotoSensor #define THRESHOLD 125 /* Light Threshold */ void motor (int mtr, int speed) { … } void motorControl (int cmd) { … } void behaviors() { … } void main () { … } In this Order!
Behavior Based “Rules” • Self-contained modules • Does not reference another Behavior, only Functions • The Behavior’s Flag and Motor Command appear only in the behavior’s code or the arbitrate command, no where else!
Coding Errors in C • Case Sensitive • Also, note that reserved words are in blue • Use LOCAL VARIABLES vice GLOBAL when appropriate • NEVER (?) use a delay command. Why? Except…?