Dyanamixel
Dependents: YOZAKURA_ARM YOZAKURA_ARM_USB YOZAKURA_ARM_USB_Keyboard YOZAKURA_ARM_Keyboard0424 ... more
MX28.cpp
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 #include "MX28.h" 00025 #include "mbed.h" 00026 //#include "SerialHalfDuplex.h" 00027 00028 MX28::MX28(PinName tx, PinName rx, int ID, int baudrate) 00029 : _mx28(tx,rx) { 00030 00031 _mx28.baud(baudrate); 00032 _ID = ID; 00033 } 00034 00035 // Set the mode of the servo 00036 // 0 = Positional (0-300 degrees) 00037 // 1 = Rotational -1 to 1 speed 00038 int MX28::SetMode(int mode) { 00039 00040 if (mode == 1) { // set CR 00041 SetCWLimit(0); 00042 SetCCWLimit(0); 00043 SetCRSpeed(0.0); 00044 } else { 00045 SetCWLimit(0); 00046 SetCCWLimit(360); 00047 SetCRSpeed(0.0); 00048 } 00049 return(0); 00050 } 00051 00052 00053 // if flag[0] is set, were blocking 00054 // if flag[1] is set, we're registering 00055 // they are mutually exclusive operations 00056 int MX28::SetGoal(int degrees, int flags) { 00057 00058 char reg_flag = 0; 00059 char data[2]; 00060 00061 // set the flag is only the register bit is set in the flag 00062 if (flags == 0x2) { 00063 reg_flag = 1; 00064 } 00065 00066 // 4095 / 360 * degrees 00067 short goal = (4095 * degrees) / 360; 00068 if (MX28_DEBUG) { 00069 printf("SetGoal to %d[degrees], 0x%x\n",degrees,goal); 00070 } 00071 00072 data[0] = goal & 0xff; // bottom 8 bits 00073 data[1] = goal >> 8; // top 8 bits 00074 00075 // write the packet, return the error code 00076 int rVal = write(_ID, MX28_REG_GOAL_POSITION, 2, data, reg_flag); 00077 00078 if (flags == 1) { 00079 // block until it comes to a halt 00080 while (isMoving()) {} 00081 } 00082 return(rVal); 00083 } 00084 00085 00086 // Set continuous rotation speed from -1 to 1 00087 int MX28::SetCRSpeed(float speed) { 00088 00089 // bit 10 = direction, 0 = CCW, 1=CW 00090 // bits 9-0 = Speed 00091 char data[2]; 00092 00093 int goal = (0x3ff * abs(speed)); 00094 00095 if (MX28_DEBUG) { 00096 printf("SetCRSpeed to 0x%x\n",goal); 00097 } 00098 00099 // Set direction CW if we have a negative speed 00100 if (speed < 0) { 00101 goal |= (0x1 << 10); 00102 } 00103 00104 data[0] = goal & 0xff; // bottom 8 bits 00105 data[1] = goal >> 8; // top 8 bits 00106 00107 // write the packet, return the error code 00108 int rVal = write(_ID, 0x20, 2, data); 00109 00110 return(rVal); 00111 } 00112 00113 00114 int MX28::SetCWLimit (int degrees) { 00115 00116 char data[2]; 00117 00118 // 4095 / 360 * degrees 00119 short limit = (4095 * degrees) / 360; 00120 00121 if (MX28_DEBUG) { 00122 printf("SetCWLimit to 0x%x\n",limit); 00123 } 00124 00125 data[0] = limit & 0xff; // bottom 8 bits 00126 data[1] = limit >> 8; // top 8 bits 00127 00128 // write the packet, return the error code 00129 return (write(_ID, MX28_REG_CW_LIMIT, 2, data)); 00130 00131 } 00132 00133 00134 int MX28::SetCCWLimit (int degrees) { 00135 00136 char data[2]; 00137 00138 // 4095 / 360 * degrees 00139 short limit = (4095 * degrees) / 360; 00140 00141 if (MX28_DEBUG) { 00142 printf("SetCCWLimit to 0x%x\n",limit); 00143 } 00144 00145 data[0] = limit & 0xff; // bottom 8 bits 00146 data[1] = limit >> 8; // top 8 bits 00147 00148 // write the packet, return the error code 00149 return (write(_ID, MX28_REG_CCW_LIMIT, 2, data)); 00150 } 00151 00152 00153 int MX28::SetID (int CurrentID, int NewID) { 00154 00155 char data[1]; 00156 data[0] = NewID; 00157 if (MX28_DEBUG) { 00158 printf("Setting ID from 0x%x to 0x%x\n",CurrentID,NewID); 00159 } 00160 return (write(CurrentID, MX28_REG_ID, 1, data)); 00161 00162 } 00163 00164 00165 // return 1 is the servo is still in flight 00166 int MX28::isMoving(void) { 00167 00168 char data[1]; 00169 read(_ID,MX28_REG_MOVING,1,data); 00170 return(data[0]); 00171 } 00172 00173 00174 void MX28::trigger(void) { 00175 00176 char TxBuf[16]; 00177 char sum = 0; 00178 00179 if (MX28_TRIGGER_DEBUG) { 00180 printf("\nTriggered\n"); 00181 } 00182 00183 // Build the TxPacket first in RAM, then we'll send in one go 00184 if (MX28_TRIGGER_DEBUG) { 00185 printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n"); 00186 } 00187 00188 TxBuf[0] = 0xFF; 00189 TxBuf[1] = 0xFF; 00190 00191 // ID - Broadcast 00192 TxBuf[2] = 0xFE; 00193 sum += TxBuf[2]; 00194 00195 if (MX28_TRIGGER_DEBUG) { 00196 printf(" ID : %d\n",TxBuf[2]); 00197 } 00198 00199 // Length 00200 TxBuf[3] = 0x02; 00201 sum += TxBuf[3]; 00202 if (MX28_TRIGGER_DEBUG) { 00203 printf(" Length %d\n",TxBuf[3]); 00204 } 00205 00206 // Instruction - ACTION 00207 TxBuf[4] = 0x04; 00208 sum += TxBuf[4]; 00209 if (MX28_TRIGGER_DEBUG) { 00210 printf(" Instruction 0x%X\n",TxBuf[5]); 00211 } 00212 00213 // Checksum 00214 TxBuf[5] = 0xFF - sum; 00215 if (MX28_TRIGGER_DEBUG) { 00216 printf(" Checksum 0x%X\n",TxBuf[5]); 00217 } 00218 00219 // Transmit the packet in one burst with no pausing 00220 for (int i = 0; i < 6 ; i++) { 00221 _mx28.putc(TxBuf[i]); 00222 } 00223 00224 // This is a broadcast packet, so there will be no reply 00225 00226 return; 00227 } 00228 00229 00230 float MX28::GetPosition(void) { 00231 00232 if (MX28_DEBUG) { 00233 printf("\nGetPosition(%d)",_ID); 00234 } 00235 00236 char data[2]; 00237 00238 int ErrorCode = read(_ID, MX28_REG_POSITION, 2, data); 00239 short position = data[0] + (data[1] << 8); 00240 float angle = (position * 360) / 4095; 00241 00242 return (angle); 00243 } 00244 00245 00246 float MX28::GetTemp (void) { 00247 00248 if (MX28_DEBUG) { 00249 printf("\nGetTemp(%d)",_ID); 00250 } 00251 char data[1]; 00252 int ErrorCode = read(_ID, MX28_REG_TEMP, 1, data); 00253 float temp = data[0]; 00254 return(temp); 00255 } 00256 00257 00258 float MX28::GetVolts (void) { 00259 00260 if (MX28_DEBUG) { 00261 printf("\nGetVolts(%d)",_ID); 00262 } 00263 char data[1]; 00264 int ErrorCode = read(_ID, MX28_REG_VOLTS, 1, data); 00265 float volts = data[0]/10.0; 00266 return(volts); 00267 } 00268 00269 00270 float MX28::GetCurrent (void) { 00271 00272 if (MX28_DEBUG) { 00273 printf("\nGetCurrent(%d)",_ID); 00274 } 00275 char data[2]; 00276 int ErrorCode = read(_ID, MX28_REG_CURRENT, 2, data); 00277 // float current = ((data[0]+(data[1] << 8))-0x800)*0.0045; 00278 short nama = (data[0]+(data[1] << 8)); 00279 float current = nama*10; 00280 return(current); 00281 } 00282 00283 00284 int MX28::TorqueEnable (int mode) { 00285 00286 char data[1]; 00287 data[0] = mode; 00288 00289 return (write(_ID, MX28_REG_TORQUE_ENABLE, 1, data)); 00290 } 00291 00292 00293 int MX28::SetTorqueLimit (float torque_lim) { 00294 00295 short limit = torque_lim * 1023; 00296 char data[2]; 00297 data[0] = limit & 0xff; // bottom 8 bits 00298 data[1] = limit >> 8; // top 8 bits 00299 00300 return (write(_ID, MX28_REG_TORQUE_LIMIT, 2, data)); 00301 } 00302 00303 int MX28::read(int ID, int start, int bytes, char* data) { 00304 00305 char PacketLength = 0x4; 00306 char TxBuf[16]; 00307 char sum = 0; 00308 char Status[16]; 00309 00310 Status[4] = 0xFE; // return code 00311 00312 if (MX28_READ_DEBUG) { 00313 printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes); 00314 } 00315 00316 // Build the TxPacket first in RAM, then we'll send in one go 00317 if (MX28_READ_DEBUG) { 00318 printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); 00319 } 00320 00321 TxBuf[0] = 0xff; 00322 TxBuf[1] = 0xff; 00323 00324 // ID 00325 TxBuf[2] = ID; 00326 sum += TxBuf[2]; 00327 if (MX28_READ_DEBUG) { 00328 printf(" ID : %d\n",TxBuf[2]); 00329 } 00330 00331 // Packet Length 00332 TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes) 00333 sum += TxBuf[3]; // Accululate the packet sum 00334 if (MX28_READ_DEBUG) { 00335 printf(" Length : 0x%x\n",TxBuf[3]); 00336 } 00337 00338 // Instruction - Read 00339 TxBuf[4] = 0x2; 00340 sum += TxBuf[4]; 00341 if (MX28_READ_DEBUG) { 00342 printf(" Instruction : 0x%x\n",TxBuf[4]); 00343 } 00344 00345 // Start Address 00346 TxBuf[5] = start; 00347 sum += TxBuf[5]; 00348 if (MX28_READ_DEBUG) { 00349 printf(" Start Address : 0x%x\n",TxBuf[5]); 00350 } 00351 00352 // Bytes to read 00353 TxBuf[6] = bytes; 00354 sum += TxBuf[6]; 00355 if (MX28_READ_DEBUG) { 00356 printf(" No bytes : 0x%x\n",TxBuf[6]); 00357 } 00358 00359 // Checksum 00360 TxBuf[7] = 0xFF - sum; 00361 if (MX28_READ_DEBUG) { 00362 printf(" Checksum : 0x%x\n",TxBuf[7]); 00363 } 00364 00365 // Transmit the packet in one burst with no pausing 00366 for (int i = 0; i<8 ; i++) { 00367 _mx28.putc(TxBuf[i]); 00368 } 00369 00370 // Wait for the bytes to be transmitted 00371 wait (0.00002); 00372 00373 // Skip if the read was to the broadcast address 00374 if (_ID != 0xFE) { 00375 00376 // Receive the Status packet 6+ number of bytes read 00377 for (int i=0; i<(6+bytes) ; i++) { 00378 Status[i] = _mx28.getc(); 00379 } 00380 00381 // Copy the data from Status into data for return 00382 for (int i=0; i < Status[3]-2 ; i++) { 00383 data[i] = Status[5+i]; 00384 } 00385 00386 if (MX28_READ_DEBUG) { 00387 printf("\nStatus Packet\n"); 00388 printf(" Header : 0x%x\n",Status[0]); 00389 printf(" Header : 0x%x\n",Status[1]); 00390 printf(" ID : 0x%x\n",Status[2]); 00391 printf(" Length : 0x%x\n",Status[3]); 00392 printf(" Error Code : 0x%x\n",Status[4]); 00393 00394 for (int i=0; i < Status[3]-2 ; i++) { 00395 printf(" Data : 0x%x\n",Status[5+i]); 00396 } 00397 00398 printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]); 00399 } 00400 00401 } // if (ID!=0xFE) 00402 if(MX28_ERROR_DEBUG && Status[4]!=0) printf("\nError Code : 0x%x\n", Status[4]); 00403 00404 return(Status[4]); 00405 } 00406 00407 00408 int MX28:: write(int ID, int start, int bytes, char* data, int flag) { 00409 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum 00410 00411 char TxBuf[16]; 00412 char sum = 0; 00413 char Status[6]; 00414 00415 if (MX28_WRITE_DEBUG) { 00416 printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag); 00417 } 00418 00419 // Build the TxPacket first in RAM, then we'll send in one go 00420 if (MX28_WRITE_DEBUG) { 00421 printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); 00422 } 00423 00424 TxBuf[0] = 0xff; 00425 TxBuf[1] = 0xff; 00426 00427 // ID 00428 TxBuf[2] = ID; 00429 sum += TxBuf[2]; 00430 00431 if (MX28_WRITE_DEBUG) { 00432 printf(" ID : %d\n",TxBuf[2]); 00433 } 00434 00435 // packet Length 00436 TxBuf[3] = 3+bytes; 00437 sum += TxBuf[3]; 00438 00439 if (MX28_WRITE_DEBUG) { 00440 printf(" Length : %d\n",TxBuf[3]); 00441 } 00442 00443 // Instruction 00444 if (flag == 1) { 00445 TxBuf[4]=0x04; 00446 sum += TxBuf[4]; 00447 } else { 00448 TxBuf[4]=0x03; 00449 sum += TxBuf[4]; 00450 } 00451 00452 if (MX28_WRITE_DEBUG) { 00453 printf(" Instruction : 0x%x\n",TxBuf[4]); 00454 } 00455 00456 // Start Address 00457 TxBuf[5] = start; 00458 sum += TxBuf[5]; 00459 if (MX28_WRITE_DEBUG) { 00460 printf(" Start : 0x%x\n",TxBuf[5]); 00461 } 00462 00463 // data 00464 for (char i=0; i<bytes ; i++) { 00465 TxBuf[6+i] = data[i]; 00466 sum += TxBuf[6+i]; 00467 if (MX28_WRITE_DEBUG) { 00468 printf(" Data : 0x%x\n",TxBuf[6+i]); 00469 } 00470 } 00471 00472 // checksum 00473 TxBuf[6+bytes] = 0xFF - sum; 00474 if (MX28_WRITE_DEBUG) { 00475 printf(" Checksum : 0x%x\n",TxBuf[6+bytes]); 00476 } 00477 00478 // Transmit the packet in one burst with no pausing 00479 for (int i = 0; i < (7 + bytes) ; i++) { 00480 _mx28.putc(TxBuf[i]); 00481 } 00482 00483 // Wait for data to transmit 00484 wait (0.00002); 00485 00486 // make sure we have a valid return 00487 Status[4]=0x00; 00488 00489 // we'll only get a reply if it was not broadcast 00490 if (_ID!=0xFE) { 00491 00492 // response is always 6 bytes 00493 // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum 00494 for (int i=0; i < 6 ; i++) { 00495 Status[i] = _mx28.getc(); 00496 } 00497 00498 // Build the TxPacket first in RAM, then we'll send in one go 00499 if (MX28_WRITE_DEBUG) { 00500 printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]); 00501 printf(" ID : %d\n",Status[2]); 00502 printf(" Length : %d\n",Status[3]); 00503 printf(" Error : 0x%x\n",Status[4]); 00504 printf(" Checksum : 0x%x\n",Status[5]); 00505 } 00506 00507 00508 } 00509 if(MX28_ERROR_DEBUG && Status[4]!=0) printf("\nError Code : 0x%x\n", Status[4]); 00510 00511 return(Status[4]); // return error code 00512 }
Generated on Tue Jul 19 2022 09:17:42 by 1.7.2