Skip to content

Commit abbbec3

Browse files
committed
- Direct Control Mode bug fixed.
1 parent c8abeff commit abbbec3

File tree

1 file changed

+71
-4
lines changed

1 file changed

+71
-4
lines changed

robotis_controller/src/robotis_controller/robotis_controller.cpp

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,14 +1109,64 @@ void RobotisController::process()
11091109
{
11101110
if(gazebo_mode_ == false)
11111111
{
1112-
queue_mutex_.lock();
1112+
// BulkRead Rx
1113+
for (auto& it : port_to_bulk_read_)
1114+
{
1115+
robot_->ports_[it.first]->setPacketTimeout(0.0);
1116+
it.second->rxPacket();
1117+
}
11131118

1114-
for (auto& it : port_to_sync_write_position_)
1119+
// -> save to robot->dxls_[]->dxl_state_
1120+
if (robot_->dxls_.size() > 0)
11151121
{
1116-
it.second->txPacket();
1117-
it.second->clearParam();
1122+
for (auto& dxl_it : robot_->dxls_)
1123+
{
1124+
Dynamixel *dxl = dxl_it.second;
1125+
std::string port_name = dxl_it.second->port_name_;
1126+
std::string joint_name = dxl_it.first;
1127+
1128+
if (dxl->bulk_read_items_.size() > 0)
1129+
{
1130+
uint32_t data = 0;
1131+
for (int i = 0; i < dxl->bulk_read_items_.size(); i++)
1132+
{
1133+
ControlTableItem *item = dxl->bulk_read_items_[i];
1134+
if (port_to_bulk_read_[port_name]->isAvailable(dxl->id_, item->address_, item->data_length_) == true)
1135+
{
1136+
data = port_to_bulk_read_[port_name]->getData(dxl->id_, item->address_, item->data_length_);
1137+
1138+
// change dxl_state
1139+
if (dxl->present_position_item_ != 0 && item->item_name_ == dxl->present_position_item_->item_name_)
1140+
dxl->dxl_state_->present_position_ = dxl->convertValue2Radian(data) - dxl->dxl_state_->position_offset_; // remove offset
1141+
else if (dxl->present_velocity_item_ != 0 && item->item_name_ == dxl->present_velocity_item_->item_name_)
1142+
dxl->dxl_state_->present_velocity_ = dxl->convertValue2Velocity(data);
1143+
else if (dxl->present_current_item_ != 0 && item->item_name_ == dxl->present_current_item_->item_name_)
1144+
dxl->dxl_state_->present_torque_ = dxl->convertValue2Torque(data);
1145+
else if (dxl->goal_position_item_ != 0 && item->item_name_ == dxl->goal_position_item_->item_name_)
1146+
dxl->dxl_state_->goal_position_ = dxl->convertValue2Radian(data) - dxl->dxl_state_->position_offset_; // remove offset
1147+
else if (dxl->goal_velocity_item_ != 0 && item->item_name_ == dxl->goal_velocity_item_->item_name_)
1148+
dxl->dxl_state_->goal_velocity_ = dxl->convertValue2Velocity(data);
1149+
else if (dxl->goal_current_item_ != 0 && item->item_name_ == dxl->goal_current_item_->item_name_)
1150+
dxl->dxl_state_->goal_torque_ = dxl->convertValue2Torque(data);
1151+
else
1152+
dxl->dxl_state_->bulk_read_table_[item->item_name_] = data;
1153+
}
1154+
}
1155+
1156+
// -> update time stamp to Robot->dxls[]->dynamixel_state.update_time_stamp
1157+
dxl->dxl_state_->update_time_stamp_ = TimeStamp(present_state.header.stamp.sec, present_state.header.stamp.nsec);
1158+
}
1159+
}
11181160
}
11191161

1162+
queue_mutex_.lock();
1163+
1164+
// for (auto& it : port_to_sync_write_position_)
1165+
// {
1166+
// it.second->txPacket();
1167+
// it.second->clearParam();
1168+
// }
1169+
11201170
if (direct_sync_write_.size() > 0)
11211171
{
11221172
for (int i = 0; i < direct_sync_write_.size(); i++)
@@ -1128,6 +1178,10 @@ void RobotisController::process()
11281178
}
11291179

11301180
queue_mutex_.unlock();
1181+
1182+
// BulkRead Tx
1183+
for (auto& it : port_to_bulk_read_)
1184+
it.second->txPacket();
11311185
}
11321186
}
11331187

@@ -1553,9 +1607,22 @@ void RobotisController::syncWriteItemCallback(const robotis_controller_msgs::Syn
15531607
void RobotisController::setControllerModeCallback(const std_msgs::String::ConstPtr &msg)
15541608
{
15551609
if (msg->data == "DirectControlMode")
1610+
{
1611+
for (auto& it : port_to_bulk_read_)
1612+
{
1613+
robot_->ports_[it.first]->setPacketTimeout(0.0);
1614+
it.second->rxPacket();
1615+
}
15561616
controller_mode_ = DirectControlMode;
1617+
}
15571618
else if (msg->data == "MotionModuleMode")
1619+
{
1620+
for (auto& it : port_to_bulk_read_)
1621+
{
1622+
it.second->txPacket();
1623+
}
15581624
controller_mode_ = MotionModuleMode;
1625+
}
15591626
}
15601627

15611628
void RobotisController::setJointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)

0 commit comments

Comments
 (0)