7 #include "RbMovableItem.h"
10 #include "RbMotorizedJoint.h"
11 #include "RbHingeLimit.h"
13 #include "RbRigidBody.h"
15 #include "RbAnimatSerial.h"
17 #define PACKET_INFO_SIZE 6 //Size of the header, packet size, message id, and checksum in bytes
18 #define START_MESSAGE_INFO_BYTE 5
33 namespace RobotIOControls
40 RbAnimatSerial::RbAnimatSerial()
51 for(
int iIndex=0; iIndex<MAX_ANIMAT_BUFFER; iIndex++)
55 RbAnimatSerial::~RbAnimatSerial()
61 {
Std_TraceMsg(0,
"Caught Error in desctructor of RbAnimatSerial\r\n",
"", -1,
false,
true);}
64 void RbAnimatSerial::Port(std::string strPort)
69 std::string RbAnimatSerial::Port() {
return m_strPort;}
71 void RbAnimatSerial::BaudRate(
int iRate)
77 int RbAnimatSerial::BaudRate() {
return m_iBaudRate;}
79 #pragma region DataAccesMethods
94 else if(strType ==
"BAUDRATE")
96 BaudRate((
int) atoi(strValue.c_str()));
102 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
107 void RbAnimatSerial::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
111 aryProperties.Add(
new TypeProperty(
"Port", AnimatPropertyType::String, AnimatPropertyDirection::Set));
112 aryProperties.Add(
new TypeProperty(
"BaudRate", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
134 for(
int iIndex=0; iIndex<iCount; iIndex++)
143 bool RbAnimatSerial::OpenIO()
148 THROW_PARAM_ERROR(Rb_Err_lFailedUartSBeeConnection, Rb_Err_strFailedUartSBeeConnection,
"ComPort",
m_strPort);
153 void RbAnimatSerial::CloseIO()
161 if(!m_Port.isInitialized())
168 void RbAnimatSerial::ResetData()
170 RemoteControl::ResetData();
178 void RbAnimatSerial::WaitForThreadNotifyReady()
180 RobotIOControl::WaitForThreadNotifyReady();
183 boost::this_thread::sleep(boost::posix_time::microseconds(10000));
186 void RbAnimatSerial::ReadData()
190 while(m_Port.available() > 0 && !bFound)
196 if(m_Port.readByte() == 0xff)
199 m_checksum = (int) m_vals[0];
206 else if(m_index == 1)
208 m_vals[m_index] = (
unsigned char) m_Port.readByte();
209 if(m_vals[m_index] == 0xff)
211 m_checksum += (int) m_vals[m_index];
220 m_vals[m_index] = (
unsigned char) m_Port.readByte();
221 m_iMessageID = m_vals[m_index];
223 m_checksum += (int) m_vals[m_index];
229 m_vals[m_index] = (
unsigned char) m_Port.readByte();
230 m_iPacketSize = m_vals[m_index];
232 m_checksum += (int) m_vals[m_index];
238 m_vals[m_index] = (
unsigned char) m_Port.readByte();
239 m_iPacketSize += (m_vals[m_index] << 8);
242 if(m_iPacketSize > 128)
246 m_checksum += (int) m_vals[m_index];
250 else if(m_index > 4 && m_index <= m_iPacketSize)
252 m_vals[m_index] = (
unsigned char) m_Port.readByte();
254 if(m_index == (m_iPacketSize-1))
257 int iChecksum = m_vals[m_index];
259 if(m_checksum%256 != iChecksum)
261 OutputDebugString(
"Checksum error. Writing Resend message.");
269 if(m_iMessageID == 2)
271 else if(m_iMessageID == 1)
273 int iStop = m_iPacketSize - 1;
275 for(
int iIdx=START_MESSAGE_INFO_BYTE; iIdx<iStop; iIdx+=DATA_SIZE)
277 m_id.bval[0] = m_vals[iIdx];
278 m_id.bval[1] = m_vals[iIdx+1];
279 m_value.bval[0] = m_vals[iIdx+2];
280 m_value.bval[1] = m_vals[iIdx+3];
281 m_value.bval[2] = m_vals[iIdx+4];
282 m_value.bval[3] = m_vals[iIdx+5];
284 SetDataValue(m_id.ival, m_value.fval);
295 m_checksum += (int) m_vals[m_index];
299 if(m_index >= MAX_ANIMAT_BUFFER)
307 void RbAnimatSerial::WriteData()
309 CStdArray<RemoteControlLinkage *> aryWrites;
311 if(FindDataToWrite(aryWrites))
312 WriteData(aryWrites);
315 void RbAnimatSerial::WriteData(CStdArray<RemoteControlLinkage *> &aryWrites)
317 int iCount = aryWrites.GetSize();
318 int checksum = 0xFF + 0xFF + 0x01;
321 m_Port.writeByte((
unsigned char) 0xFF);
322 m_Port.writeByte((
unsigned char) 0xFF);
323 m_Port.writeByte((
unsigned char) 0x01);
325 m_size.ival = HEADER_SIZE + (DATA_SIZE * iCount) + FOOTER_SIZE;
326 m_Port.writeByte((
unsigned char) m_size.bval[0]);
327 checksum += m_size.bval[0];
328 m_Port.writeByte((
unsigned char) m_size.bval[1]);
329 checksum += m_size.bval[1];
331 for(
int i=0; i<iCount; i++)
333 m_id.ival = aryWrites[i]->m_Data.m_iButtonID;
334 m_value.fval = aryWrites[i]->m_Data.m_fltValue;
335 aryWrites[i]->AppliedValue(aryWrites[i]->m_Data.m_fltValue);
337 m_Port.writeByte((
unsigned char) m_id.bval[0]);
338 m_Port.writeByte((
unsigned char) m_id.bval[1]);
339 m_Port.writeByte((
unsigned char) m_value.bval[0]);
340 m_Port.writeByte((
unsigned char) m_value.bval[1]);
341 m_Port.writeByte((
unsigned char) m_value.bval[2]);
342 m_Port.writeByte((
unsigned char) m_value.bval[3]);
348 checksum += m_id.bval[0];
349 checksum += m_id.bval[1];
350 checksum += m_value.bval[0];
351 checksum += m_value.bval[1];
352 checksum += m_value.bval[2];
353 checksum += m_value.bval[3];
356 unsigned char bchecksum = (
unsigned char) (checksum%256);
357 m_Port.writeByte(bchecksum);
364 void RbAnimatSerial::WriteAllData()
366 CStdArray<RemoteControlLinkage *> aryWrites;
369 for(
int i=0; i<iCount; i++)
372 WriteData(aryWrites);
375 void RbAnimatSerial::WriteResendData()
377 int checksum = 0xFF + 0xFF + 0x02;
380 m_Port.writeByte((
unsigned char) 0xFF);
381 m_Port.writeByte((
unsigned char) 0xFF);
382 m_Port.writeByte((
unsigned char) 0x02);
384 m_size.ival = HEADER_SIZE + FOOTER_SIZE;
385 m_Port.writeByte((
unsigned char) m_size.bval[0]);
386 checksum += m_size.bval[0];
387 m_Port.writeByte((
unsigned char) m_size.bval[1]);
388 checksum += m_size.bval[1];
390 unsigned char bchecksum = (
unsigned char) (checksum%256);
391 m_Port.writeByte(bchecksum);
404 CheckStartedStopped();
411 AnimatSim::Robotics::RemoteControl::Load(oXml);
CStdPtrArray< RemoteControlLinkage > m_aryLinks
virtual bool InSimulation()
Used to determine if we are running in a simulation, or in a real control mode.
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 SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
virtual void Initialize()
We need to override this method because it does not care if it is in simulation mode or not...
virtual void StepIO()
This method is called from within the IO thread. It calls StepIO for each part.
CStdArray< RemoteControlLinkage * > m_aryOutLinks
Only the linkages that are outlinks.
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.
virtual void ShutdownIO()
This method is called just before the IO thread is closed down. It gives the IO objects a chance to d...
virtual void SimStarting()
Called just before the simulation starts.
virtual int GetChildInt(std::string strElementName)
Gets an integer value from the element with the specified name.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Declares the vortex structure class.
Declares the vortex hinge class.
A standard xml manipulation class.
virtual void StepIO()
This method is called from within the IO thread. It calls StepIO for each part.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
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.
int m_iBaudRate
The baud rate of communications for this XBee.
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
std::string m_strPort
The serial port this Xbee communicates on.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
bool m_bUseRemoteDataTypes
bool m_bSetupComplete
Set to true once the IO is setup correctly.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.