mbedos senza corrente

Committer:
marcodesilva
Date:
Tue Oct 22 16:42:40 2019 +0000
Revision:
20:029e0fe30b0a
Parent:
19:16d5f8a3311f
Child:
21:fe5dd48bebc6
get Current funzionante

Who changed what in which revision?

UserRevisionLine numberNew 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 20:029e0fe30b0a 45 //#define SYNC_TORQUE_ENABLE_DEBUG 1
marcodesilva 17:6f85605f793d 46 //#define SYNC_SETGOAL_DEBUG 1
marcodesilva 20:029e0fe30b0a 47 #define SYNC_GET_POSITION_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 20:029e0fe30b0a 52 #define SYNC_GET_CURRENT_DEBUG 1
marcodesilva 17:6f85605f793d 53 //#define SYNC_GOAL_CURRENT_DEBUG 1
marcodesilva 20:029e0fe30b0a 54 //#define SYNC_OPERATING_MODE_DEBUG 1
marcodesilva 17:6f85605f793d 55
marcodesilva 17:6f85605f793d 56 // Protocol 2.0
marcodesilva 17:6f85605f793d 57 // N.B. all Register values are in HEX, on the on-line manual all are in DEC
marcodesilva 17:6f85605f793d 58 #define MX_REG_ID 0x7
marcodesilva 17:6f85605f793d 59 #define MX_REG_BAUD 0x8
marcodesilva 17:6f85605f793d 60 #define MX_REG_CW_LIMIT 0x30
marcodesilva 17:6f85605f793d 61 #define MX_REG_CCW_LIMIT 0x34
marcodesilva 17:6f85605f793d 62 #define MX_REG_GOAL_POSITION 0x74
marcodesilva 17:6f85605f793d 63 #define MX_REG_GOAL_VELOCITY 0x68
marcodesilva 17:6f85605f793d 64 #define MX_REG_PRESENT_INPUT_VOLT 0x90
marcodesilva 17:6f85605f793d 65 #define MX_REG_PRESENT_TEMP 0x92
marcodesilva 17:6f85605f793d 66 #define MX_REG_MOVING 0x7A
marcodesilva 17:6f85605f793d 67 #define MX_REG_PRESENT_POSITION 0x84
marcodesilva 17:6f85605f793d 68 #define MX_REG_TORQUE_ENABLE 0x40
marcodesilva 17:6f85605f793d 69 #define MX_REG_OPERATING_MODE 0xB
marcodesilva 17:6f85605f793d 70 #define MX_REG_PRESENT_CURRENT 0x7E
marcodesilva 17:6f85605f793d 71 #define MX_REG_GOAL_CURRENT 0x66
marcodesilva 17:6f85605f793d 72 #define MX_REG_CURRENT_LIMIT 0x26
marcodesilva 19:16d5f8a3311f 73 #define MX_REG_GOAL_PWM 0x64
marcodesilva 17:6f85605f793d 74 #define MX_OFF 0
marcodesilva 17:6f85605f793d 75 #define MX_ON 1
marcodesilva 17:6f85605f793d 76
marcodesilva 17:6f85605f793d 77 class MX{
marcodesilva 17:6f85605f793d 78
marcodesilva 17:6f85605f793d 79
marcodesilva 17:6f85605f793d 80 public:
marcodesilva 17:6f85605f793d 81
marcodesilva 17:6f85605f793d 82 //standard constructor
marcodesilva 17:6f85605f793d 83 MX();
marcodesilva 17:6f85605f793d 84 //constructor
marcodesilva 17:6f85605f793d 85 MX(int*broadcastID, int Nmotor, int MotorBaud, UARTSerial_half* pCommLayer);
marcodesilva 17:6f85605f793d 86 //constructor 2
marcodesilva 17:6f85605f793d 87 MX(int ID, int MotorBaud, UARTSerial_half* pCommLayer);
marcodesilva 17:6f85605f793d 88
marcodesilva 17:6f85605f793d 89 /** Set the mode of the servo
marcodesilva 17:6f85605f793d 90 * @param mode
marcodesilva 17:6f85605f793d 91 *
marcodesilva 17:6f85605f793d 92 *
marcodesilva 17:6f85605f793d 93 */
marcodesilva 17:6f85605f793d 94 int OperatingMode (int mode);
marcodesilva 17:6f85605f793d 95
marcodesilva 17:6f85605f793d 96 /** Set baud rate of all attached servos
marcodesilva 17:6f85605f793d 97 * @param mode
marcodesilva 17:6f85605f793d 98 * 0x00 = 9,600 bps
marcodesilva 17:6f85605f793d 99 * 0x01 = 57,600 bps (DEFAULT)
marcodesilva 17:6f85605f793d 100 * 0x02 = 115,200 bps
marcodesilva 17:6f85605f793d 101 * 0x03 = 1 Mbps
marcodesilva 17:6f85605f793d 102 * 0x04 = 2 Mbps
marcodesilva 17:6f85605f793d 103 * 0x05 = 3 Mbps
marcodesilva 17:6f85605f793d 104 * 0x06 = 4 Mbps
marcodesilva 17:6f85605f793d 105 * 0x06 = 4,5 Mbps
marcodesilva 17:6f85605f793d 106 */
marcodesilva 17:6f85605f793d 107 int motorSetBaud(int MotorBaud);
marcodesilva 17:6f85605f793d 108
marcodesilva 17:6f85605f793d 109 /** Set Torque enable
marcodesilva 17:6f85605f793d 110 * enableVal = 1 Torque On
marcodesilva 17:6f85605f793d 111 * enableVal = 0 Torque Off
marcodesilva 17:6f85605f793d 112 **/
marcodesilva 17:6f85605f793d 113 int TorqueEnable(bool enable);
marcodesilva 17:6f85605f793d 114
marcodesilva 17:6f85605f793d 115 /** Set goal angle in integer degrees, in positional mode
marcodesilva 17:6f85605f793d 116 *
marcodesilva 17:6f85605f793d 117 * @param degrees 0-300
marcodesilva 17:6f85605f793d 118 * @param flags, defaults to 0
marcodesilva 17:6f85605f793d 119 * flags[0] = blocking, return when goal position reached
marcodesilva 17:6f85605f793d 120 * flags[1] = register, activate with a broadcast trigger
marcodesilva 17:6f85605f793d 121 *
marcodesilva 17:6f85605f793d 122 */
marcodesilva 17:6f85605f793d 123 int SetGoal(float degrees, int flags = 0);
marcodesilva 17:6f85605f793d 124 /** Set goal angle in integer degrees, in positional mode
marcodesilva 17:6f85605f793d 125 *
marcodesilva 17:6f85605f793d 126 * @param degrees 0-360
marcodesilva 17:6f85605f793d 127 * @param speed 0-100
marcodesilva 17:6f85605f793d 128 * @param flags, defaults to 0
marcodesilva 17:6f85605f793d 129 * flags[0] = blocking, return when goal position reached
marcodesilva 17:6f85605f793d 130 * flags[1] = register, activate with a broadcast trigger
marcodesilva 17:6f85605f793d 131 *
marcodesilva 17:6f85605f793d 132 */
marcodesilva 17:6f85605f793d 133 int SetGoalSpeed(int speed, int flags = 0);
marcodesilva 17:6f85605f793d 134
marcodesilva 17:6f85605f793d 135
marcodesilva 17:6f85605f793d 136 /** Set the speed of the servo in continuous rotation mode
marcodesilva 17:6f85605f793d 137 *
marcodesilva 17:6f85605f793d 138 * @param speed, -1.0 to 1.0
marcodesilva 17:6f85605f793d 139 * -1.0 = full speed counter clock wise
marcodesilva 17:6f85605f793d 140 * 1.0 = full speed clock wise
marcodesilva 17:6f85605f793d 141 */
marcodesilva 17:6f85605f793d 142 int SetCRSpeed(float speed);
marcodesilva 17:6f85605f793d 143
marcodesilva 17:6f85605f793d 144
marcodesilva 17:6f85605f793d 145 /** Set the clockwise limit of the servo
marcodesilva 17:6f85605f793d 146 *
marcodesilva 17:6f85605f793d 147 * @param degrees, 0-360
marcodesilva 17:6f85605f793d 148 */
marcodesilva 17:6f85605f793d 149 int SetCWLimit(int degrees);
marcodesilva 17:6f85605f793d 150
marcodesilva 17:6f85605f793d 151 /** Set the counter-clockwise limit of the servo
marcodesilva 17:6f85605f793d 152 *
marcodesilva 17:6f85605f793d 153 * @param degrees, 0-360
marcodesilva 17:6f85605f793d 154 */
marcodesilva 17:6f85605f793d 155 int SetCCWLimit(int degrees);
marcodesilva 17:6f85605f793d 156
marcodesilva 17:6f85605f793d 157 // Change the ID
marcodesilva 17:6f85605f793d 158
marcodesilva 17:6f85605f793d 159 /** Change the ID of a servo
marcodesilva 17:6f85605f793d 160 *
marcodesilva 17:6f85605f793d 161 * @param CurentID 1-255
marcodesilva 17:6f85605f793d 162 * @param NewID 1-255
marcodesilva 17:6f85605f793d 163 *
marcodesilva 17:6f85605f793d 164 * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
marcodesilva 17:6f85605f793d 165 * In this situation, only one servo should be connected to the bus
marcodesilva 17:6f85605f793d 166 */
marcodesilva 17:6f85605f793d 167 int SetID(int CurrentID, int NewID);
marcodesilva 17:6f85605f793d 168
marcodesilva 17:6f85605f793d 169
marcodesilva 17:6f85605f793d 170 /** Poll to see if the servo is moving
marcodesilva 17:6f85605f793d 171 *
marcodesilva 17:6f85605f793d 172 * @returns true is the servo is moving
marcodesilva 17:6f85605f793d 173 */
marcodesilva 17:6f85605f793d 174 bool isMoving(void);
marcodesilva 17:6f85605f793d 175
marcodesilva 17:6f85605f793d 176 /** Send the broadcast "trigger" command, to activate any outstanding registered commands
marcodesilva 17:6f85605f793d 177 */
marcodesilva 17:6f85605f793d 178 void trigger(void);
marcodesilva 17:6f85605f793d 179
marcodesilva 17:6f85605f793d 180 /** Read the current angle of the servo
marcodesilva 17:6f85605f793d 181 *
marcodesilva 17:6f85605f793d 182 * @returns float in the range 0.0-300.0
marcodesilva 17:6f85605f793d 183 */
marcodesilva 17:6f85605f793d 184 float GetPosition();
marcodesilva 17:6f85605f793d 185
marcodesilva 17:6f85605f793d 186 /** Read the temperature of the servo
marcodesilva 17:6f85605f793d 187 *
marcodesilva 17:6f85605f793d 188 * @returns float temperature
marcodesilva 17:6f85605f793d 189 */
marcodesilva 17:6f85605f793d 190 float GetTemp(void);
marcodesilva 17:6f85605f793d 191
marcodesilva 17:6f85605f793d 192 /** Read the supply voltage of the servo
marcodesilva 17:6f85605f793d 193 *
marcodesilva 17:6f85605f793d 194 * @returns float voltage
marcodesilva 17:6f85605f793d 195 */
marcodesilva 17:6f85605f793d 196 float GetVolts(void);
marcodesilva 17:6f85605f793d 197 /** ENABLE
marcodesilva 17:6f85605f793d 198 *
marcodesilva 17:6f85605f793d 199 * @returns float voltage
marcodesilva 17:6f85605f793d 200 */
marcodesilva 17:6f85605f793d 201
marcodesilva 17:6f85605f793d 202 char* readPacket( int start, int length, int flag=0);
marcodesilva 17:6f85605f793d 203 int sendPacket(int start, int length, char* data, int flag=0);
marcodesilva 17:6f85605f793d 204 void Reboot();
marcodesilva 17:6f85605f793d 205
marcodesilva 17:6f85605f793d 206 char* SyncReadPacket(int start, int bytes);
marcodesilva 17:6f85605f793d 207 float* SyncGetPosition(void);
marcodesilva 20:029e0fe30b0a 208 float* SyncGetCurrent(void);
marcodesilva 17:6f85605f793d 209 int SyncTorqueEnable(bool enableVal[], int Nmotor =-1);
marcodesilva 17:6f85605f793d 210 int SyncSetGoal(float degrees[]);
marcodesilva 17:6f85605f793d 211 int SyncSetGoal(float degrees, int ID);
marcodesilva 17:6f85605f793d 212 int SyncSendPacket(int start, int bytes, char* data, int ID =-1);
marcodesilva 17:6f85605f793d 213 int SyncGoalCurrent(float mAmpere, int ID);
marcodesilva 17:6f85605f793d 214 int SyncGoalCurrent(float mAmpere[]);
marcodesilva 17:6f85605f793d 215 int SyncCurrentLimit(float mAmpere, int ID);
marcodesilva 17:6f85605f793d 216 int SyncCurrentLimit(float mAmpere[]);
marcodesilva 17:6f85605f793d 217 int SyncOperatingMode(int mode[], int ID);
marcodesilva 17:6f85605f793d 218 int SyncSetBaud(int MotorBaud[], int ID);
marcodesilva 19:16d5f8a3311f 219 int SyncGoalPWM(float values[], int ID);
marcodesilva 17:6f85605f793d 220
marcodesilva 17:6f85605f793d 221 private :
marcodesilva 17:6f85605f793d 222
marcodesilva 17:6f85605f793d 223 UARTSerial_half* _pCommLayer;
marcodesilva 17:6f85605f793d 224 int _ID;
marcodesilva 17:6f85605f793d 225 int _MotorBaud;
marcodesilva 17:6f85605f793d 226 int* _broadcastID;
marcodesilva 17:6f85605f793d 227 int _Nmotor;
marcodesilva 17:6f85605f793d 228
marcodesilva 17:6f85605f793d 229 unsigned short update_crc(unsigned short crc_accum, char *data_blk_ptr, unsigned short data_blk_size);
marcodesilva 17:6f85605f793d 230 };
marcodesilva 17:6f85605f793d 231
marcodesilva 17:6f85605f793d 232 #endif