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.h@3:5c664d91ba63, 2013-07-08 (annotated)
- Committer:
- avbotz
- Date:
- Mon Jul 08 04:02:04 2013 +0000
- Revision:
- 3:5c664d91ba63
- Parent:
- 2:e5458113c26b
Initial commit.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
avbotz | 2:e5458113c26b | 1 | #pragma once |
avbotz | 2:e5458113c26b | 2 | |
avbotz | 2:e5458113c26b | 3 | #include "mbed.h" |
avbotz | 2:e5458113c26b | 4 | #include <string> |
avbotz | 2:e5458113c26b | 5 | #include <sstream> |
avbotz | 2:e5458113c26b | 6 | |
avbotz |
1:8f9d24a87600 | 7 | #define SSC_SYNC_BYTE 255 |
avbotz |
1:8f9d24a87600 | 8 | |
avbotz | 2:e5458113c26b | 9 | // Stores values for and communicates with the Mini SSC II. This is used to control the motors and move the sub. |
avbotz |
1:8f9d24a87600 | 10 | class MiniSSC2 { |
avbotz | 2:e5458113c26b | 11 | public: |
avbotz | 2:e5458113c26b | 12 | MiniSSC2(int num_motors, int baud, PinName tx, PinName rx); |
avbotz | 2:e5458113c26b | 13 | ~MiniSSC2(); |
avbotz | 2:e5458113c26b | 14 | void send(); // send all motors' speeds to the Mini SSC II |
avbotz | 2:e5458113c26b | 15 | void send(int i_motor); // send speed of motor i_motor to the Mini SSC II |
avbotz | 2:e5458113c26b | 16 | void set(unsigned char value); // set, but don't send, all the motors to a single value |
avbotz | 2:e5458113c26b | 17 | void set(int i_motor, unsigned char value); // set, but don't send, motor number i_motor to value |
avbotz | 2:e5458113c26b | 18 | unsigned char get(int i_motor); // get the current speed for motor number i_motor |
avbotz | 2:e5458113c26b | 19 | private: |
avbotz | 2:e5458113c26b | 20 | Serial* p_device; // pointer to our serial object (used to send stuff) |
avbotz | 2:e5458113c26b | 21 | int num_motors; // number of motors, counting from 1 |
avbotz | 2:e5458113c26b | 22 | |
avbotz | 2:e5458113c26b | 23 | /* |
avbotz | 2:e5458113c26b | 24 | * These arrays send input to the Mini SSC II which in turn sends to the motors. |
avbotz | 2:e5458113c26b | 25 | * The format for the arrays: {SYNC_BYTE, servo number, speed} |
avbotz | 2:e5458113c26b | 26 | * SYNC_BYTE tells the Mini SSC II that we're about to send new instructions. |
avbotz | 2:e5458113c26b | 27 | * The servo number picks the motor. 1 = right, 2 = back, 3 = front, 4 = left |
avbotz | 2:e5458113c26b | 28 | * We set the speed anywhere between 0 and 254. 127 is off, 254 is 100% forward, 0 is 100% back. |
avbotz | 2:e5458113c26b | 29 | * On the back motor, the numbers are reversed. 0 is 100% forward, 254 is 100% back. 127 is still off. |
avbotz | 2:e5458113c26b | 30 | * The speed scales approximately linearly with the set values, though there is no motor motion until around 127+-15. |
avbotz | 2:e5458113c26b | 31 | */ |
avbotz | 2:e5458113c26b | 32 | |
avbotz | 2:e5458113c26b | 33 | //The Mini SSC II supports 8 motors. The Pololu supports 12. This array locally stores their speeds. |
avbotz | 2:e5458113c26b | 34 | unsigned char motors[24]; |
avbotz | 2:e5458113c26b | 35 | }; |
avbotz | 2:e5458113c26b | 36 | |
avbotz | 2:e5458113c26b | 37 | void ssc_send_cb(); |
avbotz | 3:5c664d91ba63 | 38 | MiniSSC2 ssc(4, 9600, p13, p14); // 4 motors, baud for Mini SSC II is 9600, Mini SSC II is connected to pins 9 and 10 |
avbotz | 3:5c664d91ba63 | 39 | Ticker ssc_to; |
avbotz | 2:e5458113c26b | 40 | |
avbotz | 3:5c664d91ba63 | 41 | Ticker led1_on, led1_off; |
avbotz | 2:e5458113c26b | 42 | |
avbotz | 2:e5458113c26b | 43 | // debug |
avbotz | 2:e5458113c26b | 44 | Serial PC(USBTX, USBRX); // tx, rx |
avbotz | 3:5c664d91ba63 | 45 | DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); |