Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Mon Mar 16 21:46:12 2015 +0000
Revision:
39:09c04fd42c94
Child:
74:88be86f83d17
Update to AX12 from ProtocarteV1

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