Torsten Dillenburg
/
pololu_class
Class to use a Pololu Micro Serial Servo Controler
main.cpp@1:5d0bc0302099, 2010-11-03 (annotated)
- Committer:
- Torsten
- Date:
- Wed Nov 03 13:08:37 2010 +0000
- Revision:
- 1:5d0bc0302099
- Parent:
- 0:e0c4e4123bdc
minor modifacation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Torsten | 0:e0c4e4123bdc | 1 | /* |
Torsten | 0:e0c4e4123bdc | 2 | Pololu Servo integration |
Torsten | 0:e0c4e4123bdc | 3 | Integration of a Pololu Micro Servo Controler for 8 Servo's |
Torsten | 0:e0c4e4123bdc | 4 | http://www.pololu.com/catalog/product/208 |
Torsten | 0:e0c4e4123bdc | 5 | Guide: http://www.pololu.com/file/0J37/ssc03a_guide.pdf |
Torsten | 0:e0c4e4123bdc | 6 | */ |
Torsten | 0:e0c4e4123bdc | 7 | |
Torsten | 0:e0c4e4123bdc | 8 | #include "mbed.h" |
Torsten | 0:e0c4e4123bdc | 9 | |
Torsten | 0:e0c4e4123bdc | 10 | //-- Header ----------------------------------------------------------- |
Torsten | 0:e0c4e4123bdc | 11 | class pololu_mssc { |
Torsten | 0:e0c4e4123bdc | 12 | public: |
Torsten | 0:e0c4e4123bdc | 13 | pololu_mssc(PinName ptx, PinName prx, PinName rst); // Constructor |
Torsten | 1:5d0bc0302099 | 14 | void move(char p_servo, char p_angle); // Move Servo(0..7) to Angle (value between 0 and 5500) |
Torsten | 0:e0c4e4123bdc | 15 | void reset(void); // Reset Controler |
Torsten | 0:e0c4e4123bdc | 16 | |
Torsten | 0:e0c4e4123bdc | 17 | private: |
Torsten | 0:e0c4e4123bdc | 18 | DigitalOut _rst; |
Torsten | 0:e0c4e4123bdc | 19 | Serial* _mssc; |
Torsten | 0:e0c4e4123bdc | 20 | |
Torsten | 0:e0c4e4123bdc | 21 | }; |
Torsten | 0:e0c4e4123bdc | 22 | |
Torsten | 0:e0c4e4123bdc | 23 | //-- Def --------------------------------------------------------------- |
Torsten | 0:e0c4e4123bdc | 24 | pololu_mssc::pololu_mssc(PinName p_tx, PinName p_rx, PinName p_rst) : _rst(p_rst) |
Torsten | 0:e0c4e4123bdc | 25 | { //Constructor |
Torsten | 0:e0c4e4123bdc | 26 | _mssc = new Serial( p_tx, p_rx); // create a new serial connetion |
Torsten | 0:e0c4e4123bdc | 27 | reset(); |
Torsten | 0:e0c4e4123bdc | 28 | } |
Torsten | 0:e0c4e4123bdc | 29 | |
Torsten | 1:5d0bc0302099 | 30 | void pololu_mssc::move(char p_servo, char p_angle) |
Torsten | 0:e0c4e4123bdc | 31 | { |
Torsten | 0:e0c4e4123bdc | 32 | char pos_hi,pos_low; |
Torsten | 0:e0c4e4123bdc | 33 | int temp; |
Torsten | 0:e0c4e4123bdc | 34 | |
Torsten | 0:e0c4e4123bdc | 35 | |
Torsten | 0:e0c4e4123bdc | 36 | //Convert the angle data into two 7-bit bytes |
Torsten | 0:e0c4e4123bdc | 37 | temp = p_angle & 0x1f80; |
Torsten | 0:e0c4e4123bdc | 38 | pos_hi = temp >> 7; |
Torsten | 0:e0c4e4123bdc | 39 | pos_low= p_angle & 0x7f; |
Torsten | 0:e0c4e4123bdc | 40 | |
Torsten | 0:e0c4e4123bdc | 41 | //Construct and send a Pololu Protocol command sentence |
Torsten | 0:e0c4e4123bdc | 42 | _mssc -> putc(0x80); //start byte |
Torsten | 0:e0c4e4123bdc | 43 | _mssc -> putc(0x01); //device id |
Torsten | 0:e0c4e4123bdc | 44 | _mssc -> putc(0x04); //command number |
Torsten | 0:e0c4e4123bdc | 45 | _mssc -> putc(p_servo); //servo number |
Torsten | 0:e0c4e4123bdc | 46 | _mssc -> putc(pos_hi); //angle data1 |
Torsten | 0:e0c4e4123bdc | 47 | _mssc -> putc(pos_low); //angle data2 |
Torsten | 0:e0c4e4123bdc | 48 | } |
Torsten | 0:e0c4e4123bdc | 49 | |
Torsten | 0:e0c4e4123bdc | 50 | void pololu_mssc::reset(void) |
Torsten | 0:e0c4e4123bdc | 51 | { |
Torsten | 0:e0c4e4123bdc | 52 | _rst =1; |
Torsten | 0:e0c4e4123bdc | 53 | wait(0.1); |
Torsten | 0:e0c4e4123bdc | 54 | _rst =0; |
Torsten | 0:e0c4e4123bdc | 55 | wait(0.1); |
Torsten | 0:e0c4e4123bdc | 56 | } |
Torsten | 0:e0c4e4123bdc | 57 | |
Torsten | 0:e0c4e4123bdc | 58 | // End ClassDef |
Torsten | 0:e0c4e4123bdc | 59 | |
Torsten | 0:e0c4e4123bdc | 60 | pololu_mssc servo_control(p9,p10,p8); |
Torsten | 0:e0c4e4123bdc | 61 | DigitalOut myled(LED1); |
Torsten | 0:e0c4e4123bdc | 62 | DigitalOut myled2(LED2); |
Torsten | 0:e0c4e4123bdc | 63 | |
Torsten | 0:e0c4e4123bdc | 64 | //---- Main ---------------------------------------------------------------------- |
Torsten | 0:e0c4e4123bdc | 65 | int main() { |
Torsten | 0:e0c4e4123bdc | 66 | myled = 1; |
Torsten | 1:5d0bc0302099 | 67 | servo_control.move(1,1000); |
Torsten | 0:e0c4e4123bdc | 68 | wait(1); |
Torsten | 1:5d0bc0302099 | 69 | servo_control.move(1,4000); |
Torsten | 0:e0c4e4123bdc | 70 | wait(1); |
Torsten | 1:5d0bc0302099 | 71 | servo_control.move(1,1000); |
Torsten | 0:e0c4e4123bdc | 72 | wait(1); |
Torsten | 0:e0c4e4123bdc | 73 | myled2 = 1; |
Torsten | 0:e0c4e4123bdc | 74 | } |
Torsten | 0:e0c4e4123bdc | 75 | |
Torsten | 0:e0c4e4123bdc | 76 |