This commit is contained in:
Minh MENG HOUR 2023-03-09 12:06:37 +01:00
parent b67608109b
commit d34fb19b48
1 changed files with 75 additions and 23 deletions

98
main.cpp Normal file → Executable file
View File

@ -12,27 +12,79 @@ float _minJointCmd = 0;
float _maxJointCmd = 1023; float _maxJointCmd = 1023;
float _minJointAngle = -180.0f; float _minJointAngle = -180.0f;
float _maxJointAngle = 180.0f; float _maxJointAngle = 180.0f;
int main()
{ std::cout << "===Initialization of the Dynamixel Motor communication====" << std::endl; int convertAnglesToJointCmd(float fJointAngle)
_oDxlHandler.setDeviceName(_poppyDxlPortName); {
_oDxlHandler.setProtocolVersion(_poppyDxlProtocol); // y = ax + b
_oDxlHandler.openPort(); float a = (_maxJointCmd-_minJointCmd) / (_maxJointAngle - _minJointAngle);
_oDxlHandler.setBaudRate(_poppyDxlBaudRate); float b = _minJointCmd - a * _minJointAngle;
_oDxlHandler.enableTorque(true); float jointCmd = a * fJointAngle + b;
std::cout << std::endl; return (int)jointCmd;
// read current joint position
std::vector<uint16_t> l_vCurrentJointPosition;
_oDxlHandler.readCurrentJointPosition(l_vCurrentJointPosition);
// display current joint position
std::cout << "vCurrentJointPosition= (" << std::endl;
for (int l_joint=0; l_joint < l_vCurrentJointPosition.size(); l_joint++)
std::cout << l_vCurrentJointPosition[l_joint] << ", ";
std::cout << ")" << std::endl;
// wait 1s
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << "===Closing the Dynamixel Motor communication====" << std::endl;
_oDxlHandler.enableTorque(false);
_oDxlHandler.closePort();
std::cout << std::endl;
return 0;
} }
void goToHomePosition()
{
std::vector<uint16_t> l_vTargetJointPosition;
for (int l_joint = 0; l_joint < _nbJoints; l_joint++)
l_vTargetJointPosition.push_back(convertAnglesToJointCmd(0.0f));
_oDxlHandler.sendTargetJointPosition(l_vTargetJointPosition);
}
void CurrentTorque()
{
std::vector<uint16_t> l_vTargetJointPosition;
for (int l_joint = 5; l_joint < _nbJoints; l_joint++)
l_vTargetJointPosition.push_back(convertAnglesToJointCmd(0.0f));
_oDxlHandler.sendTargetJointPosition(l_vTargetJointPosition);
}
void goToNewPosition()
{
std::vector<uint16_t> l_vCurrentJointPosition;
_oDxlHandler.readCurrentJointPosition(l_vCurrentJointPosition);
// display current joint position
std::cout << "newCurrentJointPosition= (" << std::endl;
for (int l_joint=0; l_joint < l_vCurrentJointPosition.size(); l_joint++)
std::cout << l_vCurrentJointPosition[l_joint] << ", ";
std::cout << ")" << std::endl;
}
int main()
{
std::cout << "===Initialization of the Dynamixel Motor communication====" << std::endl;
_oDxlHandler.setDeviceName(_poppyDxlPortName);
_oDxlHandler.setProtocolVersion(_poppyDxlProtocol);
_oDxlHandler.openPort();
_oDxlHandler.setBaudRate(_poppyDxlBaudRate);
_oDxlHandler.enableTorque(true);
std::cout << std::endl;
// read current joint position
std::vector<uint16_t> l_vCurrentJointPosition;
_oDxlHandler.readCurrentJointPosition(l_vCurrentJointPosition);
// display current joint position
std::cout << "vCurrentJointPosition= (" << std::endl;
for (int l_joint=0; l_joint < l_vCurrentJointPosition.size(); l_joint++)
std::cout << l_vCurrentJointPosition[l_joint] << ", ";
std::cout << ")" << std::endl;
// read current torque
std::vector<uint16_t>l_vCurrentJointTorque;
//goToHomePosition();
//goToNewPosition();
CurrentTorque();
// wait 1s
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << "===Closing the Dynamixel Motor communication====" << std::endl;
_oDxlHandler.enableTorque(false);
_oDxlHandler.closePort();
std::cout << std::endl;
return 0;
}