Software Tutorial 26 Sept 2009
Agenda Development Environment Brain Board API Sensors Basic Behaviour Control
MPLAB/C18 MPLAB: IDE, free download from microchip.com C18: C compiler for PIC microcontrollers, also from microchip.com
MPLAB IDE
Brain Board Capabilities PIC18F4580 –32KB program memory, 16K instructions –1536 B SRAM –256 B EEPROM Board –2 independent 5A motor drivers –6 Analog inputs (10-bit) –4 LEDs
API main.c: Master loop, setup functions brain.c: Functions called by master loop control.c: I/O filtering utilities.c: String output, delay function
API sensor_val[0-5] automatically updated Set motor1_speed, motor2_speed to a value in [-100,100] Logic: brain.c/stateMachine() Sensor conversion: control.c/readDist()
Sensors IR rangefinders –Sharp GP2D120(4-30 cm) IR line sensors –QRD1114
Sharp IR Rangefinders Pros –Low cost, low power, low mass –Sensor emissions are at speed of light –Divergence is not too bad Cons –Other IR sources can interfere (sun, other sensors) –Surface properties (absorption/reflection/transmission)
Sharp IR Rangefinders
Line Sensor Circuit
Behaviour Control Deliberative Control –“Traditional approach”, tends to use brute force –Better at being goal-directed –Path planning, localization, “AI” Reactive Control –Freeform, attempts to mirror nature –Roomba vacuum, “BEAM” robotics
Deliberative Control LocalizationCognition Perception Motion Control
Reactive Control React Perception Motion Control
Finite State Machines Easy to implement Easy to debug
Finite State Machines Exhibits different behaviour depending on the state “State” is general description of behaviour –Explore/Recharge/Hide –One state at a time “State transitions” are defined
Example State Diagram Search Attack Avoid Line
Example Code state = SEARCH while(true) { switch(state) case ATTACK: drive_straight if (nothing_on_sensors) state = SEARCH case SEARCH: drive_in_circles if (something_on_sensors) state = ATTACK else if (line_sensors_on) state = LINE case LINE: back_up state = SEARCH } Ignores lines Prioritizes ATTACK over LINE Hardcoded, unchanging behaviour
Example State Diagram Search Attack Avoid Line
Example State Diagram Search Attack Reversing Turning case LINE Augmented Finite State Machine (AFSM)
Example Code state = SEARCH while(true) { switch(state) case ATTACK: drive_straight if (nothing_on_sensors) state = SEARCH case SEARCH: drive_in_circles if (something_on_sensors) state = ATTACK else if (line_sensors_on) state = LINE case LINE: counter1 = 0 if (something_on_sensors) state = ATTACK else if counter1 < 10 drive_back else if counter1 < 20 turn_around else state = SEARCH } Allows transition to attack at any point Sequenced behaviour within state Global variable
Questions? Me: James
Appendix A Calibrating IR Rangefinders
Response Graph Important note: –Here, independent variable is distance –From a software perspective, independent variable is input voltage –Need to invert the function (in a swap x&y sense)
Response Graph
Possible Models y = a 1 e -a 0 x y = a 1 ln(x) + a 0 y = a 1 x -a 0 y = a 1 /x + a 0 Fastest to compute All models have a decent fit
Example Data Analog ValueActual Distance [cm]
Fitting Curve y = a 1 /x + a 0 Can find a 1,a 0 in many ways –Invert matrix, iterative techniques Software: MATLAB, Excel Excel Worksheet available –Uses built-in Excel matrix functions –Can also use “Goal Seek” (not in all versions of Excel)
Worksheet Input From debug output From measurement
Worksheet Output Intermediate calculation Final result (also, these cells contain the solution equations) Fitted data for display on graph (solid line)
Using Worksheet Fill in analog value/distance pairs (bordered in red on sheet) –14 pairs – take this amount of measurements or modify the sheet Results are bordered in blue on sheet
Integrating into API In main.c/setup(): –Set sensor_type[i] to SENSOR_RANGE or SENSOR_LINE (depending on type) –In example code: for (i = 0; i < 3; i++)//first 4 inputs range sensors sensor_type[i] = SENSOR_RANGE; for (i = 3; i < 6; i++)//last 4 are line sensors sensor_type[i] = SENSOR_LINE; In control.h: –For IR sensors, set each sensor’s constants to Excel-computed values #define ADx_MULT6000 #define ADx_ADD5 If the i-th sensor is a line, just check: –sensor_val[i] == ON_LINE or sensor_val[i] == OFF_LINE Otherwise, sensor_val[i] contains the distance in cm