Compare commits

...

5 Commits

Author SHA1 Message Date
Camille CONJAT 98873bfc12 Merge branch 'develop' into main 2023-03-09 09:30:41 +01:00
Camille CONJAT 5670602d22 update reading position 2023-03-09 09:29:48 +01:00
Camille CONJAT 64e07f3b50 Merge branch 'develop' into main 2023-03-09 09:23:12 +01:00
Camille CONJAT 4c3a0c5e2c makefile 2023-03-09 09:22:22 +01:00
Camille CONJAT 2389f28f43 cpp file gitted 2023-03-09 09:08:05 +01:00
2 changed files with 82 additions and 0 deletions

70
main.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <chrono>
#include <thread>
#include "DynamixelHandler.h"
// Global variables
DynamixelHandler _oDxlHandler;
std::string _poppyDxlPortName = "/dev/ttyUSB0";
float _poppyDxlProtocol = 2.0;
int _poppyDxlBaudRate = 1000000;
int _nbJoints = 6;
float _minJointCmd = 0;
float _maxJointCmd = 1023;
float _minJointAngle = -180.0f;
float _maxJointAngle = 180.0f;
int convertAnglesToJointCmd(float fJointAngle)
{ // y = ax + b
float a = (_maxJointCmd-_minJointCmd) / (_maxJointAngle - _minJointAngle);
float b = _minJointCmd - a * _minJointAngle;
float jointCmd = a * fJointAngle + b;
return (int)jointCmd;
}
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);
}
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;
// 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;
goToHomePosition();
// 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;
// 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;
}

12
makefile Normal file
View File

@ -0,0 +1,12 @@
all : dynamixel main
g++ DynamixelHandler.o main.o L/usr/local/lib -ldxl_x64_cpp -lrt
dynamixel : DynamixelHandler.cpp
g++ -c -I/home/ros/SOFTWARE/toolkit-dynamixel/include /home/ros/SOFTWARE/toolkit-dynamixel/src/DynamixelHandler.cpp
main : main.cpp
g++ -c -I/home/ros/SOFTWARE/toolkit-dynamixel/include main.cpp
clean :
rm * .o
rm * .out
rm * .exe