22#include < unistd.h>
33#include < modbus/modbus.h>
44
5- constexpr auto REGISTER_READ_LENGTH = 32 ;
5+ constexpr auto REGISTER_READ_LENGTH = 6 ;
66constexpr auto DEVICE_NAME = " /tmp/ttyUR" ;
77constexpr auto BAUDRATE = 115200 ;
88constexpr auto PARITY = ' N' ;
@@ -12,85 +12,112 @@ constexpr auto DEBUG_MODBUS = true;
1212
1313constexpr uint8_t SERVER_ID = 0x09 ;
1414
15- int main ( void ) {
16- modbus_t *mb ;
17- uint16_t tab_reg[REGISTER_READ_LENGTH] ;
15+ constexpr uint16_t GRIPPER_OUTPUT_FIRST_REG = 0x07D0 ;
16+ constexpr uint16_t GRIPPER_INPUT_FIRST_REG = 0x03E8 ;
17+ constexpr uint8_t READ_WRITE_REG_LENGTH = 3 ;
1818
19- mb = modbus_new_rtu (DEVICE_NAME, BAUDRATE, PARITY, DATA_BITS, STOP_BIT);
20- modbus_set_slave (mb, SERVER_ID);
21- modbus_set_debug (mb, DEBUG_MODBUS);
22- modbus_connect (mb);
23- printf (" \n Connected\n " );
19+ constexpr uint16_t GRIPPER_FLAGS_DEACTIVATE = 0x0000 ;
20+ constexpr uint16_t GRIPPER_FLAGS_ACTIVATE = 0x0100 ;
21+ constexpr uint16_t GRIPPER_FLAGS_ACTIVATED = 0x3100 ;
2422
25- // Read 5 registers from the address 0
26- modbus_read_registers (mb, 2000 , 6 , tab_reg);
23+ constexpr uint16_t GRIPPER_FLAGS_MOVE = 0x0900 ;
24+ constexpr uint16_t FULL_SPEED_FORCE = 0xFFFF ;
25+ constexpr uint16_t GRIPPER_OPENED = 0x0000 ;
26+ constexpr uint16_t GRIPPER_CLOSED = 0x00FF ;
2727
28- printf (" Status: %.4X %.4X %.4X %.4X %.4X %.4X\n " , tab_reg[0 ], tab_reg[1 ], tab_reg[2 ], tab_reg[3 ], tab_reg[4 ], tab_reg[5 ]);
28+ constexpr uint16_t GRIPPER_FLAGS_CLOSED = 0xB900 ;
29+ constexpr uint16_t GRIPPER_FLAGS_NO_OBJECT = 0xF900 ;
30+
31+ void sleep_100ms (){
32+ usleep (100 * 1000 ); // ms * 1000
33+ }
34+
35+ void test_connection (modbus_t *ctx){
36+ uint16_t tab_reg[REGISTER_READ_LENGTH];
2937
38+ // Read 5 registers from the address 0
39+ modbus_read_registers (ctx, GRIPPER_OUTPUT_FIRST_REG, REGISTER_READ_LENGTH, tab_reg);
3040
31- // Modbus RTU example
32- printf (" Step 1 Activation Request (clear and set rACT)\n " );
33- uint16_t activation_request_register_reset[3 ] = {0x0000 , 0x0000 , 0x0000 };
34- modbus_write_registers (mb, 0x03E8 , 3 , activation_request_register_reset);
41+ printf (" Status: %.4X %.4X %.4X %.4X %.4X %.4X\n " , tab_reg[0 ], tab_reg[1 ], tab_reg[2 ], tab_reg[3 ], tab_reg[4 ], tab_reg[5 ]);
42+ }
3543
36- uint16_t activation_request_register_set[3 ] = {0x0100 , 0x0000 , 0x0000 };
37- modbus_write_registers (mb, 0x03E8 , 3 , activation_request_register_set);
44+ void activate (modbus_t *ctx){
45+ printf (" Step: Activation Request (clear and set rACT)\n " );
46+ uint16_t activation_request_register_reset[READ_WRITE_REG_LENGTH] = {GRIPPER_FLAGS_DEACTIVATE, 0x0000 , 0x0000 };
47+ modbus_write_registers (ctx, GRIPPER_INPUT_FIRST_REG, READ_WRITE_REG_LENGTH, activation_request_register_reset);
3848
49+ uint16_t activation_request_register_set[READ_WRITE_REG_LENGTH] = {GRIPPER_FLAGS_ACTIVATE, 0x0000 , 0x0000 };
50+ modbus_write_registers (ctx, GRIPPER_INPUT_FIRST_REG, READ_WRITE_REG_LENGTH, activation_request_register_set);
51+ }
3952
40- printf ( " Step 2: Read Gripper status until the activation is completed \n " );
41- uint16_t activation_status[ 1 ] = { 0x0000 } ;
42- uint16_t activation_status_complete[ 1 ] = {0x3100 };
53+ void wait_activation_complete ( modbus_t *ctx){
54+ printf ( " Step: Read Gripper status until the activation is completed \n " ) ;
55+ uint16_t activation_status[READ_WRITE_REG_LENGTH ] = {};
4356
44- modbus_read_registers (mb, 0x07D0 , 1 , activation_status);
57+ modbus_read_registers (ctx, GRIPPER_OUTPUT_FIRST_REG, READ_WRITE_REG_LENGTH , activation_status);
4558
46- while (activation_status[0 ] != activation_status_complete[ 0 ] ){
47- printf (" Gripper not yet activated: %.4X vs. %.4X\n " , activation_status[0 ], activation_status_complete[ 0 ] );
48- modbus_read_registers (mb, 0x07D0 , 1 , activation_status);
49- usleep ( 100 * 1000 ); // ms * 1000
59+ while (activation_status[0 ] != GRIPPER_FLAGS_ACTIVATED ){
60+ printf (" Gripper not yet activated: %.4X vs. %.4X\n " , activation_status[0 ], GRIPPER_FLAGS_ACTIVATED );
61+ modbus_read_registers (ctx, GRIPPER_OUTPUT_FIRST_REG , 1 , activation_status);
62+
5063 }
51- printf (" Gripper activated, press any key to continue\n " );
52- getc (stdin);
64+ }
5365
54- printf (" BYPASS: Step 3: Move the robot to the pick-up location\n " );
66+ void close_gripper (modbus_t *ctx) {
67+ printf (" Step: Close the Gripper at full speed and full force\n " );
68+ uint16_t close_gripper_request_register[READ_WRITE_REG_LENGTH] = {GRIPPER_FLAGS_MOVE, GRIPPER_CLOSED, FULL_SPEED_FORCE};
69+ modbus_write_registers (ctx, GRIPPER_INPUT_FIRST_REG, READ_WRITE_REG_LENGTH, close_gripper_request_register);
70+ }
5571
56- printf (" Step 4: Close the Gripper at full speed and full force\n " );
57- uint16_t close_gripper_request_register[3 ] = {0x0900 , 0x00FF , 0xFFFF };
58- modbus_write_registers (mb, 0x03E8 , 3 , close_gripper_request_register);
72+ void open_gripper (modbus_t *ctx) {
73+ printf (" Step: Open the Gripper at full speed and full force\n " );
74+ uint16_t open_gripper_request_register[READ_WRITE_REG_LENGTH] = {GRIPPER_FLAGS_MOVE, GRIPPER_OPENED, FULL_SPEED_FORCE};
75+ modbus_write_registers (ctx, GRIPPER_INPUT_FIRST_REG, READ_WRITE_REG_LENGTH, open_gripper_request_register);
76+ }
5977
60- printf (" Step 5: Read Gripper status until the grip is complete\n " );
61- uint16_t gripper_status_closing[1 ] = {0x0000 };
62- uint16_t gripper_status_closed_obj[3 ] = {0xB900 , 0x00FF , 0xBD00 }; // gOBJ==0x02
63- uint16_t gripper_status_requested_position_no_obj[3 ] = {0xF900 , 0x00FF , 0xBD00 }; // gOBJ==0x03
78+ void wait_movement_complete (modbus_t *ctx){
79+ printf (" Step: Read Gripper status until movement is complete\n " );
80+ uint16_t gripper_status_movement[READ_WRITE_REG_LENGTH] = {};
6481
65- modbus_read_registers (mb, 0x07D0 , 3 , gripper_status_closing );
82+ modbus_read_registers (ctx, GRIPPER_OUTPUT_FIRST_REG, READ_WRITE_REG_LENGTH, gripper_status_movement );
6683
67- while (gripper_status_closing [0 ] != gripper_status_closed_obj[ 0 ] && gripper_status_closing [0 ] != gripper_status_requested_position_no_obj[ 0 ] ){
68- printf (" Gripper not yet closed: %.4X vs. %.4X\n " , gripper_status_closing [0 ], gripper_status_requested_position_no_obj[ 0 ] );
69- modbus_read_registers (mb, 0x07D0 , 3 , gripper_status_closing );
70- usleep ( 100 * 1000 ); // ms * 1000
84+ while (gripper_status_movement [0 ] != GRIPPER_FLAGS_CLOSED && gripper_status_movement [0 ] != GRIPPER_FLAGS_NO_OBJECT ){
85+ printf (" Gripper not yet closed: %.4X vs. %.4X or %.4X \n " , gripper_status_movement [0 ], GRIPPER_FLAGS_NO_OBJECT, GRIPPER_FLAGS_CLOSED );
86+ modbus_read_registers (ctx, GRIPPER_OUTPUT_FIRST_REG, READ_WRITE_REG_LENGTH, gripper_status_movement );
87+ sleep_100ms ();
7188 }
72- printf (" Gripper closed, press any key to continue\n " );
73- getc (stdin);
89+ }
90+
91+ int main (void ) {
92+ modbus_t *mb;
93+
94+ mb = modbus_new_rtu (DEVICE_NAME, BAUDRATE, PARITY, DATA_BITS, STOP_BIT);
95+ modbus_set_slave (mb, SERVER_ID);
96+ modbus_set_debug (mb, DEBUG_MODBUS);
97+ modbus_connect (mb);
98+ printf (" \n Connected\n " );
7499
75- printf (" BYPASS: Step 6: Move the robot to the release location\n " );
100+ test_connection (mb);
101+
102+ activate (mb);
103+ wait_activation_complete (mb);
104+ printf (" Gripper activated, press any key to continue\n " );
105+ getc (stdin);
76106
77- printf (" Step 7: Open the Gripper at full speed and full force\n " );
78- uint16_t open_gripper_request_register[3 ] = {0x0900 , 0x0000 , 0xFFFF };
79- modbus_write_registers (mb, 0x03E8 , 3 , open_gripper_request_register);
107+ printf (" BYPASS: Step: Move the robot to the pick-up location\n " );
108+
109+ close_gripper (mb);
110+ wait_movement_complete (mb);
111+ printf (" Gripper closed, press any key to continue\n " );
112+ getc (stdin);
80113
81- printf (" Step 8: Read Gripper status until opening is complete\n " );
82- uint16_t gripper_status_opening[1 ] = {0x0000 };
83- modbus_read_registers (mb, 0x07D0 , 3 , gripper_status_opening);
114+ printf (" BYPASS: Step: Move the robot to the release location\n " );
84115
85- while (gripper_status_opening[0 ] != gripper_status_closed_obj[0 ] && gripper_status_opening[0 ] != gripper_status_requested_position_no_obj[0 ]){
86- printf (" Gripper not yet opened\n " );
87- modbus_read_registers (mb, 0x07D0 , 3 , gripper_status_opening);
88- usleep (100 * 1000 ); // ms * 1000
89- }
116+ open_gripper (mb);
117+ wait_movement_complete (mb);
90118 printf (" Gripper opened\n " );
91119
92120 printf (" Test finished, closing connection to the Gripper\n " );
93-
94121 modbus_close (mb);
95122 modbus_free (mb);
96123}
0 commit comments