forked from MrPh0enix/franka-teleop-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.cpp
More file actions
103 lines (60 loc) · 2.66 KB
/
motion.cpp
File metadata and controls
103 lines (60 loc) · 2.66 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
//franka libs
#include <franka/model.h>
#include <franka/robot.h>
#include <franka/exception.h>
int main() {
try {
//connect to robot and initialize vals
franka::Robot robot(config["leader"]["robot"].as<std::string>());
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);
// lambda functions 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};
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('L');
}
std::array<double, 7> joint_pos = robot_state.q;
std::array<double, 7> joint_vel = robot_state.dq;
std::array<double, 7> command_torques = computeBilateralWithForceFeedback(robot_state);
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);
} 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;
}