23 #include "ros/serialization.h"
26 using ::common::model::BusProtocolEnum;
27 using ::common::model::DxlMotorState;
28 using ::common::model::EBusProtocol;
29 using ::common::model::EHardwareType;
30 using ::common::model::HardwareTypeEnum;
31 using ::common::model::StepperMotorState;
33 using ::std::to_string;
40 ros::NodeHandle robot_hwnh(
"joints_interface");
42 while (robot_hwnh.hasParam(
"joint_" + to_string(nb_joints + 1) +
"/id") &&
43 robot_hwnh.hasParam(
"joint_" + to_string(nb_joints + 1) +
"/name") &&
44 robot_hwnh.hasParam(
"joint_" + to_string(nb_joints + 1) +
"/type") &&
45 robot_hwnh.hasParam(
"joint_" + to_string(nb_joints + 1) +
"/bus"))
51 int currentIdStepper = 1;
53 for (
size_t j = 0; j < nb_joints; j++)
55 ROS_DEBUG(
"Initialize stepper motors");
56 int joint_id_config = 0;
61 robot_hwnh.getParam(
"joint_" + to_string(j + 1) +
"/id", joint_id_config);
62 robot_hwnh.getParam(
"joint_" + to_string(j + 1) +
"/name", joint_name);
63 robot_hwnh.getParam(
"joint_" + to_string(j + 1) +
"/type", joint_type);
64 robot_hwnh.getParam(
"joint_" + to_string(j + 1) +
"/bus", joint_bus);
65 HardwareTypeEnum eType = HardwareTypeEnum(joint_type.c_str());
66 BusProtocolEnum eBusProto = BusProtocolEnum(joint_bus.c_str());
68 if (eType == EHardwareType::STEPPER || eType == EHardwareType::FAKE_STEPPER_MOTOR)
70 std::string currentStepperNamespace =
"steppers/stepper_" + to_string(currentIdStepper);
72 auto stepperState = std::make_shared<StepperMotorState>(joint_name, eType, common::model::EComponentType::JOINT,
73 eBusProto,
static_cast<uint8_t
>(joint_id_config));
76 double offsetPos = 0.0;
77 double gear_ratio = 1.0;
79 double max_effort = 0.0;
80 double default_home_position = 0.0;
81 double limit_position_min = 0.0;
82 double limit_position_max = 0.0;
83 double motor_ratio = 0.0;
85 robot_hwnh.getParam(currentStepperNamespace +
"/offset_position", offsetPos);
86 robot_hwnh.getParam(currentStepperNamespace +
"/gear_ratio", gear_ratio);
87 robot_hwnh.getParam(currentStepperNamespace +
"/direction", direction);
88 robot_hwnh.getParam(currentStepperNamespace +
"/max_effort", max_effort);
89 robot_hwnh.getParam(currentStepperNamespace +
"/default_home_position", default_home_position);
90 robot_hwnh.getParam(currentStepperNamespace +
"/limit_position_min", limit_position_min);
91 robot_hwnh.getParam(currentStepperNamespace +
"/limit_position_max", limit_position_max);
92 robot_hwnh.getParam(currentStepperNamespace +
"/motor_ratio", motor_ratio);
95 common::model::VelocityProfile profile{};
97 if (robot_hwnh.hasParam(currentStepperNamespace +
"/v_start"))
99 robot_hwnh.getParam(currentStepperNamespace +
"/v_start", data);
100 profile.v_start =
static_cast<uint32_t
>(data);
103 if (robot_hwnh.hasParam(currentStepperNamespace +
"/a_1"))
105 robot_hwnh.getParam(currentStepperNamespace +
"/a_1", data);
106 profile.a_1 =
static_cast<uint32_t
>(data);
108 if (robot_hwnh.hasParam(currentStepperNamespace +
"/v_1"))
110 robot_hwnh.getParam(currentStepperNamespace +
"/v_1", data);
111 profile.v_1 =
static_cast<uint32_t
>(data);
113 if (robot_hwnh.hasParam(currentStepperNamespace +
"/a_max"))
115 robot_hwnh.getParam(currentStepperNamespace +
"/a_max", data);
116 profile.a_max =
static_cast<uint32_t
>(data);
118 if (robot_hwnh.hasParam(currentStepperNamespace +
"/v_max"))
120 robot_hwnh.getParam(currentStepperNamespace +
"/v_max", data);
121 profile.v_max =
static_cast<uint32_t
>(data);
123 if (robot_hwnh.hasParam(currentStepperNamespace +
"/d_max"))
125 robot_hwnh.getParam(currentStepperNamespace +
"/d_max", data);
126 profile.d_max =
static_cast<uint32_t
>(data);
128 if (robot_hwnh.hasParam(currentStepperNamespace +
"/d_1"))
130 robot_hwnh.getParam(currentStepperNamespace +
"/d_1", data);
131 profile.d_1 =
static_cast<uint32_t
>(data);
133 if (robot_hwnh.hasParam(currentStepperNamespace +
"/v_stop"))
135 robot_hwnh.getParam(currentStepperNamespace +
"/v_stop", data);
136 profile.v_stop =
static_cast<uint32_t
>(data);
140 stepperState->setOffsetPosition(offsetPos);
141 stepperState->setGearRatio(gear_ratio);
142 stepperState->setDirection(
static_cast<int8_t
>(direction));
143 stepperState->setMaxEffort(max_effort);
144 stepperState->setDefaultHomePosition(default_home_position);
145 stepperState->setLimitPositionMax(limit_position_max);
146 stepperState->setLimitPositionMin(limit_position_min);
147 stepperState->setMotorRatio(motor_ratio);
148 stepperState->setVelocityProfile(profile);
150 if (eBusProto == EBusProtocol::TTL)
151 ttl_interface->addJoint(stepperState);
156 else if (eType != EHardwareType::UNKNOWN)
158 ROS_DEBUG(
"Initialize dxl motors");
159 auto dxlState = std::make_shared<DxlMotorState>(joint_name, eType, common::model::EComponentType::JOINT,
160 static_cast<uint8_t
>(joint_id_config));
163 double offsetPos = 0.0;
165 int positionPGain = 0;
166 int positionIGain = 0;
167 int positionDGain = 0;
168 int velocityPGain = 0;
169 int velocityIGain = 0;
172 int velocityProfile = 0;
173 int accelerationProfile = 0;
174 double limit_position_min = 0.0;
175 double limit_position_max = 0.0;
176 double default_home_position = 0.0;
178 std::string currentDxlNamespace =
"dynamixels/dxl_" + to_string(currentIdDxl);
180 robot_hwnh.getParam(currentDxlNamespace +
"/offset_position", offsetPos);
181 robot_hwnh.getParam(currentDxlNamespace +
"/direction", direction);
183 robot_hwnh.getParam(currentDxlNamespace +
"/position_P_gain", positionPGain);
184 robot_hwnh.getParam(currentDxlNamespace +
"/position_I_gain", positionIGain);
185 robot_hwnh.getParam(currentDxlNamespace +
"/position_D_gain", positionDGain);
187 robot_hwnh.getParam(currentDxlNamespace +
"/velocity_P_gain", velocityPGain);
188 robot_hwnh.getParam(currentDxlNamespace +
"/velocity_I_gain", velocityIGain);
190 robot_hwnh.getParam(currentDxlNamespace +
"/FF1_gain", FF1Gain);
191 robot_hwnh.getParam(currentDxlNamespace +
"/FF2_gain", FF2Gain);
193 robot_hwnh.getParam(currentDxlNamespace +
"/velocity_profile", velocityProfile);
194 robot_hwnh.getParam(currentDxlNamespace +
"/acceleration_profile", accelerationProfile);
196 robot_hwnh.getParam(currentDxlNamespace +
"/default_home_position", default_home_position);
197 robot_hwnh.getParam(currentDxlNamespace +
"/limit_position_min", limit_position_min);
198 robot_hwnh.getParam(currentDxlNamespace +
"/limit_position_max", limit_position_max);
200 dxlState->setOffsetPosition(offsetPos);
201 dxlState->setDirection(
static_cast<int8_t
>(direction));
202 dxlState->setDefaultHomePosition(default_home_position);
204 dxlState->setPositionPGain(
static_cast<uint32_t
>(positionPGain));
205 dxlState->setPositionIGain(
static_cast<uint32_t
>(positionIGain));
206 dxlState->setPositionDGain(
static_cast<uint32_t
>(positionDGain));
208 dxlState->setVelocityPGain(
static_cast<uint32_t
>(velocityPGain));
209 dxlState->setVelocityIGain(
static_cast<uint32_t
>(velocityIGain));
211 dxlState->setFF1Gain(
static_cast<uint32_t
>(FF1Gain));
212 dxlState->setFF2Gain(
static_cast<uint32_t
>(FF2Gain));
214 if (eBusProto == EBusProtocol::TTL)
215 ttl_interface->addJoint(dxlState);
223 int main(
int argc,
char** argv)
225 ros::init(argc, argv,
"ttl_driver_node");
227 ROS_DEBUG(
"Launching ttl_driver_node");
229 ros::NodeHandle nodeHandle(
"~");
231 std::shared_ptr<ttl_driver::TtlInterfaceCore> ttl_node = std::make_shared<ttl_driver::TtlInterfaceCore>(nodeHandle);