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@0:399d7ca2d0bb, 2011-10-16 (annotated)
- Committer:
- avbotz
- Date:
- Sun Oct 16 03:54:50 2011 +0000
- Revision:
- 0:399d7ca2d0bb
- Child:
- 1:8f9d24a87600
Initial commit. ; ; Untested - Basic stuff to send to the Mini SSC 2.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
avbotz |
0:399d7ca2d0bb | 1 | /* |
avbotz |
0:399d7ca2d0bb | 2 | * Demo of communicating with Mini SSC 2. |
avbotz |
0:399d7ca2d0bb | 3 | */ |
avbotz |
0:399d7ca2d0bb | 4 | |
avbotz |
0:399d7ca2d0bb | 5 | #include "mbed.h" |
avbotz |
0:399d7ca2d0bb | 6 | |
avbotz |
0:399d7ca2d0bb | 7 | #define SSC_SYNC_BYTE 255 |
avbotz |
0:399d7ca2d0bb | 8 | |
avbotz |
0:399d7ca2d0bb | 9 | class MiniSSC2 { |
avbotz |
0:399d7ca2d0bb | 10 | public: |
avbotz |
0:399d7ca2d0bb | 11 | MiniSSC2(int num_motors); |
avbotz |
0:399d7ca2d0bb | 12 | ~MiniSSC2(); |
avbotz |
0:399d7ca2d0bb | 13 | void send(); |
avbotz |
0:399d7ca2d0bb | 14 | void send(int i_motor); |
avbotz |
0:399d7ca2d0bb | 15 | void set(int i_motor, char value); |
avbotz |
0:399d7ca2d0bb | 16 | private: |
avbotz |
0:399d7ca2d0bb | 17 | Serial* p_device; |
avbotz |
0:399d7ca2d0bb | 18 | int num_motors; |
avbotz |
0:399d7ca2d0bb | 19 | |
avbotz |
0:399d7ca2d0bb | 20 | //These arrays send input to the miniSSC2 which in turn sends to the motors. |
avbotz |
0:399d7ca2d0bb | 21 | //The format for the arrays: {SYNC_BYTE, servo number, speed} |
avbotz |
0:399d7ca2d0bb | 22 | //SYNC_BYTE tells the miniSSC2 that we're about to send new instructions. |
avbotz |
0:399d7ca2d0bb | 23 | //The servo number picks the motor. 1 = right, 2 = back, 3 = front, 4 = left |
avbotz |
0:399d7ca2d0bb | 24 | //We set the speed anywhere between 0 and 254. 127 is off, 254 is 100% forward, 0 is 100% back. Extrapolate. |
avbotz |
0:399d7ca2d0bb | 25 | //On the back motor, the numbers are reversed. 0 is 100% forward, 254 is 100% back. 127 is still off. |
avbotz |
0:399d7ca2d0bb | 26 | char motors[9]; // I think the Mini SSC2 supports 9 motors |
avbotz |
0:399d7ca2d0bb | 27 | }; |
avbotz |
0:399d7ca2d0bb | 28 | |
avbotz |
0:399d7ca2d0bb | 29 | MiniSSC2::MiniSSC2(int a) { |
avbotz |
0:399d7ca2d0bb | 30 | num_motors = a; |
avbotz |
0:399d7ca2d0bb | 31 | p_device = new Serial(p9, p10); //tx, rx |
avbotz |
0:399d7ca2d0bb | 32 | p_device->baud(9600); |
avbotz |
0:399d7ca2d0bb | 33 | for (int i = num_motors-1; i < num_motors; i++) |
avbotz |
0:399d7ca2d0bb | 34 | motors[i] = 127; |
avbotz |
0:399d7ca2d0bb | 35 | } |
avbotz |
0:399d7ca2d0bb | 36 | |
avbotz |
0:399d7ca2d0bb | 37 | MiniSSC2::~MiniSSC2() { |
avbotz |
0:399d7ca2d0bb | 38 | if (p_device != NULL) |
avbotz |
0:399d7ca2d0bb | 39 | delete p_device; |
avbotz |
0:399d7ca2d0bb | 40 | } |
avbotz |
0:399d7ca2d0bb | 41 | |
avbotz |
0:399d7ca2d0bb | 42 | void MiniSSC2::send() { |
avbotz |
0:399d7ca2d0bb | 43 | for (int i = 0; i < num_motors; i++) { |
avbotz |
0:399d7ca2d0bb | 44 | send(i); |
avbotz |
0:399d7ca2d0bb | 45 | } |
avbotz |
0:399d7ca2d0bb | 46 | } |
avbotz |
0:399d7ca2d0bb | 47 | |
avbotz |
0:399d7ca2d0bb | 48 | void MiniSSC2::send(int i_motor) { |
avbotz |
0:399d7ca2d0bb | 49 | i_motor--; |
avbotz |
0:399d7ca2d0bb | 50 | |
avbotz |
0:399d7ca2d0bb | 51 | p_device->putc(SSC_SYNC_BYTE); |
avbotz |
0:399d7ca2d0bb | 52 | p_device->putc((char)i_motor); |
avbotz |
0:399d7ca2d0bb | 53 | p_device->putc(motors[i_motor]); |
avbotz |
0:399d7ca2d0bb | 54 | } |
avbotz |
0:399d7ca2d0bb | 55 | |
avbotz |
0:399d7ca2d0bb | 56 | void MiniSSC2::set(int i_motor, char value) { |
avbotz |
0:399d7ca2d0bb | 57 | motors[i_motor-1] = value; |
avbotz |
0:399d7ca2d0bb | 58 | } |
avbotz |
0:399d7ca2d0bb | 59 | |
avbotz |
0:399d7ca2d0bb | 60 | DigitalOut led1(LED1); |
avbotz |
0:399d7ca2d0bb | 61 | |
avbotz |
0:399d7ca2d0bb | 62 | MiniSSC2 ssc(4); |
avbotz |
0:399d7ca2d0bb | 63 | Ticker ssc_to; |
avbotz |
0:399d7ca2d0bb | 64 | |
avbotz |
0:399d7ca2d0bb | 65 | void ssc_send_cb() { |
avbotz |
0:399d7ca2d0bb | 66 | ssc.send(); |
avbotz |
0:399d7ca2d0bb | 67 | } |
avbotz |
0:399d7ca2d0bb | 68 | |
avbotz |
0:399d7ca2d0bb | 69 | int main() { |
avbotz |
0:399d7ca2d0bb | 70 | ssc.set(1,33); |
avbotz |
0:399d7ca2d0bb | 71 | ssc.set(2,34); |
avbotz |
0:399d7ca2d0bb | 72 | ssc.set(3,35); |
avbotz |
0:399d7ca2d0bb | 73 | ssc.set(4,36); |
avbotz |
0:399d7ca2d0bb | 74 | |
avbotz |
0:399d7ca2d0bb | 75 | ssc_to.attach(&ssc_send_cb, 0.002); |
avbotz |
0:399d7ca2d0bb | 76 | while (true); |
avbotz |
0:399d7ca2d0bb | 77 | } |
avbotz |
0:399d7ca2d0bb | 78 | // Don't delete this comment. |