Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AX12.h
00001 /* mbed AX-12+ Servo Library 00002 * 00003 * Copyright (c) 2010, cstyles (http://mbed.org) 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining a copy 00006 * of this software and associated documentation files (the "Software"), to deal 00007 * in the Software without restriction, including without limitation the rights 00008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 * copies of the Software, and to permit persons to whom the Software is 00010 * furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in 00013 * all copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 * THE SOFTWARE. 00022 * 00023 * 00024 * 00025 */ 00026 00027 #ifndef MBED_AX12_H 00028 #define MBED_AX12_H 00029 00030 #include "SerialHalfDuplex.h" 00031 #include "mbed.h" 00032 00033 #define AX12_WRITE_DEBUG 0 00034 #define AX12_READ_DEBUG 0 00035 #define AX12_TRIGGER_DEBUG 0 00036 #define AX12_DEBUG 0 00037 #define AX12_CALIB 0 00038 00039 #define AX12_REG_ID 0x3 00040 #define AX12_REG_BAUD 0x4 00041 #define AX12_REG_CW_LIMIT 0x06 00042 #define AX12_REG_CCW_LIMIT 0x08 00043 #define AX12_REG_ENABLE_TORQUE 0x18 00044 #define AX12_REG_MAX_TORQUE 0xE 00045 #define AX12_REG_LOAD 0x28 00046 #define AX12_REG_GOAL_POSITION 0x1E 00047 #define AX12_REG_MOVING_SPEED 0x20 00048 #define AX12_REG_VOLTS 0x2A 00049 #define AX12_REG_TEMP 0x2B 00050 #define AX12_REG_MOVING 0x2E 00051 #define AX12_REG_POSITION 0x24 00052 00053 #define AX12_MODE_POSITION 0 00054 #define AX12_MODE_ROTATION 1 00055 00056 #define AX12_CW 1 00057 #define AX12_CCW 0 00058 #define AX12_BAUDRATE 115200 00059 /** Servo control class, based on a PwmOut 00060 * 00061 * Example: 00062 * @code 00063 * #include "mbed.h" 00064 * #include "AX12.h" 00065 * //AX12 VDD -> +12V 00066 * //AX12 GND -> GND 00067 * //AX12 Data -> shorted serial port pins (RX&TX connected together) 00068 * 00069 * //F446RE TEST PASSED 00070 * #define TX PC_12 00071 * #define RX PD_2 00072 * #define ID 1 00073 * #define BAUD 1000000 00074 * 00075 * AX12 myax12(TX, RX,ID,BAUD); 00076 00077 * int main() { 00078 * float pos = -1.0; 00079 * 00080 * while (true) 00081 * { 00082 * myax12.SetGoal(0); // go to 0 degrees 00083 * wait (2.5); 00084 * pos = myax12.GetPosition(); 00085 * printf("Current position (0) : %f \n", pos); 00086 * myax12.SetGoal(300); // go to 300 degrees 00087 * wait (2.5); 00088 * pos = myax12.GetPosition(); 00089 * printf("Current position (300) : %f \n", pos); 00090 * } 00091 * } 00092 * @endcode 00093 */ 00094 class AX12 { 00095 00096 public: 00097 /** Create an AX12 servo object connected to the specified serial port, with the specified ID 00098 * 00099 * @param pin tx pin 00100 * @param pin rx pin 00101 * @param int ID, the Bus ID of the servo 1-255 00102 */ 00103 AX12(PinName tx, PinName rx, int ID, int baud); 00104 00105 /** Reset servo to factory settings 00106 * 00107 */ 00108 int FactoryReset(void); 00109 00110 /** Set the mode of the servo 00111 * @param mode 00112 * 0 = Positional, default 00113 * 1 = Continuous rotation 00114 */ 00115 int SetMode(int mode); 00116 00117 /** Set goal angle in integer degrees, in positional mode 00118 * 00119 * @param degrees 0-300 00120 * @param flags, defaults to 0 00121 * flags[0] = blocking, return when goal position reached 00122 * flags[1] = register, activate with a broadcast trigger 00123 * 00124 */ 00125 int SetGoal(int degrees, int flags = 0); 00126 00127 00128 /** Set the speed of the servo in continuous rotation mode 00129 * 00130 * @param speed, -1.0 to 1.0 00131 * -1.0 = full speed counter clock wise 00132 * 1.0 = full speed clock wise 00133 */ 00134 int SetCRSpeed(float speed); 00135 00136 00137 /** Set the clockwise limit of the servo 00138 * 00139 * @param degrees, 0-300 00140 */ 00141 int SetCWLimit(int degrees); 00142 00143 /** Set the counter-clockwise limit of the servo 00144 * 00145 * @param degrees, 0-300 00146 */ 00147 int SetCCWLimit(int degrees); 00148 00149 /** Enable / disable the motor torque 00150 * 00151 * @param state, 00152 * true = torque enable 00153 * false = torque disable 00154 */ 00155 int SetTorque(bool state); 00156 00157 /** Set the torque limit of the servo 00158 * 00159 * @param percentage, 0.0-1.0 00160 */ 00161 int SetMaxTorque(float percentage); 00162 00163 /** Set baud rate of all attached servos 00164 * @param mode 00165 * 0x01 = 1,000,000 bps 00166 * 0x03 = 500,000 bps 00167 * 0x04 = 400,000 bps 00168 * 0x07 = 250,000 bps 00169 * 0x09 = 200,000 bps 00170 * 0x10 = 115,200 bps 00171 * 0x22 = 57,600 bps 00172 * 0x67 = 19,200 bps 00173 * 0xCF = 9,600 bp 00174 */ 00175 int SetBaud(int baud); 00176 // Change the ID 00177 00178 /** Change the ID of a servo 00179 * 00180 * @param CurentID 1-255 00181 * @param NewID 1-255 00182 * 00183 * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. 00184 * In this situation, only one servo should be connected to the bus 00185 */ 00186 int SetID(int CurrentID, int NewID); 00187 00188 00189 /** Poll to see if the servo is moving 00190 * 00191 * @returns true is the servo is moving 00192 */ 00193 int isMoving(void); 00194 00195 /** Send the broadcast "trigger" command, to activate any outstanding registered commands 00196 */ 00197 void trigger(void); 00198 00199 /** Read the current angle of the servo 00200 * 00201 * @returns float in the range 0.0-300.0 00202 */ 00203 float GetPosition(); 00204 00205 /** Read the temperature of the servo 00206 * 00207 * @returns float temperature 00208 */ 00209 float GetTemp(void); 00210 00211 /** Read the supply voltage of the servo 00212 * 00213 * @returns float voltage 00214 */ 00215 float GetVolts(void); 00216 00217 /** Get the current load (torque) on the servo 00218 * 00219 * @returns float load (percentage) 00220 * @attention not very accurate 00221 */ 00222 //////////////////////////////////////////////////////////////////////////////////////////////// 00223 // 00224 // !!! NOT WORKING !!! 00225 // 00226 //////////////////////////////////////////////////////////////////////////////////////////////// 00227 float GetLoad(void); 00228 00229 private : 00230 00231 SerialHalfDuplex _ax12; 00232 int _ID; 00233 int _baud; 00234 int read(int ID, int start, int length, char* data); 00235 int write(int ID, int start, int length, char* data, int flag=0); 00236 }; 00237 00238 #endif
Generated on Wed Jul 13 2022 18:26:15 by
1.7.2