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@8:c952ac46bd39, 2014-01-07 (annotated)
- Committer:
- kochansky
- Date:
- Tue Jan 07 18:35:25 2014 +0000
- Revision:
- 8:c952ac46bd39
- Parent:
- 7:18a57f597ec4
- Child:
- 9:a15ea85b40ca
working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kochansky | 0:5c8cc5bd6403 | 1 | /** |
kochansky | 8:c952ac46bd39 | 2 | * @author Przemyslaw Kochanski <przemyslaw@kochanski.biz> |
kochansky | 8:c952ac46bd39 | 3 | * |
kochansky | 8:c952ac46bd39 | 4 | * @Section LICENSE |
kochansky | 8:c952ac46bd39 | 5 | * |
kochansky | 8:c952ac46bd39 | 6 | * Copyright (C) 2014 Przemyslaw Kochanski, MIT License |
kochansky | 8:c952ac46bd39 | 7 | * |
kochansky | 8:c952ac46bd39 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kochansky | 8:c952ac46bd39 | 9 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kochansky | 8:c952ac46bd39 | 10 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kochansky | 8:c952ac46bd39 | 11 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kochansky | 8:c952ac46bd39 | 12 | * furnished to do so, subject to the following conditions: |
kochansky | 8:c952ac46bd39 | 13 | * |
kochansky | 8:c952ac46bd39 | 14 | * The above copyright notice and this permission notice shall be included in all copies or |
kochansky | 8:c952ac46bd39 | 15 | * substantial portions of the Software. |
kochansky | 8:c952ac46bd39 | 16 | * |
kochansky | 8:c952ac46bd39 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kochansky | 8:c952ac46bd39 | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kochansky | 8:c952ac46bd39 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kochansky | 8:c952ac46bd39 | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kochansky | 8:c952ac46bd39 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kochansky | 0:5c8cc5bd6403 | 22 | * |
kochansky | 0:5c8cc5bd6403 | 23 | * @section DESCRIPTION |
kochansky | 0:5c8cc5bd6403 | 24 | * |
kochansky | 0:5c8cc5bd6403 | 25 | * Library for Pololu Maestro Servo Controller |
kochansky | 0:5c8cc5bd6403 | 26 | * Serial Servo Commands: http://www.pololu.com/docs/0J40/5.e |
kochansky | 0:5c8cc5bd6403 | 27 | */ |
kochansky | 0:5c8cc5bd6403 | 28 | |
kochansky | 0:5c8cc5bd6403 | 29 | #ifndef MAESTRO |
kochansky | 0:5c8cc5bd6403 | 30 | #define MAESTRO |
kochansky | 0:5c8cc5bd6403 | 31 | |
kochansky | 0:5c8cc5bd6403 | 32 | #include "mbed.h" |
kochansky | 0:5c8cc5bd6403 | 33 | |
kochansky | 0:5c8cc5bd6403 | 34 | /** |
kochansky | 0:5c8cc5bd6403 | 35 | * Compact Protocol Command Bytes |
kochansky | 0:5c8cc5bd6403 | 36 | */ |
kochansky | 3:1c654893354d | 37 | #define SET_TARGET 0x84 |
kochansky | 5:0fd491357750 | 38 | #define SET_MULTIPLE_TARGETS 0x9F |
kochansky | 3:1c654893354d | 39 | #define SET_SPEED 0x87 |
kochansky | 3:1c654893354d | 40 | #define SET_ACCELERATION 0x89 |
kochansky | 3:1c654893354d | 41 | #define SET_PWM 0x8A |
kochansky | 3:1c654893354d | 42 | #define GET_POSITION 0x90 |
kochansky | 3:1c654893354d | 43 | #define GET_MOVING_STATE 0x93 |
kochansky | 3:1c654893354d | 44 | #define GET_ERRORS 0xA1 |
kochansky | 3:1c654893354d | 45 | #define GO_HOME 0xA2 |
kochansky | 3:1c654893354d | 46 | #define BAUD_RATE_IDICATION 0xAA |
kochansky | 0:5c8cc5bd6403 | 47 | |
kochansky | 0:5c8cc5bd6403 | 48 | /** |
kochansky | 5:0fd491357750 | 49 | * Errors bits numbers |
kochansky | 5:0fd491357750 | 50 | */ |
kochansky | 5:0fd491357750 | 51 | #define SERIAL_SIGNAL_ERROR 0 |
kochansky | 5:0fd491357750 | 52 | #define SERIAL_OVERRUN_ERROR 1 |
kochansky | 5:0fd491357750 | 53 | #define SERIAL_RX_BUFFER_FULL_ERROR 2 |
kochansky | 5:0fd491357750 | 54 | #define SERIAL_CRC_ERROR 3 |
kochansky | 5:0fd491357750 | 55 | #define SERIAL_PROTOCOL_ERROR 4 |
kochansky | 5:0fd491357750 | 56 | #define SERIAL_TIMEOUT_ERROR 5 |
kochansky | 5:0fd491357750 | 57 | #define SCRIPT_STACK_ERROR 6 |
kochansky | 5:0fd491357750 | 58 | #define SCRIPT_CALL_STACK_ERROR 7 |
kochansky | 5:0fd491357750 | 59 | #define SCRIPT_PROGRAM_COUNTER_ERROR 8 |
kochansky | 5:0fd491357750 | 60 | |
kochansky | 5:0fd491357750 | 61 | /** |
kochansky | 0:5c8cc5bd6403 | 62 | * Pololu Maestro Servo Controller |
kochansky | 0:5c8cc5bd6403 | 63 | */ |
kochansky | 0:5c8cc5bd6403 | 64 | class Maestro |
kochansky | 0:5c8cc5bd6403 | 65 | { |
kochansky | 0:5c8cc5bd6403 | 66 | |
kochansky | 1:c14b79e3f39b | 67 | public: |
kochansky | 0:5c8cc5bd6403 | 68 | |
kochansky | 0:5c8cc5bd6403 | 69 | /** |
kochansky | 0:5c8cc5bd6403 | 70 | * Constructor |
kochansky | 0:5c8cc5bd6403 | 71 | * |
kochansky | 0:5c8cc5bd6403 | 72 | * @param tx - mbed pin to use for TX serial line |
kochansky | 0:5c8cc5bd6403 | 73 | * @param rx - mbed pin to use for RX serial line |
kochansky | 0:5c8cc5bd6403 | 74 | */ |
kochansky | 0:5c8cc5bd6403 | 75 | Maestro(PinName tx, PinName rx); |
kochansky | 0:5c8cc5bd6403 | 76 | |
kochansky | 4:ef4d23c023ea | 77 | /** |
kochansky | 4:ef4d23c023ea | 78 | * Sets baud rate |
kochansky | 4:ef4d23c023ea | 79 | * |
kochansky | 4:ef4d23c023ea | 80 | * @param baud - Baud Rate to be set |
kochansky | 4:ef4d23c023ea | 81 | */ |
kochansky | 4:ef4d23c023ea | 82 | void setBaudRate(uint16_t baud); |
kochansky | 3:1c654893354d | 83 | |
kochansky | 0:5c8cc5bd6403 | 84 | /** |
kochansky | 0:5c8cc5bd6403 | 85 | * Sets the target of a channel to a specified value |
kochansky | 0:5c8cc5bd6403 | 86 | * |
kochansky | 0:5c8cc5bd6403 | 87 | * @param channel - number a servo channel |
kochansky | 0:5c8cc5bd6403 | 88 | * @param target - the pulse width to transmit in units of quarter-microseconds |
kochansky | 0:5c8cc5bd6403 | 89 | */ |
kochansky | 3:1c654893354d | 90 | void setTarget(uint8_t channel, uint16_t target); |
kochansky | 0:5c8cc5bd6403 | 91 | |
kochansky | 4:ef4d23c023ea | 92 | /** |
kochansky | 4:ef4d23c023ea | 93 | * Sets specified channel's servo to a specified angle |
kochansky | 4:ef4d23c023ea | 94 | * |
kochansky | 4:ef4d23c023ea | 95 | * @param channel - number a servo channel |
kochansky | 4:ef4d23c023ea | 96 | * @param angle - target angle of a servo |
kochansky | 4:ef4d23c023ea | 97 | */ |
kochansky | 4:ef4d23c023ea | 98 | void setServoAngle(uint8_t channel, int8_t angle); |
kochansky | 4:ef4d23c023ea | 99 | |
kochansky | 5:0fd491357750 | 100 | /** |
kochansky | 5:0fd491357750 | 101 | * Simultaneously sets the targets for a contiguous block of channels |
kochansky | 5:0fd491357750 | 102 | * |
kochansky | 5:0fd491357750 | 103 | * @param count - number of channels in the contiguous block |
kochansky | 5:0fd491357750 | 104 | * @param firstChannel - lowest channel number in the contiguous block |
kochansky | 5:0fd491357750 | 105 | * @param target - target values (the pulse width to transmit in units of |
kochansky | 5:0fd491357750 | 106 | * quarter-microseconds) for each of the channels, in order by channel number |
kochansky | 5:0fd491357750 | 107 | */ |
kochansky | 6:1d8357775b6d | 108 | void setMultipleTargets(uint8_t firstChannel, uint8_t count, uint16_t* targets); |
kochansky | 6:1d8357775b6d | 109 | |
kochansky | 8:c952ac46bd39 | 110 | /** |
kochansky | 8:c952ac46bd39 | 111 | * Simultaneously sets specified contiguous block of servo channels to a specified angles |
kochansky | 8:c952ac46bd39 | 112 | * |
kochansky | 8:c952ac46bd39 | 113 | * @param count - number of servo channels in the contiguous block |
kochansky | 8:c952ac46bd39 | 114 | * @param firstChannel - lowest servo channel number in the contiguous block |
kochansky | 8:c952ac46bd39 | 115 | * @param angles - target values (the pulse width to transmit in units of |
kochansky | 8:c952ac46bd39 | 116 | * quarter-microseconds) for each of the servo channels, in order by channel number |
kochansky | 8:c952ac46bd39 | 117 | */ |
kochansky | 6:1d8357775b6d | 118 | void setServosAngles(uint8_t firstChannel, uint8_t count, int8_t* angles); |
kochansky | 0:5c8cc5bd6403 | 119 | |
kochansky | 0:5c8cc5bd6403 | 120 | /** |
kochansky | 2:39356b408262 | 121 | * Limits the speed at which a servo channel's output value changes |
kochansky | 0:5c8cc5bd6403 | 122 | * |
kochansky | 0:5c8cc5bd6403 | 123 | * @param channel - number of a servo channel |
kochansky | 8:c952ac46bd39 | 124 | * @param speed - speed of the servo in units of 0.25 us / (10 ms) |
kochansky | 0:5c8cc5bd6403 | 125 | */ |
kochansky | 5:0fd491357750 | 126 | void setSpeed(uint8_t channel, uint16_t speed); |
kochansky | 0:5c8cc5bd6403 | 127 | |
kochansky | 0:5c8cc5bd6403 | 128 | /** |
kochansky | 2:39356b408262 | 129 | * Limits the acceleration of a servo channel's output |
kochansky | 0:5c8cc5bd6403 | 130 | * |
kochansky | 0:5c8cc5bd6403 | 131 | * @param channel - number of a servo channel |
kochansky | 8:c952ac46bd39 | 132 | * @param acceleration - acceleration of the servo in units of (0.25 us) / (10 ms) / (80 ms) |
kochansky | 0:5c8cc5bd6403 | 133 | */ |
kochansky | 5:0fd491357750 | 134 | void setAcceleration(uint8_t channel, uint16_t acceleration); |
kochansky | 5:0fd491357750 | 135 | |
kochansky | 5:0fd491357750 | 136 | /** |
kochansky | 5:0fd491357750 | 137 | * Sets the PWM output to the specified on time and period |
kochansky | 5:0fd491357750 | 138 | * |
kochansky | 5:0fd491357750 | 139 | * @param channel - number of a servo channel |
kochansky | 8:c952ac46bd39 | 140 | * @param time - time in units of 1/48 us |
kochansky | 8:c952ac46bd39 | 141 | * @param period - period in units of 1/48 us |
kochansky | 5:0fd491357750 | 142 | */ |
kochansky | 5:0fd491357750 | 143 | void setPWM(uint8_t channel, uint16_t time, uint16_t period); |
kochansky | 5:0fd491357750 | 144 | |
kochansky | 5:0fd491357750 | 145 | /** |
kochansky | 5:0fd491357750 | 146 | * Gets current servo position |
kochansky | 5:0fd491357750 | 147 | * |
kochansky | 5:0fd491357750 | 148 | * @param channel - number of a servo channel |
kochansky | 5:0fd491357750 | 149 | * |
kochansky | 5:0fd491357750 | 150 | * @return - current pulse width that the Maestro is transmitting on the channel |
kochansky | 5:0fd491357750 | 151 | */ |
kochansky | 5:0fd491357750 | 152 | uint16_t getPosition(uint8_t channel); |
kochansky | 5:0fd491357750 | 153 | |
kochansky | 5:0fd491357750 | 154 | /** |
kochansky | 5:0fd491357750 | 155 | * Determine whether the servo outputs have reached their targets or are still changing |
kochansky | 5:0fd491357750 | 156 | * |
kochansky | 5:0fd491357750 | 157 | * @return - true if servos are moving, false otherwise |
kochansky | 5:0fd491357750 | 158 | */ |
kochansky | 5:0fd491357750 | 159 | bool getMovingState(); |
kochansky | 5:0fd491357750 | 160 | |
kochansky | 5:0fd491357750 | 161 | /** |
kochansky | 5:0fd491357750 | 162 | * Examine the errors that the Maestro has detected |
kochansky | 5:0fd491357750 | 163 | * |
kochansky | 5:0fd491357750 | 164 | * @return - error bits |
kochansky | 5:0fd491357750 | 165 | */ |
kochansky | 5:0fd491357750 | 166 | uint16_t getErrors(); |
kochansky | 5:0fd491357750 | 167 | |
kochansky | 5:0fd491357750 | 168 | /** |
kochansky | 5:0fd491357750 | 169 | * Send all servos and outputs to their home positions |
kochansky | 5:0fd491357750 | 170 | */ |
kochansky | 5:0fd491357750 | 171 | void goHome(); |
kochansky | 1:c14b79e3f39b | 172 | |
kochansky | 1:c14b79e3f39b | 173 | private: |
kochansky | 1:c14b79e3f39b | 174 | |
kochansky | 1:c14b79e3f39b | 175 | Serial serial; |
kochansky | 1:c14b79e3f39b | 176 | }; |
kochansky | 0:5c8cc5bd6403 | 177 | |
kochansky | 7:18a57f597ec4 | 178 | #endif // Maestro |