Dependents:   Snackotron Single_Axis NewCyroroProto AX12 ... more

Committer:
chris
Date:
Sun Apr 10 20:58:21 2011 +0000
Revision:
2:5ea99c37a2d7
Parent:
1:93ad80f5fde7
Child:
3:ced71d1b2558
Revised read/write function code to prevent hangs when a response packet is missed.
Converted debug statements to #ifdef statements
Added additional parameter to constructor to pass in baud rate

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