7 #include "RbMovableItem.h"
10 #include "RbMotorizedJoint.h"
11 #include "RbHingeLimit.h"
13 #include "RbRigidBody.h"
15 #include "RbFirmataPart.h"
16 #include "RbFirmataDynamixelServo.h"
17 #include "RbFirmataController.h"
23 namespace RobotIOControls
32 RbFirmataDynamixelServo::RbFirmataDynamixelServo()
44 m_iLastGetRegisterID = 0;
45 m_iLastGetRegisterValue = 0;
48 RbFirmataDynamixelServo::~RbFirmataDynamixelServo()
52 float RbFirmataDynamixelServo::QuantizeServoPosition(
float fltPos)
54 return RbDynamixelServo::QuantizeServoPosition(fltPos);
57 float RbFirmataDynamixelServo::QuantizeServoVelocity(
float fltVel)
59 return RbDynamixelServo::QuantizeServoVelocity(fltVel);
62 void RbFirmataDynamixelServo::IOComponentID(
int iID)
65 RbFirmataPart::IOComponentID(iID);
69 int RbFirmataDynamixelServo::ReadIsMoving(
int iServoID)
74 m_lpFirmata->sendDynamixelGetRegister(
m_iServoID, P_MOVING, 1);
75 bool bRet = m_lpFirmata->waitForSysExMessage(SYSEX_DYNAMIXEL_GET_REGISTER, 2);
81 int RbFirmataDynamixelServo::ReadLED(
int iServoID)
85 m_lpFirmata->sendDynamixelGetRegister(
m_iServoID, P_LED, 1);
86 bool bRet = m_lpFirmata->waitForSysExMessage(SYSEX_DYNAMIXEL_GET_REGISTER, 2);
92 int RbFirmataDynamixelServo::ReadAlarmShutdown(
int iServoID)
96 m_lpFirmata->sendDynamixelGetRegister(
m_iServoID, P_ALARM_SHUTDOWN, 1);
97 bool bRet = m_lpFirmata->waitForSysExMessage(SYSEX_DYNAMIXEL_GET_REGISTER, 2);
103 void RbFirmataDynamixelServo::WriteReturnDelayTime(
int iServoID,
int iVal)
107 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_RETURN_DELAY_TIME, 1, iVal);
112 void RbFirmataDynamixelServo::WriteCCWAngleLimit(
int iServoID,
int iVal)
116 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_CCW_ANGLE_LIMIT_L, 2, iVal);
121 void RbFirmataDynamixelServo::WriteCWAngleLimit(
int iServoID,
int iVal)
125 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_CW_ANGLE_LIMIT_L, 2, iVal);
130 void RbFirmataDynamixelServo::WriteCWComplianceMargin(
int iServoID,
int iVal)
134 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_CW_COMPLIANCE_MARGIN, 1, iVal);
139 void RbFirmataDynamixelServo::WriteCCWComplianceMargin(
int iServoID,
int iVal)
143 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_CCW_COMPLIANCE_MARGIN, 1, iVal);
148 void RbFirmataDynamixelServo::WriteCWComplianceSlope(
int iServoID,
int iVal)
152 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_CW_COMPLIANCE_SLOPE, 1, iVal);
157 void RbFirmataDynamixelServo::WriteCCWComplianceSlope(
int iServoID,
int iVal)
161 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_CCW_COMPLIANCE_SLOPE, 1, iVal);
166 void RbFirmataDynamixelServo::WriteMaxTorque(
int iServoID,
int iVal)
170 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_MAX_TORQUE_L, 2, iVal);
175 void RbFirmataDynamixelServo::WriteTorqueLimit(
int iServoID,
int iVal)
179 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_MAX_TORQUE_L, 2, iVal);
184 void RbFirmataDynamixelServo::WriteGoalPosition(
int iServoID,
int iPos)
188 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_GOAL_POSITION_L, 2, iPos);
193 void RbFirmataDynamixelServo::WriteMovingSpeed(
int iServoID,
int iVelocity)
197 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, P_MOVING_SPEED_L, 2, iVelocity);
202 void RbFirmataDynamixelServo::SetRegister(
unsigned char reg,
unsigned char length,
unsigned int value)
204 m_lpFirmata->sendDynamixelSetRegister(
m_iServoID, reg, length, value);
207 int RbFirmataDynamixelServo::GetRegister(
unsigned char reg,
unsigned char length)
209 m_lpFirmata->sendDynamixelGetRegister(
m_iServoID, reg, length);
210 bool bRet = m_lpFirmata->waitForSysExMessage(SYSEX_DYNAMIXEL_GET_REGISTER, 2);
211 if(bRet && m_iLastGetRegisterID == reg)
212 return m_iLastGetRegisterValue;
221 int iPos = ConvertPosFloatToFP(fltPos);
227 m_lpFirmata->sendDynamixelMove(
m_iServoID, iPos, iVel);
251 m_lpFirmata->sendDynamixelServoDetach(
m_iServoID);
260 m_EDynamixelTransmitError = m_lpFirmata->EDynamixelTransmitError.connect(boost::bind(&RbFirmataDynamixelServo::DynamixelTransmitError,
this, _1, _2));
261 m_EDynamixelGetRegister = m_lpFirmata->EDynamixelGetRegister.connect(boost::bind(&RbFirmataDynamixelServo::DynamixelGetRegister,
this, _1, _2, _3));
266 m_lpFirmata->sendDynamixelServoAttach(
m_iServoID);
274 void RbFirmataDynamixelServo::WaitForMoveToFinish()
281 m_lpFirmata->sendDynamixelStopped(
m_iServoID);
282 m_lpFirmata->waitForSysExMessage(SYSEX_DYNAMIXEL_STOPPED, 10);
286 #pragma region DataAccesMethods
288 float *RbFirmataDynamixelServo::GetDataPointer(
const std::string &strDataType)
290 float *fltVal = RbDynamixelServo::GetDataPointer(strDataType);
304 if(RbDynamixelServo::SetData(strDataType, strValue))
309 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
314 void RbFirmataDynamixelServo::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
317 RbDynamixelServo::QueryProperties(aryProperties);
322 void RbFirmataDynamixelServo::UpdateMotorData()
326 if(m_lpFirmata->_dynamixelServos[
m_iServoID]._allChanged)
327 UpdateAllMotorData();
328 else if(m_lpFirmata->_dynamixelServos[
m_iServoID]._keyChanged)
329 UpdateKeyMotorData();
333 void RbFirmataDynamixelServo::UpdateKeyMotorData()
343 m_lpFirmata->_dynamixelServos[
m_iServoID]._keyChanged =
false;
346 unsigned long long lEndTick =
m_lpSim->GetTimerTick();
353 void RbFirmataDynamixelServo::UpdateAllMotorData()
355 UpdateKeyMotorData();
367 m_lpFirmata->_dynamixelServos[
m_iServoID]._allChanged =
false;
372 void RbFirmataDynamixelServo::DynamixelRecieved(
const int &iServoID)
381 void RbFirmataDynamixelServo::DynamixelTransmitError(
const int &iCmd,
const int &iServoID)
387 case SYSEX_DYNAMIXEL_STOP:
390 case SYSEX_DYNAMIXEL_CONFIGURE_SERVO:
395 std::cout <<
"Transmit error Cmd: " << iCmd <<
", servo: " << iServoID <<
"\r\n";
399 void RbFirmataDynamixelServo::DynamixelGetRegister(
const unsigned char &iServoID,
const unsigned char &iReg,
const unsigned int &iValue)
403 m_iLastGetRegisterID = iReg;
404 m_iLastGetRegisterValue = iValue;
410 case P_MODEL_NUMBER_L:
413 case P_FIRMWARE_VERSION:
416 case P_RETURN_DELAY_TIME:
419 case P_CW_ANGLE_LIMIT_L:
422 case P_CCW_ANGLE_LIMIT_L:
434 case P_ALARM_SHUTDOWN:
442 void RbFirmataDynamixelServo::AddMotorUpdate(
int iPos,
int iSpeed)
445 m_lpFirmata->sendDynamixelSynchMoveAdd(
m_iServoID, iPos, iSpeed);
501 unsigned long long lStepStartTick =
m_lpSim->GetTimerTick();
507 unsigned long long lEndStartTick =
m_lpSim->GetTimerTick();
508 m_fltStepIODuration =
m_lpSim->TimerDiff_m(lStepStartTick, lEndStartTick);
516 void RbFirmataDynamixelServo::StepSimulation()
519 RbDynamixelServo::StepSimulation();
522 void RbFirmataDynamixelServo::ResetSimulation()
525 RbDynamixelServo::ResetSimulation();
530 RbFirmataPart::Load(oXml);
531 RbDynamixelServo::Load(oXml);
534 void RbFirmataDynamixelServo::MicroSleep(
unsigned int iTime)
540 Simulator *RbFirmataDynamixelServo::GetSimulator()
unsigned long long m_lStartServoUpdateTick
The tick when we start a servo update.
virtual float ConvertFPVelocity(int iVel)
Gets the floating point value of the servo actual velocity. The fixed point value is retrieved from t...
virtual void ShutdownMotor()
Shuts the motor down cleanly and ensures that it is not continuing to move after processing has stopp...
int m_iCWComplianceSlope
Used to set the CW compliance slope of the servo.
int m_iMinSimPos
This is the minimum angle in fixed point that can be used for this servo as specified in the simulati...
virtual bool InSimulation()
Used to determine if we are running in a simulation, or in a real control mode.
int m_iMaxTorque
Used to set the maximum torque of the servo.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
int m_iCCWComplianceMargin
Used to set the CCW compliance margin of the servo.
float m_fltLowLimit
Low constraint limit value.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Simulator * m_lpSim
The pointer to a Simulation.
int m_iMaxSimPos
This is the maximum angle in fixed point that can be used for this servo as specified in the simulati...
int m_iVoltage
The current voltage that was last read in for this servo.
virtual void Initialize()
Initializes this object.
virtual float ConvertFPLoad(int iLoad)
Gets the floating point value of the servo load. The fixed point value is retrieved from the servo an...
int m_iLastGoalVelocity
Keeps track of the last servo goal velocity that we set.
virtual void Stop()
Attempts to stop the motor at its current position.
int m_iServoID
ID used for communications with this servo.
virtual void StepIO(int iPartIdx)
This method is used to send/recieve the actual IO. This will often be in a seperate thread than the S...
int m_iNextGoalVelocity
This is the goal velocity that you would like to use in the next time step.
int m_iLastGoalPos
Keeps track of the last servo goal position that we set.
float m_fltHiLimit
Hi constraint limit value.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
float m_fltPresentPos
The current position that was last read in for this servo.
int m_iModelNum
Keeps track of the servos model number. This is updated once at the start of the sim and then used th...
int m_iTemperature
The current temperature that was last read in for this servo.
virtual int ServoID()
Gets the servo ID being used for communications.
int m_iPresentPos
The current position that was last read in for this servo.
virtual void ShutdownMotor()
Shuts the motor down cleanly and ensures that it is not continuing to move after processing has stopp...
Declares the vortex structure class.
int m_iFirmwareVersion
Keeps track of the servos firmware version. This is updated once at the start of the sim and then use...
float m_fltReadParamTime
The time taken to read the params of this motor for the current step.
virtual int ConvertFloatVelocity(float fltVelocity)
Gets the fixed point velocity form the floating point.
int m_iNextGoalPos
This is the goal position that you would like to use in the next time step.
Declares the vortex hinge class.
A standard xml manipulation class.
int m_iIsMoving
Keeps track of whether this servo is moving.
virtual void StepSimulation()
Step the simulation for this object.
int m_iLED
Keeps track of LED Status.
int m_iLoad
The current load that was last read in for this servo.
int m_iCCWAngleLimit
Keeps track of the counter-clockwise limit.
int m_iReturnDelayTime
Keeps track of the servos return delay time.
virtual void ConfigureServo()
Checks the current limits on the motor and sets them according to the simulation params.
virtual void ShutdownIO()
This method is called just before the IO thread is closed down. It gives the IO objects a chance to d...
float m_fltPresentVelocity
The current velocity that was last read in for this servo.
MotorizedJoint * m_lpMotorJoint
Pointer to an associated motorized joint.
float m_fltLoad
The current load that was last read in for this servo.
int m_iCWComplianceMargin
Used to set the CW compliance margin of the servo.
virtual void ResetSimulation()
Resets the simulation back to time 0.
int m_iTorqueLimit
Keeps track of the torque limit.
virtual void SetMaxSimPos(float fltVal)
Sets the maximum position that the simulation will allow for this joint in radians.
virtual void SetMinSimPos(float fltVal)
Sets the minimum position that the simulation will allow for this joint in radians.
int m_iCCWComplianceSlope
Used to set the CCW compliance slope of the servo.
virtual void Move(float fltPos, float fltVel)
Moves the motor to the specified position at the given speed.
virtual void SetupIO()
This method is called after all connections to whatever control board have been made. It is meant to be used to setup connection information specific to this part. 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. That is what this method is for.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
int m_iAlarm
Keeps track of alarm Status.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
int m_iCWAngleLimit
Keeps track of the clockwise limit.
float m_fltVoltage
The current voltage that was last read in for this servo.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void InitMotorData()
Initializes the internal data on position and velocity from the actual motor.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
float m_fltTemperature
The current temperature that was last read in for this servo.
virtual void InitMotorData()
Initializes the internal data on position and velocity from the actual motor.
int m_iPresentVelocity
The current velocity that was last read in for this servo.
virtual void Initialize()
Initializes this object.