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)

Dependencies:   mbed

Committer:
avbotz
Date:
Sun Feb 26 02:03:17 2012 +0000
Revision:
2:e5458113c26b
Parent:
1:8f9d24a87600
Child:
3:5c664d91ba63
Reassigns the motors to servos 0-3 and uses scanf() for input; ; Also, it works now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
avbotz 2:e5458113c26b 1 /*
avbotz 2:e5458113c26b 2 * Demo that communicates with the MiniSSC 2. This also works with the Pololu Mini Maestro.
avbotz 1:8f9d24a87600 3 */
avbotz 1:8f9d24a87600 4
avbotz 1:8f9d24a87600 5 #include "mbed.h"
avbotz 1:8f9d24a87600 6 #include "minissc.h"
avbotz 1:8f9d24a87600 7
avbotz 1:8f9d24a87600 8 // MiniSSC2's Constructor
avbotz 2:e5458113c26b 9 MiniSSC2::MiniSSC2(int num_motors, int baud, PinName tx, PinName rx) {
avbotz 2:e5458113c26b 10 this->num_motors = num_motors;
avbotz 2:e5458113c26b 11 p_device = new Serial(tx, rx); // (tx, rx) opens up new serial device (p_device is Serial* pointer)
avbotz 2:e5458113c26b 12 p_device->format(8, Serial::None, 1);
avbotz 2:e5458113c26b 13 p_device->baud(baud); // Set the baud.
avbotz 2:e5458113c26b 14 set(127); // The motors should start stationary (zero power)
avbotz 1:8f9d24a87600 15 }
avbotz 1:8f9d24a87600 16
avbotz 1:8f9d24a87600 17 // MiniSSC2's Destructor
avbotz 1:8f9d24a87600 18 MiniSSC2::~MiniSSC2() {
avbotz 1:8f9d24a87600 19 if (p_device != NULL) {
avbotz 2:e5458113c26b 20 // must do this. otherwise, you'll have memory leakage & you may not be able to re-open the serial port later
avbotz 2:e5458113c26b 21 delete p_device;
avbotz 1:8f9d24a87600 22 }
avbotz 1:8f9d24a87600 23 }
avbotz 1:8f9d24a87600 24
avbotz 1:8f9d24a87600 25 void MiniSSC2::send() {
avbotz 1:8f9d24a87600 26 for (int i = 0; i < num_motors; i++) {
avbotz 1:8f9d24a87600 27 send(i);
avbotz 1:8f9d24a87600 28 }
avbotz 1:8f9d24a87600 29 }
avbotz 1:8f9d24a87600 30
avbotz 1:8f9d24a87600 31 void MiniSSC2::send(int i_motor) {
avbotz 2:e5458113c26b 32 // format: {sync byte, motor id, motor power, newline}
avbotz 2:e5458113c26b 33 // example: {SSC_SYNC_BYTE, 2, 24, '\n'} sets motor 2 to power level 24
avbotz 1:8f9d24a87600 34 p_device->putc(SSC_SYNC_BYTE);
avbotz 2:e5458113c26b 35 p_device->putc((unsigned char)i_motor);
avbotz 2:e5458113c26b 36 p_device->putc(motors[i_motor]);
avbotz 1:8f9d24a87600 37 }
avbotz 1:8f9d24a87600 38
avbotz 2:e5458113c26b 39 void MiniSSC2::set(unsigned char value) {
avbotz 2:e5458113c26b 40 for (int i = 0; i < num_motors; i++) {
avbotz 1:8f9d24a87600 41 set(i, value);
avbotz 1:8f9d24a87600 42 }
avbotz 1:8f9d24a87600 43 }
avbotz 1:8f9d24a87600 44
avbotz 2:e5458113c26b 45 void MiniSSC2::set(int i_motor, unsigned char value) {
avbotz 2:e5458113c26b 46 motors[i_motor] = value;
avbotz 1:8f9d24a87600 47 }
avbotz 1:8f9d24a87600 48
avbotz 2:e5458113c26b 49 unsigned char MiniSSC2::get(int i_motor) {
avbotz 2:e5458113c26b 50 return motors[i_motor];
avbotz 1:8f9d24a87600 51 }
avbotz 1:8f9d24a87600 52
avbotz 1:8f9d24a87600 53 void ssc_send_cb() {
avbotz 2:e5458113c26b 54 PC.printf("Sent (callback)\n");
avbotz 1:8f9d24a87600 55 ssc.send(); // is there a more efficient way to do this?
avbotz 1:8f9d24a87600 56 }
avbotz 1:8f9d24a87600 57
avbotz 2:e5458113c26b 58 /* MAIN FUNCTION */
avbotz 1:8f9d24a87600 59 int main() {
avbotz 2:e5458113c26b 60 led1 = 1;
avbotz 2:e5458113c26b 61 wait_ms(500);
avbotz 2:e5458113c26b 62 //ssc_to.attach(&ssc_send_cb, 1); // run ssc_send_cb() at 10 Hz (Ticker) (increase if possible)
avbotz 2:e5458113c26b 63 PC.baud(115200);
avbotz 2:e5458113c26b 64
avbotz 2:e5458113c26b 65 PC.printf("\n\r\n\rHello.");
avbotz 1:8f9d24a87600 66
avbotz 1:8f9d24a87600 67 while (true) {
avbotz 2:e5458113c26b 68 for (int i = 0; i < 4; i++) {
avbotz 2:e5458113c26b 69 unsigned char in = 127;
avbotz 2:e5458113c26b 70 PC.printf("> ");
avbotz 2:e5458113c26b 71 PC.scanf("%d", &in);
avbotz 2:e5458113c26b 72 ssc.set(i, in);
avbotz 2:e5458113c26b 73 PC.printf("\n\rSaved motor %d, power %d\n\r", i, (int)in);
avbotz 2:e5458113c26b 74 }
avbotz 2:e5458113c26b 75 ssc.send();
avbotz 2:e5458113c26b 76 PC.printf("Sent all motors\n\r\n\r");
avbotz 1:8f9d24a87600 77 }
avbotz 1:8f9d24a87600 78 }
avbotz 2:e5458113c26b 79 // Don't delete this comment