mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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