@@ -105,6 +105,8 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
105
105
dxl_id_.push_back (static_cast <uint8_t >(stoi (gpio.parameters .at (" ID" ))));
106
106
} else if (gpio.parameters .at (" type" ) == " sensor" ) {
107
107
sensor_id_.push_back (static_cast <uint8_t >(stoi (gpio.parameters .at (" ID" ))));
108
+ } else if (gpio.parameters .at (" type" ) == " controller" ) {
109
+ controller_id_.push_back (static_cast <uint8_t >(stoi (gpio.parameters .at (" ID" ))));
108
110
} else {
109
111
RCLCPP_ERROR_STREAM (logger_, " Invalid DXL / Sensor type" );
110
112
exit (-1 );
@@ -115,6 +117,35 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
115
117
int trying_cnt = 60 ;
116
118
int cnt = 0 ;
117
119
120
+ if (controller_id_.size () > 0 ) {
121
+ while (trying_connect) {
122
+ std::vector<uint8_t > id_arr;
123
+ for (auto controller : controller_id_) {
124
+ id_arr.push_back (controller);
125
+ }
126
+ if (dxl_comm_->InitDxlComm (id_arr, port_name_, baud_rate_) == DxlError::OK) {
127
+ RCLCPP_INFO_STREAM (logger_, " Trying to connect to the communication port..." );
128
+ trying_connect = false ;
129
+ } else {
130
+ sleep (1 );
131
+ cnt++;
132
+ if (cnt > trying_cnt) {
133
+ RCLCPP_ERROR_STREAM (logger_, " Cannot connect communication port! :(" );
134
+ cnt = 0 ;
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ if (!InitControllerItems ()) {
141
+ RCLCPP_ERROR_STREAM (logger_, " Error: InitControllerItems" );
142
+ return hardware_interface::CallbackReturn::ERROR;
143
+ }
144
+
145
+ trying_connect = true ;
146
+ trying_cnt = 60 ;
147
+ cnt = 0 ;
148
+
118
149
while (trying_connect) {
119
150
std::vector<uint8_t > id_arr;
120
151
for (auto dxl : dxl_id_) {
@@ -566,6 +597,7 @@ bool DynamixelHardware::CommReset()
566
597
usleep (200 * 1000 );
567
598
}
568
599
if (!result) {continue ;}
600
+ if (!InitControllerItems ()) {continue ;}
569
601
if (!InitDxlItems ()) {continue ;}
570
602
if (!InitDxlReadItems ()) {continue ;}
571
603
if (!InitDxlWriteItems ()) {continue ;}
@@ -582,48 +614,71 @@ bool DynamixelHardware::CommReset()
582
614
return false ;
583
615
}
584
616
585
- bool DynamixelHardware::InitDxlItems ()
586
- {
587
- RCLCPP_INFO_STREAM (logger_, " $$$$$ Init Dxl Items" );
617
+ bool DynamixelHardware::retryWriteItem (uint8_t id, const std::string& item_name, uint32_t value) {
618
+ rclcpp::Time start_time = this ->now ();
619
+ rclcpp::Duration error_duration (0 , 0 );
620
+
621
+ while (true ) {
622
+ if (dxl_comm_->WriteItem (id, item_name, value) == DxlError::OK) {
623
+ RCLCPP_INFO_STREAM (
624
+ logger_,
625
+ " [ID:" << std::to_string (id) << " ] item_name:" << item_name.c_str () << " \t data:" << value);
626
+ return true ;
627
+ }
628
+
629
+ error_duration = this ->now () - start_time;
630
+ if (error_duration.seconds () * 1000 >= err_timeout_ms_) {
631
+ RCLCPP_ERROR_STREAM (
632
+ logger_,
633
+ " [ID:" << std::to_string (id) << " ] Write Item error (Timeout: " <<
634
+ error_duration.seconds () * 1000 << " ms/" << err_timeout_ms_ << " ms)" );
635
+ return false ;
636
+ }
637
+ RCLCPP_WARN_STREAM (
638
+ logger_,
639
+ " [ID:" << std::to_string (id) << " ] Write Item retry..." );
640
+ }
641
+ }
642
+
643
+ bool DynamixelHardware::initItems (const std::string& type_filter) {
644
+ RCLCPP_INFO_STREAM (logger_, " $$$$$ Init Items for type: " << type_filter);
588
645
for (const hardware_interface::ComponentInfo & gpio : info_.gpios ) {
646
+ if (gpio.parameters .at (" type" ) != type_filter) {
647
+ continue ;
648
+ }
589
649
uint8_t id = static_cast <uint8_t >(stoi (gpio.parameters .at (" ID" )));
650
+
590
651
// First write items containing "Limit"
591
652
for (auto it : gpio.parameters ) {
592
653
if (it.first != " ID" && it.first != " type" && it.first .find (" Limit" ) != std::string::npos) {
593
- if (dxl_comm_->WriteItem (
594
- id, it.first ,
595
- static_cast <uint32_t >(stoi (it.second ))) != DxlError::OK)
596
- {
597
- RCLCPP_ERROR_STREAM (logger_, " [ID:" << std::to_string (id) << " ] Write Item error" );
654
+ if (!retryWriteItem (id, it.first , static_cast <uint32_t >(stoi (it.second )))) {
598
655
return false ;
599
656
}
600
- RCLCPP_INFO_STREAM (
601
- logger_,
602
- " [ID:" << std::to_string (id) << " ] item_name:" << it.first .c_str () << " \t data:" <<
603
- stoi (it.second ));
604
657
}
605
658
}
606
659
607
660
// Then write the remaining items
608
661
for (auto it : gpio.parameters ) {
609
662
if (it.first != " ID" && it.first != " type" && it.first .find (" Limit" ) == std::string::npos) {
610
- if (dxl_comm_->WriteItem (
611
- id, it.first ,
612
- static_cast <uint32_t >(stoi (it.second ))) != DxlError::OK)
613
- {
614
- RCLCPP_ERROR_STREAM (logger_, " [ID:" << std::to_string (id) << " ] Write Item error" );
663
+ if (!retryWriteItem (id, it.first , static_cast <uint32_t >(stoi (it.second )))) {
615
664
return false ;
616
665
}
617
- RCLCPP_INFO_STREAM (
618
- logger_,
619
- " [ID:" << std::to_string (id) << " ] item_name:" << it.first .c_str () << " \t data:" <<
620
- stoi (it.second ));
621
666
}
622
667
}
623
668
}
624
669
return true ;
625
670
}
626
671
672
+ bool DynamixelHardware::InitControllerItems ()
673
+ {
674
+ return initItems (" controller" );
675
+ }
676
+
677
+ bool DynamixelHardware::InitDxlItems ()
678
+ {
679
+ return initItems (" dxl" ) && initItems (" sensor" );
680
+ }
681
+
627
682
bool DynamixelHardware::InitDxlReadItems ()
628
683
{
629
684
RCLCPP_INFO_STREAM (logger_, " $$$$$ Init Dxl Read Items" );
0 commit comments