Dyanamixel

Dependents:   YOZAKURA_ARM YOZAKURA_ARM_USB YOZAKURA_ARM_USB_Keyboard YOZAKURA_ARM_Keyboard0424 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AX12.cpp Source File

AX12.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 "AX12.h"
00025 #include "mbed.h"
00026 //#include "SerialHalfDuplex.h"
00027 
00028 AX12::AX12(PinName tx, PinName rx, int ID, int baudrate)
00029         : _ax12(tx,rx) {
00030 
00031     _ax12.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 AX12::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(300);
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 AX12::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     // 1023 / 300 * degrees
00067     short goal = (1023 * degrees) / 300;
00068     if (AX12_DEBUG) {
00069         printf("SetGoal to 0x%x\n",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, AX12_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 AX12::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 (AX12_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 AX12::SetCWLimit (int degrees) {
00115 
00116     char data[2];
00117     
00118     // 1023 / 300 * degrees
00119     short limit = (1023 * degrees) / 300;
00120 
00121     if (AX12_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, AX12_REG_CW_LIMIT, 2, data));
00130 
00131 }
00132 
00133 
00134 int AX12::SetCCWLimit (int degrees) {
00135 
00136     char data[2];
00137 
00138     // 1023 / 300 * degrees
00139     short limit = (1023 * degrees) / 300;
00140 
00141     if (AX12_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, AX12_REG_CCW_LIMIT, 2, data));
00150 }
00151 
00152 
00153 int AX12::SetID (int CurrentID, int NewID) {
00154 
00155     char data[1];
00156     data[0] = NewID;
00157     if (AX12_DEBUG) {
00158         printf("Setting ID from 0x%x to 0x%x\n",CurrentID,NewID);
00159     }
00160     return (write(CurrentID, AX12_REG_ID, 1, data));
00161 
00162 }
00163 
00164 
00165 // return 1 is the servo is still in flight
00166 int AX12::isMoving(void) {
00167 
00168     char data[1];
00169     read(_ID,AX12_REG_MOVING,1,data);
00170     return(data[0]);
00171 }
00172 
00173 
00174 void AX12::trigger(void) {
00175 
00176     char TxBuf[16];
00177     char sum = 0;
00178 
00179     if (AX12_TRIGGER_DEBUG) {
00180         printf("\nTriggered\n");
00181     }
00182 
00183     // Build the TxPacket first in RAM, then we'll send in one go
00184     if (AX12_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 (AX12_TRIGGER_DEBUG) {
00196         printf("  ID : %d\n",TxBuf[2]);
00197     }
00198 
00199     // Length
00200     TxBuf[3] = 0x02;
00201     sum += TxBuf[3];
00202     if (AX12_TRIGGER_DEBUG) {
00203         printf("  Length %d\n",TxBuf[3]);
00204     }
00205 
00206     // Instruction - ACTION
00207     TxBuf[4] = 0x04;
00208     sum += TxBuf[4];
00209     if (AX12_TRIGGER_DEBUG) {
00210         printf("  Instruction 0x%X\n",TxBuf[5]);
00211     }
00212 
00213     // Checksum
00214     TxBuf[5] = 0xFF - sum;
00215     if (AX12_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         _ax12.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 AX12::GetPosition(void) {
00231 
00232     if (AX12_DEBUG) {
00233         printf("\nGetPosition(%d)",_ID);
00234     }
00235 
00236     char data[2];
00237 
00238     int ErrorCode = read(_ID, AX12_REG_POSITION, 2, data);
00239     short position = data[0] + (data[1] << 8);
00240     float angle = (position * 300)/1024;
00241     
00242     return (angle);
00243 }
00244 
00245 
00246 float AX12::GetTemp (void) {
00247 
00248     if (AX12_DEBUG) {
00249         printf("\nGetTemp(%d)",_ID);
00250     }
00251     char data[1];
00252     int ErrorCode = read(_ID, AX12_REG_TEMP, 1, data);
00253     float temp = data[0];
00254     return(temp);
00255 }
00256 
00257 
00258 float AX12::GetVolts (void) {
00259     if (AX12_DEBUG) {
00260         printf("\nGetVolts(%d)",_ID);
00261     }
00262     char data[1];
00263     int ErrorCode = read(_ID, AX12_REG_VOLTS, 1, data);
00264     float volts = data[0]/10.0;
00265     return(volts);
00266 }
00267 
00268 
00269 int AX12::TorqueEnable (int mode) {
00270 
00271     char data[1];
00272     data[0] = mode;
00273 
00274     return (write(_ID, AX12_REG_TORQUE_ENABLE, 1, data));
00275 }
00276 
00277 
00278 int AX12::SetTorqueLimit (float torque_lim) {
00279     
00280     short limit = torque_lim * 1023;
00281     char data[2];
00282     data[0] = limit & 0xff; // bottom 8 bits
00283     data[1] = limit >> 8;   // top 8 bits
00284 
00285     return (write(_ID, AX12_REG_TORQUE_LIMIT, 2, data));
00286 }
00287 
00288 
00289 int AX12::read(int ID, int start, int bytes, char* data) {
00290 
00291     char PacketLength = 0x4;
00292     char TxBuf[16];
00293     char sum = 0;
00294     char Status[16];
00295 
00296     Status[4] = 0xFE; // return code
00297 
00298     if (AX12_READ_DEBUG) {
00299         printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes);
00300     }
00301 
00302     // Build the TxPacket first in RAM, then we'll send in one go
00303     if (AX12_READ_DEBUG) {
00304         printf("\nInstruction Packet\n  Header : 0xFF, 0xFF\n");
00305     }
00306 
00307     TxBuf[0] = 0xff;
00308     TxBuf[1] = 0xff;
00309 
00310     // ID
00311     TxBuf[2] = ID;
00312     sum += TxBuf[2];
00313     if (AX12_READ_DEBUG) {
00314         printf("  ID : %d\n",TxBuf[2]);
00315     }
00316 
00317     // Packet Length
00318     TxBuf[3] = PacketLength;    // Length = 4 ; 2 + 1 (start) = 1 (bytes)
00319     sum += TxBuf[3];            // Accululate the packet sum
00320     if (AX12_READ_DEBUG) {
00321         printf("  Length : 0x%x\n",TxBuf[3]);
00322     }
00323 
00324     // Instruction - Read
00325     TxBuf[4] = 0x2;
00326     sum += TxBuf[4];
00327     if (AX12_READ_DEBUG) {
00328         printf("  Instruction : 0x%x\n",TxBuf[4]);
00329     }
00330 
00331     // Start Address
00332     TxBuf[5] = start;
00333     sum += TxBuf[5];
00334     if (AX12_READ_DEBUG) {
00335         printf("  Start Address : 0x%x\n",TxBuf[5]);
00336     }
00337 
00338     // Bytes to read
00339     TxBuf[6] = bytes;
00340     sum += TxBuf[6];
00341     if (AX12_READ_DEBUG) {
00342         printf("  No bytes : 0x%x\n",TxBuf[6]);
00343     }
00344 
00345     // Checksum
00346     TxBuf[7] = 0xFF - sum;
00347     if (AX12_READ_DEBUG) {
00348         printf("  Checksum : 0x%x\n",TxBuf[7]);
00349     }
00350 
00351     // Transmit the packet in one burst with no pausing
00352     for (int i = 0; i<8 ; i++) {
00353         _ax12.putc(TxBuf[i]);
00354     }
00355 
00356     // Wait for the bytes to be transmitted
00357     wait (0.00002);
00358 
00359     // Skip if the read was to the broadcast address
00360     if (_ID != 0xFE) {
00361 
00362         // Receive the Status packet 6+ number of bytes read
00363         for (int i=0; i<(6+bytes) ; i++) {
00364             Status[i] = _ax12.getc();
00365         }
00366 
00367         // Copy the data from Status into data for return
00368         for (int i=0; i < Status[3]-2 ; i++) {
00369             data[i] = Status[5+i];
00370         }
00371 
00372         if (AX12_READ_DEBUG) {
00373             printf("\nStatus Packet\n");
00374             printf("  Header : 0x%x\n",Status[0]);
00375             printf("  Header : 0x%x\n",Status[1]);
00376             printf("  ID : 0x%x\n",Status[2]);
00377             printf("  Length : 0x%x\n",Status[3]);
00378             printf("  Error Code : 0x%x\n",Status[4]);
00379 
00380             for (int i=0; i < Status[3]-2 ; i++) {
00381                 printf("  Data : 0x%x\n",Status[5+i]);
00382             }
00383 
00384             printf("  Checksum : 0x%x\n",Status[5+(Status[3]-2)]);
00385         }
00386 
00387     } // if (ID!=0xFE)
00388     
00389     if(AX12_ERROR_DEBUG && Status[4]!=0) printf("\nError Code : 0x%x\n", Status[4]);
00390 
00391     return(Status[4]);
00392 }
00393 
00394 
00395 int AX12:: write(int ID, int start, int bytes, char* data, int flag) {
00396 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum
00397 
00398     char TxBuf[16];
00399     char sum = 0;
00400     char Status[6];
00401 
00402     if (AX12_WRITE_DEBUG) {
00403         printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag);
00404     }
00405 
00406     // Build the TxPacket first in RAM, then we'll send in one go
00407     if (AX12_WRITE_DEBUG) {
00408         printf("\nInstruction Packet\n  Header : 0xFF, 0xFF\n");
00409     }
00410 
00411     TxBuf[0] = 0xff;
00412     TxBuf[1] = 0xff;
00413 
00414     // ID
00415     TxBuf[2] = ID;
00416     sum += TxBuf[2];
00417 
00418     if (AX12_WRITE_DEBUG) {
00419         printf("  ID : %d\n",TxBuf[2]);
00420     }
00421 
00422     // packet Length
00423     TxBuf[3] = 3+bytes;
00424     sum += TxBuf[3];
00425 
00426     if (AX12_WRITE_DEBUG) {
00427         printf("  Length : %d\n",TxBuf[3]);
00428     }
00429 
00430     // Instruction
00431     if (flag == 1) {
00432         TxBuf[4]=0x04;
00433         sum += TxBuf[4];
00434     } else {
00435         TxBuf[4]=0x03;
00436         sum += TxBuf[4];
00437     }
00438 
00439     if (AX12_WRITE_DEBUG) {
00440         printf("  Instruction : 0x%x\n",TxBuf[4]);
00441     }
00442 
00443     // Start Address
00444     TxBuf[5] = start;
00445     sum += TxBuf[5];
00446     if (AX12_WRITE_DEBUG) {
00447         printf("  Start : 0x%x\n",TxBuf[5]);
00448     }
00449 
00450     // data
00451     for (char i=0; i<bytes ; i++) {
00452         TxBuf[6+i] = data[i];
00453         sum += TxBuf[6+i];
00454         if (AX12_WRITE_DEBUG) {
00455             printf("  Data : 0x%x\n",TxBuf[6+i]);
00456         }
00457     }
00458 
00459     // checksum
00460     TxBuf[6+bytes] = 0xFF - sum;
00461     if (AX12_WRITE_DEBUG) {
00462         printf("  Checksum : 0x%x\n",TxBuf[6+bytes]);
00463     }
00464 
00465     // Transmit the packet in one burst with no pausing
00466     for (int i = 0; i < (7 + bytes) ; i++) {
00467         _ax12.putc(TxBuf[i]);
00468     }
00469 
00470     // Wait for data to transmit
00471     wait (0.00002);
00472 
00473     // make sure we have a valid return
00474     Status[4]=0x00;
00475 
00476     // we'll only get a reply if it was not broadcast
00477     if (_ID!=0xFE) {
00478 
00479         // response is always 6 bytes
00480         // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
00481         for (int i=0; i < 6 ; i++) {
00482             Status[i] = _ax12.getc();
00483         }
00484 
00485         // Build the TxPacket first in RAM, then we'll send in one go
00486         if (AX12_WRITE_DEBUG) {
00487             printf("\nStatus Packet\n  Header : 0x%X, 0x%X\n",Status[0],Status[1]);
00488             printf("  ID : %d\n",Status[2]);
00489             printf("  Length : %d\n",Status[3]);
00490             printf("  Error : 0x%x\n",Status[4]);
00491             printf("  Checksum : 0x%x\n",Status[5]);
00492         }
00493 
00494     }
00495 
00496     if(AX12_ERROR_DEBUG && Status[4]!=0) printf("\nError Code : 0x%x\n", Status[4]);
00497 
00498     return(Status[4]); // return error code
00499 }