610 likes | 1.23k Views
Arduino for Robots. Seminars at RSSC.org Sergei Grichine slg@quakemap.com. Agenda. Why Arduino Common boards – Uno, Mega , Pro Mini, Leonardo and just a chip. Why Arduino. Well designed cheap hardware Insanely simple programming at the beginning
E N D
Arduino for Robots Seminars at RSSC.org Sergei Grichine slg@quakemap.com
Agenda • Why Arduino • Common boards – Uno, Mega, Pro Mini, Leonardo and just a chip
Why Arduino • Well designed cheap hardware • Insanely simple programming at the beginning • Friendly IDE – “keep it simple-stupid!” • Exceptional documentation • Community support, samples, forums, experts • Have a question? Just google it!
Arduino Specs • Not much power – 16 MHz crystal, close to 16 MIPS • 8-bit AVR instruction set • Uno / ATmega328 • Flash Memory: 32 KB of which 0.5 KB used by bootloader • SRAM: 2 KB (a.k.a. RAM) • EEPROM: 1 KB (persistent storage for settings) • Mega: • Flash Memory: 256 KB of which 8 KB used by bootloader • SRAM: 8 KB • EEPROM: 4 KB
Code tips • Not C++, Not quite Java – but close • Google “arduino reference” • Functions - http://arduino.cc/en/Reference/HomePage • http://arduino-info.wikispaces.com/HOME
Code tour • Code: • Serial communication to a PC • Cooperative Multitasking • Preemptive Multitasking
Serial Communication • BalancerHeadServo • Short ASCII messages (one-liners) • Start symbol (for example, *) • Channel • Command • Position-based, separated values • Checksum *3 5 456 553 87 214
Serial Communication (Arduino side) • BalancerHeadServo
Serial Communication (Timing) • At 115200 baud = 10K bytes/sec *3 5 456 553 87 214 • 20 characters = 2ms • At 9600 baud – 25ms
Multitasking • Typical tasks: • Read Sensors • Compute • Actuate • Communicate
Cooperative Multitasking • Call functions from the loop() • Expect functions to return promptly • Common “tick” maintained by the loop code • Individual timing logic for each function
Cooperative Multitasking • BalancerDfr
Cooperative Multitasking • Everything is tied together • Delays can cause failure • Poor separation of code
Preemptive Multitasking • Some kind of OS/Dispatcher required • Tasks are independent functions • Tasks are interrupted at any time • Tasks can sleep() and yield() • Use “volatile” for shared variables
Preemptive Multitasking BalancerHeadServo #include <AVR_OS.h> AVR_OS os = AVR_OS(); // Create instance of OS multitasker os.os_init(); os.os_schedule_task(taskReadFeedbackPot, NULL, 0); os.os_schedule_task(taskActuate, NULL, 0); os.os_schedule_task(taskRemote, NULL, 0); os.os_schedule_task(taskReportState, NULL, 0); loop() { os.os_loop(); }
Preemptive Multitasking // Define task 3 - check for commands over the serial line void taskRemote(void *arg) { while(1) { if(!CheckSerial()) // false if no input { os.os_sleep(20); // no input; must be at least TICK_INTERVAL 2 } else { os.os_yield(); // have more to read } } }
Arduino for Robots Seminars at RSSC.org Sergei Grichine slg@quakemap.com