Skip to content

Commit 00fa969

Browse files
authored
Merge pull request #11 from AGH-CEAI/feature/connection_error_handling
ADD: reconnecting to modbus on configure
2 parents becf2c1 + b2bdcd2 commit 00fa969

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

robotiq_hande_driver/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* [PR-11](https://github.com/AGH-CEAI/robotiq_hande_driver/pull/11) - Added automatic reconnection to Modbus during configuration.
1213
* [PR-7](https://github.com/AGH-CEAI/robotiq_hande_driver/pull/7) - Integration of ModbusRTU communication with ros2_control Hardware Interface
1314
* [PR-2](https://github.com/AGH-CEAI/robotiq_hande_driver/pull/2) - ModbusRTU communication
1415
* [PR-2](https://github.com/AGH-CEAI/robotiq_hande_driver/pull/2) - Protocol abstraction
@@ -22,4 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2223

2324
### Fixed
2425

26+
* [PR-11](https://github.com/AGH-CEAI/robotiq_hande_driver/pull/11) - Fixed an issue where the Modbus connection check always failed.
27+
2528
### Security

robotiq_hande_driver/hardware/include/robotiq_hande_driver/communication.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class Communication {
136136
* @note The status should be checked to verify successful execution. An exception is thrown if
137137
* communication issues occur.
138138
*/
139-
void read_write_registers() {
139+
int read_write_registers() {
140140
/**
141141
* @brief Read and write modbus registers at once
142142
*
@@ -147,18 +147,22 @@ class Communication {
147147
* @param int read_addr
148148
* @param int read_nb
149149
* @param uint16_t *dest
150-
* @return int >0 on success
150+
* @return int FAILURE_MODBUS on failure, nonnegative value for success
151151
* @note The status should be checked to verify successful execution. An exception is thrown
152152
* if communication issues occur.
153153
*/
154-
modbus_write_and_read_registers(
154+
int result;
155+
156+
result = modbus_write_and_read_registers(
155157
mb_,
156158
GRIPPER_INPUT_FIRST_REG,
157159
OUTPUT_REGISTER_WORD_LENGTH,
158160
reinterpret_cast<uint16_t*>(output_bytes_),
159161
GRIPPER_OUTPUT_FIRST_REG,
160162
INPUT_REGISTER_WORD_LENGTH,
161163
reinterpret_cast<uint16_t*>(input_bytes_));
164+
165+
return result;
162166
};
163167

164168
/**

robotiq_hande_driver/hardware/src/communication.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ namespace robotiq_hande_driver {
77
Communication::Communication() : input_bytes_{}, output_bytes_{} {}
88

99
int Communication::connect() {
10-
uint16_t activation_status[1] = {0x0000};
1110
int result;
1211

1312
modbus_connect(mb_);
1413

15-
result = modbus_read_registers(mb_, GRIPPER_OUTPUT_FIRST_REG, 1, activation_status);
14+
result = read_write_registers();
1615

1716
return result;
1817
}

robotiq_hande_driver/hardware/src/hande_hardware_interface.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace robotiq_hande_driver {
77

88
static constexpr auto THROTTLE_1000_MS = 1000;
99
static constexpr auto ACTIVATION_MAX_ITER = 100;
10+
static constexpr auto RECONNECT_MAX_ITER = 10;
1011
inline void wait_100ms() {
1112
usleep(100 * 1000);
1213
}
@@ -70,24 +71,37 @@ HWI::CallbackReturn RobotiqHandeHardwareInterface::on_init(const HWI::HardwareIn
7071

7172
HWI::CallbackReturn RobotiqHandeHardwareInterface::on_configure(
7273
const rlccp_lc::State& /*previous_state*/) {
73-
int result;
74+
int result = FAILURE_MODBUS;
7475

75-
RCLCPP_INFO(
76+
RCLCPP_INFO(get_logger(), "Connecting to ModbusRTU");
77+
RCLCPP_DEBUG(
7678
get_logger(),
7779
"Connecting to tty:%s, baudrate:%d, parity:%c, data_bits:%d, stop_bit:%d",
7880
tty_port_.c_str(),
7981
baudrate_,
8082
parity_,
8183
data_bits_,
8284
stop_bit_);
85+
8386
result = gripper_driver_.configure();
8487

85-
// TODO(issue#10) Modbus connection check always fails
86-
// if(result == FAILURE_MODBUS) {
87-
// RCLCPP_INFO(get_logger(), "Failed to configure Hand-E Gripper");
88-
// return HWI::CallbackReturn::FAILURE;
89-
// }
90-
RCLCPP_INFO(get_logger(), "Configured Hand-E Gripper: %d", result);
88+
for(int iter = 0; result == FAILURE_MODBUS && iter < RECONNECT_MAX_ITER; iter++) {
89+
RCLCPP_DEBUG(
90+
get_logger(), "Reconfiguring Hand-E Gripper attempt: %d; result: %d", iter, result);
91+
92+
wait_100ms();
93+
wait_100ms();
94+
95+
gripper_driver_.cleanup();
96+
result = gripper_driver_.configure();
97+
}
98+
99+
if(result == FAILURE_MODBUS) {
100+
RCLCPP_INFO(get_logger(), "Failed to configure Hand-E Gripper");
101+
return HWI::CallbackReturn::FAILURE;
102+
}
103+
104+
RCLCPP_INFO(get_logger(), "Connected");
91105
return HWI::CallbackReturn::SUCCESS;
92106
}
93107

0 commit comments

Comments
 (0)