Robot secondaire

Dependencies:   RoboClaw mbed StepperMotor

Fork of RoboClaw by Simon Emarre

Committer:
sype
Date:
Thu May 05 08:46:08 2016 +0000
Revision:
77:f19cc7f81f2a
Parent:
51:1056dd73a748
Child:
71:5590dbe8393a
commit homologation

Who changed what in which revision?

UserRevisionLine numberNew 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 51:1056dd73a748 44 #define AX12_REG_LOAD 0x28
sype 41:b5a2fbc20beb 45
sype 41:b5a2fbc20beb 46 #define AX12_MODE_POSITION 0
sype 41:b5a2fbc20beb 47 #define AX12_MODE_ROTATION 1
sype 41:b5a2fbc20beb 48
sype 41:b5a2fbc20beb 49 #define AX12_CW 1
sype 41:b5a2fbc20beb 50 #define AX12_CCW 0
sype 41:b5a2fbc20beb 51
sype 46:5658af4e5149 52 /** Servo control class, based on a PwmOut
sype 41:b5a2fbc20beb 53 *
sype 41:b5a2fbc20beb 54 * Example:
sype 41:b5a2fbc20beb 55 * @code
sype 41:b5a2fbc20beb 56 * #include "mbed.h"
sype 41:b5a2fbc20beb 57 * #include "AX12.h"
sype 41:b5a2fbc20beb 58 *
sype 41:b5a2fbc20beb 59 * int main() {
sype 41:b5a2fbc20beb 60 *
sype 41:b5a2fbc20beb 61 * AX12 myax12 (p9, p10, 1);
sype 41:b5a2fbc20beb 62 *
sype 41:b5a2fbc20beb 63 * while (1) {
sype 41:b5a2fbc20beb 64 * myax12.SetGoal(0); // go to 0 degrees
sype 41:b5a2fbc20beb 65 * wait (2.0);
sype 41:b5a2fbc20beb 66 * myax12.SetGoal(300); // go to 300 degrees
sype 41:b5a2fbc20beb 67 * wait (2.0);
sype 41:b5a2fbc20beb 68 * }
sype 41:b5a2fbc20beb 69 * }
sype 41:b5a2fbc20beb 70 * @endcode
sype 41:b5a2fbc20beb 71 */
sype 41:b5a2fbc20beb 72 class AX12 {
sype 41:b5a2fbc20beb 73
sype 41:b5a2fbc20beb 74 public:
sype 41:b5a2fbc20beb 75
sype 41:b5a2fbc20beb 76 /** Create an AX12 servo object connected to the specified serial port, with the specified ID
sype 41:b5a2fbc20beb 77 *
sype 41:b5a2fbc20beb 78 * @param pin tx pin
sype 41:b5a2fbc20beb 79 * @param pin rx pin
sype 41:b5a2fbc20beb 80 * @param int ID, the Bus ID of the servo 1-255
sype 41:b5a2fbc20beb 81 */
sype 41:b5a2fbc20beb 82 AX12(PinName tx, PinName rx, int ID, int baud=1000000);
sype 41:b5a2fbc20beb 83
sype 41:b5a2fbc20beb 84 /** Set the mode of the servo
sype 41:b5a2fbc20beb 85 * @param mode
sype 41:b5a2fbc20beb 86 * 0 = Positional, default
sype 41:b5a2fbc20beb 87 * 1 = Continuous rotation
sype 41:b5a2fbc20beb 88 */
sype 41:b5a2fbc20beb 89 int setMode(int mode);
sype 41:b5a2fbc20beb 90
sype 41:b5a2fbc20beb 91 /** Set baud rate of all attached servos
sype 41:b5a2fbc20beb 92 * @param mode
sype 41:b5a2fbc20beb 93 * 0x01 = 1,000,000 bps
sype 41:b5a2fbc20beb 94 * 0x03 = 500,000 bps
sype 41:b5a2fbc20beb 95 * 0x04 = 400,000 bps
sype 41:b5a2fbc20beb 96 * 0x07 = 250,000 bps
sype 41:b5a2fbc20beb 97 * 0x09 = 200,000 bps
sype 41:b5a2fbc20beb 98 * 0x10 = 115,200 bps
sype 41:b5a2fbc20beb 99 * 0x22 = 57,600 bps
sype 41:b5a2fbc20beb 100 * 0x67 = 19,200 bps
sype 41:b5a2fbc20beb 101 * 0xCF = 9,600 bp
sype 41:b5a2fbc20beb 102 */
sype 41:b5a2fbc20beb 103 int setBaud(int baud);
sype 41:b5a2fbc20beb 104
sype 41:b5a2fbc20beb 105
sype 41:b5a2fbc20beb 106 /** Set goal angle in integer degrees, in positional mode
sype 41:b5a2fbc20beb 107 *
sype 41:b5a2fbc20beb 108 * @param degrees 0-300
sype 41:b5a2fbc20beb 109 * @param flags, defaults to 0
sype 41:b5a2fbc20beb 110 * flags[0] = blocking, return when goal position reached
sype 41:b5a2fbc20beb 111 * flags[1] = register, activate with a broadcast trigger
sype 41:b5a2fbc20beb 112 *
sype 41:b5a2fbc20beb 113 */
sype 41:b5a2fbc20beb 114 int setGoal(int degrees, int flags = 0);
sype 41:b5a2fbc20beb 115
sype 41:b5a2fbc20beb 116 /** Get goal angle (internal, not from the AX12)
sype 41:b5a2fbc20beb 117 *
sype 41:b5a2fbc20beb 118 */
sype 41:b5a2fbc20beb 119
sype 41:b5a2fbc20beb 120 int getGoal(){return _goal;}
sype 41:b5a2fbc20beb 121
sype 41:b5a2fbc20beb 122
sype 41:b5a2fbc20beb 123 /** Set the torque limit of the servo
sype 41:b5a2fbc20beb 124 *
sype 41:b5a2fbc20beb 125 * @param maxTorque, 0-1024
sype 41:b5a2fbc20beb 126 */
sype 41:b5a2fbc20beb 127 int setMaxTorque(int maxTorque);
sype 41:b5a2fbc20beb 128
sype 41:b5a2fbc20beb 129
sype 41:b5a2fbc20beb 130 /** Set the speed of the servo in continuous rotation mode
sype 41:b5a2fbc20beb 131 *
sype 41:b5a2fbc20beb 132 * @param speed, -1.0 to 1.0
sype 41:b5a2fbc20beb 133 * -1.0 = full speed counter clock wise
sype 41:b5a2fbc20beb 134 * 1.0 = full speed clock wise
sype 41:b5a2fbc20beb 135 */
sype 41:b5a2fbc20beb 136 int setCRSpeed(float speed);
sype 41:b5a2fbc20beb 137
sype 41:b5a2fbc20beb 138
sype 41:b5a2fbc20beb 139 /** Set the clockwise limit of the servo
sype 41:b5a2fbc20beb 140 *
sype 41:b5a2fbc20beb 141 * @param degrees, 0-300
sype 41:b5a2fbc20beb 142 */
sype 41:b5a2fbc20beb 143 int setCWLimit(int degrees);
sype 41:b5a2fbc20beb 144
sype 41:b5a2fbc20beb 145 /** Set the counter-clockwise limit of the servo
sype 41:b5a2fbc20beb 146 *
sype 41:b5a2fbc20beb 147 * @param degrees, 0-300
sype 41:b5a2fbc20beb 148 */
sype 41:b5a2fbc20beb 149 int setCCWLimit(int degrees);
sype 41:b5a2fbc20beb 150
sype 41:b5a2fbc20beb 151 // Change the ID
sype 41:b5a2fbc20beb 152
sype 41:b5a2fbc20beb 153 /** Change the ID of a servo
sype 41:b5a2fbc20beb 154 *
sype 41:b5a2fbc20beb 155 * @param CurentID 1-255
sype 41:b5a2fbc20beb 156 * @param NewID 1-255
sype 41:b5a2fbc20beb 157 *
sype 41:b5a2fbc20beb 158 * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
sype 41:b5a2fbc20beb 159 * In this situation, only one servo should be connected to the bus
sype 41:b5a2fbc20beb 160 */
sype 41:b5a2fbc20beb 161 int setID(int CurrentID, int NewID);
sype 41:b5a2fbc20beb 162
sype 41:b5a2fbc20beb 163
sype 41:b5a2fbc20beb 164 /** Poll to see if the servo is moving
sype 41:b5a2fbc20beb 165 *
sype 41:b5a2fbc20beb 166 * @returns true is the servo is moving
sype 41:b5a2fbc20beb 167 */
sype 41:b5a2fbc20beb 168 int isMoving(void);
sype 41:b5a2fbc20beb 169
sype 41:b5a2fbc20beb 170 /** Send the broadcast "trigger" command, to activate any outstanding registered commands
sype 41:b5a2fbc20beb 171 */
sype 41:b5a2fbc20beb 172 void trigger(void);
sype 41:b5a2fbc20beb 173
sype 41:b5a2fbc20beb 174 /** Read the current angle of the servo
sype 41:b5a2fbc20beb 175 *
sype 41:b5a2fbc20beb 176 * @returns float in the range 0.0-300.0
sype 41:b5a2fbc20beb 177 */
sype 41:b5a2fbc20beb 178 float getPosition();
sype 41:b5a2fbc20beb 179
sype 41:b5a2fbc20beb 180 /** Read the temperature of the servo
sype 41:b5a2fbc20beb 181 *
sype 41:b5a2fbc20beb 182 * @returns float temperature
sype 41:b5a2fbc20beb 183 */
sype 41:b5a2fbc20beb 184 float getTemp(void);
sype 41:b5a2fbc20beb 185
sype 41:b5a2fbc20beb 186 /** Read the supply voltage of the servo
sype 41:b5a2fbc20beb 187 *
sype 41:b5a2fbc20beb 188 * @returns float voltage
sype 41:b5a2fbc20beb 189 */
sype 41:b5a2fbc20beb 190 float getVolts(void);
sype 50:2dea82393beb 191 float getLoad(void);
sype 41:b5a2fbc20beb 192 int read(int ID, int start, int length, char* data);
sype 41:b5a2fbc20beb 193 int write(int ID, int start, int length, char* data, int flag=0);
sype 41:b5a2fbc20beb 194
sype 41:b5a2fbc20beb 195 private :
sype 41:b5a2fbc20beb 196
sype 41:b5a2fbc20beb 197 Serial _ax12;
sype 41:b5a2fbc20beb 198 int _ID;
sype 41:b5a2fbc20beb 199 int _baud;
sype 41:b5a2fbc20beb 200 int _goal;
sype 41:b5a2fbc20beb 201
sype 41:b5a2fbc20beb 202 };
sype 41:b5a2fbc20beb 203
sype 41:b5a2fbc20beb 204 #endif