mbedos senza corrente
MX.h@18:862a4afc1fbe, 2019-10-22 (annotated)
- Committer:
- marcodesilva
- Date:
- Tue Oct 22 10:03:04 2019 +0000
- Revision:
- 18:862a4afc1fbe
- Parent:
- 17:6f85605f793d
pwm aggiunta;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcodesilva | 17:6f85605f793d | 1 | /* mbed MX-64 Servo Library |
marcodesilva | 17:6f85605f793d | 2 | * |
marcodesilva | 17:6f85605f793d | 3 | * Copyright (c) 2010, cstyles (http://mbed.org) |
marcodesilva | 17:6f85605f793d | 4 | * |
marcodesilva | 17:6f85605f793d | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
marcodesilva | 17:6f85605f793d | 6 | * of this software and associated documentation files (the "Software"), to deal |
marcodesilva | 17:6f85605f793d | 7 | * in the Software without restriction, including without limitation the rights |
marcodesilva | 17:6f85605f793d | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
marcodesilva | 17:6f85605f793d | 9 | * copies of the Software, and to permit persons to whom the Software is |
marcodesilva | 17:6f85605f793d | 10 | * furnished to do so, subject to the following conditions: |
marcodesilva | 17:6f85605f793d | 11 | * |
marcodesilva | 17:6f85605f793d | 12 | * The above copyright notice and this permission notice shall be included in |
marcodesilva | 17:6f85605f793d | 13 | * all copies or substantial portions of the Software. |
marcodesilva | 17:6f85605f793d | 14 | * |
marcodesilva | 17:6f85605f793d | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
marcodesilva | 17:6f85605f793d | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
marcodesilva | 17:6f85605f793d | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
marcodesilva | 17:6f85605f793d | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
marcodesilva | 17:6f85605f793d | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
marcodesilva | 17:6f85605f793d | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
marcodesilva | 17:6f85605f793d | 21 | * THE SOFTWARE. |
marcodesilva | 17:6f85605f793d | 22 | */ |
marcodesilva | 17:6f85605f793d | 23 | |
marcodesilva | 17:6f85605f793d | 24 | #ifndef MBED_MX_H |
marcodesilva | 17:6f85605f793d | 25 | #define MBED_MX_H |
marcodesilva | 17:6f85605f793d | 26 | |
marcodesilva | 17:6f85605f793d | 27 | #include "mbed.h" |
marcodesilva | 17:6f85605f793d | 28 | #include "UARTSerial_half.h" |
marcodesilva | 17:6f85605f793d | 29 | |
marcodesilva | 17:6f85605f793d | 30 | //#define SENDPACKET_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 31 | //#define SENDPACKET_DEBUG_INSTRUCTION_PKT 1 |
marcodesilva | 17:6f85605f793d | 32 | //#define SENDPACKET_DEBUG_STATUS_PKT 1 |
marcodesilva | 17:6f85605f793d | 33 | //#define READ_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 34 | //#define READ_DEBUG_INSTRUCTION_PKT 1 |
marcodesilva | 17:6f85605f793d | 35 | //#define READ_DEBUG_STATUS_PKT 1 |
marcodesilva | 17:6f85605f793d | 36 | //#define GETPOSITION_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 37 | |
marcodesilva | 17:6f85605f793d | 38 | //#define TORQUE_ENABLE_DEBUGDEBUG 1 |
marcodesilva | 17:6f85605f793d | 39 | //#define REBOOT_ENABLE_DEBUG |
marcodesilva | 17:6f85605f793d | 40 | //#define SETGOAL_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 41 | //#define SETGOAL_SPEED_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 42 | //#define OPERATING_MODE_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 43 | //#define SETBAUD_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 44 | |
marcodesilva | 17:6f85605f793d | 45 | //#define SYNC_TORQUE_ENABLE_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 46 | //#define SYNC_SETGOAL_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 47 | //#define SYNC_GETPOSITION_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 48 | //#define SYNC_SENDPACKET_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 49 | //#define SYNC_SENDPACKET_DEBUG_PACKETONLY 1 |
marcodesilva | 17:6f85605f793d | 50 | //#define SYNC_READPACKET_DEBUG_PACKETONLY 1 |
marcodesilva | 17:6f85605f793d | 51 | //#define SYNC_SET_BAUD_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 52 | //#define SYNC_GOAL_CURRENT_DEBUG 1 |
marcodesilva | 17:6f85605f793d | 53 | |
marcodesilva | 17:6f85605f793d | 54 | |
marcodesilva | 17:6f85605f793d | 55 | // Protocol 2.0 |
marcodesilva | 17:6f85605f793d | 56 | // N.B. all Register values are in HEX, on the on-line manual all are in DEC |
marcodesilva | 17:6f85605f793d | 57 | #define MX_REG_ID 0x7 |
marcodesilva | 17:6f85605f793d | 58 | #define MX_REG_BAUD 0x8 |
marcodesilva | 17:6f85605f793d | 59 | #define MX_REG_CW_LIMIT 0x30 |
marcodesilva | 17:6f85605f793d | 60 | #define MX_REG_CCW_LIMIT 0x34 |
marcodesilva | 17:6f85605f793d | 61 | #define MX_REG_GOAL_POSITION 0x74 |
marcodesilva | 17:6f85605f793d | 62 | #define MX_REG_GOAL_VELOCITY 0x68 |
marcodesilva | 17:6f85605f793d | 63 | #define MX_REG_PRESENT_INPUT_VOLT 0x90 |
marcodesilva | 17:6f85605f793d | 64 | #define MX_REG_PRESENT_TEMP 0x92 |
marcodesilva | 17:6f85605f793d | 65 | #define MX_REG_MOVING 0x7A |
marcodesilva | 17:6f85605f793d | 66 | #define MX_REG_PRESENT_POSITION 0x84 |
marcodesilva | 17:6f85605f793d | 67 | #define MX_REG_TORQUE_ENABLE 0x40 |
marcodesilva | 17:6f85605f793d | 68 | #define MX_REG_OPERATING_MODE 0xB |
marcodesilva | 17:6f85605f793d | 69 | #define MX_REG_PRESENT_CURRENT 0x7E |
marcodesilva | 17:6f85605f793d | 70 | #define MX_REG_GOAL_CURRENT 0x66 |
marcodesilva | 17:6f85605f793d | 71 | #define MX_REG_CURRENT_LIMIT 0x26 |
marcodesilva | 18:862a4afc1fbe | 72 | #define MX_REG_GOAL_PWM 0x64 |
marcodesilva | 17:6f85605f793d | 73 | #define MX_OFF 0 |
marcodesilva | 17:6f85605f793d | 74 | #define MX_ON 1 |
marcodesilva | 17:6f85605f793d | 75 | |
marcodesilva | 17:6f85605f793d | 76 | class MX{ |
marcodesilva | 17:6f85605f793d | 77 | |
marcodesilva | 17:6f85605f793d | 78 | |
marcodesilva | 17:6f85605f793d | 79 | public: |
marcodesilva | 17:6f85605f793d | 80 | |
marcodesilva | 17:6f85605f793d | 81 | //standard constructor |
marcodesilva | 17:6f85605f793d | 82 | MX(); |
marcodesilva | 17:6f85605f793d | 83 | //constructor |
marcodesilva | 17:6f85605f793d | 84 | MX(int*broadcastID, int Nmotor, int MotorBaud, UARTSerial_half* pCommLayer); |
marcodesilva | 17:6f85605f793d | 85 | //constructor 2 |
marcodesilva | 17:6f85605f793d | 86 | MX(int ID, int MotorBaud, UARTSerial_half* pCommLayer); |
marcodesilva | 17:6f85605f793d | 87 | |
marcodesilva | 17:6f85605f793d | 88 | /** Set the mode of the servo |
marcodesilva | 17:6f85605f793d | 89 | * @param mode |
marcodesilva | 17:6f85605f793d | 90 | * |
marcodesilva | 17:6f85605f793d | 91 | * |
marcodesilva | 17:6f85605f793d | 92 | */ |
marcodesilva | 17:6f85605f793d | 93 | int OperatingMode (int mode); |
marcodesilva | 17:6f85605f793d | 94 | |
marcodesilva | 17:6f85605f793d | 95 | /** Set baud rate of all attached servos |
marcodesilva | 17:6f85605f793d | 96 | * @param mode |
marcodesilva | 17:6f85605f793d | 97 | * 0x00 = 9,600 bps |
marcodesilva | 17:6f85605f793d | 98 | * 0x01 = 57,600 bps (DEFAULT) |
marcodesilva | 17:6f85605f793d | 99 | * 0x02 = 115,200 bps |
marcodesilva | 17:6f85605f793d | 100 | * 0x03 = 1 Mbps |
marcodesilva | 17:6f85605f793d | 101 | * 0x04 = 2 Mbps |
marcodesilva | 17:6f85605f793d | 102 | * 0x05 = 3 Mbps |
marcodesilva | 17:6f85605f793d | 103 | * 0x06 = 4 Mbps |
marcodesilva | 17:6f85605f793d | 104 | * 0x06 = 4,5 Mbps |
marcodesilva | 17:6f85605f793d | 105 | */ |
marcodesilva | 17:6f85605f793d | 106 | int motorSetBaud(int MotorBaud); |
marcodesilva | 17:6f85605f793d | 107 | |
marcodesilva | 17:6f85605f793d | 108 | /** Set Torque enable |
marcodesilva | 17:6f85605f793d | 109 | * enableVal = 1 Torque On |
marcodesilva | 17:6f85605f793d | 110 | * enableVal = 0 Torque Off |
marcodesilva | 17:6f85605f793d | 111 | **/ |
marcodesilva | 17:6f85605f793d | 112 | int TorqueEnable(bool enable); |
marcodesilva | 17:6f85605f793d | 113 | |
marcodesilva | 17:6f85605f793d | 114 | /** Set goal angle in integer degrees, in positional mode |
marcodesilva | 17:6f85605f793d | 115 | * |
marcodesilva | 17:6f85605f793d | 116 | * @param degrees 0-300 |
marcodesilva | 17:6f85605f793d | 117 | * @param flags, defaults to 0 |
marcodesilva | 17:6f85605f793d | 118 | * flags[0] = blocking, return when goal position reached |
marcodesilva | 17:6f85605f793d | 119 | * flags[1] = register, activate with a broadcast trigger |
marcodesilva | 17:6f85605f793d | 120 | * |
marcodesilva | 17:6f85605f793d | 121 | */ |
marcodesilva | 17:6f85605f793d | 122 | int SetGoal(float degrees, int flags = 0); |
marcodesilva | 17:6f85605f793d | 123 | /** Set goal angle in integer degrees, in positional mode |
marcodesilva | 17:6f85605f793d | 124 | * |
marcodesilva | 17:6f85605f793d | 125 | * @param degrees 0-360 |
marcodesilva | 17:6f85605f793d | 126 | * @param speed 0-100 |
marcodesilva | 17:6f85605f793d | 127 | * @param flags, defaults to 0 |
marcodesilva | 17:6f85605f793d | 128 | * flags[0] = blocking, return when goal position reached |
marcodesilva | 17:6f85605f793d | 129 | * flags[1] = register, activate with a broadcast trigger |
marcodesilva | 17:6f85605f793d | 130 | * |
marcodesilva | 17:6f85605f793d | 131 | */ |
marcodesilva | 17:6f85605f793d | 132 | int SetGoalSpeed(int speed, int flags = 0); |
marcodesilva | 17:6f85605f793d | 133 | |
marcodesilva | 17:6f85605f793d | 134 | |
marcodesilva | 17:6f85605f793d | 135 | /** Set the speed of the servo in continuous rotation mode |
marcodesilva | 17:6f85605f793d | 136 | * |
marcodesilva | 17:6f85605f793d | 137 | * @param speed, -1.0 to 1.0 |
marcodesilva | 17:6f85605f793d | 138 | * -1.0 = full speed counter clock wise |
marcodesilva | 17:6f85605f793d | 139 | * 1.0 = full speed clock wise |
marcodesilva | 17:6f85605f793d | 140 | */ |
marcodesilva | 17:6f85605f793d | 141 | int SetCRSpeed(float speed); |
marcodesilva | 17:6f85605f793d | 142 | |
marcodesilva | 17:6f85605f793d | 143 | |
marcodesilva | 17:6f85605f793d | 144 | /** Set the clockwise limit of the servo |
marcodesilva | 17:6f85605f793d | 145 | * |
marcodesilva | 17:6f85605f793d | 146 | * @param degrees, 0-360 |
marcodesilva | 17:6f85605f793d | 147 | */ |
marcodesilva | 17:6f85605f793d | 148 | int SetCWLimit(int degrees); |
marcodesilva | 17:6f85605f793d | 149 | |
marcodesilva | 17:6f85605f793d | 150 | /** Set the counter-clockwise limit of the servo |
marcodesilva | 17:6f85605f793d | 151 | * |
marcodesilva | 17:6f85605f793d | 152 | * @param degrees, 0-360 |
marcodesilva | 17:6f85605f793d | 153 | */ |
marcodesilva | 17:6f85605f793d | 154 | int SetCCWLimit(int degrees); |
marcodesilva | 17:6f85605f793d | 155 | |
marcodesilva | 17:6f85605f793d | 156 | // Change the ID |
marcodesilva | 17:6f85605f793d | 157 | |
marcodesilva | 17:6f85605f793d | 158 | /** Change the ID of a servo |
marcodesilva | 17:6f85605f793d | 159 | * |
marcodesilva | 17:6f85605f793d | 160 | * @param CurentID 1-255 |
marcodesilva | 17:6f85605f793d | 161 | * @param NewID 1-255 |
marcodesilva | 17:6f85605f793d | 162 | * |
marcodesilva | 17:6f85605f793d | 163 | * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. |
marcodesilva | 17:6f85605f793d | 164 | * In this situation, only one servo should be connected to the bus |
marcodesilva | 17:6f85605f793d | 165 | */ |
marcodesilva | 17:6f85605f793d | 166 | int SetID(int CurrentID, int NewID); |
marcodesilva | 17:6f85605f793d | 167 | |
marcodesilva | 17:6f85605f793d | 168 | |
marcodesilva | 17:6f85605f793d | 169 | /** Poll to see if the servo is moving |
marcodesilva | 17:6f85605f793d | 170 | * |
marcodesilva | 17:6f85605f793d | 171 | * @returns true is the servo is moving |
marcodesilva | 17:6f85605f793d | 172 | */ |
marcodesilva | 17:6f85605f793d | 173 | bool isMoving(void); |
marcodesilva | 17:6f85605f793d | 174 | |
marcodesilva | 17:6f85605f793d | 175 | /** Send the broadcast "trigger" command, to activate any outstanding registered commands |
marcodesilva | 17:6f85605f793d | 176 | */ |
marcodesilva | 17:6f85605f793d | 177 | void trigger(void); |
marcodesilva | 17:6f85605f793d | 178 | |
marcodesilva | 17:6f85605f793d | 179 | /** Read the current angle of the servo |
marcodesilva | 17:6f85605f793d | 180 | * |
marcodesilva | 17:6f85605f793d | 181 | * @returns float in the range 0.0-300.0 |
marcodesilva | 17:6f85605f793d | 182 | */ |
marcodesilva | 17:6f85605f793d | 183 | float GetPosition(); |
marcodesilva | 17:6f85605f793d | 184 | |
marcodesilva | 17:6f85605f793d | 185 | /** Read the temperature of the servo |
marcodesilva | 17:6f85605f793d | 186 | * |
marcodesilva | 17:6f85605f793d | 187 | * @returns float temperature |
marcodesilva | 17:6f85605f793d | 188 | */ |
marcodesilva | 17:6f85605f793d | 189 | float GetTemp(void); |
marcodesilva | 17:6f85605f793d | 190 | |
marcodesilva | 17:6f85605f793d | 191 | /** Read the supply voltage of the servo |
marcodesilva | 17:6f85605f793d | 192 | * |
marcodesilva | 17:6f85605f793d | 193 | * @returns float voltage |
marcodesilva | 17:6f85605f793d | 194 | */ |
marcodesilva | 17:6f85605f793d | 195 | float GetVolts(void); |
marcodesilva | 17:6f85605f793d | 196 | /** ENABLE |
marcodesilva | 17:6f85605f793d | 197 | * |
marcodesilva | 17:6f85605f793d | 198 | * @returns float voltage |
marcodesilva | 17:6f85605f793d | 199 | */ |
marcodesilva | 17:6f85605f793d | 200 | |
marcodesilva | 17:6f85605f793d | 201 | char* readPacket( int start, int length, int flag=0); |
marcodesilva | 17:6f85605f793d | 202 | int sendPacket(int start, int length, char* data, int flag=0); |
marcodesilva | 17:6f85605f793d | 203 | void Reboot(); |
marcodesilva | 17:6f85605f793d | 204 | |
marcodesilva | 17:6f85605f793d | 205 | char* SyncReadPacket(int start, int bytes); |
marcodesilva | 17:6f85605f793d | 206 | float* SyncGetPosition(void); |
marcodesilva | 17:6f85605f793d | 207 | int SyncTorqueEnable(bool enableVal[], int Nmotor =-1); |
marcodesilva | 17:6f85605f793d | 208 | int SyncSetGoal(float degrees[]); |
marcodesilva | 17:6f85605f793d | 209 | int SyncSetGoal(float degrees, int ID); |
marcodesilva | 17:6f85605f793d | 210 | int SyncSendPacket(int start, int bytes, char* data, int ID =-1); |
marcodesilva | 17:6f85605f793d | 211 | int SyncGoalCurrent(float mAmpere, int ID); |
marcodesilva | 17:6f85605f793d | 212 | int SyncGoalCurrent(float mAmpere[]); |
marcodesilva | 17:6f85605f793d | 213 | int SyncCurrentLimit(float mAmpere, int ID); |
marcodesilva | 17:6f85605f793d | 214 | int SyncCurrentLimit(float mAmpere[]); |
marcodesilva | 17:6f85605f793d | 215 | int SyncOperatingMode(int mode[], int ID); |
marcodesilva | 17:6f85605f793d | 216 | int SyncSetBaud(int MotorBaud[], int ID); |
marcodesilva | 18:862a4afc1fbe | 217 | int SyncGoalPwm(float tension, int ID); |
marcodesilva | 17:6f85605f793d | 218 | |
marcodesilva | 17:6f85605f793d | 219 | private : |
marcodesilva | 17:6f85605f793d | 220 | |
marcodesilva | 17:6f85605f793d | 221 | UARTSerial_half* _pCommLayer; |
marcodesilva | 17:6f85605f793d | 222 | int _ID; |
marcodesilva | 17:6f85605f793d | 223 | int _MotorBaud; |
marcodesilva | 17:6f85605f793d | 224 | int* _broadcastID; |
marcodesilva | 17:6f85605f793d | 225 | int _Nmotor; |
marcodesilva | 17:6f85605f793d | 226 | |
marcodesilva | 17:6f85605f793d | 227 | unsigned short update_crc(unsigned short crc_accum, char *data_blk_ptr, unsigned short data_blk_size); |
marcodesilva | 17:6f85605f793d | 228 | }; |
marcodesilva | 17:6f85605f793d | 229 | |
marcodesilva | 17:6f85605f793d | 230 | #endif |