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

minissc.cpp

Committer:
avbotz
Date:
2011-10-16
Revision:
0:399d7ca2d0bb
Child:
1:8f9d24a87600

File content as of revision 0:399d7ca2d0bb:

/* 
 * 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.