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);
00010 packet.setParam(0, startingAddress);
00011 packet.setParam(1, dataLength);
00012 packet.setChecksum();
00013
00014
00015 for (int i=0 ; i<=PACKET_RETRY_FACTOR ; i++)
00016 {
00017 sendErr = sendPacket(&packet);
00018 if (sendErr == DXL_SUCCESS)
00019 {
00020 receiveErr = receivePacketWait(&statusPacket);
00021 if (receiveErr == DXL_SUCCESS)
00022 {
00023 i = PACKET_RETRY_FACTOR;
00024 }
00025 else {
00026
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);
00052 return statusPacket.getError();
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);
00067 if (error == DXL_SUCCESS){
00068 error = statusPacket.getError();
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 {
00100
00101 CDxlPacket packet(mID, INST_WRITE, dataLength+1);
00102
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);
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;
00136 else
00137 continue;
00138
00139 error = statusPacket.getError();
00140 if (error != DXL_CHECKSUM_ERROR)
00141 {
00142 i=PACKET_RETRY_FACTOR;
00143 }
00144
00145 }
00146 break;
00147 }
00148 }
00149
00150
00151
00152 return error;
00153 }
00154 else
00155 {
00156
00157 if (mpGroup != NULL)
00158 return mpGroup->writeData(mID, startingAddress, dataLength, data);
00159 else
00160 return DXL_PKT_SEND_ERROR;
00161 }
00162 }