Skip to content

Commit d51e71d

Browse files
leeminju531doosan-robotics
authored andcommitted
Fixes hw interfaces (servoj -> servojRT)
1 parent ec472f3 commit d51e71d

File tree

2 files changed

+94
-75
lines changed

2 files changed

+94
-75
lines changed

dsr_controller2/config/dsr_controller2.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dsr_moveit_controller:
7272
ros__parameters:
7373
command_interfaces:
7474
- position
75+
- velocity
7576
state_interfaces:
7677
- position
7778
- velocity

dsr_hardware2/src/dsr_hw_interface2.cpp

+93-75
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,34 @@ CallbackReturn DRHWInterface::on_init(const hardware_interface::HardwareInfo & i
271271
}
272272

273273
Drfl.set_auto_servo_off(0, 5.0);
274-
// Drfl.connect_rt_control();
275-
// string version = "v1.0";
276-
// float period = 0.001; // 1 msec
277-
// int losscount = 4;
278-
// Drfl.set_rt_control_output(version, period, losscount);
279-
// usleep(nDelay);
280-
// Drfl.start_rt_control();
281274

275+
// Virtual controller doesn't support real time connection.
276+
if(m_mode != "virtual") {
277+
if (!Drfl.connect_rt_control(m_host)) {
278+
RCLCPP_ERROR(rclcpp::get_logger("dsr_hw_interface2"), "Unable to connect RT control stream");
279+
return CallbackReturn::FAILURE;
280+
}
281+
RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "Connected RT control stream");
282+
const std::string version = "v1.0";
283+
const float period = 0.001;
284+
const int losscount = 4;
285+
if (!Drfl.set_rt_control_output(version, period, losscount)) {
286+
RCLCPP_ERROR(rclcpp::get_logger("dsr_hw_interface2"), "Unable to connect RT control stream");
287+
return CallbackReturn::FAILURE;
288+
}
289+
290+
if (!Drfl.start_rt_control()) {
291+
RCLCPP_ERROR(rclcpp::get_logger("dsr_hw_interface2"), "Unable to start RT control");
292+
return CallbackReturn::FAILURE;
293+
}
294+
295+
RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "Setting velocity and acceleration limits");
296+
float limit[6] = {70.0f,70.0f,70.0f,70.0f,70.0f,70.0f};
297+
if (!Drfl.set_velj_rt(limit)) return CallbackReturn::ERROR;
298+
if (!Drfl.set_accj_rt(limit)) return CallbackReturn::ERROR;
299+
}
300+
301+
Drfl.set_safety_mode(SAFETY_MODE_AUTONOMOUS,SAFETY_MODE_EVENT_MOVE);
282302
return CallbackReturn::SUCCESS;
283303
}
284304

@@ -320,23 +340,36 @@ std::vector<hardware_interface::CommandInterface> DRHWInterface::export_command_
320340

321341
return_type DRHWInterface::read(const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
322342
{
323-
324-
LPROBOT_POSE pose = Drfl.GetCurrentPose();
325-
if(nullptr == pose) {
326-
RCLCPP_WARN(rclcpp::get_logger("dsr_hw_interface2"),
327-
"[read] GetCurrentPose retrieves nullptr");
328-
return return_type::ERROR; //? what effection of this to control node
329-
}
330-
for(int i=0;i<6;i++){
331-
joint_position_[i] = deg2rad(pose->_fPosition[i]);
332-
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), " [READ] joint_pos[%d] : %.3f ", i, joint_position_[i]);
333-
}
343+
if(m_mode == "real") {
344+
const LPRT_OUTPUT_DATA_LIST data = Drfl.read_data_rt();
345+
for(int i=0;i<6;i++) {
346+
joint_position_[i] = static_cast<float>(data->actual_joint_position[i] * (M_PI / 180.0f));
347+
joint_velocities_[i] = static_cast<float>(data->actual_joint_velocity[i] * (M_PI / 180.0f));
348+
}
349+
}else if(m_mode == "virtual") {
350+
LPROBOT_POSE pose = Drfl.GetCurrentPose();
351+
if(nullptr == pose) {
352+
RCLCPP_WARN(rclcpp::get_logger("dsr_hw_interface2"),
353+
"[read] GetCurrentPose retrieves nullptr");
354+
return return_type::ERROR; //? what effection of this to control node
355+
}
356+
for(int i=0;i<6;i++){
357+
joint_position_[i] = deg2rad(pose->_fPosition[i]);
358+
}
359+
}else {
360+
RCLCPP_ERROR(rclcpp::get_logger("dsr_hw_interface2"),
361+
"'mode' is neither 'real' nor 'virtual.'" );
362+
}
363+
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[READ] joint_position_ : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
364+
// ,joint_position_[0]
365+
// ,joint_position_[1]
366+
// ,joint_position_[2]
367+
// ,joint_position_[3]
368+
// ,joint_position_[4]
369+
// ,joint_position_[5]);
334370
return return_type::OK;
335371
}
336372

