Robotis Dynamixel MX-12W Servo Library
Dependents: SpindleBot_1_5b Utilisatio_MX12_V4
This is my attempt to adapt Chris Styles's AX12 library to work with my Dynamixel MX12 servos. This library is still very much a work in progress, and it may have some/many errors in it, but hopefully I will keep improving it to bring it up to snuff.
Dynamixel aficionados should also check out This MX28 library for a completely separate library that provides very similar functionality, and I wish I had known it existed before I started my work...
minimal example
#include "mbed.h" #include "MX12.h" int main() { MX12 mymx12 (p9, p10, 1); // ID=1 while (1) { mymx12.Set_Goal_Position(0); // go to 0 degrees wait (2.0); mymx12.Set_Goal_Position(300); // go to 300 degrees wait (2.0); } }
MX12.h@0:29900c3a4a50, 2014-11-25 (annotated)
- Committer:
- labmrd
- Date:
- Tue Nov 25 03:07:36 2014 +0000
- Revision:
- 0:29900c3a4a50
- Child:
- 1:946a27210553
Adapting AX12 library to include more MX12 commands
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
labmrd | 0:29900c3a4a50 | 1 | /* mbed AX-12+ Servo Library |
labmrd | 0:29900c3a4a50 | 2 | * |
labmrd | 0:29900c3a4a50 | 3 | * Copyright (c) 2010, cstyles (http://mbed.org) |
labmrd | 0:29900c3a4a50 | 4 | * |
labmrd | 0:29900c3a4a50 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
labmrd | 0:29900c3a4a50 | 6 | * of this software and associated documentation files (the "Software"), to deal |
labmrd | 0:29900c3a4a50 | 7 | * in the Software without restriction, including without limitation the rights |
labmrd | 0:29900c3a4a50 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
labmrd | 0:29900c3a4a50 | 9 | * copies of the Software, and to permit persons to whom the Software is |
labmrd | 0:29900c3a4a50 | 10 | * furnished to do so, subject to the following conditions: |
labmrd | 0:29900c3a4a50 | 11 | * |
labmrd | 0:29900c3a4a50 | 12 | * The above copyright notice and this permission notice shall be included in |
labmrd | 0:29900c3a4a50 | 13 | * all copies or substantial portions of the Software. |
labmrd | 0:29900c3a4a50 | 14 | * |
labmrd | 0:29900c3a4a50 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
labmrd | 0:29900c3a4a50 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
labmrd | 0:29900c3a4a50 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
labmrd | 0:29900c3a4a50 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
labmrd | 0:29900c3a4a50 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
labmrd | 0:29900c3a4a50 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
labmrd | 0:29900c3a4a50 | 21 | * THE SOFTWARE. |
labmrd | 0:29900c3a4a50 | 22 | */ |
labmrd | 0:29900c3a4a50 | 23 | |
labmrd | 0:29900c3a4a50 | 24 | #ifndef MBED_MX12_H |
labmrd | 0:29900c3a4a50 | 25 | #define MBED_MX12_H |
labmrd | 0:29900c3a4a50 | 26 | |
labmrd | 0:29900c3a4a50 | 27 | #include "mbed.h" |
labmrd | 0:29900c3a4a50 | 28 | |
labmrd | 0:29900c3a4a50 | 29 | #define MX12_WRITE_DEBUG 1 |
labmrd | 0:29900c3a4a50 | 30 | #define MX12_READ_DEBUG 1 |
labmrd | 0:29900c3a4a50 | 31 | #define MX12_TRIGGER_DEBUG 1 |
labmrd | 0:29900c3a4a50 | 32 | #define MX12_DEBUG 1 |
labmrd | 0:29900c3a4a50 | 33 | |
labmrd | 0:29900c3a4a50 | 34 | #define MX12_REG_ID 0x3 |
labmrd | 0:29900c3a4a50 | 35 | #define MX12_REG_CW_LIMIT 0x06 |
labmrd | 0:29900c3a4a50 | 36 | #define MX12_REG_CCW_LIMIT 0x08 |
labmrd | 0:29900c3a4a50 | 37 | #define MX12_REG_GOAL_POSITION 0x1E |
labmrd | 0:29900c3a4a50 | 38 | #define MX12_REG_MOVING_SPEED 0x20 |
labmrd | 0:29900c3a4a50 | 39 | #define MX12_REG_VOLTS 0x2A |
labmrd | 0:29900c3a4a50 | 40 | #define MX12_REG_TEMP 0x2B |
labmrd | 0:29900c3a4a50 | 41 | #define MX12_REG_MOVING 0x2E |
labmrd | 0:29900c3a4a50 | 42 | #define MX12_REG_POSITION 0x24 |
labmrd | 0:29900c3a4a50 | 43 | |
labmrd | 0:29900c3a4a50 | 44 | #define MX12_MODE_POSITION 0 |
labmrd | 0:29900c3a4a50 | 45 | #define MX12_MODE_ROTATION 1 |
labmrd | 0:29900c3a4a50 | 46 | |
labmrd | 0:29900c3a4a50 | 47 | #define MX12_CW 1 |
labmrd | 0:29900c3a4a50 | 48 | #define MX12_CCW 0 |
labmrd | 0:29900c3a4a50 | 49 | |
labmrd | 0:29900c3a4a50 | 50 | #define MAX_DELAY_BETWEEN_CHARCTERS_IN_MS 10 |
labmrd | 0:29900c3a4a50 | 51 | |
labmrd | 0:29900c3a4a50 | 52 | /** Servo control class, based on a PwmOut |
labmrd | 0:29900c3a4a50 | 53 | * |
labmrd | 0:29900c3a4a50 | 54 | * Example: |
labmrd | 0:29900c3a4a50 | 55 | * @code |
labmrd | 0:29900c3a4a50 | 56 | * #include "mbed.h" |
labmrd | 0:29900c3a4a50 | 57 | * #include "MX12.h" |
labmrd | 0:29900c3a4a50 | 58 | * |
labmrd | 0:29900c3a4a50 | 59 | * int main() { |
labmrd | 0:29900c3a4a50 | 60 | * |
labmrd | 0:29900c3a4a50 | 61 | * MX12 mymx12 (p9, p10, 1); |
labmrd | 0:29900c3a4a50 | 62 | * |
labmrd | 0:29900c3a4a50 | 63 | * while (1) { |
labmrd | 0:29900c3a4a50 | 64 | * mymx12.SetGoal(0); // go to 0 degrees |
labmrd | 0:29900c3a4a50 | 65 | * wait (2.0); |
labmrd | 0:29900c3a4a50 | 66 | * mymx12.SetGoal(300); // go to 300 degrees |
labmrd | 0:29900c3a4a50 | 67 | * wait (2.0); |
labmrd | 0:29900c3a4a50 | 68 | * } |
labmrd | 0:29900c3a4a50 | 69 | * } |
labmrd | 0:29900c3a4a50 | 70 | * @endcode |
labmrd | 0:29900c3a4a50 | 71 | */ |
labmrd | 0:29900c3a4a50 | 72 | class MX12 { |
labmrd | 0:29900c3a4a50 | 73 | |
labmrd | 0:29900c3a4a50 | 74 | public: |
labmrd | 0:29900c3a4a50 | 75 | |
labmrd | 0:29900c3a4a50 | 76 | /** Create an MX12 servo object connected to the specified serial port, with the specified ID |
labmrd | 0:29900c3a4a50 | 77 | * |
labmrd | 0:29900c3a4a50 | 78 | * @param pin tx pin |
labmrd | 0:29900c3a4a50 | 79 | * @param pin rx pin |
labmrd | 0:29900c3a4a50 | 80 | * @param int ID, the Bus ID of the servo 1-255 |
labmrd | 0:29900c3a4a50 | 81 | */ |
labmrd | 0:29900c3a4a50 | 82 | MX12(PinName tx, PinName rx, int ID); |
labmrd | 0:29900c3a4a50 | 83 | |
labmrd | 0:29900c3a4a50 | 84 | /** Set the mode of the servo |
labmrd | 0:29900c3a4a50 | 85 | * @param mode |
labmrd | 0:29900c3a4a50 | 86 | * 0 = Positional, default |
labmrd | 0:29900c3a4a50 | 87 | * 1 = Continuous rotation |
labmrd | 0:29900c3a4a50 | 88 | */ |
labmrd | 0:29900c3a4a50 | 89 | int SetMode(int mode); |
labmrd | 0:29900c3a4a50 | 90 | |
labmrd | 0:29900c3a4a50 | 91 | /** Set goal angle in integer degrees, in positional mode |
labmrd | 0:29900c3a4a50 | 92 | * |
labmrd | 0:29900c3a4a50 | 93 | * @param degrees 0-300 |
labmrd | 0:29900c3a4a50 | 94 | * @param flags, defaults to 0 |
labmrd | 0:29900c3a4a50 | 95 | * flags[0] = blocking, return when goal position reached |
labmrd | 0:29900c3a4a50 | 96 | * flags[1] = register, activate with a broadcast trigger |
labmrd | 0:29900c3a4a50 | 97 | * |
labmrd | 0:29900c3a4a50 | 98 | */ |
labmrd | 0:29900c3a4a50 | 99 | int SetGoal(float radians, int flags = 0); |
labmrd | 0:29900c3a4a50 | 100 | |
labmrd | 0:29900c3a4a50 | 101 | |
labmrd | 0:29900c3a4a50 | 102 | /** Set the speed of the servo in continuous rotation mode |
labmrd | 0:29900c3a4a50 | 103 | * |
labmrd | 0:29900c3a4a50 | 104 | * @param speed, -1.0 to 1.0 |
labmrd | 0:29900c3a4a50 | 105 | * -1.0 = full speed counter clock wise |
labmrd | 0:29900c3a4a50 | 106 | * 1.0 = full speed clock wise |
labmrd | 0:29900c3a4a50 | 107 | */ |
labmrd | 0:29900c3a4a50 | 108 | int SetCRSpeed(float speed); |
labmrd | 0:29900c3a4a50 | 109 | |
labmrd | 0:29900c3a4a50 | 110 | |
labmrd | 0:29900c3a4a50 | 111 | /** Set the clockwise limit of the servo |
labmrd | 0:29900c3a4a50 | 112 | * |
labmrd | 0:29900c3a4a50 | 113 | * @param degrees, 0-300 |
labmrd | 0:29900c3a4a50 | 114 | */ |
labmrd | 0:29900c3a4a50 | 115 | int SetCWLimit(int degrees); |
labmrd | 0:29900c3a4a50 | 116 | |
labmrd | 0:29900c3a4a50 | 117 | /** Set the counter-clockwise limit of the servo |
labmrd | 0:29900c3a4a50 | 118 | * |
labmrd | 0:29900c3a4a50 | 119 | * @param degrees, 0-300 |
labmrd | 0:29900c3a4a50 | 120 | */ |
labmrd | 0:29900c3a4a50 | 121 | int SetCCWLimit(int degrees); |
labmrd | 0:29900c3a4a50 | 122 | |
labmrd | 0:29900c3a4a50 | 123 | // Change the ID |
labmrd | 0:29900c3a4a50 | 124 | |
labmrd | 0:29900c3a4a50 | 125 | /** Change the ID of a servo |
labmrd | 0:29900c3a4a50 | 126 | * |
labmrd | 0:29900c3a4a50 | 127 | * @param CurentID 1-255 |
labmrd | 0:29900c3a4a50 | 128 | * @param NewID 1-255 |
labmrd | 0:29900c3a4a50 | 129 | * |
labmrd | 0:29900c3a4a50 | 130 | * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. |
labmrd | 0:29900c3a4a50 | 131 | * In this situation, only one servo should be connected to the bus |
labmrd | 0:29900c3a4a50 | 132 | */ |
labmrd | 0:29900c3a4a50 | 133 | int SetID(int CurrentID, int NewID); |
labmrd | 0:29900c3a4a50 | 134 | |
labmrd | 0:29900c3a4a50 | 135 | |
labmrd | 0:29900c3a4a50 | 136 | /** Poll to see if the servo is moving |
labmrd | 0:29900c3a4a50 | 137 | * |
labmrd | 0:29900c3a4a50 | 138 | * @returns true is the servo is moving |
labmrd | 0:29900c3a4a50 | 139 | */ |
labmrd | 0:29900c3a4a50 | 140 | int isMoving(void); |
labmrd | 0:29900c3a4a50 | 141 | |
labmrd | 0:29900c3a4a50 | 142 | /** Send the broadcast "trigger" command, to activate any outstanding registered commands |
labmrd | 0:29900c3a4a50 | 143 | */ |
labmrd | 0:29900c3a4a50 | 144 | void trigger(void); |
labmrd | 0:29900c3a4a50 | 145 | |
labmrd | 0:29900c3a4a50 | 146 | /** Read the current angle of the servo |
labmrd | 0:29900c3a4a50 | 147 | * |
labmrd | 0:29900c3a4a50 | 148 | * @returns float in the range 0.0-300.0 |
labmrd | 0:29900c3a4a50 | 149 | */ |
labmrd | 0:29900c3a4a50 | 150 | float GetPosition(); |
labmrd | 0:29900c3a4a50 | 151 | |
labmrd | 0:29900c3a4a50 | 152 | /** Read the temperature of the servo |
labmrd | 0:29900c3a4a50 | 153 | * |
labmrd | 0:29900c3a4a50 | 154 | * @returns float temperature |
labmrd | 0:29900c3a4a50 | 155 | */ |
labmrd | 0:29900c3a4a50 | 156 | float GetTemp(void); |
labmrd | 0:29900c3a4a50 | 157 | |
labmrd | 0:29900c3a4a50 | 158 | /** Read the supply voltage of the servo |
labmrd | 0:29900c3a4a50 | 159 | * |
labmrd | 0:29900c3a4a50 | 160 | * @returns float voltage |
labmrd | 0:29900c3a4a50 | 161 | */ |
labmrd | 0:29900c3a4a50 | 162 | float GetVolts(void); |
labmrd | 0:29900c3a4a50 | 163 | |
labmrd | 0:29900c3a4a50 | 164 | private : |
labmrd | 0:29900c3a4a50 | 165 | |
labmrd | 0:29900c3a4a50 | 166 | SerialHalfDuplex _mx12; |
labmrd | 0:29900c3a4a50 | 167 | int _ID; |
labmrd | 0:29900c3a4a50 | 168 | |
labmrd | 0:29900c3a4a50 | 169 | int read(int ID, int start, int length, char* data); |
labmrd | 0:29900c3a4a50 | 170 | int write(int ID, int start, int length, char* data, int flag=0); |
labmrd | 0:29900c3a4a50 | 171 | |
labmrd | 0:29900c3a4a50 | 172 | }; |
labmrd | 0:29900c3a4a50 | 173 | |
labmrd | 0:29900c3a4a50 | 174 | #endif |