![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
tes ir atas semua
Dependencies: mbed ADS1115 StepperMotor SRF05 TPA81new
AX12/AX12.h@49:d23d76689933, 2019-03-06 (annotated)
- Committer:
- hisyamfs
- Date:
- Wed Mar 06 13:17:10 2019 +0000
- Revision:
- 49:d23d76689933
- Parent:
- 12:1e3227a6fcd7
tes bisa semua, tanpa servo. uvtron nggak bisa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hisyamfs | 12:1e3227a6fcd7 | 1 | /* mbed AX-12+ Servo Library |
hisyamfs | 12:1e3227a6fcd7 | 2 | * |
hisyamfs | 12:1e3227a6fcd7 | 3 | * Copyright (c) 2010, cstyles (http://mbed.org) |
hisyamfs | 12:1e3227a6fcd7 | 4 | * |
hisyamfs | 12:1e3227a6fcd7 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hisyamfs | 12:1e3227a6fcd7 | 6 | * of this software and associated documentation files (the "Software"), to deal |
hisyamfs | 12:1e3227a6fcd7 | 7 | * in the Software without restriction, including without limitation the rights |
hisyamfs | 12:1e3227a6fcd7 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hisyamfs | 12:1e3227a6fcd7 | 9 | * copies of the Software, and to permit persons to whom the Software is |
hisyamfs | 12:1e3227a6fcd7 | 10 | * furnished to do so, subject to the following conditions: |
hisyamfs | 12:1e3227a6fcd7 | 11 | * |
hisyamfs | 12:1e3227a6fcd7 | 12 | * The above copyright notice and this permission notice shall be included in |
hisyamfs | 12:1e3227a6fcd7 | 13 | * all copies or substantial portions of the Software. |
hisyamfs | 12:1e3227a6fcd7 | 14 | * |
hisyamfs | 12:1e3227a6fcd7 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hisyamfs | 12:1e3227a6fcd7 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hisyamfs | 12:1e3227a6fcd7 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hisyamfs | 12:1e3227a6fcd7 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hisyamfs | 12:1e3227a6fcd7 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hisyamfs | 12:1e3227a6fcd7 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hisyamfs | 12:1e3227a6fcd7 | 21 | * THE SOFTWARE. |
hisyamfs | 12:1e3227a6fcd7 | 22 | */ |
hisyamfs | 12:1e3227a6fcd7 | 23 | |
hisyamfs | 12:1e3227a6fcd7 | 24 | #ifndef MBED_AX12_H |
hisyamfs | 12:1e3227a6fcd7 | 25 | #define MBED_AX12_H |
hisyamfs | 12:1e3227a6fcd7 | 26 | |
hisyamfs | 12:1e3227a6fcd7 | 27 | #include "mbed.h" |
hisyamfs | 12:1e3227a6fcd7 | 28 | |
hisyamfs | 12:1e3227a6fcd7 | 29 | #define AX12_REG_ID 0x3 |
hisyamfs | 12:1e3227a6fcd7 | 30 | #define AX12_REG_BAUD 0x4 |
hisyamfs | 12:1e3227a6fcd7 | 31 | #define AX12_REG_CW_LIMIT 0x06 |
hisyamfs | 12:1e3227a6fcd7 | 32 | #define AX12_REG_CCW_LIMIT 0x08 |
hisyamfs | 12:1e3227a6fcd7 | 33 | #define AX12_REG_GOAL_POSITION 0x1E |
hisyamfs | 12:1e3227a6fcd7 | 34 | #define AX12_REG_MOVING_SPEED 0x20 |
hisyamfs | 12:1e3227a6fcd7 | 35 | #define AX12_REG_VOLTS 0x2A |
hisyamfs | 12:1e3227a6fcd7 | 36 | #define AX12_REG_TEMP 0x2B |
hisyamfs | 12:1e3227a6fcd7 | 37 | #define AX12_REG_MOVING 0x2E |
hisyamfs | 12:1e3227a6fcd7 | 38 | #define AX12_REG_POSITION 0x24 |
hisyamfs | 12:1e3227a6fcd7 | 39 | #define AX12_TORQUE_ENABLE 0x18 |
hisyamfs | 12:1e3227a6fcd7 | 40 | |
hisyamfs | 12:1e3227a6fcd7 | 41 | #define AX12_MODE_POSITION 0 |
hisyamfs | 12:1e3227a6fcd7 | 42 | #define AX12_MODE_ROTATION 1 |
hisyamfs | 12:1e3227a6fcd7 | 43 | |
hisyamfs | 12:1e3227a6fcd7 | 44 | #define AX12_CW 1 |
hisyamfs | 12:1e3227a6fcd7 | 45 | #define AX12_CCW 0 |
hisyamfs | 12:1e3227a6fcd7 | 46 | |
hisyamfs | 12:1e3227a6fcd7 | 47 | /** Servo control class for AX12 and other compatible Dynamixel servo |
hisyamfs | 12:1e3227a6fcd7 | 48 | * |
hisyamfs | 12:1e3227a6fcd7 | 49 | * Example: |
hisyamfs | 12:1e3227a6fcd7 | 50 | * @code |
hisyamfs | 12:1e3227a6fcd7 | 51 | * #include "mbed.h" |
hisyamfs | 12:1e3227a6fcd7 | 52 | * #include "AX12.h" |
hisyamfs | 12:1e3227a6fcd7 | 53 | * |
hisyamfs | 12:1e3227a6fcd7 | 54 | * int main() { |
hisyamfs | 12:1e3227a6fcd7 | 55 | * // The controlled servo has ID = 1 |
hisyamfs | 12:1e3227a6fcd7 | 56 | * // Connected to pin p9 as TX and p10 as RX |
hisyamfs | 12:1e3227a6fcd7 | 57 | * // p11 used as toggle between TX and RX mode |
hisyamfs | 12:1e3227a6fcd7 | 58 | * // The baudrate used to communicate with servo is 1 000 000 |
hisyamfs | 12:1e3227a6fcd7 | 59 | * AX12 myax12 (p9, p10, p11, 1, 1000000); |
hisyamfs | 12:1e3227a6fcd7 | 60 | * |
hisyamfs | 12:1e3227a6fcd7 | 61 | * while (1) { |
hisyamfs | 12:1e3227a6fcd7 | 62 | * myax12.SetGoal(0); // go to 0 degrees |
hisyamfs | 12:1e3227a6fcd7 | 63 | * wait (2.0); |
hisyamfs | 12:1e3227a6fcd7 | 64 | * myax12.SetGoal(300); // go to 300 degrees |
hisyamfs | 12:1e3227a6fcd7 | 65 | * wait (2.0); |
hisyamfs | 12:1e3227a6fcd7 | 66 | * } |
hisyamfs | 12:1e3227a6fcd7 | 67 | * } |
hisyamfs | 12:1e3227a6fcd7 | 68 | * @endcode |
hisyamfs | 12:1e3227a6fcd7 | 69 | */ |
hisyamfs | 12:1e3227a6fcd7 | 70 | class AX12 { |
hisyamfs | 12:1e3227a6fcd7 | 71 | |
hisyamfs | 12:1e3227a6fcd7 | 72 | public: |
hisyamfs | 12:1e3227a6fcd7 | 73 | |
hisyamfs | 12:1e3227a6fcd7 | 74 | /** Create an AX12 servo object connected to the specified serial port, with the specified ID |
hisyamfs | 12:1e3227a6fcd7 | 75 | * |
hisyamfs | 12:1e3227a6fcd7 | 76 | * @param pin tx pin |
hisyamfs | 12:1e3227a6fcd7 | 77 | * @param pin rx pin |
hisyamfs | 12:1e3227a6fcd7 | 78 | * @param pin tx enable, to switch between tx and rx |
hisyamfs | 12:1e3227a6fcd7 | 79 | * @param int ID, the Bus ID of the servo 1-255 |
hisyamfs | 12:1e3227a6fcd7 | 80 | * @param int baud, the baudrate of the servo |
hisyamfs | 12:1e3227a6fcd7 | 81 | */ |
hisyamfs | 12:1e3227a6fcd7 | 82 | AX12(PinName tx, PinName rx, PinName tx_ena, int ID, int baud); |
hisyamfs | 12:1e3227a6fcd7 | 83 | |
hisyamfs | 12:1e3227a6fcd7 | 84 | /** Set the mode of the servo |
hisyamfs | 12:1e3227a6fcd7 | 85 | * @param mode |
hisyamfs | 12:1e3227a6fcd7 | 86 | * 0 = Positional, default |
hisyamfs | 12:1e3227a6fcd7 | 87 | * 1 = Continuous rotation |
hisyamfs | 12:1e3227a6fcd7 | 88 | */ |
hisyamfs | 12:1e3227a6fcd7 | 89 | int SetMode(int mode); |
hisyamfs | 12:1e3227a6fcd7 | 90 | |
hisyamfs | 12:1e3227a6fcd7 | 91 | /** Set baud rate of all attached servos |
hisyamfs | 12:1e3227a6fcd7 | 92 | * @param mode |
hisyamfs | 12:1e3227a6fcd7 | 93 | * 0x01 = 1,000,000 bps |
hisyamfs | 12:1e3227a6fcd7 | 94 | * 0x03 = 500,000 bps |
hisyamfs | 12:1e3227a6fcd7 | 95 | * 0x04 = 400,000 bps |
hisyamfs | 12:1e3227a6fcd7 | 96 | * 0x07 = 250,000 bps |
hisyamfs | 12:1e3227a6fcd7 | 97 | * 0x09 = 200,000 bps |
hisyamfs | 12:1e3227a6fcd7 | 98 | * 0x10 = 115,200 bps |
hisyamfs | 12:1e3227a6fcd7 | 99 | * 0x22 = 57,600 bps |
hisyamfs | 12:1e3227a6fcd7 | 100 | * 0x67 = 19,200 bps |
hisyamfs | 12:1e3227a6fcd7 | 101 | * 0xCF = 9,600 bp |
hisyamfs | 12:1e3227a6fcd7 | 102 | */ |
hisyamfs | 12:1e3227a6fcd7 | 103 | int SetBaud(int baud); |
hisyamfs | 12:1e3227a6fcd7 | 104 | |
hisyamfs | 12:1e3227a6fcd7 | 105 | /** Set goal angle in integer degrees, in positional mode |
hisyamfs | 12:1e3227a6fcd7 | 106 | * |
hisyamfs | 12:1e3227a6fcd7 | 107 | * @param degrees 0-300 |
hisyamfs | 12:1e3227a6fcd7 | 108 | * @param flags, defaults to 0 |
hisyamfs | 12:1e3227a6fcd7 | 109 | * flags[0] = blocking, return when goal position reached |
hisyamfs | 12:1e3227a6fcd7 | 110 | * flags[1] = register, activate with a broadcast trigger |
hisyamfs | 12:1e3227a6fcd7 | 111 | * |
hisyamfs | 12:1e3227a6fcd7 | 112 | */ |
hisyamfs | 12:1e3227a6fcd7 | 113 | int SetGoal(int degrees, int flags = 0); |
hisyamfs | 12:1e3227a6fcd7 | 114 | |
hisyamfs | 12:1e3227a6fcd7 | 115 | /** Set the position and speed of 18 servo from ID 1-18 at the same time |
hisyamfs | 12:1e3227a6fcd7 | 116 | * |
hisyamfs | 12:1e3227a6fcd7 | 117 | * @param degrees1..18 The position of servo with ID 1..18 |
hisyamfs | 12:1e3227a6fcd7 | 118 | * @param speed1..18 The speed of servo with ID 1..18 |
hisyamfs | 12:1e3227a6fcd7 | 119 | * |
hisyamfs | 12:1e3227a6fcd7 | 120 | */ |
hisyamfs | 12:1e3227a6fcd7 | 121 | void MultSetGoal(int degrees1, float speed1, |
hisyamfs | 12:1e3227a6fcd7 | 122 | int degrees2, float speed2, |
hisyamfs | 12:1e3227a6fcd7 | 123 | int degrees3, float speed3, |
hisyamfs | 12:1e3227a6fcd7 | 124 | int degrees4, float speed4, |
hisyamfs | 12:1e3227a6fcd7 | 125 | int degrees5, float speed5, |
hisyamfs | 12:1e3227a6fcd7 | 126 | int degrees6, float speed6, |
hisyamfs | 12:1e3227a6fcd7 | 127 | int degrees7, float speed7, |
hisyamfs | 12:1e3227a6fcd7 | 128 | int degrees8, float speed8, |
hisyamfs | 12:1e3227a6fcd7 | 129 | int degrees9, float speed9, |
hisyamfs | 12:1e3227a6fcd7 | 130 | int degrees10, float speed10, |
hisyamfs | 12:1e3227a6fcd7 | 131 | int degrees11, float speed11, |
hisyamfs | 12:1e3227a6fcd7 | 132 | int degrees12, float speed12, |
hisyamfs | 12:1e3227a6fcd7 | 133 | int degrees13, float speed13, |
hisyamfs | 12:1e3227a6fcd7 | 134 | int degrees14, float speed14, |
hisyamfs | 12:1e3227a6fcd7 | 135 | int degrees15, float speed15, |
hisyamfs | 12:1e3227a6fcd7 | 136 | int degrees16, float speed16, |
hisyamfs | 12:1e3227a6fcd7 | 137 | int degrees17, float speed17, |
hisyamfs | 12:1e3227a6fcd7 | 138 | int degrees18, float speed18); |
hisyamfs | 12:1e3227a6fcd7 | 139 | |
hisyamfs | 12:1e3227a6fcd7 | 140 | /** Set the speed of the servo in continuous rotation mode |
hisyamfs | 12:1e3227a6fcd7 | 141 | * |
hisyamfs | 12:1e3227a6fcd7 | 142 | * @param speed, -1.0 to 1.0 |
hisyamfs | 12:1e3227a6fcd7 | 143 | * -1.0 = full speed counter clock wise |
hisyamfs | 12:1e3227a6fcd7 | 144 | * 1.0 = full speed clock wise |
hisyamfs | 12:1e3227a6fcd7 | 145 | */ |
hisyamfs | 12:1e3227a6fcd7 | 146 | int SetCRSpeed(float speed); |
hisyamfs | 12:1e3227a6fcd7 | 147 | |
hisyamfs | 12:1e3227a6fcd7 | 148 | /** Set the clockwise limit of the servo |
hisyamfs | 12:1e3227a6fcd7 | 149 | * |
hisyamfs | 12:1e3227a6fcd7 | 150 | * @param degrees, 0-300 |
hisyamfs | 12:1e3227a6fcd7 | 151 | */ |
hisyamfs | 12:1e3227a6fcd7 | 152 | int SetCWLimit(int degrees); |
hisyamfs | 12:1e3227a6fcd7 | 153 | |
hisyamfs | 12:1e3227a6fcd7 | 154 | /** Set the counter-clockwise limit of the servo |
hisyamfs | 12:1e3227a6fcd7 | 155 | * |
hisyamfs | 12:1e3227a6fcd7 | 156 | * @param degrees, 0-300 |
hisyamfs | 12:1e3227a6fcd7 | 157 | */ |
hisyamfs | 12:1e3227a6fcd7 | 158 | int SetCCWLimit(int degrees); |
hisyamfs | 12:1e3227a6fcd7 | 159 | |
hisyamfs | 12:1e3227a6fcd7 | 160 | /** Change the ID of a servo controlled |
hisyamfs | 12:1e3227a6fcd7 | 161 | * |
hisyamfs | 12:1e3227a6fcd7 | 162 | * @param ID 1-255 |
hisyamfs | 12:1e3227a6fcd7 | 163 | * |
hisyamfs | 12:1e3227a6fcd7 | 164 | * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. |
hisyamfs | 12:1e3227a6fcd7 | 165 | * In this situation, only one servo should be connected to the bus |
hisyamfs | 12:1e3227a6fcd7 | 166 | */ |
hisyamfs | 12:1e3227a6fcd7 | 167 | void ControlID (int ID); |
hisyamfs | 12:1e3227a6fcd7 | 168 | |
hisyamfs | 12:1e3227a6fcd7 | 169 | /** Change the ID of a servo |
hisyamfs | 12:1e3227a6fcd7 | 170 | * |
hisyamfs | 12:1e3227a6fcd7 | 171 | * @param CurentID 0-253 |
hisyamfs | 12:1e3227a6fcd7 | 172 | * @param NewID 0-253 |
hisyamfs | 12:1e3227a6fcd7 | 173 | * |
hisyamfs | 12:1e3227a6fcd7 | 174 | * If a servo ID is not know, the broadcast address of 254 (0xFE) can be used for CurrentID. |
hisyamfs | 12:1e3227a6fcd7 | 175 | * In this situation, only one servo should be connected to the bus |
hisyamfs | 12:1e3227a6fcd7 | 176 | */ |
hisyamfs | 12:1e3227a6fcd7 | 177 | int SetID(int CurrentID, int NewID); |
hisyamfs | 12:1e3227a6fcd7 | 178 | |
hisyamfs | 12:1e3227a6fcd7 | 179 | |
hisyamfs | 12:1e3227a6fcd7 | 180 | /** Poll to see if the servo is moving |
hisyamfs | 12:1e3227a6fcd7 | 181 | * |
hisyamfs | 12:1e3227a6fcd7 | 182 | * @returns true is the servo is moving |
hisyamfs | 12:1e3227a6fcd7 | 183 | */ |
hisyamfs | 12:1e3227a6fcd7 | 184 | int isMoving(void); |
hisyamfs | 12:1e3227a6fcd7 | 185 | |
hisyamfs | 12:1e3227a6fcd7 | 186 | |
hisyamfs | 12:1e3227a6fcd7 | 187 | /** Send the broadcast "trigger" command, to activate any outstanding registered commands |
hisyamfs | 12:1e3227a6fcd7 | 188 | */ |
hisyamfs | 12:1e3227a6fcd7 | 189 | void trigger(void); |
hisyamfs | 12:1e3227a6fcd7 | 190 | |
hisyamfs | 12:1e3227a6fcd7 | 191 | |
hisyamfs | 12:1e3227a6fcd7 | 192 | /** Read the current angle of the servo |
hisyamfs | 12:1e3227a6fcd7 | 193 | * |
hisyamfs | 12:1e3227a6fcd7 | 194 | * @returns float in the range 0.0-300.0 |
hisyamfs | 12:1e3227a6fcd7 | 195 | */ |
hisyamfs | 12:1e3227a6fcd7 | 196 | float GetPosition(); |
hisyamfs | 12:1e3227a6fcd7 | 197 | |
hisyamfs | 12:1e3227a6fcd7 | 198 | |
hisyamfs | 12:1e3227a6fcd7 | 199 | /** Read the temperature of the servo |
hisyamfs | 12:1e3227a6fcd7 | 200 | * |
hisyamfs | 12:1e3227a6fcd7 | 201 | * @returns float temperature |
hisyamfs | 12:1e3227a6fcd7 | 202 | */ |
hisyamfs | 12:1e3227a6fcd7 | 203 | float GetTemp(void); |
hisyamfs | 12:1e3227a6fcd7 | 204 | |
hisyamfs | 12:1e3227a6fcd7 | 205 | |
hisyamfs | 12:1e3227a6fcd7 | 206 | /** Read the supply voltage of the servo |
hisyamfs | 12:1e3227a6fcd7 | 207 | * |
hisyamfs | 12:1e3227a6fcd7 | 208 | * @returns float voltage |
hisyamfs | 12:1e3227a6fcd7 | 209 | */ |
hisyamfs | 12:1e3227a6fcd7 | 210 | float GetVolts(void); |
hisyamfs | 12:1e3227a6fcd7 | 211 | |
hisyamfs | 12:1e3227a6fcd7 | 212 | /** Set torque enable of the servo |
hisyamfs | 12:1e3227a6fcd7 | 213 | */ |
hisyamfs | 12:1e3227a6fcd7 | 214 | void SetTorqueEnable(char val); |
hisyamfs | 12:1e3227a6fcd7 | 215 | |
hisyamfs | 12:1e3227a6fcd7 | 216 | int read(int ID, int start, int length, char* data); |
hisyamfs | 12:1e3227a6fcd7 | 217 | |
hisyamfs | 12:1e3227a6fcd7 | 218 | int write(int ID, int start, int length, char* data, int flag=0); |
hisyamfs | 12:1e3227a6fcd7 | 219 | |
hisyamfs | 12:1e3227a6fcd7 | 220 | int sync_write(char* data); |
hisyamfs | 12:1e3227a6fcd7 | 221 | |
hisyamfs | 12:1e3227a6fcd7 | 222 | private : |
hisyamfs | 12:1e3227a6fcd7 | 223 | |
hisyamfs | 12:1e3227a6fcd7 | 224 | Serial _ax12; |
hisyamfs | 12:1e3227a6fcd7 | 225 | DigitalOut _ena_tx; |
hisyamfs | 12:1e3227a6fcd7 | 226 | char TxBuf[10]; |
hisyamfs | 12:1e3227a6fcd7 | 227 | char Status[10]; |
hisyamfs | 12:1e3227a6fcd7 | 228 | static const char start_reg = 0x1E; |
hisyamfs | 12:1e3227a6fcd7 | 229 | static const char length_data = 4; |
hisyamfs | 12:1e3227a6fcd7 | 230 | static const char total_ID = 18; |
hisyamfs | 12:1e3227a6fcd7 | 231 | int _ID; |
hisyamfs | 12:1e3227a6fcd7 | 232 | int _baud_bit; |
hisyamfs | 12:1e3227a6fcd7 | 233 | int _wait_sent; |
hisyamfs | 12:1e3227a6fcd7 | 234 | int _timeout_tick; |
hisyamfs | 12:1e3227a6fcd7 | 235 | int _error_code; |
hisyamfs | 12:1e3227a6fcd7 | 236 | char _data[2]; |
hisyamfs | 12:1e3227a6fcd7 | 237 | char _reg_flag; |
hisyamfs | 12:1e3227a6fcd7 | 238 | }; |
hisyamfs | 12:1e3227a6fcd7 | 239 | |
hisyamfs | 12:1e3227a6fcd7 | 240 | #endif |