Dependents:   Snackotron Single_Axis NewCyroroProto AX12 ... more

Committer:
chris
Date:
Sun Apr 10 21:20:44 2011 +0000
Revision:
3:ced71d1b2558
Parent:
2:5ea99c37a2d7
Added baud rate function to the AX12 class

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"
chris 0:be51952765ec 28
chris 2:5ea99c37a2d7 29 //#define AX12_WRITE_DEBUG 0
chris 2:5ea99c37a2d7 30 //#define AX12_READ_DEBUG 0
chris 2:5ea99c37a2d7 31 //#define AX12_TRIGGER_DEBUG 0
chris 2:5ea99c37a2d7 32 //#define AX12_DEBUG 0
chris 0:be51952765ec 33
chris 0:be51952765ec 34 #define AX12_REG_ID 0x3
chris 3:ced71d1b2558 35 #define AX12_REG_BAUD 0x4
chris 0:be51952765ec 36 #define AX12_REG_CW_LIMIT 0x06
chris 0:be51952765ec 37 #define AX12_REG_CCW_LIMIT 0x08
chris 0:be51952765ec 38 #define AX12_REG_GOAL_POSITION 0x1E
chris 0:be51952765ec 39 #define AX12_REG_MOVING_SPEED 0x20
chris 0:be51952765ec 40 #define AX12_REG_VOLTS 0x2A
chris 0:be51952765ec 41 #define AX12_REG_TEMP 0x2B
chris 0:be51952765ec 42 #define AX12_REG_MOVING 0x2E
chris 0:be51952765ec 43 #define AX12_REG_POSITION 0x24
chris 0:be51952765ec 44
chris 0:be51952765ec 45 #define AX12_MODE_POSITION 0
chris 0:be51952765ec 46 #define AX12_MODE_ROTATION 1
chris 0:be51952765ec 47
chris 0:be51952765ec 48 #define AX12_CW 1
chris 0:be51952765ec 49 #define AX12_CCW 0
chris 0:be51952765ec 50
chris 1:93ad80f5fde7 51 /** Servo control class, based on a PwmOut
chris 1:93ad80f5fde7 52 *
chris 1:93ad80f5fde7 53 * Example:
chris 1:93ad80f5fde7 54 * @code
chris 1:93ad80f5fde7 55 * #include "mbed.h"
chris 1:93ad80f5fde7 56 * #include "AX12.h"
chris 1:93ad80f5fde7 57 *
chris 1:93ad80f5fde7 58 * int main() {
chris 1:93ad80f5fde7 59 *
chris 1:93ad80f5fde7 60 * AX12 myax12 (p9, p10, 1);
chris 1:93ad80f5fde7 61 *
chris 1:93ad80f5fde7 62 * while (1) {
chris 1:93ad80f5fde7 63 * myax12.SetGoal(0); // go to 0 degrees
chris 1:93ad80f5fde7 64 * wait (2.0);
chris 1:93ad80f5fde7 65 * myax12.SetGoal(300); // go to 300 degrees
chris 1:93ad80f5fde7 66 * wait (2.0);
chris 1:93ad80f5fde7 67 * }
chris 1:93ad80f5fde7 68 * }
chris 1:93ad80f5fde7 69 * @endcode
chris 1:93ad80f5fde7 70 */
chris 0:be51952765ec 71 class AX12 {
chris 0:be51952765ec 72
chris 0:be51952765ec 73 public:
chris 0:be51952765ec 74
chris 1:93ad80f5fde7 75 /** Create an AX12 servo object connected to the specified serial port, with the specified ID
chris 1:93ad80f5fde7 76 *
chris 1:93ad80f5fde7 77 * @param pin tx pin
chris 1:93ad80f5fde7 78 * @param pin rx pin
chris 1:93ad80f5fde7 79 * @param int ID, the Bus ID of the servo 1-255
chris 1:93ad80f5fde7 80 */
chris 2:5ea99c37a2d7 81 AX12(PinName tx, PinName rx, int ID, int baud=1000000);
chris 0:be51952765ec 82
chris 1:93ad80f5fde7 83 /** Set the mode of the servo
chris 1:93ad80f5fde7 84 * @param mode
chris 1:93ad80f5fde7 85 * 0 = Positional, default
chris 1:93ad80f5fde7 86 * 1 = Continuous rotation
chris 1:93ad80f5fde7 87 */
chris 1:93ad80f5fde7 88 int SetMode(int mode);
chris 0:be51952765ec 89
chris 3:ced71d1b2558 90 /** Set baud rate of all attached servos
chris 3:ced71d1b2558 91 * @param mode
chris 3:ced71d1b2558 92 * 0x01 = 1,000,000 bps
chris 3:ced71d1b2558 93 * 0x03 = 500,000 bps
chris 3:ced71d1b2558 94 * 0x04 = 400,000 bps
chris 3:ced71d1b2558 95 * 0x07 = 250,000 bps
chris 3:ced71d1b2558 96 * 0x09 = 200,000 bps
chris 3:ced71d1b2558 97 * 0x10 = 115,200 bps
chris 3:ced71d1b2558 98 * 0x22 = 57,600 bps
chris 3:ced71d1b2558 99 * 0x67 = 19,200 bps
chris 3:ced71d1b2558 100 * 0xCF = 9,600 bp
chris 3:ced71d1b2558 101 */
chris 3:ced71d1b2558 102 int SetBaud(int baud);
chris 3:ced71d1b2558 103
chris 3:ced71d1b2558 104
chris 1:93ad80f5fde7 105 /** Set goal angle in integer degrees, in positional mode
chris 1:93ad80f5fde7 106 *
chris 1:93ad80f5fde7 107 * @param degrees 0-300
chris 1:93ad80f5fde7 108 * @param flags, defaults to 0
chris 1:93ad80f5fde7 109 * flags[0] = blocking, return when goal position reached
chris 1:93ad80f5fde7 110 * flags[1] = register, activate with a broadcast trigger
chris 1:93ad80f5fde7 111 *
chris 1:93ad80f5fde7 112 */
chris 0:be51952765ec 113 int SetGoal(int degrees, int flags = 0);
chris 0:be51952765ec 114
chris 0:be51952765ec 115
chris 1:93ad80f5fde7 116 /** Set the speed of the servo in continuous rotation mode
chris 1:93ad80f5fde7 117 *
chris 1:93ad80f5fde7 118 * @param speed, -1.0 to 1.0
chris 1:93ad80f5fde7 119 * -1.0 = full speed counter clock wise
chris 1:93ad80f5fde7 120 * 1.0 = full speed clock wise
chris 1:93ad80f5fde7 121 */
chris 0:be51952765ec 122 int SetCRSpeed(float speed);
chris 0:be51952765ec 123
chris 1:93ad80f5fde7 124
chris 1:93ad80f5fde7 125 /** Set the clockwise limit of the servo
chris 1:93ad80f5fde7 126 *
chris 1:93ad80f5fde7 127 * @param degrees, 0-300
chris 1:93ad80f5fde7 128 */
chris 0:be51952765ec 129 int SetCWLimit(int degrees);
chris 1:93ad80f5fde7 130
chris 1:93ad80f5fde7 131 /** Set the counter-clockwise limit of the servo
chris 1:93ad80f5fde7 132 *
chris 1:93ad80f5fde7 133 * @param degrees, 0-300
chris 1:93ad80f5fde7 134 */
chris 0:be51952765ec 135 int SetCCWLimit(int degrees);
chris 0:be51952765ec 136
chris 0:be51952765ec 137 // Change the ID
chris 1:93ad80f5fde7 138
chris 1:93ad80f5fde7 139 /** Change the ID of a servo
chris 1:93ad80f5fde7 140 *
chris 1:93ad80f5fde7 141 * @param CurentID 1-255
chris 1:93ad80f5fde7 142 * @param NewID 1-255
chris 1:93ad80f5fde7 143 *
chris 1:93ad80f5fde7 144 * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
chris 1:93ad80f5fde7 145 * In this situation, only one servo should be connected to the bus
chris 1:93ad80f5fde7 146 */
chris 0:be51952765ec 147 int SetID(int CurrentID, int NewID);
chris 0:be51952765ec 148
chris 1:93ad80f5fde7 149
chris 1:93ad80f5fde7 150 /** Poll to see if the servo is moving
chris 1:93ad80f5fde7 151 *
chris 1:93ad80f5fde7 152 * @returns true is the servo is moving
chris 1:93ad80f5fde7 153 */
chris 0:be51952765ec 154 int isMoving(void);
chris 0:be51952765ec 155
chris 1:93ad80f5fde7 156 /** Send the broadcast "trigger" command, to activate any outstanding registered commands
chris 1:93ad80f5fde7 157 */
chris 0:be51952765ec 158 void trigger(void);
chris 0:be51952765ec 159
chris 1:93ad80f5fde7 160 /** Read the current angle of the servo
chris 1:93ad80f5fde7 161 *
chris 1:93ad80f5fde7 162 * @returns float in the range 0.0-300.0
chris 1:93ad80f5fde7 163 */
chris 0:be51952765ec 164 float GetPosition();
chris 0:be51952765ec 165
chris 1:93ad80f5fde7 166 /** Read the temperature of the servo
chris 1:93ad80f5fde7 167 *
chris 1:93ad80f5fde7 168 * @returns float temperature
chris 1:93ad80f5fde7 169 */
chris 0:be51952765ec 170 float GetTemp(void);
chris 1:93ad80f5fde7 171
chris 1:93ad80f5fde7 172 /** Read the supply voltage of the servo
chris 1:93ad80f5fde7 173 *
chris 1:93ad80f5fde7 174 * @returns float voltage
chris 1:93ad80f5fde7 175 */
chris 0:be51952765ec 176 float GetVolts(void);
chris 0:be51952765ec 177
chris 2:5ea99c37a2d7 178 int read(int ID, int start, int length, char* data);
chris 2:5ea99c37a2d7 179 int write(int ID, int start, int length, char* data, int flag=0);
chris 2:5ea99c37a2d7 180
chris 0:be51952765ec 181 private :
chris 0:be51952765ec 182
chris 0:be51952765ec 183 SerialHalfDuplex _ax12;
chris 0:be51952765ec 184 int _ID;
chris 2:5ea99c37a2d7 185 int _baud;
chris 0:be51952765ec 186
chris 1:93ad80f5fde7 187
chris 0:be51952765ec 188 };
chris 0:be51952765ec 189
chris 0:be51952765ec 190 #endif