Romain Ame
/
Timer71pt
Fork de Timer après le match à 61 points
Fork of Timer by
AX12/AX12.h@85:38cbf5fd22e1, 2016-05-06 (annotated)
- Committer:
- IceTeam
- Date:
- Fri May 06 09:49:01 2016 +0200
- Revision:
- 85:38cbf5fd22e1
Rajout des AX12
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IceTeam | 85:38cbf5fd22e1 | 1 | /* mbed AX-12+ Servo Library |
IceTeam | 85:38cbf5fd22e1 | 2 | * |
IceTeam | 85:38cbf5fd22e1 | 3 | * Copyright (c) 2010, cstyles (http://mbed.org) |
IceTeam | 85:38cbf5fd22e1 | 4 | * |
IceTeam | 85:38cbf5fd22e1 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
IceTeam | 85:38cbf5fd22e1 | 6 | * of this software and associated documentation files (the "Software"), to deal |
IceTeam | 85:38cbf5fd22e1 | 7 | * in the Software without restriction, including without limitation the rights |
IceTeam | 85:38cbf5fd22e1 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
IceTeam | 85:38cbf5fd22e1 | 9 | * copies of the Software, and to permit persons to whom the Software is |
IceTeam | 85:38cbf5fd22e1 | 10 | * furnished to do so, subject to the following conditions: |
IceTeam | 85:38cbf5fd22e1 | 11 | * |
IceTeam | 85:38cbf5fd22e1 | 12 | * The above copyright notice and this permission notice shall be included in |
IceTeam | 85:38cbf5fd22e1 | 13 | * all copies or substantial portions of the Software. |
IceTeam | 85:38cbf5fd22e1 | 14 | * |
IceTeam | 85:38cbf5fd22e1 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
IceTeam | 85:38cbf5fd22e1 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
IceTeam | 85:38cbf5fd22e1 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
IceTeam | 85:38cbf5fd22e1 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
IceTeam | 85:38cbf5fd22e1 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
IceTeam | 85:38cbf5fd22e1 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
IceTeam | 85:38cbf5fd22e1 | 21 | * THE SOFTWARE. |
IceTeam | 85:38cbf5fd22e1 | 22 | */ |
IceTeam | 85:38cbf5fd22e1 | 23 | |
IceTeam | 85:38cbf5fd22e1 | 24 | #ifndef MBED_AX12_H |
IceTeam | 85:38cbf5fd22e1 | 25 | #define MBED_AX12_H |
IceTeam | 85:38cbf5fd22e1 | 26 | |
IceTeam | 85:38cbf5fd22e1 | 27 | #include "mbed.h" |
IceTeam | 85:38cbf5fd22e1 | 28 | |
IceTeam | 85:38cbf5fd22e1 | 29 | //#define AX12_WRITE_DEBUG 0 |
IceTeam | 85:38cbf5fd22e1 | 30 | //#define AX12_READ_DEBUG 0 |
IceTeam | 85:38cbf5fd22e1 | 31 | //#define AX12_TRIGGER_DEBUG 0 |
IceTeam | 85:38cbf5fd22e1 | 32 | //#define AX12_DEBUG 0 |
IceTeam | 85:38cbf5fd22e1 | 33 | |
IceTeam | 85:38cbf5fd22e1 | 34 | #define AX12_REG_ID 0x3 |
IceTeam | 85:38cbf5fd22e1 | 35 | #define AX12_REG_BAUD 0x4 |
IceTeam | 85:38cbf5fd22e1 | 36 | #define AX12_REG_CW_LIMIT 0x06 |
IceTeam | 85:38cbf5fd22e1 | 37 | #define AX12_REG_CCW_LIMIT 0x08 |
IceTeam | 85:38cbf5fd22e1 | 38 | #define AX12_REG_GOAL_POSITION 0x1E |
IceTeam | 85:38cbf5fd22e1 | 39 | #define AX12_REG_MOVING_SPEED 0x20 |
IceTeam | 85:38cbf5fd22e1 | 40 | #define AX12_REG_VOLTS 0x2A |
IceTeam | 85:38cbf5fd22e1 | 41 | #define AX12_REG_TEMP 0x2B |
IceTeam | 85:38cbf5fd22e1 | 42 | #define AX12_REG_MOVING 0x2E |
IceTeam | 85:38cbf5fd22e1 | 43 | #define AX12_REG_POSITION 0x24 |
IceTeam | 85:38cbf5fd22e1 | 44 | #define AX12_REG_LOAD 0x28 |
IceTeam | 85:38cbf5fd22e1 | 45 | |
IceTeam | 85:38cbf5fd22e1 | 46 | #define AX12_MODE_POSITION 0 |
IceTeam | 85:38cbf5fd22e1 | 47 | #define AX12_MODE_ROTATION 1 |
IceTeam | 85:38cbf5fd22e1 | 48 | |
IceTeam | 85:38cbf5fd22e1 | 49 | #define AX12_CW 1 |
IceTeam | 85:38cbf5fd22e1 | 50 | #define AX12_CCW 0 |
IceTeam | 85:38cbf5fd22e1 | 51 | |
IceTeam | 85:38cbf5fd22e1 | 52 | /** Servo control class, based on a PwmOut |
IceTeam | 85:38cbf5fd22e1 | 53 | * |
IceTeam | 85:38cbf5fd22e1 | 54 | * Example: |
IceTeam | 85:38cbf5fd22e1 | 55 | * @code |
IceTeam | 85:38cbf5fd22e1 | 56 | * #include "mbed.h" |
IceTeam | 85:38cbf5fd22e1 | 57 | * #include "AX12.h" |
IceTeam | 85:38cbf5fd22e1 | 58 | * |
IceTeam | 85:38cbf5fd22e1 | 59 | * int main() { |
IceTeam | 85:38cbf5fd22e1 | 60 | * |
IceTeam | 85:38cbf5fd22e1 | 61 | * AX12 myax12 (p9, p10, 1); |
IceTeam | 85:38cbf5fd22e1 | 62 | * |
IceTeam | 85:38cbf5fd22e1 | 63 | * while (1) { |
IceTeam | 85:38cbf5fd22e1 | 64 | * myax12.SetGoal(0); // go to 0 degrees |
IceTeam | 85:38cbf5fd22e1 | 65 | * wait (2.0); |
IceTeam | 85:38cbf5fd22e1 | 66 | * myax12.SetGoal(300); // go to 300 degrees |
IceTeam | 85:38cbf5fd22e1 | 67 | * wait (2.0); |
IceTeam | 85:38cbf5fd22e1 | 68 | * } |
IceTeam | 85:38cbf5fd22e1 | 69 | * } |
IceTeam | 85:38cbf5fd22e1 | 70 | * @endcode |
IceTeam | 85:38cbf5fd22e1 | 71 | */ |
IceTeam | 85:38cbf5fd22e1 | 72 | class AX12 { |
IceTeam | 85:38cbf5fd22e1 | 73 | |
IceTeam | 85:38cbf5fd22e1 | 74 | public: |
IceTeam | 85:38cbf5fd22e1 | 75 | |
IceTeam | 85:38cbf5fd22e1 | 76 | /** Create an AX12 servo object connected to the specified serial port, with the specified ID |
IceTeam | 85:38cbf5fd22e1 | 77 | * |
IceTeam | 85:38cbf5fd22e1 | 78 | * @param pin tx pin |
IceTeam | 85:38cbf5fd22e1 | 79 | * @param pin rx pin |
IceTeam | 85:38cbf5fd22e1 | 80 | * @param int ID, the Bus ID of the servo 1-255 |
IceTeam | 85:38cbf5fd22e1 | 81 | */ |
IceTeam | 85:38cbf5fd22e1 | 82 | AX12(PinName tx, PinName rx, int ID, int baud=1000000); |
IceTeam | 85:38cbf5fd22e1 | 83 | |
IceTeam | 85:38cbf5fd22e1 | 84 | /** Set the mode of the servo |
IceTeam | 85:38cbf5fd22e1 | 85 | * @param mode |
IceTeam | 85:38cbf5fd22e1 | 86 | * 0 = Positional, default |
IceTeam | 85:38cbf5fd22e1 | 87 | * 1 = Continuous rotation |
IceTeam | 85:38cbf5fd22e1 | 88 | */ |
IceTeam | 85:38cbf5fd22e1 | 89 | int setMode(int mode); |
IceTeam | 85:38cbf5fd22e1 | 90 | |
IceTeam | 85:38cbf5fd22e1 | 91 | /** Set baud rate of all attached servos |
IceTeam | 85:38cbf5fd22e1 | 92 | * @param mode |
IceTeam | 85:38cbf5fd22e1 | 93 | * 0x01 = 1,000,000 bps |
IceTeam | 85:38cbf5fd22e1 | 94 | * 0x03 = 500,000 bps |
IceTeam | 85:38cbf5fd22e1 | 95 | * 0x04 = 400,000 bps |
IceTeam | 85:38cbf5fd22e1 | 96 | * 0x07 = 250,000 bps |
IceTeam | 85:38cbf5fd22e1 | 97 | * 0x09 = 200,000 bps |
IceTeam | 85:38cbf5fd22e1 | 98 | * 0x10 = 115,200 bps |
IceTeam | 85:38cbf5fd22e1 | 99 | * 0x22 = 57,600 bps |
IceTeam | 85:38cbf5fd22e1 | 100 | * 0x67 = 19,200 bps |
IceTeam | 85:38cbf5fd22e1 | 101 | * 0xCF = 9,600 bp |
IceTeam | 85:38cbf5fd22e1 | 102 | */ |
IceTeam | 85:38cbf5fd22e1 | 103 | int setBaud(int baud); |
IceTeam | 85:38cbf5fd22e1 | 104 | |
IceTeam | 85:38cbf5fd22e1 | 105 | |
IceTeam | 85:38cbf5fd22e1 | 106 | /** Set goal angle in integer degrees, in positional mode |
IceTeam | 85:38cbf5fd22e1 | 107 | * |
IceTeam | 85:38cbf5fd22e1 | 108 | * @param degrees 0-300 |
IceTeam | 85:38cbf5fd22e1 | 109 | * @param flags, defaults to 0 |
IceTeam | 85:38cbf5fd22e1 | 110 | * flags[0] = blocking, return when goal position reached |
IceTeam | 85:38cbf5fd22e1 | 111 | * flags[1] = register, activate with a broadcast trigger |
IceTeam | 85:38cbf5fd22e1 | 112 | * |
IceTeam | 85:38cbf5fd22e1 | 113 | */ |
IceTeam | 85:38cbf5fd22e1 | 114 | int setGoal(int degrees, int flags = 0); |
IceTeam | 85:38cbf5fd22e1 | 115 | |
IceTeam | 85:38cbf5fd22e1 | 116 | /** Get goal angle (internal, not from the AX12) |
IceTeam | 85:38cbf5fd22e1 | 117 | * |
IceTeam | 85:38cbf5fd22e1 | 118 | */ |
IceTeam | 85:38cbf5fd22e1 | 119 | |
IceTeam | 85:38cbf5fd22e1 | 120 | int getGoal(){return _goal;} |
IceTeam | 85:38cbf5fd22e1 | 121 | |
IceTeam | 85:38cbf5fd22e1 | 122 | |
IceTeam | 85:38cbf5fd22e1 | 123 | /** Set the torque limit of the servo |
IceTeam | 85:38cbf5fd22e1 | 124 | * |
IceTeam | 85:38cbf5fd22e1 | 125 | * @param maxTorque, 0-1024 |
IceTeam | 85:38cbf5fd22e1 | 126 | */ |
IceTeam | 85:38cbf5fd22e1 | 127 | int setMaxTorque(int maxTorque); |
IceTeam | 85:38cbf5fd22e1 | 128 | |
IceTeam | 85:38cbf5fd22e1 | 129 | |
IceTeam | 85:38cbf5fd22e1 | 130 | /** Set the speed of the servo in continuous rotation mode |
IceTeam | 85:38cbf5fd22e1 | 131 | * |
IceTeam | 85:38cbf5fd22e1 | 132 | * @param speed, -1.0 to 1.0 |
IceTeam | 85:38cbf5fd22e1 | 133 | * -1.0 = full speed counter clock wise |
IceTeam | 85:38cbf5fd22e1 | 134 | * 1.0 = full speed clock wise |
IceTeam | 85:38cbf5fd22e1 | 135 | */ |
IceTeam | 85:38cbf5fd22e1 | 136 | int setCRSpeed(float speed); |
IceTeam | 85:38cbf5fd22e1 | 137 | |
IceTeam | 85:38cbf5fd22e1 | 138 | /** Set the speed of the servo |
IceTeam | 85:38cbf5fd22e1 | 139 | * |
IceTeam | 85:38cbf5fd22e1 | 140 | */ |
IceTeam | 85:38cbf5fd22e1 | 141 | int setSpeed(int speed); |
IceTeam | 85:38cbf5fd22e1 | 142 | |
IceTeam | 85:38cbf5fd22e1 | 143 | |
IceTeam | 85:38cbf5fd22e1 | 144 | /** Set the clockwise limit of the servo |
IceTeam | 85:38cbf5fd22e1 | 145 | * |
IceTeam | 85:38cbf5fd22e1 | 146 | * @param degrees, 0-300 |
IceTeam | 85:38cbf5fd22e1 | 147 | */ |
IceTeam | 85:38cbf5fd22e1 | 148 | int setCWLimit(int degrees); |
IceTeam | 85:38cbf5fd22e1 | 149 | |
IceTeam | 85:38cbf5fd22e1 | 150 | /** Set the counter-clockwise limit of the servo |
IceTeam | 85:38cbf5fd22e1 | 151 | * |
IceTeam | 85:38cbf5fd22e1 | 152 | * @param degrees, 0-300 |
IceTeam | 85:38cbf5fd22e1 | 153 | */ |
IceTeam | 85:38cbf5fd22e1 | 154 | int setCCWLimit(int degrees); |
IceTeam | 85:38cbf5fd22e1 | 155 | |
IceTeam | 85:38cbf5fd22e1 | 156 | // Change the ID |
IceTeam | 85:38cbf5fd22e1 | 157 | |
IceTeam | 85:38cbf5fd22e1 | 158 | /** Change the ID of a servo |
IceTeam | 85:38cbf5fd22e1 | 159 | * |
IceTeam | 85:38cbf5fd22e1 | 160 | * @param CurentID 1-255 |
IceTeam | 85:38cbf5fd22e1 | 161 | * @param NewID 1-255 |
IceTeam | 85:38cbf5fd22e1 | 162 | * |
IceTeam | 85:38cbf5fd22e1 | 163 | * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. |
IceTeam | 85:38cbf5fd22e1 | 164 | * In this situation, only one servo should be connected to the bus |
IceTeam | 85:38cbf5fd22e1 | 165 | */ |
IceTeam | 85:38cbf5fd22e1 | 166 | int setID(int CurrentID, int NewID); |
IceTeam | 85:38cbf5fd22e1 | 167 | |
IceTeam | 85:38cbf5fd22e1 | 168 | |
IceTeam | 85:38cbf5fd22e1 | 169 | /** Poll to see if the servo is moving |
IceTeam | 85:38cbf5fd22e1 | 170 | * |
IceTeam | 85:38cbf5fd22e1 | 171 | * @returns true is the servo is moving |
IceTeam | 85:38cbf5fd22e1 | 172 | */ |
IceTeam | 85:38cbf5fd22e1 | 173 | int isMoving(void); |
IceTeam | 85:38cbf5fd22e1 | 174 | |
IceTeam | 85:38cbf5fd22e1 | 175 | /** Send the broadcast "trigger" command, to activate any outstanding registered commands |
IceTeam | 85:38cbf5fd22e1 | 176 | */ |
IceTeam | 85:38cbf5fd22e1 | 177 | void trigger(void); |
IceTeam | 85:38cbf5fd22e1 | 178 | |
IceTeam | 85:38cbf5fd22e1 | 179 | /** Read the current angle of the servo |
IceTeam | 85:38cbf5fd22e1 | 180 | * |
IceTeam | 85:38cbf5fd22e1 | 181 | * @returns float in the range 0.0-300.0 |
IceTeam | 85:38cbf5fd22e1 | 182 | */ |
IceTeam | 85:38cbf5fd22e1 | 183 | float getPosition(); |
IceTeam | 85:38cbf5fd22e1 | 184 | |
IceTeam | 85:38cbf5fd22e1 | 185 | /** Read the temperature of the servo |
IceTeam | 85:38cbf5fd22e1 | 186 | * |
IceTeam | 85:38cbf5fd22e1 | 187 | * @returns float temperature |
IceTeam | 85:38cbf5fd22e1 | 188 | */ |
IceTeam | 85:38cbf5fd22e1 | 189 | float getTemp(void); |
IceTeam | 85:38cbf5fd22e1 | 190 | |
IceTeam | 85:38cbf5fd22e1 | 191 | /** Read the supply voltage of the servo |
IceTeam | 85:38cbf5fd22e1 | 192 | * |
IceTeam | 85:38cbf5fd22e1 | 193 | * @returns float voltage |
IceTeam | 85:38cbf5fd22e1 | 194 | */ |
IceTeam | 85:38cbf5fd22e1 | 195 | float getVolts(void); |
IceTeam | 85:38cbf5fd22e1 | 196 | float getLoad(void); |
IceTeam | 85:38cbf5fd22e1 | 197 | int read(int ID, int start, int length, char* data); |
IceTeam | 85:38cbf5fd22e1 | 198 | int write(int ID, int start, int length, char* data, int flag=0); |
IceTeam | 85:38cbf5fd22e1 | 199 | |
IceTeam | 85:38cbf5fd22e1 | 200 | private : |
IceTeam | 85:38cbf5fd22e1 | 201 | |
IceTeam | 85:38cbf5fd22e1 | 202 | Serial _ax12; |
IceTeam | 85:38cbf5fd22e1 | 203 | int _ID; |
IceTeam | 85:38cbf5fd22e1 | 204 | int _baud; |
IceTeam | 85:38cbf5fd22e1 | 205 | int _goal; |
IceTeam | 85:38cbf5fd22e1 | 206 | |
IceTeam | 85:38cbf5fd22e1 | 207 | }; |
IceTeam | 85:38cbf5fd22e1 | 208 | |
IceTeam | 85:38cbf5fd22e1 | 209 | #endif |