Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Mon Apr 13 16:53:19 2015 +0000
Revision:
74:88be86f83d17
Parent:
39:09c04fd42c94
Child:
109:53918ba98306
bla

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 74:88be86f83d17 115
Jagang 74:88be86f83d17 116 /** Set the torque limit of the servo
Jagang 74:88be86f83d17 117 *
Jagang 74:88be86f83d17 118 * @param maxTorque, 0-1024
Jagang 74:88be86f83d17 119 */
Jagang 74:88be86f83d17 120 int setMaxTorque(int maxTorque);
Jagang 39:09c04fd42c94 121
Jagang 39:09c04fd42c94 122
Jagang 39:09c04fd42c94 123 /** Set the speed of the servo in continuous rotation mode
Jagang 39:09c04fd42c94 124 *
Jagang 39:09c04fd42c94 125 * @param speed, -1.0 to 1.0
Jagang 39:09c04fd42c94 126 * -1.0 = full speed counter clock wise
Jagang 39:09c04fd42c94 127 * 1.0 = full speed clock wise
Jagang 39:09c04fd42c94 128 */
Jagang 74:88be86f83d17 129 int setCRSpeed(float speed);
Jagang 39:09c04fd42c94 130
Jagang 39:09c04fd42c94 131
Jagang 39:09c04fd42c94 132 /** Set the clockwise limit of the servo
Jagang 39:09c04fd42c94 133 *
Jagang 39:09c04fd42c94 134 * @param degrees, 0-300
Jagang 39:09c04fd42c94 135 */
Jagang 74:88be86f83d17 136 int setCWLimit(int degrees);
Jagang 39:09c04fd42c94 137
Jagang 39:09c04fd42c94 138 /** Set the counter-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 setCCWLimit(int degrees);
Jagang 39:09c04fd42c94 143
Jagang 39:09c04fd42c94 144 // Change the ID
Jagang 39:09c04fd42c94 145
Jagang 39:09c04fd42c94 146 /** Change the ID of a servo
Jagang 39:09c04fd42c94 147 *
Jagang 39:09c04fd42c94 148 * @param CurentID 1-255
Jagang 39:09c04fd42c94 149 * @param NewID 1-255
Jagang 39:09c04fd42c94 150 *
Jagang 39:09c04fd42c94 151 * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
Jagang 39:09c04fd42c94 152 * In this situation, only one servo should be connected to the bus
Jagang 39:09c04fd42c94 153 */
Jagang 74:88be86f83d17 154 int setID(int CurrentID, int NewID);
Jagang 39:09c04fd42c94 155
Jagang 39:09c04fd42c94 156
Jagang 39:09c04fd42c94 157 /** Poll to see if the servo is moving
Jagang 39:09c04fd42c94 158 *
Jagang 39:09c04fd42c94 159 * @returns true is the servo is moving
Jagang 39:09c04fd42c94 160 */
Jagang 39:09c04fd42c94 161 int isMoving(void);
Jagang 39:09c04fd42c94 162
Jagang 39:09c04fd42c94 163 /** Send the broadcast "trigger" command, to activate any outstanding registered commands
Jagang 39:09c04fd42c94 164 */
Jagang 39:09c04fd42c94 165 void trigger(void);
Jagang 39:09c04fd42c94 166
Jagang 39:09c04fd42c94 167 /** Read the current angle of the servo
Jagang 39:09c04fd42c94 168 *
Jagang 39:09c04fd42c94 169 * @returns float in the range 0.0-300.0
Jagang 39:09c04fd42c94 170 */
Jagang 74:88be86f83d17 171 float getPosition();
Jagang 39:09c04fd42c94 172
Jagang 39:09c04fd42c94 173 /** Read the temperature of the servo
Jagang 39:09c04fd42c94 174 *
Jagang 39:09c04fd42c94 175 * @returns float temperature
Jagang 39:09c04fd42c94 176 */
Jagang 74:88be86f83d17 177 float getTemp(void);
Jagang 39:09c04fd42c94 178
Jagang 39:09c04fd42c94 179 /** Read the supply voltage of the servo
Jagang 39:09c04fd42c94 180 *
Jagang 39:09c04fd42c94 181 * @returns float voltage
Jagang 39:09c04fd42c94 182 */
Jagang 74:88be86f83d17 183 float getVolts(void);
Jagang 39:09c04fd42c94 184
Jagang 39:09c04fd42c94 185 int read(int ID, int start, int length, char* data);
Jagang 39:09c04fd42c94 186 int write(int ID, int start, int length, char* data, int flag=0);
Jagang 39:09c04fd42c94 187
Jagang 39:09c04fd42c94 188 private :
Jagang 39:09c04fd42c94 189
Jagang 39:09c04fd42c94 190 Serial _ax12;
Jagang 39:09c04fd42c94 191 int _ID;
Jagang 39:09c04fd42c94 192 int _baud;
Jagang 39:09c04fd42c94 193
Jagang 39:09c04fd42c94 194
Jagang 39:09c04fd42c94 195 };
Jagang 39:09c04fd42c94 196
Jagang 39:09c04fd42c94 197 #endif