Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Tue May 05 16:35:53 2015 +0000
Revision:
109:53918ba98306
Parent:
74:88be86f83d17
IA

Who changed what in which revision?

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