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)
minissc.cpp
- Committer:
- avbotz
- Date:
- 2012-02-26
- Revision:
- 2:e5458113c26b
- Parent:
- 1:8f9d24a87600
- Child:
- 3:5c664d91ba63
File content as of revision 2:e5458113c26b:
/* * Demo that communicates with the MiniSSC 2. This also works with the Pololu Mini Maestro. */ #include "mbed.h" #include "minissc.h" // MiniSSC2's Constructor MiniSSC2::MiniSSC2(int num_motors, int baud, PinName tx, PinName rx) { this->num_motors = num_motors; p_device = new Serial(tx, rx); // (tx, rx) opens up new serial device (p_device is Serial* pointer) p_device->format(8, Serial::None, 1); p_device->baud(baud); // Set the baud. set(127); // The motors should start stationary (zero power) } // MiniSSC2's Destructor MiniSSC2::~MiniSSC2() { if (p_device != NULL) { // must do this. otherwise, you'll have memory leakage & you may not be able to re-open the serial port later delete p_device; } } 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, newline} // example: {SSC_SYNC_BYTE, 2, 24, '\n'} sets motor 2 to power level 24 p_device->putc(SSC_SYNC_BYTE); p_device->putc((unsigned char)i_motor); p_device->putc(motors[i_motor]); } void MiniSSC2::set(unsigned char value) { for (int i = 0; i < num_motors; i++) { set(i, value); } } void MiniSSC2::set(int i_motor, unsigned char value) { motors[i_motor] = value; } unsigned char MiniSSC2::get(int i_motor) { return motors[i_motor]; } void ssc_send_cb() { PC.printf("Sent (callback)\n"); ssc.send(); // is there a more efficient way to do this? } /* MAIN FUNCTION */ int main() { led1 = 1; wait_ms(500); //ssc_to.attach(&ssc_send_cb, 1); // run ssc_send_cb() at 10 Hz (Ticker) (increase if possible) PC.baud(115200); PC.printf("\n\r\n\rHello."); while (true) { for (int i = 0; i < 4; i++) { unsigned char in = 127; PC.printf("> "); PC.scanf("%d", &in); ssc.set(i, in); PC.printf("\n\rSaved motor %d, power %d\n\r", i, (int)in); } ssc.send(); PC.printf("Sent all motors\n\r\n\r"); } } // Don't delete this comment