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