337-
338-
std::vector<float> previous_joint_position_command_float(6, std::numeric_limits<float>::quiet_NaN()); // 초기값 NaN
339-
340373
bool positionCommandRunning(const std::vector<double>& lhs, const std::vector<double>& rhs) {
341374
double var = 0;
342375
for(int i=0; i<lhs.size(); i++) {
@@ -346,84 +379,69 @@ bool positionCommandRunning(const std::vector<double>& lhs, const std::vector<do
346379
}
347380

348381
vector<vector<float>> joint_position_commands;
349-
bool done = false;
350382
return_type DRHWInterface::write(const rclcpp::Time &, const rclcpp::Duration &dt)
351383
{
384+
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] dt : %.3f", float(dt.seconds()) );
385+
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] joint_position_command_ : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
386+
// ,joint_position_command_[0]
387+
// ,joint_position_command_[1]
388+
// ,joint_position_command_[2]
389+
// ,joint_position_command_[3]
390+
// ,joint_position_command_[4]
391+
// ,joint_position_command_[5]);
392+
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] joint_velocities_command_ : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
393+
// ,joint_velocities_command_[0]
394+
// ,joint_velocities_command_[1]
395+
// ,joint_velocities_command_[2]
396+
// ,joint_velocities_command_[3]
397+
// ,joint_velocities_command_[4]
398+
// ,joint_velocities_command_[5]);
399+
static bool idle = false;
400+
// TODO: this seems to be a workaround. refer to hardware design of 'prepare_command_mode_switch'
352401
if(positionCommandRunning(pre_joint_position_command_, joint_position_command_)) {
353-
// RCLCPP_WARN(rclcpp::get_logger("dsr_hw_interface2"), "Updated ");
354-
Drfl.set_safety_mode(SAFETY_MODE_AUTONOMOUS,SAFETY_MODE_EVENT_MOVE);
402+
if(true == idle) {
403+
// This is workaround to overcome issues :
404+
// move_joint (drfl) API internally sent safety_off right after moving.
405+
// which occurs problems like :
406+
// "move_joint service command -> trajectory command => error ! "
407+
Drfl.set_safety_mode(SAFETY_MODE_AUTONOMOUS,SAFETY_MODE_EVENT_MOVE);
408+
idle = false;
409+
}
410+
355411
float pos[6];
356-
float limitVel[6] = {100.0f, 100.0f, 100.0f, 100.0f, 100.0f, 100.0f};
357-
float limitAcc[6] = {100.0f, 100.0f, 100.0f, 100.0f, 100.0f, 100.0f};
412+
float targetVel[6];
358413
for(int i=0;i<6;i++) {
359-
pos[i] = static_cast<float>(joint_position_command_[i]* (180.0f / M_PI));
414+
pos[i] = static_cast<float>(joint_position_command_[i] * (180.0f / M_PI));
415+
targetVel[i] = static_cast<float>(joint_velocities_command_[i] * (180.0f / M_PI));
360416
}
361-
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] pos : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
362-
// ,pos[0] * ( M_PI / 180.0f)
363-
// ,pos[1] * ( M_PI / 180.0f)
364-
// ,pos[2] * ( M_PI / 180.0f)
365-
// ,pos[3] * ( M_PI / 180.0f)
366-
// ,pos[4] * ( M_PI / 180.0f)
367-
// ,pos[5] * ( M_PI / 180.0f)
368-
// );
369-
if(joint_position_commands.size() < 3) {
370-
joint_position_commands.emplace_back(std::vector<float> {pos[0],pos[1],pos[2],pos[3],pos[4],pos[5]});
371-
pre_joint_position_command_ = joint_position_command_;
372-
done = true;
373-
return return_type::OK;
417+
if(m_mode == "real") {
418+
float margin = 1.5; // Setted margin since most host aren't RT.
419+
float acc[6] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; // Complied with internal profile.
420+
Drfl.servoj_rt(pos, targetVel, acc, float(dt.seconds() * margin));
374421
}
375-
else{
376-
if(joint_position_commands.size() == 3 && done) {
377-
378-
for(auto cmd : joint_position_commands) {
379-
// RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] cmd : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
380-
// ,cmd[0] * ( M_PI / 180.0f)
381-
// ,cmd[1] * ( M_PI / 180.0f)
382-
// ,cmd[2] * ( M_PI / 180.0f)
383-
// ,cmd[3] * ( M_PI / 180.0f)
384-
// ,cmd[4] * ( M_PI / 180.0f)
385-
// ,cmd[5] * ( M_PI / 180.0f)
386-
// );
387-
if(m_nVersionDRCF >= 3000000) {
388-
Drfl.amovej(pos, limitVel, limitVel); // Workaround. needed updated.
389-
}
390-
else {
391-
// Drfl.servoj(cmd.data(), limitVel, limitAcc, float(dt.seconds())*1.7, DR_SERVO_OVERRIDE);
392-
Drfl.servoj(cmd.data(), limitVel, limitAcc, float(dt.seconds()), DR_SERVO_QUEUE);
393-
}
394-
this_thread::sleep_for(chrono::milliseconds(1));
395-
done = false;
396-
}
397-
}
398-
if(m_nVersionDRCF >= 3000000) {
399-
Drfl.amovej(pos, limitVel, limitVel); // Workaround. needed updated.
400-
}
401-
else {
402-
// Drfl.servoj(cmd.data(), limitVel, limitAcc, float(dt.seconds())*1.7, DR_SERVO_OVERRIDE);
403-
Drfl.servoj(pos, limitVel, limitAcc, float(dt.seconds()), DR_SERVO_QUEUE);
404-
}
422+
else { // "virtual"
423+
float target_vel_acc[6] = {70.0f, 70.0f, 70.0f, 70.0f, 70.0f, 70.0f};
424+
Drfl.amovej(pos, target_vel_acc, target_vel_acc); // Workaround. needed updated.
405425
}
406426
pre_joint_position_command_ = joint_position_command_;
407427
return return_type::OK;
408428
}
409-
410-
joint_position_commands.clear();
411-
done = false;
412-
// RCLCPP_WARN(rclcpp::get_logger("dsr_hw_interface2"), "NOT Updated ");
429+
idle = true;
413430
pre_joint_position_command_ = joint_position_command_;
414431
return return_type::OK;
415432
}
416433

417434
DRHWInterface::~DRHWInterface()
418435
{
419436
Drfl.close_connection();
437+
Drfl.disconnect_rt_control();
420438

421439
RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"),"_______________________________________________\n");
422440
RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2")," CONNECTION IS CLOSED");
423441
RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"),"_______________________________________________\n");
424442
}
425443

426-
}
444+
}
427445

428446

429447
const char* GetRobotStateString(int nState)

0 commit comments

Comments
 (0)