new ax12 lib

Dependents:   2014-ax12-test

Fork of AX12 by Chris Styles

Committer:
ppr2013G2
Date:
Thu Jun 26 15:26:50 2014 +0000
Revision:
6:99c6ddabe9e6
Parent:
5:fd39f18236cf
fix podcast id

Who changed what in which revision?

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