Dyanamixel

Dependents:   YOZAKURA_ARM YOZAKURA_ARM_USB YOZAKURA_ARM_USB_Keyboard YOZAKURA_ARM_Keyboard0424 ... more

Committer:
yusuke_kyo
Date:
Tue Aug 18 11:43:09 2015 +0000
Revision:
14:874b672847bc
Parent:
9:0927a7b74806
linear->MX28

Who changed what in which revision?

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