@@ -603,7 +603,7 @@ void RobotisController::initializeDevice(const std::string init_file_path)
603
603
604
604
void RobotisController::gazeboTimerThread ()
605
605
{
606
- ros::Rate gazebo_rate (1000 / CONTROL_CYCLE_MSEC );
606
+ ros::Rate gazebo_rate (1000 / robot_-> getControlCycle () );
607
607
608
608
while (!stop_timer_)
609
609
{
@@ -662,7 +662,7 @@ void RobotisController::msgQueueThread()
662
662
ros::ServiceServer joint_module_server = ros_node.advertiseService (" /robotis/get_present_joint_ctrl_modules" ,
663
663
&RobotisController::getCtrlModuleCallback, this );
664
664
665
- ros::WallDuration duration (CONTROL_CYCLE_MSEC / 1000.0 );
665
+ ros::WallDuration duration (robot_-> getControlCycle () / 1000.0 );
666
666
while (ros_node.ok ())
667
667
callback_queue.callAvailable (duration);
668
668
}
@@ -679,8 +679,8 @@ void *RobotisController::timerThread(void *param)
679
679
680
680
while (!controller->stop_timer_ )
681
681
{
682
- next_time.tv_sec += (next_time.tv_nsec + CONTROL_CYCLE_MSEC * 1000000 ) / 1000000000 ;
683
- next_time.tv_nsec = (next_time.tv_nsec + CONTROL_CYCLE_MSEC * 1000000 ) % 1000000000 ;
682
+ next_time.tv_sec += (next_time.tv_nsec + controller-> robot_ -> getControlCycle () * 1000000 ) / 1000000000 ;
683
+ next_time.tv_nsec = (next_time.tv_nsec + controller-> robot_ -> getControlCycle () * 1000000 ) % 1000000000 ;
684
684
685
685
controller->process ();
686
686
@@ -1109,14 +1109,64 @@ void RobotisController::process()
1109
1109
{
1110
1110
if (gazebo_mode_ == false )
1111
1111
{
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
+ }
1113
1118
1114
- for (auto & it : port_to_sync_write_position_)
1119
+ // -> save to robot->dxls_[]->dxl_state_
1120
+ if (robot_->dxls_ .size () > 0 )
1115
1121
{
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
+ }
1118
1160
}
1119
1161
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
+
1120
1170
if (direct_sync_write_.size () > 0 )
1121
1171
{
1122
1172
for (int i = 0 ; i < direct_sync_write_.size (); i++)
@@ -1128,6 +1178,10 @@ void RobotisController::process()
1128
1178
}
1129
1179
1130
1180
queue_mutex_.unlock ();
1181
+
1182
+ // BulkRead Tx
1183
+ for (auto & it : port_to_bulk_read_)
1184
+ it.second ->txPacket ();
1131
1185
}
1132
1186
}
1133
1187
@@ -1375,7 +1429,7 @@ void RobotisController::addMotionModule(MotionModule *module)
1375
1429
}
1376
1430
}
1377
1431
1378
- module ->initialize (CONTROL_CYCLE_MSEC , robot_);
1432
+ module ->initialize (robot_-> getControlCycle () , robot_);
1379
1433
motion_modules_.push_back (module );
1380
1434
motion_modules_.unique ();
1381
1435
}
@@ -1397,7 +1451,7 @@ void RobotisController::addSensorModule(SensorModule *module)
1397
1451
}
1398
1452
}
1399
1453
1400
- module ->initialize (CONTROL_CYCLE_MSEC , robot_);
1454
+ module ->initialize (robot_-> getControlCycle () , robot_);
1401
1455
sensor_modules_.push_back (module );
1402
1456
sensor_modules_.unique ();
1403
1457
}
@@ -1553,9 +1607,22 @@ void RobotisController::syncWriteItemCallback(const robotis_controller_msgs::Syn
1553
1607
void RobotisController::setControllerModeCallback (const std_msgs::String::ConstPtr &msg)
1554
1608
{
1555
1609
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
+ }
1556
1616
controller_mode_ = DirectControlMode;
1617
+ }
1557
1618
else if (msg->data == " MotionModuleMode" )
1619
+ {
1620
+ for (auto & it : port_to_bulk_read_)
1621
+ {
1622
+ it.second ->txPacket ();
1623
+ }
1558
1624
controller_mode_ = MotionModuleMode;
1625
+ }
1559
1626
}
1560
1627
1561
1628
void RobotisController::setJointStatesCallback (const sensor_msgs::JointState::ConstPtr &msg)
@@ -1664,7 +1731,7 @@ void RobotisController::setJointCtrlModuleThread(const robotis_controller_msgs::
1664
1731
for (std::list<MotionModule *>::iterator _stop_m_it = _stop_modules.begin (); _stop_m_it != _stop_modules.end (); _stop_m_it++)
1665
1732
{
1666
1733
while ((*_stop_m_it)->isRunning ())
1667
- usleep (CONTROL_CYCLE_MSEC * 1000 );
1734
+ usleep (robot_-> getControlCycle () * 1000 );
1668
1735
}
1669
1736
1670
1737
// disable module(s)
@@ -1879,7 +1946,7 @@ void RobotisController::setCtrlModuleThread(std::string ctrl_module)
1879
1946
for (auto stop_m_it = stop_modules.begin (); stop_m_it != stop_modules.end (); stop_m_it++)
1880
1947
{
1881
1948
while ((*stop_m_it)->isRunning ())
1882
- usleep (CONTROL_CYCLE_MSEC * 1000 );
1949
+ usleep (robot_-> getControlCycle () * 1000 );
1883
1950
}
1884
1951
1885
1952
// disable module(s)
0 commit comments