]> defiant.homedns.org Git - wt_open_manipulator.git/blob - include/open_manipulator_i2c.h
Added ~get_error service
[wt_open_manipulator.git] / include / open_manipulator_i2c.h
1 #include <chrono>
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <sys/ioctl.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <mutex>
8 #include <linux/i2c-dev.h>
9 #include <hardware_interface/joint_command_interface.h>
10 #include <hardware_interface/joint_state_interface.h>
11 #include <hardware_interface/robot_hw.h>
12 #include "std_srvs/SetBool.h"
13 #include "wt_open_manipulator/GetError.h"
14
15 #define I2C_FILE "/dev/i2c-2"
16 #define I2C_ADDR (0x60>>1)
17 #define XM_OPERATING_MODE 11
18 #define XM_TORQUE_ENABLE 64
19 #define XM_HARDWARE_ERROR_STATUS 70
20 #define XM_GOAL_CURRENT 102
21 #define XM_PROFILE_ACCELERATION 108
22 #define XM_PROFILE_VELOCITY 112
23 #define XM_GOAL_POSITION 116
24 #define XM_PRESENT_POSITION 132
25 #define XM_PRESENT_CURRENT 126
26 #define XM_PRESENT_VELOCITY 128
27 #define CMD_EOF 0xaa
28 #define STEPS_TO_RADIAN (2*M_PI/4096)
29 #define GRIPPER_RADIAN_TO_METER -0.015
30 #define VELOCITY_STEPS_TO_RADS (0.229*2*M_PI/60)
31
32 class WtOpenManipulatorI2C : public hardware_interface::RobotHW
33 {
34         public:
35                 WtOpenManipulatorI2C(ros::NodeHandle nh) {
36                         uint8_t mode=255;
37                         this->nh = nh;
38                         torque_enable_service = nh.advertiseService("torque_enable", &WtOpenManipulatorI2C::cbTorqueEnable, this);
39                         get_error_service = nh.advertiseService("get_error", &WtOpenManipulatorI2C::cbGetError, this);
40
41                         // connect and register the joint state interface
42                         hardware_interface::JointStateHandle state_handle_1("joint1", &pos[0], &vel[0], &eff[0]);
43                         jnt_state_interface.registerHandle(state_handle_1);
44                         hardware_interface::JointStateHandle state_handle_2("joint2", &pos[1], &vel[1], &eff[1]);
45                         jnt_state_interface.registerHandle(state_handle_2);
46                         hardware_interface::JointStateHandle state_handle_3("joint3", &pos[2], &vel[2], &eff[2]);
47                         jnt_state_interface.registerHandle(state_handle_3);
48                         hardware_interface::JointStateHandle state_handle_4("joint4", &pos[3], &vel[3], &eff[3]);
49                         jnt_state_interface.registerHandle(state_handle_4);
50                         hardware_interface::JointStateHandle state_handle_gripper("gripper", &pos[4], &vel[4], &eff[4]);
51                         jnt_state_interface.registerHandle(state_handle_gripper);
52                         registerInterface(&jnt_state_interface);
53
54                         // connect and register the joint position interface
55                         hardware_interface::JointHandle pos_handle_1(state_handle_1, &cmd[0]);
56                         jnt_pos_interface.registerHandle(pos_handle_1);
57                         hardware_interface::JointHandle pos_handle_2(state_handle_2, &cmd[1]);
58                         jnt_pos_interface.registerHandle(pos_handle_2);
59                         hardware_interface::JointHandle pos_handle_3(state_handle_3, &cmd[2]);
60                         jnt_pos_interface.registerHandle(pos_handle_3);
61                         hardware_interface::JointHandle pos_handle_4(state_handle_4, &cmd[3]);
62                         jnt_pos_interface.registerHandle(pos_handle_4);
63                         hardware_interface::JointHandle pos_handle_gripper(state_handle_gripper, &cmd[4]);
64                         jnt_pos_interface.registerHandle(pos_handle_gripper);
65                         registerInterface(&jnt_pos_interface);
66
67                         // Settings
68                         if (dynamixel_writeword(15, XM_GOAL_CURRENT, 200) != 1) {
69                                 ROS_ERROR("I2C dynamixel id=15 write goal current error");
70                                 exit(1);
71                         }
72                         if (dynamixel_readbyte(15, XM_OPERATING_MODE, &mode) != 1) {
73                                 ROS_ERROR("I2C dynamixel id=15 read operating Mode error");
74                                 exit(1);
75                         }
76                         if (mode != 5) {
77                                 if (!torque_enable(false)) {
78                                         exit(1);
79                                 }
80                                 if (dynamixel_writebyte(15, XM_OPERATING_MODE, 5) != 1) { // Current-based Position Control Mode
81                                         ROS_ERROR("I2C dynamixel id=15 write operating Mode error");
82                                         exit(1);
83                                 }
84                         }
85                         if (dynamixel_writedword(15, XM_PROFILE_ACCELERATION, 20) != 1) {
86                                 ROS_ERROR("I2C dynamixel id=15 write profile Acceleration error");
87                                 exit(1);
88                         }
89                         if (dynamixel_writedword(15, XM_PROFILE_VELOCITY, 200) != 1) {
90                                 ROS_ERROR("I2C dynamixel id=15 write profile Velocity error");
91                                 exit(1);
92                         }
93                         if (!torque_enable(true)) {
94                                 exit(1);
95                         }
96                 }
97
98                 // Converts pos_encoder from wheels to wheel angles
99                 void read(ros::Duration period) {
100                         int32_t value;
101                         int16_t current;
102                         auto t1 = std::chrono::high_resolution_clock::now();
103
104                         // arm
105                         for(int i=0, id=11; id<=14; id++, i++) {
106                                 if (dynamixel_readdword(id, XM_PRESENT_POSITION, &value) == 1) {
107                                         pos[i] = STEPS_TO_RADIAN*(value-2048);
108                                 } else {
109                                         ROS_ERROR("I2C dynamixel id=%d position read error", id);
110                                 }
111
112                                 if (dynamixel_readdword(id, XM_PRESENT_VELOCITY, &value) == 1) {
113                                         vel[i] = value*VELOCITY_STEPS_TO_RADS;
114                                 } else {
115                                         ROS_ERROR("I2C dynamixel id=%d velocity read error", id);
116                                 }
117
118 #ifdef READ_EFFORT
119                                 if (dynamixel_readword(id, XM_PRESENT_CURRENT, &current) == 1) {
120                                         eff[i] = current*2.69e-3;
121                                 } else {
122                                         ROS_ERROR("I2C dynamixel id=%d current read error", id);
123                                 }
124 #endif
125                         }
126
127                         // gripper
128                         if (dynamixel_readdword(15, XM_PRESENT_POSITION, &value) == 1) {
129                                 pos[4] = STEPS_TO_RADIAN*(value-2048)*GRIPPER_RADIAN_TO_METER;
130                         } else {
131                                 ROS_ERROR("I2C dynamixel id=15 position read error");
132                         }
133                         if (dynamixel_readdword(15, XM_PRESENT_VELOCITY, &value) == 1) {
134                                 vel[4] = value*VELOCITY_STEPS_TO_RADS*GRIPPER_RADIAN_TO_METER;
135                         } else {
136                                 ROS_ERROR("I2C dynamixel id=15 velocity read error");
137                         }
138 #ifdef READ_EFFORT
139                         if (dynamixel_readword(15, XM_PRESENT_CURRENT, &current) == 1) {
140                                 eff[4] = current*2.69e-3;
141                         } else {
142                                 ROS_ERROR("I2C dynamixel id=15 current read error");
143                         }
144 #endif
145
146                         ROS_DEBUG("pos: %.2f %.2f %.2f %.2f %.2f", pos[0]*180/M_PI, pos[1]*180/M_PI, pos[2]*180/M_PI, pos[3]*180/M_PI, pos[4]);
147                         ROS_DEBUG("velocity: %.2f %.2f %.2f %.2f %.2f", vel[0]*30/M_PI, vel[1]*30/M_PI, vel[2]*30/M_PI, vel[3]*30/M_PI, vel[4]);
148 #ifdef READ_EFFORT
149                         ROS_DEBUG("current: %.2f %.2f %.2f %.2f %.2f", eff[0], eff[1], eff[2], eff[3], eff[4]);
150 #endif
151                         auto t2 = std::chrono::high_resolution_clock::now();
152                         int64_t duration = std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 ).count();
153                         ROS_DEBUG("read duration: %lldms", duration);
154                 }
155
156                 // Writes current velocity command to hardware
157                 void write() {
158                         int32_t value;
159
160                         if (memcmp(cmd, cmd_pre, sizeof(cmd)) != 0) { // Only write new position when changed
161                                 ROS_DEBUG("cmd: %.2f %.2f %.2f %.2f %.2f", cmd[0]*180/M_PI, cmd[1]*180/M_PI, cmd[2]*180/M_PI, cmd[3]*180/M_PI, cmd[4]);
162
163                                 // arm
164                                 for(int i=0, id=11; id<=14; id++, i++) {
165                                         if (isnan(cmd[i])) {
166                                                 ROS_WARN("Desired angle for id=%d is NaN, skipping write.", id);
167                                                 continue;
168                                         }
169
170                                         value = cmd[i]/STEPS_TO_RADIAN + 2048;
171
172                                         if (dynamixel_writedword(id, XM_GOAL_POSITION, value) == 1) {
173                                                 cmd_pre[i] = cmd[i];
174                                         } else {
175                                                 ROS_ERROR("I2C dynamixel id=%d write error", id);
176                                         }
177                                 }
178
179                                 // gripper
180                                 if (isnan(cmd[4]) || cmd[4] < -0.010 || cmd[4] > 0.019) {
181                                         ROS_WARN("Desired angle for id=15 is NaN or outside joint limit, skipping write.");
182                                 } else {
183                                         value = cmd[4]/GRIPPER_RADIAN_TO_METER/STEPS_TO_RADIAN + 2048;
184                                         if (dynamixel_writedword(15, XM_GOAL_POSITION, value) == 1) {
185                                                 cmd_pre[4] = cmd[4];
186                                         } else {
187                                                 ROS_ERROR("I2C dynamixel id=15 write error");
188                                         }
189                                 }
190                         }
191                 }
192
193                 bool torque_enable(bool enable) {
194                         bool ret = true;
195                         for(int id=11; id<=15; id++) {
196                                 if (dynamixel_writebyte(id, XM_TORQUE_ENABLE, enable) != 1) {
197                                         ROS_ERROR("I2C dynamixel id=%d torque change", id);
198                                         ret = false;
199                                 }
200                         }
201                         return ret;
202                 }
203
204         private:
205                 hardware_interface::JointStateInterface jnt_state_interface;
206                 hardware_interface::PositionJointInterface jnt_pos_interface;
207                 ros::NodeHandle nh;
208                 double cmd[5] = {NAN, NAN, NAN, NAN, NAN};
209                 double cmd_pre[5] = {NAN, NAN, NAN, NAN, NAN}; // determine if cmd changed
210                 double pos[5] = {0, 0, 0, 0, 0};
211                 double vel[5] = {0, 0, 0, 0, 0};
212                 double eff[5] = {0, 0, 0, 0, 0};
213                 ros::ServiceServer torque_enable_service, get_error_service;
214                 std::mutex comm_mutex;
215
216                 int dynamixel_write(uint8_t *write_data, int write_num, uint8_t *read_data, int read_num) {
217                         int file;
218                         int ret;
219                         uint8_t read_buf[read_num+1]; // for unknown reason need to read one more byte with USI Slave
220
221                         comm_mutex.lock();
222                         if ((file = open(I2C_FILE, O_RDWR)) < 0) {
223                                 perror("open");
224                                 goto error;
225                         }
226
227                         if (ioctl(file, I2C_SLAVE, I2C_ADDR) < 0) {
228                                 perror("ioctl");
229                                 goto error;
230                         }
231
232                         if ((ret = ::write(file, write_data, write_num)) != write_num) {
233                                 perror("i2c write");
234                                 goto error;
235                         }
236
237                         // wait at least the half duplex transmission time, assume 29 (=14+15) bytes send/received: 1/(115200.0/10/29) <= 2.6ms
238                         usleep(3.2*1e3);
239                         if (read_num > 0) {
240                                 if ((ret = ::read(file, read_buf, read_num+1)) != read_num+1) {
241                                         perror("i2c read");
242                                         goto error;
243                                 }
244                                 ret--; // remove extra byte
245                                 memcpy(read_data, read_buf, ret);
246                         }
247
248                         error:
249                                 close(file);
250                                 comm_mutex.unlock();
251                                 return ret;
252                 }
253
254                 uint8_t dynamixel_writebyte(uint8_t id, uint8_t cmd, uint8_t value) {
255                         uint8_t write_data[5] = {id, cmd, 1, value, CMD_EOF};
256                         uint8_t read_data[2];
257                         int ret = dynamixel_write(write_data, 5, read_data, 2);
258                         if (ret == 2 && read_data[1] == id) {
259                                 return read_data[0];
260                         }
261                         return 255;
262                 }
263
264                 uint8_t dynamixel_writeword(uint8_t id, uint8_t cmd, int16_t value) {
265                         uint8_t write_data[6] = {id, cmd, 2, (uint8_t)(value>>8), (uint8_t)value, CMD_EOF};
266                         uint8_t read_data[2];
267                         int ret = dynamixel_write(write_data, 6, read_data, 2);
268                         if (ret == 2 && read_data[1] == id) {
269                                 return read_data[0];
270                         }
271                         return 255;
272                 }
273
274                 uint8_t dynamixel_writedword(uint8_t id, uint8_t cmd, int32_t value) {
275                         uint8_t write_data[8] = {id, cmd, 4, (uint8_t)(value>>24), (uint8_t)(value>>16), (uint8_t)(value>>8), (uint8_t)value, CMD_EOF};
276                         uint8_t read_data[2];
277                         int ret = dynamixel_write(write_data, 8, read_data, 2);
278                         if (ret == 2 && read_data[1] == id) {
279                                 return read_data[0];
280                         }
281                         return 255;
282                 }
283
284                 uint8_t dynamixel_readbyte(uint8_t id, uint8_t cmd, uint8_t *value) {
285                         uint8_t write_data[4] = {id, cmd, 0x11, CMD_EOF};
286                         uint8_t read_data[3];
287                         int ret = dynamixel_write(write_data, 4, read_data, 3);
288                         if (ret == 3 && read_data[1] == id) {
289                                 *value = read_data[2];
290                                 return read_data[0];
291                         }
292                         return 255;
293                 }
294
295                 uint8_t dynamixel_readword(uint8_t id, uint8_t cmd, int16_t *value) {
296                         uint8_t write_data[4] = {id, cmd, 0x12, CMD_EOF};
297                         uint8_t read_data[4];
298                         int ret = dynamixel_write(write_data, 4, read_data, 4);
299                         if (ret == 4 && read_data[1] == id) {
300                                 *value = (read_data[2]<<8) | read_data[3];
301                                 return read_data[0];
302                         }
303                         return 255;
304                 }
305
306                 uint8_t dynamixel_readdword(uint8_t id, uint8_t cmd, int32_t *value) {
307                         uint8_t write_data[4] = {id, cmd, 0x14, CMD_EOF};
308                         uint8_t read_data[6];
309                         int ret = dynamixel_write(write_data, 4, read_data, 6);
310                         if (ret == 6 && read_data[1] == id) {
311                                 *value = (read_data[2]<<24) | (read_data[3]<<16) | (read_data[4]<<8) | read_data[5];
312                                 return read_data[0];
313                         }
314                         return 255;
315                 }
316
317                 bool cbTorqueEnable(std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res) {
318                         ROS_INFO("Setting torque enable=%d", req.data);
319                         res.success = torque_enable(req.data);
320                         return true;
321                 }
322
323                 bool cbGetError(wt_open_manipulator::GetError::Request &req, wt_open_manipulator::GetError::Response &res) {
324                         ROS_INFO("Reading Hardware Error Status");
325
326                         for(int id=11, i=0; id<=15; id++, i++) {
327                                 wt_open_manipulator::HardwareError hw_error;
328                                 uint8_t value;
329                                 if (dynamixel_readbyte(id, XM_HARDWARE_ERROR_STATUS, &value) != 1) {
330                                         ROS_ERROR("I2C dynamixel id=%d reading hardware error status", id);
331                                         return false;
332                                 }
333                                 hw_error.id = id;
334                                 hw_error.inputVoltage = value & (1<<0);
335                                 hw_error.overHeating = value & (1<<2);
336                                 hw_error.motorEncoder = value & (1<<3);
337                                 hw_error.electricalShock = value & (1<<4);
338                                 hw_error.overload = value & (1<<5);
339                                 res.errors.push_back(hw_error);
340                         }
341
342                         return true;
343                 }
344 };