CDxlGeneric.cpp
Go to the documentation of this file.
00001 #include <threemxl/platform/hardware/dynamixel/CDxlGeneric.h>
00002 
00003 int CDxlGeneric::readData(BYTE startingAddress, BYTE dataLength, BYTE *data)
00004 {
00005         int sendErr = 0 ;
00006         int receiveErr = 0 ;
00007         CDxlStatusPacket statusPacket(dataLength);
00008 
00009         CDxlPacket packet(mID, INST_READ, 2);                                                                                           // create request package
00010         packet.setParam(0, startingAddress);
00011         packet.setParam(1, dataLength);
00012         packet.setChecksum();
00013 
00014         //do the serial communication
00015         for (int i=0 ; i<=PACKET_RETRY_FACTOR ; i++)
00016         {
00017                 sendErr = sendPacket(&packet);                                                                                                  // send data request
00018                 if (sendErr == DXL_SUCCESS)
00019                 {
00020                         receiveErr = receivePacketWait(&statusPacket);                                                                  // receive data
00021                         if (receiveErr == DXL_SUCCESS)
00022                         {
00023                                 i = PACKET_RETRY_FACTOR; //success
00024                         }
00025                         else {
00026                         //      mLogCrawlLn("receivePacketWait error for ID "<< mID);
00027                                 mLogWarningLn("receivePacketWait error for ID "<< mID);
00028                                 if (i == PACKET_RETRY_FACTOR){ return receiveErr;}
00029                                 else
00030                                 {
00031                                         mLogCrawlLn("retrying to read data from servo with ID "<< mID << ", Try " << i+1 );
00032                                 }
00033                         }
00034                 }
00035                 else
00036                 {
00037                         mLogCrawlLn("sendPacket error for ID "<< mID);
00038                         if (i == PACKET_RETRY_FACTOR)
00039                         {
00040                                 return sendErr;
00041                         }
00042                         else
00043                         {
00044                                 mLogCrawlLn("retrying to read data from servo with ID "<< mID << ", Try " << i+1 );
00045                         }
00046                 }
00047         }
00048 
00049         mLogCrawlLn("Received status:" << statusPacket.getPktString());
00050 
00051         statusPacket.getParams(0, dataLength, data);                                                                            // process received data
00052         return statusPacket.getError();                                                                                         // get error out of received data
00053 
00054 }
00055 
00056 int CDxlGeneric::ping()
00057 {
00058         mLogInfoLn("sending ping to servo with ID:"<< mID);
00059         CDxlPacket packet(mID, INST_PING, 0);
00060         packet.setChecksum();
00061         int error = sendPacket(&packet);
00062         if (error != DXL_SUCCESS)
00063                 return error;
00064 
00065         CDxlStatusPacket statusPacket(0);
00066         error = receivePacketWait(&statusPacket);// , 0, (int)100E3);   // 100 ms
00067         if (error == DXL_SUCCESS){
00068                 error = statusPacket.getError(); // Perhaps the dynamixel itself has an error
00069                 mLogCrawlLn("servo with ID:"<< mID << " returned "<< (int)error << " on ping");
00070         }
00071         else
00072                 mLogDebugLn("ping failed for ID " << mID);
00073 
00074         return error;
00075 }
00076 
00077 int CDxlGeneric::action()
00078 {
00079         mLogCrawlLn("action() called for servo with ID:"<< mID);
00080         CDxlPacket packet(mID, INST_ACTION, 0);
00081         packet.setChecksum();
00082         return sendPacket(&packet, false);
00083 }
00084 
00085 int CDxlGeneric::reset()
00086 {
00087         mLogCrawlLn("reset() called for servo with ID:"<< mID);
00088         CDxlPacket packet(mID, INST_RESET, 0);
00089         packet.setChecksum();
00090         return sendPacket(&packet, false);
00091 }
00092 
00096 int CDxlGeneric::writeData(BYTE startingAddress, BYTE dataLength, BYTE *data, bool shouldSyncWrite)
00097 {
00098            if (!shouldSyncWrite)
00099                 {       // Direct operation on serial port
00100 
00101                         CDxlPacket packet(mID, INST_WRITE, dataLength+1);
00102                         // Two parameters
00103                         packet.setParam(0, startingAddress);
00104                         packet.setParams(1, dataLength, data);
00105                         packet.setChecksum();
00106 
00107                         int error = DXL_SUCCESS;
00108 
00109                         for (int i=0 ; i<=PACKET_RETRY_FACTOR ; i++)
00110                         {
00111                                 int sendErr = sendPacket(&packet, mRetlevel==2);                                                                                                                // send data request
00112                                 if (sendErr != DXL_SUCCESS)
00113                                         if (i == PACKET_RETRY_FACTOR)
00114                                                 return sendErr;
00115                                         else
00116                                                 continue;
00117 
00118                                 switch (mRetlevel)
00119                                 {
00120                                         case 0:
00121                                                 i=PACKET_RETRY_FACTOR;
00122                                                 mLogCrawlLn("not waiting for return packet because mReturnlevel == 0");
00123                                                 break;
00124                                         case 1:
00125                                                 i=PACKET_RETRY_FACTOR;
00126                                                 mLogCrawlLn("not waiting for return packet because mReturnlevel == 1");
00127                                                 break;
00128                                         case 2:
00129                                                 {
00130                                                         mLogCrawlLn("waiting for status return packet, try "<< i);
00131                                                         CDxlStatusPacket statusPacket(0);
00132                                                         int receiveErr = receivePacketWait(&statusPacket);
00133                                                         if (receiveErr != DXL_SUCCESS)
00134                                                                 if (i == PACKET_RETRY_FACTOR)
00135                                                                         return receiveErr; //we return the error from the port driver if we could not receive correctly
00136                                                                 else
00137                                                                         continue;
00138 
00139                                                         error = statusPacket.getError();                                                // get status error out of received data
00140                                                         if (error != DXL_CHECKSUM_ERROR)
00141                                                         {
00142                                                                 i=PACKET_RETRY_FACTOR;                                                          //only retry on checksum errors
00143                                                         }
00144 
00145                                                 }
00146                                                 break;
00147                                 }
00148                         }
00149 
00150 
00151                         // We're done. Return status error!
00152                         return error;
00153                 }
00154            else
00155            {
00156                // do group write (syncwritepacket)
00157                    if (mpGroup != NULL)
00158                            return mpGroup->writeData(mID, startingAddress, dataLength, data);
00159                    else
00160                            return DXL_PKT_SEND_ERROR;
00161            }
00162 }


threemxl
Author(s):
autogenerated on Fri Aug 28 2015 13:21:08