22#define ROBOTIQ_HANDE_DRIVER__COMMUNICATION_HPP_
33
44#include < modbus/modbus.h>
5+ #include < atomic>
56#include < cstring>
7+ #include < optional>
68#include < string>
9+ #include < thread>
710
811namespace robotiq_hande_driver {
912
@@ -74,6 +77,56 @@ class Communication {
7477 slave_id_ = slave_id;
7578 };
7679
80+ /* *
81+ * @brief Reads and writes input/output registers at once.
82+ *
83+ * @param none
84+ * @return None.
85+ * @note The status should be checked to verify successful execution. An exception is thrown if
86+ * communication issues occur.
87+ */
88+ void read_write_registers () {
89+ /* *
90+ * @brief Read and write modbus registers at once
91+ *
92+ * @param modbus_t *ctx
93+ * @param int write_addr
94+ * @param int write_nb
95+ * @param uint16_t *src
96+ * @param int read_addr
97+ * @param int read_nb
98+ * @param uint16_t *dest
99+ * @return int FAILURE_MODBUS on failure, nonnegative value for success
100+ * @note The status should be checked to verify successful execution. An exception is thrown
101+ * if communication issues occur.
102+ */
103+ // int result;
104+
105+ while (running) {
106+ // sleep for 100ms
107+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
108+
109+ for (size_t i = 0 ; i < static_cast <size_t >(OutputBytes::BYTES_MAX); ++i) {
110+ output_bytes_modbus_[i] = output_bytes_[i].load (std::memory_order_relaxed);
111+ }
112+
113+ modbus_write_and_read_registers (
114+ mb_,
115+ GRIPPER_INPUT_FIRST_REG,
116+ OUTPUT_REGISTER_WORD_LENGTH,
117+ reinterpret_cast <uint16_t *>(output_bytes_modbus_),
118+ GRIPPER_OUTPUT_FIRST_REG,
119+ INPUT_REGISTER_WORD_LENGTH,
120+ reinterpret_cast <uint16_t *>(input_bytes_modbus_));
121+
122+ for (size_t i = 0 ; i < static_cast <size_t >(InputBytes::BYTES_MAX); ++i) {
123+ input_bytes_[i].store (input_bytes_modbus_[i], std::memory_order_relaxed);
124+ }
125+ }
126+
127+ // return result;
128+ };
129+
77130 /* *
78131 * @brief Initializes communication layer.
79132 *
@@ -90,6 +143,8 @@ class Communication {
90143 modbus_set_debug (mb_, DEBUG_MODBUS);
91144 result = connect ();
92145
146+ my_thread.emplace (&Communication::read_write_registers, this );
147+
93148 return result;
94149 };
95150
@@ -102,6 +157,10 @@ class Communication {
102157 * communication issues occur.
103158 */
104159 void cleanup () {
160+ if (my_thread && my_thread->joinable ()) {
161+ my_thread->join ();
162+ }
163+
105164 disconnect ();
106165 modbus_free (mb_);
107166 };
@@ -128,43 +187,6 @@ class Communication {
128187 modbus_close (mb_);
129188 };
130189
131- /* *
132- * @brief Reads and writes input/output registers at once.
133- *
134- * @param none
135- * @return None.
136- * @note The status should be checked to verify successful execution. An exception is thrown if
137- * communication issues occur.
138- */
139- int read_write_registers () {
140- /* *
141- * @brief Read and write modbus registers at once
142- *
143- * @param modbus_t *ctx
144- * @param int write_addr
145- * @param int write_nb
146- * @param uint16_t *src
147- * @param int read_addr
148- * @param int read_nb
149- * @param uint16_t *dest
150- * @return int FAILURE_MODBUS on failure, nonnegative value for success
151- * @note The status should be checked to verify successful execution. An exception is thrown
152- * if communication issues occur.
153- */
154- int result;
155-
156- result = modbus_write_and_read_registers (
157- mb_,
158- GRIPPER_INPUT_FIRST_REG,
159- OUTPUT_REGISTER_WORD_LENGTH,
160- reinterpret_cast <uint16_t *>(output_bytes_),
161- GRIPPER_OUTPUT_FIRST_REG,
162- INPUT_REGISTER_WORD_LENGTH,
163- reinterpret_cast <uint16_t *>(input_bytes_));
164-
165- return result;
166- };
167-
168190 /* *
169191 * @brief Sets output bytes to zeros.
170192 *
@@ -174,7 +196,10 @@ class Communication {
174196 * communication issues occur.
175197 */
176198 void clear_output_bytes () {
177- memset (output_bytes_, 0 , sizeof (output_bytes_));
199+ // memset(output_bytes_, 0, sizeof(output_bytes_));
200+ for (size_t i = 0 ; i < static_cast <size_t >(OutputBytes::BYTES_MAX); ++i) {
201+ output_bytes_[i].store (0 , std::memory_order_relaxed);
202+ }
178203 };
179204
180205 /* *
@@ -186,7 +211,7 @@ class Communication {
186211 * communication issues occur.
187212 */
188213 uint8_t get_input_byte (InputBytes index) {
189- return input_bytes_[static_cast <uint>(index)];
214+ return input_bytes_[static_cast <uint>(index)]. load (std::memory_order_relaxed) ;
190215 };
191216
192217 /* *
@@ -199,7 +224,7 @@ class Communication {
199224 * communication issues occur.
200225 */
201226 void set_output_byte (OutputBytes index, uint8_t value) {
202- output_bytes_[static_cast <uint>(index)] = value;
227+ output_bytes_[static_cast <uint>(index)]. store ( value, std::memory_order_relaxed) ;
203228 };
204229
205230 /* *
@@ -210,8 +235,13 @@ class Communication {
210235 * @return None.
211236 */
212237 void write_action_bit (uint8_t position_bit, bool value) {
213- output_bytes_[static_cast <uint>(OutputBytes::ACTION_REQUEST)] = bit_set_to (
214- output_bytes_[static_cast <uint>(OutputBytes::ACTION_REQUEST)], position_bit, value);
238+ output_bytes_[static_cast <uint>(OutputBytes::ACTION_REQUEST)].store (
239+ bit_set_to (
240+ output_bytes_[static_cast <uint>(OutputBytes::ACTION_REQUEST)].load (
241+ std::memory_order_relaxed),
242+ position_bit,
243+ value),
244+ std::memory_order_relaxed);
215245 };
216246
217247 /* *
@@ -238,8 +268,14 @@ class Communication {
238268
239269 modbus_t * mb_;
240270
241- uint8_t input_bytes_[static_cast <size_t >(InputBytes::BYTES_MAX)];
242- uint8_t output_bytes_[static_cast <size_t >(OutputBytes::BYTES_MAX)];
271+ uint8_t input_bytes_modbus_[static_cast <size_t >(InputBytes::BYTES_MAX)];
272+ uint8_t output_bytes_modbus_[static_cast <size_t >(OutputBytes::BYTES_MAX)];
273+
274+ std::atomic<uint8_t > input_bytes_[static_cast <size_t >(InputBytes::BYTES_MAX)];
275+ std::atomic<uint8_t > output_bytes_[static_cast <size_t >(OutputBytes::BYTES_MAX)];
276+
277+ std::atomic<bool > running{true };
278+ std::optional<std::thread> my_thread;
243279};
244280} // namespace robotiq_hande_driver
245281#endif // ROBOTIQ_HANDE_DRIVER__COMMUNICATION_HPP_
0 commit comments