7 #include "RbMovableItem.h"
10 #include "RbMotorizedJoint.h"
11 #include "RbHingeLimit.h"
13 #include "RbRigidBody.h"
15 #include "RbDynamixelUSB.h"
16 #include "RbDynamixelUSBServo.h"
22 namespace RobotIOControls
24 namespace DynamixelUSB
31 RbDynamixelUSB::RbDynamixelUSB()
37 RbDynamixelUSB::~RbDynamixelUSB()
43 {
Std_TraceMsg(0,
"Caught Error in desctructor of RbDynamixelUSB\r\n",
"", -1,
false,
true);}
46 void RbDynamixelUSB::PortNumber(
int iPort)
49 m_iPortNumber = iPort;
52 int RbDynamixelUSB::PortNumber() {
return m_iPortNumber;}
54 void RbDynamixelUSB::BaudRate(
int iRate)
56 if( !((iRate == 1) || (iRate == 3) || (iRate == 4) || (iRate == 7) || (iRate == 9) ||
57 (iRate == 9) || (iRate == 16) || (iRate == 34) || (iRate == 103) || (iRate == 207)) )
58 THROW_PARAM_ERROR(Rb_Err_lInvalidBaudRate, Rb_Err_strInvalidBaudRate,
"Baud rate", iRate);
62 int RbDynamixelUSB::BaudRate() {
return m_iBaudRate;}
64 void RbDynamixelUSB::SetRegister(
unsigned char iServo,
unsigned char reg,
unsigned char length,
unsigned int value)
67 dxl_write_word(iServo, reg, value);
69 dxl_write_byte(iServo, reg, value);
72 int RbDynamixelUSB::GetRegister(
unsigned char iServo,
unsigned char reg,
unsigned char length)
75 return dxl_read_word(iServo, reg);
77 return dxl_read_byte(iServo, reg);
80 #pragma region DataAccesMethods
86 return RobotIOControl::GetDataPointer(strDataType);
93 if(RobotIOControl::SetData(strDataType, strValue,
false))
96 if(strType ==
"PORTNUMBER")
98 PortNumber((
int) atoi(strValue.c_str()));
101 else if(strType ==
"BAUDRATE")
103 BaudRate((
int) atoi(strValue.c_str()));
109 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
114 void RbDynamixelUSB::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
116 RobotIOControl::QueryProperties(aryProperties);
118 aryProperties.Add(
new TypeProperty(
"ComPort", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
119 aryProperties.Add(
new TypeProperty(
"BaudRate", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
124 bool RbDynamixelUSB::OpenIO()
126 if(!dxl_initialize(m_iPortNumber, m_iBaudRate))
127 THROW_PARAM_ERROR(Rb_Err_lFailedDynamixelConnection, Rb_Err_strFailedDynamixelConnection,
"Port", m_iPortNumber);
132 void RbDynamixelUSB::CloseIO()
134 TRACE_DEBUG(
"CloseIO.");
140 void RbDynamixelUSB::ProcessIO()
156 boost::this_thread::sleep(boost::posix_time::microseconds(1000));
162 m_aryMotorData.RemoveAll();
164 SendSynchronousMoveCommand();
173 catch(CStdErrorInfo oError)
185 bool RbDynamixelUSB::SendSynchronousMoveCommand()
187 int iServos = m_aryMotorData.GetSize();
188 int iDataPerServo = 4;
192 dxl_set_txpacket_id(BROADCAST_ID);
193 dxl_set_txpacket_instruction(INST_SYNC_WRITE);
194 dxl_set_txpacket_parameter(0, P_GOAL_POSITION_L);
195 dxl_set_txpacket_parameter(1, iDataPerServo);
197 for(
int iServo=0; iServo<iServos; iServo++ )
201 int iOffset = (2+(iDataPerServo+1)*iServo);
202 dxl_set_txpacket_parameter((iOffset+0), lpServo->m_iID);
203 dxl_set_txpacket_parameter((iOffset+1), dxl_get_lowbyte(lpServo->m_iGoalPos));
204 dxl_set_txpacket_parameter((iOffset+2), dxl_get_highbyte(lpServo->m_iGoalPos));
205 dxl_set_txpacket_parameter((iOffset+3), dxl_get_lowbyte(lpServo->m_iGoalVelocity));
206 dxl_set_txpacket_parameter((iOffset+4), dxl_get_highbyte(lpServo->m_iGoalVelocity));
209 dxl_set_txpacket_length((iDataPerServo+1)*iServos+4);
212 int CommStatus = dxl_get_result();
213 if( CommStatus == COMM_RXSUCCESS )
217 std::cout << GetCommStatus(CommStatus);
234 if(dxl_get_rxpacket_error(ERRBIT_VOLTAGE) == 1)
235 return "Input voltage error!";
237 if(dxl_get_rxpacket_error(ERRBIT_ANGLE) == 1)
238 return "Angle limit error!\n";
240 if(dxl_get_rxpacket_error(ERRBIT_OVERHEAT) == 1)
241 return "Overheat error!\n";
243 if(dxl_get_rxpacket_error(ERRBIT_RANGE) == 1)
244 return "Out of range error!\n";
246 if(dxl_get_rxpacket_error(ERRBIT_CHECKSUM) == 1)
247 return "Checksum error!\n";
249 if(dxl_get_rxpacket_error(ERRBIT_OVERLOAD) == 1)
250 return "Overload error!\n";
252 if(dxl_get_rxpacket_error(ERRBIT_INSTRUCTION) == 1)
253 return "Instruction code error!\n";
255 return "Unknown error";
258 std::string RbDynamixelUSB::GetCommStatus(
int CommStatus)
263 return "COMM_TXFAIL: Failed transmit instruction packet!\r\n";
267 return "COMM_TXERROR: Incorrect instruction packet!\r\n";
271 return "COMM_RXFAIL: Failed get status packet from device!\r\n";
275 return "COMM_RXWAITING: Now recieving status packet!\r\n";
279 return "COMM_RXTIMEOUT: There is no status packet!\r\n";
283 return "COMM_RXCORRUPT: Incorrect status packet!\r\n";
287 return "This is unknown error code!\r\n";
294 RobotIOControl::Load(oXml);
297 PortNumber(oXml.
GetChildInt(
"PortNumber", m_iPortNumber));
298 BaudRate(oXml.
GetChildInt(
"BaudRate", m_iBaudRate));
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual bool InSimulation()
Used to determine if we are running in a simulation, or in a real control mode.
virtual std::string GetErrorCode()
Checks the error code and returns an associated error message.
Simulator * m_lpSim
The pointer to a Simulation.
virtual bool Paused()
Gets whether the Simulation is paused.
virtual bool IntoElem()
Goes into the next element where the cursor is located.
boost::interprocess::interprocess_condition m_WaitForIOSetupCond
Condition used to determine when the IO is setup.
bool m_bStopIO
Flags the thread processing loop to exit.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
virtual int GetChildInt(std::string strElementName)
Gets an integer value from the element with the specified name.
virtual void StepIO()
This method is called from within the IO thread. It calls StepIO for each part.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
Declares the vortex structure class.
Declares the vortex hinge class.
A standard xml manipulation class.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
bool m_bIOPaused
Is set to true once the IO loop is paused.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
bool m_bPauseIO
Set to true to pause the IO processing. Set back to false to resume it.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
virtual void SetupIO()
This method is called after all connections to whatever control board have been made. It calls each parts SetupIO method. For example, We connect to a Firmata microcontroller like an Arduino, and then do a setup that could take some time. We should not attempt to setup any of the pins until after the board itself has been setup. After that we need to loop through and setup all the parts.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
bool m_bIOThreadProcessing
True while the io thread processing loop is going on.
bool m_bSetupComplete
Set to true once the IO is setup correctly.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.