Team DIANA / Mbed 2 deprecated AX12

Dependencies:   mbed

Fork of AX12 by Chris Styles

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