This program contains a class that makes it easy for the mbed to communicate with the Mini SSC II or the Pololu Maestro in SSC compatibility mode. (they are servo/motor controllers)
Diff: minissc.cpp
- Revision:
- 1:8f9d24a87600
- Parent:
- 0:399d7ca2d0bb
- Child:
- 2:e5458113c26b
--- a/minissc.cpp Sun Oct 16 03:54:50 2011 +0000 +++ b/minissc.cpp Sun Oct 16 05:08:27 2011 +0000 @@ -1,78 +1,67 @@ -/* - * Demo of communicating with Mini SSC 2. - */ - -#include "mbed.h" - -#define SSC_SYNC_BYTE 255 - -class MiniSSC2 { - public: - MiniSSC2(int num_motors); - ~MiniSSC2(); - void send(); - void send(int i_motor); - void set(int i_motor, char value); - private: - Serial* p_device; - int num_motors; - - //These arrays send input to the miniSSC2 which in turn sends to the motors. - //The format for the arrays: {SYNC_BYTE, servo number, speed} - //SYNC_BYTE tells the miniSSC2 that we're about to send new instructions. - //The servo number picks the motor. 1 = right, 2 = back, 3 = front, 4 = left - //We set the speed anywhere between 0 and 254. 127 is off, 254 is 100% forward, 0 is 100% back. Extrapolate. - //On the back motor, the numbers are reversed. 0 is 100% forward, 254 is 100% back. 127 is still off. - char motors[9]; // I think the Mini SSC2 supports 9 motors -}; - -MiniSSC2::MiniSSC2(int a) { - num_motors = a; - p_device = new Serial(p9, p10); //tx, rx - p_device->baud(9600); - for (int i = num_motors-1; i < num_motors; i++) - motors[i] = 127; -} - -MiniSSC2::~MiniSSC2() { - if (p_device != NULL) - delete p_device; -} - -void MiniSSC2::send() { - for (int i = 0; i < num_motors; i++) { - send(i); - } -} - -void MiniSSC2::send(int i_motor) { - i_motor--; - - p_device->putc(SSC_SYNC_BYTE); - p_device->putc((char)i_motor); - p_device->putc(motors[i_motor]); -} - -void MiniSSC2::set(int i_motor, char value) { - motors[i_motor-1] = value; -} - -DigitalOut led1(LED1); - -MiniSSC2 ssc(4); -Ticker ssc_to; - -void ssc_send_cb() { - ssc.send(); -} - -int main() { - ssc.set(1,33); - ssc.set(2,34); - ssc.set(3,35); - ssc.set(4,36); - - ssc_to.attach(&ssc_send_cb, 0.002); - while (true); -} -// Don't delete this comment. +/* + * Demo of communicating with Mini SSC 2. + */ + +#include "mbed.h" +#include "minissc.h" + +// MiniSSC2's Constructor +MiniSSC2::MiniSSC2(int a, int baud) { + num_motors = a; + p_device = new Serial(p9, p10); // (tx, rx) opens up new serial device (p_device is Serial* pointer + p_device->baud(baud); // Set the baud. + + set(127); +} + +// MiniSSC2's Destructor +MiniSSC2::~MiniSSC2() { + if (p_device != NULL) { + delete p_device; // must do this. otherwise, you'll have memory leakage & you may not be able to re-open the serial port + } +} + +void MiniSSC2::send() { + for (int i = 0; i < num_motors; i++) { + send(i); + } +} + +void MiniSSC2::send(int i_motor) { + // format: {sync byte, motor id, motor power} + // example: {255, 2, 24} + p_device->putc(SSC_SYNC_BYTE); + p_device->putc((char)i_motor); + p_device->putc(motors[i_motor-1]); +} + +void MiniSSC2::set(char value) { + for (int i = 1; i <= num_motors; i++) { + set(i, value); + } +} + +void MiniSSC2::set(int i_motor, char value) { + motors[i_motor-1] = value; +} + +char MiniSSC2::get(int i_motor) { + return motors[i_motor-1]; +} + +MiniSSC2 ssc(4, 9600); // 4 motors, baud for MiniSSC2 is 9600 +Ticker ssc_to; + +void ssc_send_cb() { + ssc.send(); // is there a more efficient way to do this? +} + +// MAIN FUNCTION ============================================================== +int main() { + ssc_to.attach(&ssc_send_cb, 0.002); // run ssc_send_cb() every 0.002 seconds (Ticker) + + while (true) { + ssc.set(127); + } +} +// Don't delete this comment.