forked from MrPh0enix/franka-teleop-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollower1.cpp
More file actions
579 lines (402 loc) · 18.3 KB
/
follower1.cpp
File metadata and controls
579 lines (402 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#include <iostream>
#include <cstring>
#include <string>
#include <mutex>
#include <thread>
#include <atomic>
#include <yaml-cpp/yaml.h>
// Key listener
#include <termios.h>
#include <fcntl.h>
// for UDP socket
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
//capnp message
#include <capnp/message.h>
#include <capnp/serialize.h>
#include "messages/robot-state.capnp.h"
//franka libs
#include <franka/model.h>
#include <franka/robot.h>
#include <franka/exception.h>
#include <franka/rate_limiting.h>
#include "examples_common.h"
#define BUFFER_SIZE 2048
RobotState::Reader shared_leader_state;
franka::RobotState shared_robot_state;
std::mutex state_mutex;
std::atomic<bool> running{true};
std::atomic<bool> sub_connected{false}; // detects if subscriber connected
std::atomic<char> control_rob{'L'}; //default to leader(L)
void pubThread (const YAML::Node& config) {
//config
std::string ip = config["follower"]["ip"].as<std::string>();
int port = config["follower"]["port"].as<int>();
// socket params
int sockPub = socket(AF_INET, SOCK_DGRAM, 0);
char buffer[BUFFER_SIZE];
struct sockaddr_in send_addr{};
send_addr.sin_family = AF_INET;
send_addr.sin_port = htons(port);
inet_pton(AF_INET, ip.c_str(), &send_addr.sin_addr);
const int pub_freq = config["global"]["freq"].as<int>(); //Hz
while (running.load()) {
//build message
capnp::MallocMessageBuilder message;
RobotState::Builder follower_state = message.initRoot<RobotState>();
franka::RobotState state_to_publish;
{
std::lock_guard<std::mutex> lock(state_mutex);
state_to_publish = shared_robot_state;
}
follower_state.setTime(123456);
follower_state.setJoint1Pos(state_to_publish.q[0]);
follower_state.setJoint2Pos(state_to_publish.q[1]);
follower_state.setJoint3Pos(state_to_publish.q[2]);
follower_state.setJoint4Pos(state_to_publish.q[3]);
follower_state.setJoint5Pos(state_to_publish.q[4]);
follower_state.setJoint6Pos(state_to_publish.q[5]);
follower_state.setJoint7Pos(state_to_publish.q[6]);
follower_state.setJoint1Vel(state_to_publish.dq[0]);
follower_state.setJoint2Vel(state_to_publish.dq[1]);
follower_state.setJoint3Vel(state_to_publish.dq[2]);
follower_state.setJoint4Vel(state_to_publish.dq[3]);
follower_state.setJoint5Vel(state_to_publish.dq[4]);
follower_state.setJoint6Vel(state_to_publish.dq[5]);
follower_state.setJoint7Vel(state_to_publish.dq[6]);
follower_state.setJoint1Torque(state_to_publish.tau_J[0]);
follower_state.setJoint2Torque(state_to_publish.tau_J[1]);
follower_state.setJoint3Torque(state_to_publish.tau_J[2]);
follower_state.setJoint4Torque(state_to_publish.tau_J[3]);
follower_state.setJoint5Torque(state_to_publish.tau_J[4]);
follower_state.setJoint6Torque(state_to_publish.tau_J[5]);
follower_state.setJoint7Torque(state_to_publish.tau_J[6]);
follower_state.setJoint1ExtTorque(state_to_publish.tau_ext_hat_filtered[0]);
follower_state.setJoint2ExtTorque(state_to_publish.tau_ext_hat_filtered[1]);
follower_state.setJoint3ExtTorque(state_to_publish.tau_ext_hat_filtered[2]);
follower_state.setJoint4ExtTorque(state_to_publish.tau_ext_hat_filtered[3]);
follower_state.setJoint5ExtTorque(state_to_publish.tau_ext_hat_filtered[4]);
follower_state.setJoint6ExtTorque(state_to_publish.tau_ext_hat_filtered[5]);
follower_state.setJoint7ExtTorque(state_to_publish.tau_ext_hat_filtered[6]);
follower_state.setControlRobot(static_cast<uint8_t>(control_rob.load()));
kj::VectorOutputStream state_message;
capnp::writeMessage(state_message, message);
kj::ArrayPtr<const kj::byte> sz_state_message = state_message.getArray();
sendto(sockPub, sz_state_message.begin(), sz_state_message.size(), 0, (struct sockaddr *)&send_addr, sizeof(send_addr));
// sleep
std::this_thread::sleep_for(std::chrono::milliseconds(1000/pub_freq));
}
close(sockPub);
}
void subThread (const YAML::Node& config) {
int port = config["leader"]["port"].as<int>();
//socket params
int sockSub = socket(AF_INET, SOCK_DGRAM, 0);
char buffer[BUFFER_SIZE];
struct sockaddr_in recv_addr{};
recv_addr.sin_family = AF_INET;
recv_addr.sin_port = htons(port); // port for leader publisher
recv_addr.sin_addr.s_addr = INADDR_ANY; // listen on all addresses
const int sub_freq = config["global"]["freq"].as<int>(); //Hz
if (bind(sockSub, (struct sockaddr *)&recv_addr, sizeof(recv_addr)) < 0) {
perror("Subscriber socket bind failed");
close(sockSub);
}
std::cout << "Subscriber lsitening on port: " << port << std::endl;
while(running.load()) {
ssize_t n = recvfrom(sockSub, buffer, sizeof(buffer), 0, nullptr, nullptr);
if (n < 0) {
perror("Socket failed to recv info!");
break;
} else {
sub_connected.store(true);
}
// unpack message
std::vector<capnp::word> alignedBuffer((n + sizeof(capnp::word) - 1) / sizeof(capnp::word));
memcpy(alignedBuffer.data(), buffer, n);
kj::ArrayPtr<const capnp::word> receivedData(alignedBuffer.data(), alignedBuffer.size());
capnp::FlatArrayMessageReader reader(receivedData);
RobotState::Reader leader_state = reader.getRoot<RobotState>();
{
std::lock_guard<std::mutex> lock(state_mutex);
shared_leader_state = leader_state;
}
//set the current control robot
control_rob.store(static_cast<char>(leader_state.getControlRobot()));
std::this_thread::sleep_for(std::chrono::milliseconds(1000/sub_freq));
}
close(sockSub);
}
void keyListener() {
termios newT, oldT;
tcgetattr(STDIN_FILENO, &oldT); //current terminal settings for backup
newT = oldT;
newT.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newT);
// Make thread non blocking
int oldFlags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldFlags | O_NONBLOCK);
std::cout << "Running ... Press Q to exit .." << std::endl;
char key;
while(running.load()) {
key = getchar();
if (key == 'q' || key =='Q') {
running.store(false);
std::cout << "Q Pressed ...." << std::endl;
}
usleep(10000); //delay
}
// restore terminal on exit
tcsetattr(STDIN_FILENO, TCSANOW, &oldT);
fcntl(STDIN_FILENO, F_SETFL, oldFlags);
}
int main () {
// scale for P and D values
constexpr double scale = 1.3;
try {
// config
YAML::Node config = YAML::LoadFile("teleop_config.yml");
// Define PGain and DGain and velo_limits
std::vector<double> P_gain = config["follower"]["p_vals"].as<std::vector<double>>();
std::vector<double> D_gain = config["follower"]["d_vals"].as<std::vector<double>>();
std::vector<double> velo_limits_vec = config["global"]["velo_limits"].as<std::vector<double>>();
std::array<double, 7> velo_limits;
std::copy(velo_limits_vec.begin(), velo_limits_vec.end(), velo_limits.begin()); // convert to array.
//constants for force feedback
std::vector<double> C_q = config["global"]["C_q"].as<std::vector<double>>();
std::vector<double> C_v = config["global"]["C_v"].as<std::vector<double>>();
std::vector<double> C_y = config["global"]["C_y"].as<std::vector<double>>();
std::vector<double> C_f = config["global"]["C_f"].as<std::vector<double>>();
// contact switch sensitivity
const double contact_threshold = config["global"]["contact_threshold"].as<double>();
//connect to robot and initialize vals
franka::Robot robot(config["follower"]["robot"].as<std::string>());
shared_robot_state = robot.readOnce();
franka::Model model = robot.loadModel();
// move robot to start
const std::array<double, 7> home_pos = {0.0, -0.78539816, 0.0, -2.35619449, 0.0, 1.57079633, 0.78539816};
MotionGenerator motion_generator(0.5, home_pos);
robot.control(motion_generator);
// start sub thread
std::thread sub_thread(subThread, std::cref(config));
// start pub thread
std::thread pub_thread(pubThread, std::cref(config));
//key listener thread
std::thread key_thread(keyListener);
// set collision behavior
robot.setCollisionBehavior({{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}},
{{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}},
{{100.0, 100.0, 100.0, 100.0, 100.0, 100.0}},
{{100.0, 100.0, 100.0, 100.0, 100.0, 100.0}});
// lambda function to compute torques
auto computeUnilateralTrqs = [&](std::array<double, 7>& joint_pos, std::array<double, 7>& joint_vel) {
// initialize trqs
std::array<double, 7> torques = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
if (!sub_connected.load()) {
return torques;
};
RobotState::Reader leader_state;
{
std::lock_guard<std::mutex> lock(state_mutex);
leader_state = shared_leader_state;
}
std::array<double, 7> leader_pos = {
leader_state.getJoint1Pos(),
leader_state.getJoint2Pos(),
leader_state.getJoint3Pos(),
leader_state.getJoint4Pos(),
leader_state.getJoint5Pos(),
leader_state.getJoint6Pos(),
leader_state.getJoint7Pos()
};
// // limit velocity of joints
std::array<double, 7> target_pos = franka::limitRate(velo_limits, leader_pos, joint_pos);
// std::array<double, 7> target_pos = leader_pos;
// Compute torques
for (int i = 0; i < 7; ++i) {
double vel = joint_vel[i];
if (i == 6) {
double pos_error = target_pos[i] - joint_pos[i] - 1.57;
torques[i] = (scale * P_gain[i] * pos_error) - (scale * D_gain[i] * vel);
} else {
double pos_error = target_pos[i] - joint_pos[i];
torques[i] = (scale * P_gain[i] * pos_error) - (scale * D_gain[i] * vel);
}
};
return torques;
};
auto computeBilateralTrqs = [&](std::array<double, 7>& joint_pos, std::array<double, 7>& joint_vel) {
// initialize trqs
std::array<double, 7> torques = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
if (!sub_connected.load() || control_rob.load() == 'F') {
return torques;
};
RobotState::Reader leader_state;
{
std::lock_guard<std::mutex> lock(state_mutex);
leader_state = shared_leader_state;
}
std::array<double, 7> leader_pos = {
leader_state.getJoint1Pos(),
leader_state.getJoint2Pos(),
leader_state.getJoint3Pos(),
leader_state.getJoint4Pos(),
leader_state.getJoint5Pos(),
leader_state.getJoint6Pos(),
leader_state.getJoint7Pos()
};
// // limit velocity of joints
std::array<double, 7> target_pos = franka::limitRate(velo_limits, leader_pos, joint_pos);
// std::array<double, 7> target_pos = leader_pos;
// Compute torques
for (int i = 0; i < 7; ++i) {
double pos_error = target_pos[i] - joint_pos[i];
double vel = joint_vel[i];
torques[i] = (scale * P_gain[i] * pos_error) - (scale * D_gain[i] * vel);
};
return torques;
};
auto computeBilateralTrqs2 = [&](std::array<double, 7>& joint_pos, std::array<double, 7>& joint_vel) {
// initialize trqs
std::array<double, 7> torques = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
if (!sub_connected.load()) {
return torques;
};
RobotState::Reader leader_state;
{
std::lock_guard<std::mutex> lock(state_mutex);
leader_state = shared_leader_state;
}
std::array<double, 7> leader_pos = {
leader_state.getJoint1Pos(),
leader_state.getJoint2Pos(),
leader_state.getJoint3Pos(),
leader_state.getJoint4Pos(),
leader_state.getJoint5Pos(),
leader_state.getJoint6Pos(),
leader_state.getJoint7Pos()
};
// // limit velocity of joints
std::array<double, 7> target_pos = franka::limitRate(velo_limits, leader_pos, joint_pos);
// std::array<double, 7> target_pos = leader_pos;
// Compute torques
for (int i = 0; i < 7; ++i) {
double pos_error = target_pos[i] - joint_pos[i];
double vel = joint_vel[i];
torques[i] = (scale * P_gain[i] * pos_error) - (scale * D_gain[i] * vel);
};
return torques;
};
auto computeBilateralWithForceFeedback = [&](const franka::RobotState& robot_state) {
// initialize trqs
std::array<double, 7> torques = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
// initialize acclerations
std::array<double, 7> acc = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
if (!sub_connected.load()) {
return torques;
};
RobotState::Reader leader_state;
{
std::lock_guard<std::mutex> lock(state_mutex);
leader_state = shared_leader_state;
}
std::array<double, 7> leader_pos = {
leader_state.getJoint1Pos(),
leader_state.getJoint2Pos(),
leader_state.getJoint3Pos(),
leader_state.getJoint4Pos(),
leader_state.getJoint5Pos(),
leader_state.getJoint6Pos(),
leader_state.getJoint7Pos()
};
std::array<double, 7> leader_vel = {
leader_state.getJoint1Vel(),
leader_state.getJoint2Vel(),
leader_state.getJoint3Vel(),
leader_state.getJoint4Vel(),
leader_state.getJoint5Vel(),
leader_state.getJoint6Vel(),
leader_state.getJoint7Vel()
};
std::array<double, 7> leader_ext_trq = {
leader_state.getJoint1ExtTorque(),
leader_state.getJoint2ExtTorque(),
leader_state.getJoint3ExtTorque(),
leader_state.getJoint4ExtTorque(),
leader_state.getJoint5ExtTorque(),
leader_state.getJoint6ExtTorque(),
leader_state.getJoint7ExtTorque()
};
std::array<double, 7> joint_pos = robot_state.q;
std::array<double, 7> joint_vel = robot_state.dq;
std::array<double, 7> ext_trq = robot_state.tau_ext_hat_filtered;
// moment of inertia matrix
std::array<double, 49> MOI = model.mass(robot_state);
// Compute accelerations
for (int i = 0; i < 7; ++i) {
double pos_error = joint_pos[i] - leader_pos[i];
double vel_error = joint_vel[i] - leader_vel[i];
double vel_tot = joint_vel[i] + leader_vel[i];
double ext_trq_tot = ext_trq[i] + leader_ext_trq[i];
if (i == 6) {
acc[i] = - ((C_q[i] / 2) * (pos_error)) - ((C_v[i] / 2) * (vel_error))
- ((C_y[i] / 2) * (vel_tot)) - ((C_f[i] / (2 * 1)) * (ext_trq_tot));
}
}
// Compute torques
for (int i = 0; i < 7; i++) {
if (i == 6) {
for (int j = 0; j < 7; j++) {
torques[i] += MOI[i*7 + j] * acc[j];
}
}
}
return torques;
};
// control callback function
auto trq_control_callback = [&] (const franka::RobotState& robot_state, franka::Duration period) -> franka::Torques {
if (!running.load()) {
std::cout << "Exiting .... " << std::endl;
return franka::MotionFinished(franka::Torques({0, 0, 0, 0, 0, 0, 0}));
}
{
std::lock_guard<std::mutex> lock(state_mutex);
shared_robot_state = robot_state;
}
// detect contact
std::array<double, 7> ext_trq = robot_state.tau_ext_hat_filtered;
bool anyOf = std::any_of(ext_trq.begin(), ext_trq.end(), [&contact_threshold](double x){ return std::abs(x) > contact_threshold;});
if (anyOf) {
control_rob.store('F');
};
std::array<double, 7> joint_pos = robot_state.q;
std::array<double, 7> joint_vel = robot_state.dq;
std::array<double, 7> command_torques = computeUnilateralTrqs(joint_pos, joint_vel);
return command_torques;
};
while (running.load()) {
try {
//execute control loop
robot.control(trq_control_callback);
} catch (const franka::Exception& ex) {
// print exception
std::cout << ex.what() << std::endl;
// auto recover
robot.automaticErrorRecovery();
}
}
// stop thread
running.store(false);
sub_thread.join();
pub_thread.join();
key_thread.join();
} catch (const std::exception& ex) {
std::cerr << "Standard exception: " << ex.what() << std::endl;
return -1;
} catch (...) {
std::cerr << "Unknown exception caught" << std::endl;
return -1;
}
return 0;
}