Skip to content

Commit e697b6d

Browse files
committed
ADD: full communication layer to modbus
1 parent 36d378e commit e697b6d

File tree

9 files changed

+258
-132
lines changed

9 files changed

+258
-132
lines changed

robotiq_hande_driver/CMakeLists.txt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
66
endif()
77

88
find_package(ament_cmake REQUIRED)
9-
find_package(serial REQUIRED)
10-
11-
add_library(
12-
include/application.hpp
13-
include/protocol_logic.hpp
14-
include/communication.hpp
15-
src/application.cpp
16-
src/protocol_logic.cpp
17-
src/communication.cpp
9+
10+
# add_library(
11+
# libmodbus
12+
# )
13+
14+
include_directories(
15+
"include"
16+
"src"
1817
)
1918

19+
add_executable(communication src/communication.cpp)
20+
add_executable(protocol_logic src/protocol_logic.cpp)
21+
add_executable(application src/application.cpp)
2022
add_executable(hande_driver src/hande_driver.cpp)
23+
2124
target_include_directories(hande_driver PUBLIC
2225
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
2326
$<INSTALL_INTERFACE:include>)

robotiq_hande_driver/include/application.hpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@
88

99
namespace hande_driver
1010
{
11-
struct {
12-
bool isReset;
13-
bool isReady;
14-
bool isMoving;
15-
bool isStopped;
16-
bool isOpened;
17-
bool isClosed;
18-
bool objectDetected;
19-
} Status;
20-
21-
struct { // Structure declaration
22-
int myNum; // Member (int variable)
23-
string myString; // Member (string variable)
24-
} FaultStatus;
2511

2612
class ApplicationLayer{
27-
public:
13+
public:
14+
15+
struct Status{
16+
bool isReset;
17+
bool isReady;
18+
bool isMoving;
19+
bool isStopped;
20+
bool isOpened;
21+
bool isClosed;
22+
bool objectDetected;
23+
};
24+
25+
struct FaultStatus{
26+
bool isError;
27+
};
28+
2829
ApplicationLayer();
2930

3031
~ApplicationLayer();
@@ -90,7 +91,7 @@ class ApplicationLayer{
9091
* @return none
9192
* @note see status on success, exception thrown if communicatoin issues
9293
*/
93-
void update_status()
94+
void update_status();
9495

9596
/**
9697
* @brief Returns the gripper status
@@ -135,7 +136,7 @@ class ApplicationLayer{
135136
* @return none
136137
* @note see status on success, exception thrown if communicatoin issues
137138
*/
138-
void ApplicationLayer::set_position(uint8_t position)
139+
void set_position(uint8_t position);
139140

140141
/**
141142
* @brief Returns the gripper current
@@ -146,6 +147,11 @@ class ApplicationLayer{
146147
*/
147148
uint16_t current();
148149

150+
151+
void read();
152+
153+
void write();
154+
149155
private:
150156
/**
151157
* Protocol logic, used for middle level abstraction
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include <stdint.h>
22
#include <stddef.h>
33
#include <vector>
4-
#include <serial.h>
4+
#include <modbus/modbus.h>
55

66
/**
77
* @brief This class contains low level gripper commands and status
88
*/
99

1010
namespace hande_driver
1111
{
12+
constexpr auto registerWordLength = 3;
1213

1314
class Communication{
1415
public:
@@ -35,37 +36,19 @@ class Communication{
3536
void disconnect();
3637

3738
/**
38-
* @brief Send command to gripper
39+
* @brief Read and write input.output registers at once
3940
*
40-
* @param data Data sent to gripper
41-
* @param resp_data_len Expected data length of received response
42-
* @return none
43-
* @note see status on success, exception thrown in case of communication issues
44-
*/
45-
void send_command(const std::vector<uint8_t>& data, size_t resp_data_len);
46-
47-
/**
48-
* @brief Get status frame from gripper
49-
*
50-
* @param data_len Data length to read
51-
* @return none
52-
* @note see status on success, exception thrown in case of communication issues
53-
*/
54-
void get_status(size_t data_len);
55-
56-
/**
57-
* @brief Set callback to be triggered by a timer to update the gripper status
58-
* TODO: make a thread?
59-
*
60-
* @param freq Refresh rate frequency, no higher than 200Hz including commands
41+
* @param none
6142
* @return none
6243
* @note see status on success, exception thrown in case of communication issues
6344
*/
64-
void arm_get_status_callback(uint16_t freq);
45+
void read_write_registers();
6546

66-
protected:
67-
serial::Serial serial_;
47+
uint16_t input_registers[registerWordLength];
48+
uint16_t output_registers[registerWordLength];
6849

6950
private:
51+
modbus_t *mb;
52+
7053
};
71-
} // namespace hande_driver
54+
} // namespace hande_driver

robotiq_hande_driver/include/protocol_logic.hpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace hande_driver
1010
{
1111

1212
// Action Request
13+
constexpr uint8_t actionRequestByte = 0;
1314
constexpr uint8_t activatePositionByte = 0; // rACT
1415
enum Activate{
1516
DEACTIVATE_GRIPPER,
@@ -35,26 +36,30 @@ enum AutoReleaseDirection{
3536
};
3637

3738
//Position Request
39+
constexpr uint8_t positionRequestByte = 3;
3840
// 0x00 - Open position, with 50 mm opening
3941
// 0xFF - Closed
4042
// Opening / count: ≈0.2 mm for 50 mm stroke
4143

4244
//Speed Request
45+
constexpr uint8_t speedRequestByte = 4;
4346
// 0x00 - Minimum speed
4447
// 0xFF - Maximum speed
4548

4649
//Force Request
50+
constexpr uint8_t forceRequestByte = 5;
4751
// 0x00 - Minimum force
4852
// 0xFF - Maximum force
4953

5054
// Gripper Status
55+
constexpr uint8_t statusByte = 0;
5156
constexpr uint8_t activationStatusPositionByte = 0; // gACT
5257
enum ActivationStatus{
5358
GRIPPER_RESET,
5459
GRIPPER_ACTIVATION
5560
};
5661

57-
constexpr uint8_t goToStatusPositionByte = 3; // gGTO
62+
constexpr uint8_t actionStatusPositionByte = 3; // gGTO
5863
enum ActionStatus{
5964
STOPPED,
6065
GO_TO_POSITION_REQUEST
@@ -77,10 +82,13 @@ enum ObjectDetectionStatus{
7782
};
7883

7984
// Fault Status
85+
constexpr uint8_t faultStatusByte = 2;
8086
// Position Request Echo
87+
constexpr uint8_t positionRequestEchoByte = 3;
8188
// Position (current)
89+
constexpr uint8_t positionByte = 4;
8290
// Current
83-
91+
constexpr uint8_t currentByte = 5;
8492

8593
class ProtocolLogic{
8694
public:
@@ -89,14 +97,23 @@ class ProtocolLogic{
8997
~ProtocolLogic();
9098

9199
/**
92-
* @brief Resets the gripper: deactivate and activate again
100+
* @brief Resets the gripper
93101
*
94102
* @param none
95103
* @return none
96104
* @note see status on success, exception thrown if communicatoin issues
97105
*/
98106
void reset();
99107

108+
/**
109+
* @brief Sets the gripper
110+
*
111+
* @param none
112+
* @return none
113+
* @note see status on success, exception thrown if communicatoin issues
114+
*/
115+
void set();
116+
100117
/**
101118
* @brief Emergency auto-release, gripper fingers are slowly opened, reactivation necessary
102119
*
@@ -124,7 +141,7 @@ class ProtocolLogic{
124141
* @return none
125142
* @note see status on success, exception thrown if communicatoin issues
126143
*/
127-
void go_to(uint8_t position, uint8_t velocity, uint8_t force, bool arm_callback=true);
144+
void go_to(uint8_t position, uint8_t velocity, uint8_t force);
128145

129146
/**
130147
* @brief Stops the gripper
@@ -205,35 +222,48 @@ class ProtocolLogic{
205222
uint8_t get_current();
206223

207224
/**
208-
* @brief Arm a callback to be triggered when gripper starts moving
225+
* @brief Decode modbus registers and refresh apropriate data
209226
*
210227
* @return none
211228
*/
212-
void wait_until_moving();
229+
void refresh_registers();
213230

214231
/**
215-
* @brief Arm a callback to be triggered when gripper stops moving
232+
* @brief Read 8bit registers from 16bit words
216233
*
234+
* @param reg pointer to 8bit register
235+
* @param byte 8bit register position in 16bit modbus frame
217236
* @return none
218237
*/
219-
void wait_until_stopped();
238+
void read_register(uint8_t &reg, uint8_t byte);
239+
240+
/**
241+
* @brief Set n-th bit to x value
242+
*
243+
* @param number
244+
* @param n n-th bit in number
245+
* @param x bool value
246+
* @return uint number with n-th bit set to x
247+
*/
248+
uint bit_set_to(uint number, uint n, bool x);
220249

221250
private:
222251
// Gripper
252+
uint8_t status_;
223253
ActivationStatus activation_status_;
224254
ActionStatus action_status_;
225255
GripperStatus gripper_status_;
226256
ObjectDetectionStatus object_detection_status_;
227257
//Fault
228-
258+
uint8_t fault_status;
229259

230260
/**
231-
* @brief Requested position in normalized 0-225 value
261+
* @brief Requested position in normalized 0-255 value
232262
*/
233-
uint8_t position_request;
263+
uint8_t position_request_echo;
234264

235265
/**
236-
* @brief Position in normalized 0-225 value
266+
* @brief Position in normalized 0-255 value
237267
*/
238268
uint8_t position;
239269

0 commit comments

Comments
 (0)