7 #include "RbMovableItem.h"
10 #include "RbMotorizedJoint.h"
11 #include "RbHingeLimit.h"
13 #include "RbRigidBody.h"
15 #include "RbDynamixelServo.h"
18 #define DYN_LENGTH (3)
19 #define DYN_INSTRUCTION (4)
20 #define DYN_ERRBIT (4)
21 #define DYN_PARAMETER (5)
22 #define DYN_DEFAULT_BAUDNUMBER (1)
28 namespace RobotIOControls
35 RbDynamixelServo::RbDynamixelServo()
101 RbDynamixelServo::~RbDynamixelServo()
111 void RbDynamixelServo::RecalculateParams()
159 void RbDynamixelServo::MinPosFP(
int iVal)
166 int RbDynamixelServo::MinPosFP() {
return m_iMinPosFP;}
168 void RbDynamixelServo::MaxPosFP(
int iVal)
175 int RbDynamixelServo::MaxPosFP() {
return m_iMaxPosFP;}
177 void RbDynamixelServo::MinAngle(
float fltVal)
186 void RbDynamixelServo::MaxAngle(
float fltVal)
195 void RbDynamixelServo::MinVelocityFP(
int iVal)
204 void RbDynamixelServo::MaxVelocityFP(
int iVal)
213 void RbDynamixelServo::RPMPerFPUnit(
float fltVal)
222 void RbDynamixelServo::MinLoadFP(
int iVal)
229 int RbDynamixelServo::MinLoadFP() {
return m_iMinLoadFP;}
231 void RbDynamixelServo::MaxLoadFP(
int iVal)
238 int RbDynamixelServo::MaxLoadFP() {
return m_iMaxLoadFP;}
242 void RbDynamixelServo::CWComplianceMargin(
int iVal)
250 void RbDynamixelServo::CCWComplianceMargin(
int iVal)
258 void RbDynamixelServo::CWComplianceSlope(
int iVal)
266 void RbDynamixelServo::CCWComplianceSlope(
int iVal)
272 int RbDynamixelServo::MaxTorque() {
return m_iMaxTorque;}
274 void RbDynamixelServo::MaxTorque(
int iVal)
280 float RbDynamixelServo::ConvertPosFPToFloat(
int iPos)
287 int RbDynamixelServo::ConvertPosFloatToFP(
float fltPos)
538 int iPos = ConvertPosFloatToFP(fltPos);
556 int iPos = ConvertPosFloatToFP(fltPos);
573 float fltPos = ConvertPosFPToFloat(iGoal);
614 float fltPos = ConvertPosFPToFloat(iPos);
668 if(fltVelocity < -0.4)
669 fltVelocity = fltVelocity;
758 int iVoltage = ReadPresentVoltage(
m_iServoID);
773 float fltVoltage = iVoltage/100.0;
801 float fltTemp = (float) ReadPresentTemperature(
m_iServoID);
837 bIsBlueOn = iLED & 0x04;
838 bIsGreenOn = iLED & 0x02;
839 bIsRedOn = iLED & 0x01;
902 void RbDynamixelServo::WaitForMoveToFinish()
936 boost::this_thread::sleep(boost::posix_time::microseconds(100));
939 boost::this_thread::sleep(boost::posix_time::microseconds(100));
954 int iRetCWComplMargin = GetCWComplianceMargin_FP();
955 int iRetCCWComplMargin = GetCCWComplianceMargin_FP();
956 int iRetCWComplSlope = GetCWComplianceSlope_FP();
957 int iRetCCWComplSlope = GetCCWComplianceSlope_FP();
998 WaitForMoveToFinish();
1023 std::cout <<
"Shutting down servo: " <<
m_iServoID <<
", Pos: " << iCurPos <<
"\r\n";
1036 if(iVal >= 0 && iVal < 256)
1078 int iPos = ConvertPosFloatToFP(fltVal);
1104 Std_IsAboveMin((
float) 0, fltVal,
true,
"TranslationRange",
true);
1107 RecalculateParams();
1136 return ConvertPosFPToFloat(iPos);
1163 int iPos = ConvertPosFloatToFP(fltVal);
1191 return ConvertPosFPToFloat(iPos);
1194 void RbDynamixelServo::SetCWComplianceMargin_FP(
int iVal)
1196 if(iVal >= 0 && iVal <= 255)
1200 int RbDynamixelServo::GetCWComplianceMargin_FP()
1205 void RbDynamixelServo::SetCCWComplianceMargin_FP(
int iVal)
1207 if(iVal >= 0 && iVal <= 255)
1211 int RbDynamixelServo::GetCCWComplianceMargin_FP()
1216 void RbDynamixelServo::SetCWComplianceSlope_FP(
int iVal)
1218 if(iVal >= 0 && iVal <= 255)
1222 int RbDynamixelServo::GetCWComplianceSlope_FP()
1227 void RbDynamixelServo::SetCCWComplianceSlope_FP(
int iVal)
1229 if(iVal >= 0 && iVal <= 255)
1233 int RbDynamixelServo::GetCCWComplianceSlope_FP()
1238 void RbDynamixelServo::SetMaxTorque_FP(
int iVal)
1240 if(iVal >= 0 && iVal <= 1023)
1244 int RbDynamixelServo::GetMaxTorque_FP()
1356 void RbDynamixelServo::SetMotorPosVel()
1393 float RbDynamixelServo::QuantizeServoPosition(
float fltPos)
1395 int iPos = ConvertPosFloatToFP(fltPos);
1396 return ConvertPosFPToFloat(iPos);
1399 float RbDynamixelServo::QuantizeServoVelocity(
float fltVel)
1405 float *RbDynamixelServo::GetDataPointer(
const std::string &strDataType)
1409 if(strType ==
"READPARAMTIME")
1411 else if(strType ==
"IOPOS")
1413 else if(strType ==
"IOVELOCITY")
1419 bool RbDynamixelServo::SetData(
const std::string &strDataType,
const std::string &strValue)
1423 if(strType ==
"SERVOID")
1425 ServoID((
int) atoi(strValue.c_str()));
1429 if(strType ==
"QUERYMOTORDATA")
1435 if(strType ==
"MINPOSFP")
1437 MinPosFP((
int) atoi(strValue.c_str()));
1441 if(strType ==
"MAXPOSFP")
1443 MaxPosFP((
int) atoi(strValue.c_str()));
1447 if(strType ==
"MINANGLE")
1449 MinAngle((
float) atof(strValue.c_str()));
1453 if(strType ==
"MAXANGLE")
1455 MaxAngle((
float) atof(strValue.c_str()));
1459 if(strType ==
"MINVELOCITYFP")
1461 MinVelocityFP((
int) atoi(strValue.c_str()));
1465 if(strType ==
"MAXVELOCITYFP")
1467 MaxVelocityFP((
int) atoi(strValue.c_str()));
1471 if(strType ==
"CWCOMPLIANCEMARGIN")
1473 CWComplianceMargin((
int) atoi(strValue.c_str()));
1477 if(strType ==
"CCWCOMPLIANCEMARGIN")
1479 CCWComplianceMargin((
int) atoi(strValue.c_str()));
1483 if(strType ==
"CWCOMPLIANCESLOPE")
1485 CWComplianceSlope((
int) atoi(strValue.c_str()));
1489 if(strType ==
"CCWCOMPLIANCESLOPE")
1491 CCWComplianceSlope((
int) atoi(strValue.c_str()));
1495 if(strType ==
"MAXTORQUE")
1497 MaxTorque((
int) atoi(strValue.c_str()));
1501 if(strType ==
"RPMPERFPUNIT")
1503 RPMPerFPUnit((
float) atof(strValue.c_str()));
1507 if(strType ==
"MINLOADFP")
1509 MinLoadFP((
int) atoi(strValue.c_str()));
1513 if(strType ==
"MAXLOADFP")
1515 MaxLoadFP((
int) atoi(strValue.c_str()));
1519 if(strType ==
"TRANSLATIONRANGE")
1525 if(strType ==
"RESETTOSTARTPOS")
1534 void RbDynamixelServo::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
1536 aryProperties.Add(
new TypeProperty(
"ReadParamTime", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
1537 aryProperties.Add(
new TypeProperty(
"IOPos", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
1538 aryProperties.Add(
new TypeProperty(
"IOVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
1540 aryProperties.Add(
new TypeProperty(
"ServoID", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1541 aryProperties.Add(
new TypeProperty(
"QueryMotorData", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
1543 aryProperties.Add(
new TypeProperty(
"MinPosFP", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1544 aryProperties.Add(
new TypeProperty(
"MaxPosFP", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1545 aryProperties.Add(
new TypeProperty(
"MinAngle", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1546 aryProperties.Add(
new TypeProperty(
"MaxAngle", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1547 aryProperties.Add(
new TypeProperty(
"MinVelocityFP", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1548 aryProperties.Add(
new TypeProperty(
"MaxVelocityFP", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1549 aryProperties.Add(
new TypeProperty(
"RPMPerFPUnit", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1550 aryProperties.Add(
new TypeProperty(
"MinLoadFP", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1551 aryProperties.Add(
new TypeProperty(
"MaxLoadFP", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1552 aryProperties.Add(
new TypeProperty(
"TranslationRange", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1553 aryProperties.Add(
new TypeProperty(
"ResetToStartPos", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
1555 aryProperties.Add(
new TypeProperty(
"CWComplianceMargin", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1556 aryProperties.Add(
new TypeProperty(
"CCWComplianceMargin", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1557 aryProperties.Add(
new TypeProperty(
"CWComplianceSlope", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1558 aryProperties.Add(
new TypeProperty(
"CCWComplianceSlope", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1559 aryProperties.Add(
new TypeProperty(
"MaxTorque", AnimatPropertyType::Integer, AnimatPropertyDirection::Set));
1562 void RbDynamixelServo::ResetSimulation()
1571 void RbDynamixelServo::StepSimulation()
1584 if(
m_lpMotorJoint->MotorType() == eJointMotorType::PositionVelocityControl)
1591 else if(
m_lpMotorJoint->MotorType() == eJointMotorType::VelocityControl)
1598 if(fabs(fltSetVelocity) < 1e-4)
1608 else if(fltSetVelocity > 0)
1629 if(!GetSimulator()->InSimulation())
1648 void RbDynamixelServo::GetLimitValues()
1658 Prismatic *lpPrismatic =
dynamic_cast<Prismatic *
>(
m_lpMotorJoint);
float m_fltPosFloatToFPSlope
The conversion factor to convert radians to FP position.
virtual float ConvertFPVelocity(int iVel)
Gets the floating point value of the servo actual velocity. The fixed point value is retrieved from t...
virtual float GetActualTemperatureFahrenheit()
Gets the floating point value of the servo actual temperature. The fixed point value is retrieved fro...
virtual void SetCWAngleLimit(float fltLimit)
Sets the limit for the CW limit of the servo using radians.
int m_iTotalAngle
Maximum angle the servo can move in fixed point number. m_iMaxPosFP - m_iMinPosFP.
int m_iCWComplianceSlope
Used to set the CW compliance slope of the servo.
int m_iMaxVelocityFP
Maximum value that can be set for the velocity.
int m_iMinSimPos
This is the minimum angle in fixed point that can be used for this servo as specified in the simulati...
virtual void SetCWAngleLimit_FP(int iVal)
Sets the limit for the CW limit of the servo using fixed point value.
float m_fltConvertFPToPosS
The conversion factor to convert FP velocity value to rad/s.
virtual bool GetIsAlarmShutdown()
Returns whether the alarm shutdown state is active.
virtual float GetGoalPosition()
Gets the floating point value of the servo goal position. The fixed point value is retrieved from the...
virtual float GetActualLoad()
Gets the floating point value of the servo actual torque. The fixed point value is retrieved from the...
int m_iMaxTorque
Used to set the maximum torque of the servo.
virtual int GetModelNumber()
Returns whether the model number of the servo.
bool m_bVelStopPosSet
Keeps track of whether we have already set velocity to stop when doing velocity control.
virtual void SetGoalPosition_FP(int iPos)
Sets the fixed point value of the servo goal position. This is the exact value that will be sent to t...
virtual float GetActualTemperatureCelcius()
Gets the floating point value of the servo actual temperature. The fixed point value is retrieved fro...
int m_iCCWComplianceMargin
Used to set the CCW compliance margin of the servo.
int m_iMaxLoadFP
Maximum value that can be set for the load.
float m_fltLowLimit
Low constraint limit value.
virtual float GetActualVelocity()
Gets the floating point value of the servo actual velocity. The fixed point value is retrieved from t...
virtual int GetCCWAngleLimit_FP()
Gets the limit for the CCW limit of the servo in fixed point value.
virtual void SetReturnDelayTime_FP(int iVal)
Sets the return delay time of the servo.
float m_fltMaxAngle
Maximum value that can be set for the position in floating point number.
virtual bool IntoElem()
Goes into the next element where the cursor is located.
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 int GetActualLoad_FP()
Gets the fixed point value of the servo actual load. This is the exact value returned from the servo...
float m_fltConvertPosSToFP
The conversion factor to convert rad/s to FP velocity.
virtual int GetActualVelocity_FP()
Gets the fixed point value of the servo actual velocity. This is the exact value returned from the se...
virtual float ConvertFPLoad(int iLoad)
Gets the floating point value of the servo load. The fixed point value is retrieved from the servo an...
float m_fltMinAngle
Minimum value that can be set for the position in floating point number.
int m_iLastGoalVelocity
Keeps track of the last servo goal velocity that we set.
virtual int GetReturnDelayTime_FP()
Gets the return delay time of the servo.
virtual void Stop()
Attempts to stop the motor at its current position.
int m_iServoID
ID used for communications with this servo.
int m_iMinLoadFP
Minimum value that can be set for the load.
virtual float TranslationRange()
Gets the the total range of translation for a prismatic joint (meters).
virtual int GetMinSimPos_FP()
Gets the minimum position in fixed point that the simulation will allow for the joint.
bool Std_InValidRange(int iMinVal, int iMaxVal, int iVal, bool bThrowError, std::string strParamName)
Tests whether a number is within a valid range.
virtual float GetMaxSimPos()
Gets the maximum position in radians that the simulation will allow for the joint.
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_fltFloatToFPTranslation
Conversion factor for when we are doing prismatic joints. Converts rotations to linear movements...
virtual int GetChildInt(std::string strElementName)
Gets an integer value from the element with the specified name.
float m_fltHiLimit
Hi constraint limit value.
int m_iMinPosFP
Minimum value that can be set for the position in fixed point number.
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.
virtual int LastGoalVelocity_FP()
Gets the last goal velocity that was sent to the servo. This is used so we can see the last value we ...
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.
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.
virtual int GetIDNumber()
Returns whether the servo ID number.
Declares the vortex hinge class.
virtual int GetGoalVelocity_FP()
Gets the fixed point value of the servo velocity. This is the exact value returned from the servo...
virtual float GetActualPosition()
Gets the floating point value of the servo actual position. The fixed point value is retrieved from t...
virtual void Move(float fltPos, float fltVel)
Moves the motor to the specified position at the given speed.
float m_fltPosFloatToFPIntercept
The conversion factor to convert radians to FP position.
virtual void SetGoalPosition(float fltPos)
Sets the floating point value of the servo goal position. This is the position in radians...
bool m_bNeedSetVelStopPos
A standard xml manipulation class.
virtual int GetCWAngleLimit_FP()
Gets the limit for the CW limit of the servo in fixed point value.
float m_fltMaxPosSec
Stores the maximum rad/sec for this motor.
int m_iMinVelocityFP
Minimum value that can be set for the velocity.
int m_iLoad
The current load that was last read in for this servo.
virtual float GetCWAngleLimit()
Gets the limit for the CW limit of the servo in radian values.
float m_fltConvertFPToLoad
Used to conver the load fixed point value back to a percentage of load.
float m_fltMaxSimPos
This is the maximum angle in radians that can be used for this servo as specified in the simulation...
virtual int GetActualVoltage_FP()
Gets the fixed point value of the servo actual voltage. This is the exact value returned from the ser...
virtual void SetNextMaximumVelocity()
Sets the servo goal speed to 0 for maximum velocity the next time the IO for this servo is processed...
virtual void SetCCWAngleLimit(float fltLimit)
Sets the limit for the CCW limit of the servo using radians.
float m_fltPresentVelocity
The current velocity that was last read in for this servo.
MotorizedJoint * m_lpMotorJoint
Pointer to an associated motorized joint.
virtual int GetMaxSimPos_FP()
Gets the maximum position in fixed point that the simulation will allow for the joint.
float m_fltFPToFloatTranslation
Conversion factor for when we are doing prismatic joints. Converts rotations to linear movements...
bool Std_ToBool(int iVal)
Converts a value toa bool.
bool Std_IsBelowMax(int iMaxVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is below a maximum value.
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 bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
virtual void GetIsLEDOn(bool &bIsBlueOn, bool &bIsGreenOn, bool &bIsRedOn)
Returns whether the servo blue led is currently on.
float m_fltTranslationRange
The total translation range over which a prismatic joint can move. This does not apply to hinge joint...
float m_fltRPMPerFPUnit
Stores the maximum rot/min for this motor. The max rotations (rad) per fixed point unit...
virtual void SetMaxSimPos(float fltVal)
Sets the maximum position that the simulation will allow for this joint in radians.
virtual int GetActualPosition_FP()
Gets the fixed point value of the servo actual position. This is the exact value returned from the se...
virtual void SetMinSimPos(float fltVal)
Sets the minimum position that the simulation will allow for this joint in radians.
virtual void SetTorqueLimit_FP(int iVal)
Sets the torque limit of the servo using fixed point value.
virtual float GetCCWAngleLimit()
Gets the limit for the CCW limit of the servo in radian values.
int m_iCCWComplianceSlope
Used to set the CCW compliance slope of the servo.
float m_fltTotalAngle
Maximum angle the servo can move in floating point number. m_iMaxPosFP - m_iMinPosFP.
bool m_bResetToStartPos
If true then we should reset to the start position at the beginning of the simulation.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
virtual float GetGoalVelocity()
Gets the floating point value of the servo velocity. The fixed point value is retrieved from the serv...
virtual int GetGoalPosition_FP()
Gets the fixed point value of the servo goal position. This is the exact value returned from the serv...
float m_fltIOPos
Used to report back the IO position of the servo at each step.
int m_iMaxPosFP
Maximum value that can be set for the position in fixed point number.
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 SetGoalVelocity_FP(int iVelocity)
Sets the fixed point value of the servo velocity. This is the exact value that will be sent to the se...
virtual void SetCCWAngleLimit_FP(int iVal)
Sets the limit for the CCW limit of the servo using fixed point value.
virtual void SetMaximumVelocity()
Sets the servo goal speed to 0 for maximum velocity.
virtual void InitMotorData()
Initializes the internal data on position and velocity from the actual motor.
virtual void SetNextGoalVelocity_FP(int iVelocity)
Sets the fixed point value of the servo velocity for the next time the IO for this servo is processed...
virtual void SetGoalVelocity(float fltVelocity)
Sets the floating point value of the servo velocity. This is the velocity in rad/s. It will be converted to a fixed point value based on the motor configuration and then used to set the velocity.
virtual int GetFirmwareVersion()
Returns whether the servo firmware version number.
virtual void SetNextGoalVelocity(float fltVelocity)
Sets the floating point value of the servo velocity the next time the IO for this servo is processed...
float m_fltPosFPToFloatIntercept
The conversion factor to convert FP position to floating point position..
float m_fltMinSimPos
This is the minimum angle in radians that can be used for this servo as specified in the simulation...
virtual float GetActualVoltage()
Gets the floating point value of the servo actual voltage. The fixed point value is retrieved from th...
virtual bool GetIsMoving()
Returns whether the servo is currently moving or has already reached its goal state.
virtual float GetMinSimPos()
Gets the minimum position in radians that the simulation will allow for the joint.
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 SetNextGoalPosition(float fltPos)
Sets the floating point value of the servo goal position that will be used the next time the iO for t...
virtual int GetTorqueLimit_FP()
Gets the torque limit of the servo in fixed point value.
float m_fltIOVelocity
Used to report back the IO velocity of the servo at each step.
int m_iPresentVelocity
The current velocity that was last read in for this servo.
int m_iCenterPosFP
The center point value in fixed point numbers.
virtual int LastGoalPosition_FP()
Gets the last goal position that was sent to the servo. This is used so we can see the last value we ...
virtual void ConfigureServo()
Checks the current limits on the motor and sets them according to the simulation params.
float m_fltPosFPToFloatSlope
The conversion factor to convert FP position to floating point position.
virtual void SetNextGoalPosition_FP(int iPos)
Sets the fixed point value of the servo goal position that will be used the next time the IO for this...
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.