Library for Pololu Maestro Servo Controller http://www.pololu.com/docs/0J40
Library for Pololu Maestro Servo Controller
Library docs: https://developer.mbed.org/users/kochansky/code/Maestro/docs/f374c7b60318/classMaestro.html
Example usage:
#include "mbed.h" #include "Maestro.h" Maestro maestro(PTC4,PTC3); int main() { maestro.setBaudRate(9600); // limit speed for each servo for (uint8_t i = 0; i < 17; i++) { maestro.setSpeed(i, 10); } while (true) { // set servo on channel 0 to 90 degrees maestro.setServoAngle(0, 90); wait(2); // set servo on channel 0 to 45 degrees maestro.setServoAngle(0, 45); wait(2); } }
Serial commands based on manual: http://www.pololu.com/docs/0J40/5.e
Device pinout:
Maestro.h@6:1d8357775b6d, 2014-01-06 (annotated)
- Committer:
- kochansky
- Date:
- Mon Jan 06 13:16:37 2014 +0000
- Revision:
- 6:1d8357775b6d
- Parent:
- 5:0fd491357750
- Child:
- 7:18a57f597ec4
fixed multiple servos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kochansky | 0:5c8cc5bd6403 | 1 | /** |
kochansky | 0:5c8cc5bd6403 | 2 | * @author Przemysław Kochański |
kochansky | 0:5c8cc5bd6403 | 3 | * |
kochansky | 0:5c8cc5bd6403 | 4 | * @section DESCRIPTION |
kochansky | 0:5c8cc5bd6403 | 5 | * |
kochansky | 0:5c8cc5bd6403 | 6 | * Library for Pololu Maestro Servo Controller |
kochansky | 0:5c8cc5bd6403 | 7 | * Serial Servo Commands: http://www.pololu.com/docs/0J40/5.e |
kochansky | 0:5c8cc5bd6403 | 8 | */ |
kochansky | 0:5c8cc5bd6403 | 9 | |
kochansky | 0:5c8cc5bd6403 | 10 | #ifndef MAESTRO |
kochansky | 0:5c8cc5bd6403 | 11 | #define MAESTRO |
kochansky | 0:5c8cc5bd6403 | 12 | |
kochansky | 0:5c8cc5bd6403 | 13 | #include "mbed.h" |
kochansky | 0:5c8cc5bd6403 | 14 | |
kochansky | 0:5c8cc5bd6403 | 15 | /** |
kochansky | 0:5c8cc5bd6403 | 16 | * Compact Protocol Command Bytes |
kochansky | 0:5c8cc5bd6403 | 17 | */ |
kochansky | 3:1c654893354d | 18 | #define SET_TARGET 0x84 |
kochansky | 5:0fd491357750 | 19 | #define SET_MULTIPLE_TARGETS 0x9F |
kochansky | 3:1c654893354d | 20 | #define SET_SPEED 0x87 |
kochansky | 3:1c654893354d | 21 | #define SET_ACCELERATION 0x89 |
kochansky | 3:1c654893354d | 22 | #define SET_PWM 0x8A |
kochansky | 3:1c654893354d | 23 | #define GET_POSITION 0x90 |
kochansky | 3:1c654893354d | 24 | #define GET_MOVING_STATE 0x93 |
kochansky | 3:1c654893354d | 25 | #define GET_ERRORS 0xA1 |
kochansky | 3:1c654893354d | 26 | #define GO_HOME 0xA2 |
kochansky | 3:1c654893354d | 27 | #define BAUD_RATE_IDICATION 0xAA |
kochansky | 0:5c8cc5bd6403 | 28 | |
kochansky | 0:5c8cc5bd6403 | 29 | /** |
kochansky | 5:0fd491357750 | 30 | * Errors bits numbers |
kochansky | 5:0fd491357750 | 31 | */ |
kochansky | 5:0fd491357750 | 32 | #define SERIAL_SIGNAL_ERROR 0 |
kochansky | 5:0fd491357750 | 33 | #define SERIAL_OVERRUN_ERROR 1 |
kochansky | 5:0fd491357750 | 34 | #define SERIAL_RX_BUFFER_FULL_ERROR 2 |
kochansky | 5:0fd491357750 | 35 | #define SERIAL_CRC_ERROR 3 |
kochansky | 5:0fd491357750 | 36 | #define SERIAL_PROTOCOL_ERROR 4 |
kochansky | 5:0fd491357750 | 37 | #define SERIAL_TIMEOUT_ERROR 5 |
kochansky | 5:0fd491357750 | 38 | #define SCRIPT_STACK_ERROR 6 |
kochansky | 5:0fd491357750 | 39 | #define SCRIPT_CALL_STACK_ERROR 7 |
kochansky | 5:0fd491357750 | 40 | #define SCRIPT_PROGRAM_COUNTER_ERROR 8 |
kochansky | 5:0fd491357750 | 41 | |
kochansky | 5:0fd491357750 | 42 | /** |
kochansky | 0:5c8cc5bd6403 | 43 | * Pololu Maestro Servo Controller |
kochansky | 0:5c8cc5bd6403 | 44 | */ |
kochansky | 0:5c8cc5bd6403 | 45 | class Maestro |
kochansky | 0:5c8cc5bd6403 | 46 | { |
kochansky | 0:5c8cc5bd6403 | 47 | |
kochansky | 1:c14b79e3f39b | 48 | public: |
kochansky | 0:5c8cc5bd6403 | 49 | |
kochansky | 0:5c8cc5bd6403 | 50 | /** |
kochansky | 0:5c8cc5bd6403 | 51 | * Constructor |
kochansky | 0:5c8cc5bd6403 | 52 | * |
kochansky | 0:5c8cc5bd6403 | 53 | * @param tx - mbed pin to use for TX serial line |
kochansky | 0:5c8cc5bd6403 | 54 | * @param rx - mbed pin to use for RX serial line |
kochansky | 0:5c8cc5bd6403 | 55 | */ |
kochansky | 0:5c8cc5bd6403 | 56 | Maestro(PinName tx, PinName rx); |
kochansky | 0:5c8cc5bd6403 | 57 | |
kochansky | 4:ef4d23c023ea | 58 | /** |
kochansky | 4:ef4d23c023ea | 59 | * Sets baud rate |
kochansky | 4:ef4d23c023ea | 60 | * |
kochansky | 4:ef4d23c023ea | 61 | * @param baud - Baud Rate to be set |
kochansky | 4:ef4d23c023ea | 62 | */ |
kochansky | 4:ef4d23c023ea | 63 | void setBaudRate(uint16_t baud); |
kochansky | 3:1c654893354d | 64 | |
kochansky | 0:5c8cc5bd6403 | 65 | /** |
kochansky | 0:5c8cc5bd6403 | 66 | * Sets the target of a channel to a specified value |
kochansky | 0:5c8cc5bd6403 | 67 | * |
kochansky | 0:5c8cc5bd6403 | 68 | * @param channel - number a servo channel |
kochansky | 0:5c8cc5bd6403 | 69 | * @param target - the pulse width to transmit in units of quarter-microseconds |
kochansky | 0:5c8cc5bd6403 | 70 | */ |
kochansky | 3:1c654893354d | 71 | void setTarget(uint8_t channel, uint16_t target); |
kochansky | 0:5c8cc5bd6403 | 72 | |
kochansky | 4:ef4d23c023ea | 73 | /** |
kochansky | 4:ef4d23c023ea | 74 | * Sets specified channel's servo to a specified angle |
kochansky | 4:ef4d23c023ea | 75 | * |
kochansky | 4:ef4d23c023ea | 76 | * @param channel - number a servo channel |
kochansky | 4:ef4d23c023ea | 77 | * @param angle - target angle of a servo |
kochansky | 4:ef4d23c023ea | 78 | */ |
kochansky | 4:ef4d23c023ea | 79 | void setServoAngle(uint8_t channel, int8_t angle); |
kochansky | 4:ef4d23c023ea | 80 | |
kochansky | 5:0fd491357750 | 81 | /** |
kochansky | 5:0fd491357750 | 82 | * Simultaneously sets the targets for a contiguous block of channels |
kochansky | 5:0fd491357750 | 83 | * |
kochansky | 5:0fd491357750 | 84 | * @param count - number of channels in the contiguous block |
kochansky | 5:0fd491357750 | 85 | * @param firstChannel - lowest channel number in the contiguous block |
kochansky | 5:0fd491357750 | 86 | * @param target - target values (the pulse width to transmit in units of |
kochansky | 5:0fd491357750 | 87 | * quarter-microseconds) for each of the channels, in order by channel number |
kochansky | 5:0fd491357750 | 88 | */ |
kochansky | 6:1d8357775b6d | 89 | void setMultipleTargets(uint8_t firstChannel, uint8_t count, uint16_t* targets); |
kochansky | 6:1d8357775b6d | 90 | |
kochansky | 6:1d8357775b6d | 91 | void setServosAngles(uint8_t firstChannel, uint8_t count, int8_t* angles); |
kochansky | 0:5c8cc5bd6403 | 92 | |
kochansky | 0:5c8cc5bd6403 | 93 | /** |
kochansky | 2:39356b408262 | 94 | * Limits the speed at which a servo channel's output value changes |
kochansky | 0:5c8cc5bd6403 | 95 | * |
kochansky | 0:5c8cc5bd6403 | 96 | * @param channel - number of a servo channel |
kochansky | 0:5c8cc5bd6403 | 97 | * @param speed - speed of the servo in units of 0.25 μs / (10 ms) |
kochansky | 0:5c8cc5bd6403 | 98 | */ |
kochansky | 5:0fd491357750 | 99 | void setSpeed(uint8_t channel, uint16_t speed); |
kochansky | 0:5c8cc5bd6403 | 100 | |
kochansky | 0:5c8cc5bd6403 | 101 | /** |
kochansky | 2:39356b408262 | 102 | * Limits the acceleration of a servo channel's output |
kochansky | 0:5c8cc5bd6403 | 103 | * |
kochansky | 0:5c8cc5bd6403 | 104 | * @param channel - number of a servo channel |
kochansky | 0:5c8cc5bd6403 | 105 | * @param acceleration - acceleration of the servo in units of (0.25 μs) / (10 ms) / (80 ms) |
kochansky | 0:5c8cc5bd6403 | 106 | */ |
kochansky | 5:0fd491357750 | 107 | void setAcceleration(uint8_t channel, uint16_t acceleration); |
kochansky | 5:0fd491357750 | 108 | |
kochansky | 5:0fd491357750 | 109 | /** |
kochansky | 5:0fd491357750 | 110 | * Sets the PWM output to the specified on time and period |
kochansky | 5:0fd491357750 | 111 | * |
kochansky | 5:0fd491357750 | 112 | * @param channel - number of a servo channel |
kochansky | 5:0fd491357750 | 113 | * @param time - time in units of 1/48 μs |
kochansky | 5:0fd491357750 | 114 | * @param period - period in units of 1/48 μs |
kochansky | 5:0fd491357750 | 115 | */ |
kochansky | 5:0fd491357750 | 116 | void setPWM(uint8_t channel, uint16_t time, uint16_t period); |
kochansky | 5:0fd491357750 | 117 | |
kochansky | 5:0fd491357750 | 118 | /** |
kochansky | 5:0fd491357750 | 119 | * Gets current servo position |
kochansky | 5:0fd491357750 | 120 | * |
kochansky | 5:0fd491357750 | 121 | * @param channel - number of a servo channel |
kochansky | 5:0fd491357750 | 122 | * |
kochansky | 5:0fd491357750 | 123 | * @return - current pulse width that the Maestro is transmitting on the channel |
kochansky | 5:0fd491357750 | 124 | */ |
kochansky | 5:0fd491357750 | 125 | uint16_t getPosition(uint8_t channel); |
kochansky | 5:0fd491357750 | 126 | |
kochansky | 5:0fd491357750 | 127 | /** |
kochansky | 5:0fd491357750 | 128 | * Determine whether the servo outputs have reached their targets or are still changing |
kochansky | 5:0fd491357750 | 129 | * |
kochansky | 5:0fd491357750 | 130 | * @return - true if servos are moving, false otherwise |
kochansky | 5:0fd491357750 | 131 | */ |
kochansky | 5:0fd491357750 | 132 | bool getMovingState(); |
kochansky | 5:0fd491357750 | 133 | |
kochansky | 5:0fd491357750 | 134 | /** |
kochansky | 5:0fd491357750 | 135 | * Examine the errors that the Maestro has detected |
kochansky | 5:0fd491357750 | 136 | * |
kochansky | 5:0fd491357750 | 137 | * @return - error bits |
kochansky | 5:0fd491357750 | 138 | */ |
kochansky | 5:0fd491357750 | 139 | uint16_t getErrors(); |
kochansky | 5:0fd491357750 | 140 | |
kochansky | 5:0fd491357750 | 141 | /** |
kochansky | 5:0fd491357750 | 142 | * Send all servos and outputs to their home positions |
kochansky | 5:0fd491357750 | 143 | */ |
kochansky | 5:0fd491357750 | 144 | void goHome(); |
kochansky | 1:c14b79e3f39b | 145 | |
kochansky | 1:c14b79e3f39b | 146 | private: |
kochansky | 1:c14b79e3f39b | 147 | |
kochansky | 1:c14b79e3f39b | 148 | Serial serial; |
kochansky | 3:1c654893354d | 149 | Serial pc; |
kochansky | 1:c14b79e3f39b | 150 | }; |
kochansky | 0:5c8cc5bd6403 | 151 | |
kochansky | 0:5c8cc5bd6403 | 152 | #endif // Maestro |