forked from MrPh0enix/franka-teleop-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
executable file
·327 lines (226 loc) · 9.58 KB
/
player.cpp
File metadata and controls
executable file
·327 lines (226 loc) · 9.58 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
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <mutex>
#include <thread>
#include <atomic>
#include <chrono>
#include <unistd.h>
#include <franka/duration.h>
#include <franka/exception.h>
#include <franka/model.h>
#include <franka/robot.h>
#include <franka/rate_limiting.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <capnp/message.h>
#include <capnp/serialize.h>
#include "messages/robot-state.capnp.h"
franka::RobotState shared_robot_state;
std::mutex state_mutex;
std::atomic<bool> running{true};
void pubThread() {
//socket params
int sockPub = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(49185);
inet_pton(AF_INET, "224.3.29.71", &addr.sin_addr);
const int pub_freq = 100; //Hz
// build a capnp message
capnp::MallocMessageBuilder message{};
RobotState::Builder state_builder = message.initRoot<RobotState>();
uint64_t robot_time = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
while (running.load()) {
franka::RobotState state_to_publish;
{
std::lock_guard<std::mutex> lock(state_mutex);
state_to_publish = shared_robot_state;
}
// build message
state_builder.setTime(robot_time);
state_builder.setJoint1Pos(state_to_publish.q[0]);
state_builder.setJoint2Pos(state_to_publish.q[1]);
state_builder.setJoint3Pos(state_to_publish.q[2]);
state_builder.setJoint4Pos(state_to_publish.q[3]);
state_builder.setJoint5Pos(state_to_publish.q[4]);
state_builder.setJoint6Pos(state_to_publish.q[5]);
state_builder.setJoint7Pos(state_to_publish.q[6]);
state_builder.setJoint1Vel(state_to_publish.dq[0]);
state_builder.setJoint2Vel(state_to_publish.dq[1]);
state_builder.setJoint3Vel(state_to_publish.dq[2]);
state_builder.setJoint4Vel(state_to_publish.dq[3]);
state_builder.setJoint5Vel(state_to_publish.dq[4]);
state_builder.setJoint6Vel(state_to_publish.dq[5]);
state_builder.setJoint7Vel(state_to_publish.dq[6]);
state_builder.setJoint1Torque(state_to_publish.tau_J[0]);
state_builder.setJoint2Torque(state_to_publish.tau_J[1]);
state_builder.setJoint3Torque(state_to_publish.tau_J[2]);
state_builder.setJoint4Torque(state_to_publish.tau_J[3]);
state_builder.setJoint5Torque(state_to_publish.tau_J[4]);
state_builder.setJoint6Torque(state_to_publish.tau_J[5]);
state_builder.setJoint7Torque(state_to_publish.tau_J[6]);
state_builder.setJoint1ExtTorque(state_to_publish.tau_ext_hat_filtered[0]);
state_builder.setJoint2ExtTorque(state_to_publish.tau_ext_hat_filtered[1]);
state_builder.setJoint3ExtTorque(state_to_publish.tau_ext_hat_filtered[2]);
state_builder.setJoint4ExtTorque(state_to_publish.tau_ext_hat_filtered[3]);
state_builder.setJoint5ExtTorque(state_to_publish.tau_ext_hat_filtered[4]);
state_builder.setJoint6ExtTorque(state_to_publish.tau_ext_hat_filtered[5]);
state_builder.setJoint7ExtTorque(state_to_publish.tau_ext_hat_filtered[6]);
// Flatten to a single byte array
auto flatArray = capnp::messageToFlatArray(message);
auto bytes = flatArray.asBytes();
sendto(sockPub, bytes.begin(), bytes.size(), 0,
(sockaddr*)&addr, sizeof(addr));
std::this_thread::sleep_for(std::chrono::milliseconds(1000/pub_freq));
}
close(sockPub);
}
void subThread() {
const int sub_freq = 100; //Hz
// message size.
char buffer[2048];
//socket params
int sockSub = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(49186);
addr.sin_addr.s_addr = INADDR_ANY; //listen on all addresses
if (bind(sockSub, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Socket bind failed");
close(sockSub);
}
std::cout << "Subscriber listening on port: 49186" << std::endl;
while (running.load()) {
sockaddr_in senderAddr{};
socklen_t senderLen = sizeof(senderAddr);
ssize_t bytes = recvfrom(sockSub, buffer, sizeof(buffer), 0,
(struct sockaddr*)&senderAddr, &senderLen);
std::cout << "Received : " << std::endl;
// capn proto decode
kj::ArrayPtr<capnp::word> words(
reinterpret_cast<capnp::word*>(buffer),
bytes / sizeof(capnp::word)
);
capnp::FlatArrayMessageReader reader(words);
RobotState::Reader state = reader.getRoot<RobotState>();
std::cout << "Timestamp: " << state.getTime() <<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000/sub_freq));
}
close(sockSub);
}
int main(int argc, char** argv) {
// scale for P and D values
constexpr double scale = 1.3;
// contact switch sensitivity
const double contact_threshold = 2.5;
// Define PGain and DGain
std::array<double, 7> P_gain = {
scale * 800.0,
scale * 800.0,
scale * 600.0,
scale * 800.0,
scale * 150.0,
scale * 150.0,
scale * 50.0
};
std::array<double, 7> D_gain = {
scale * 50.0,
scale * 50.0,
scale * 50.0,
scale * 50.0,
scale * 30.0,
scale * 25.0,
scale * 10.0
};
// Homing point (static point the robot tries to reach)
const std::array<double, 7> home_pos = {0.0, -0.78539816, 0.0, -2.35619449, 0.0, 1.57079633, 0.78539816};
// velocity limits
const std::array<double, 7> velo_limits = {8.0, 7.0, 7.0, 7.0, 6.0, 6.0, 4.0};
try {
// connect to robot
franka::Robot robot("192.168.170.2");
//initialize shared robot state
shared_robot_state = robot.readOnce();
//start publisher and subscriber thread
std::thread pub_thread(pubThread);
std::thread sub_thread(subThread);
// load the kinematics and dynamics model
franka::Model model = robot.loadModel();
// 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}});
auto computeTrqs = [&](std::array<double, 7>& joint_pos, std::array<double, 7>& joint_vel) {
// initialize trqs
std::array<double, 7> torques = {};
// limit velocity of joints
std::array<double, 7> target_pos = franka::limitRate(velo_limits, home_pos, joint_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] = P_gain[i] * pos_error - D_gain[i] * vel;
};
return torques;
};
// trq callback functions
double time = 0.0;
int step = 0;
bool contact_flag = false;
double last_contact_time = 0.0;
auto trq_control_callback = [&step, &time, &contact_flag, &last_contact_time, &computeTrqs, &contact_threshold]
(const franka::RobotState& robot_state, franka::Duration period) -> franka::Torques {
{
std::lock_guard<std::mutex> lock(state_mutex);
shared_robot_state = robot_state;
}
// time passed since the loop started
time += period.toSec();
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;
// send command torques after checking ext trq.
bool anyOf = std::any_of(ext_trq.begin(), ext_trq.end(), [&contact_threshold](double x){ return std::abs(x) > contact_threshold;});
if (anyOf) {
contact_flag = true;
last_contact_time = time;
return {{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
} else if (contact_flag){
// check if 2 sec passed since stopped contact.
if (time - last_contact_time > 2.0) {
contact_flag = false;
return {{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
} else {
return {{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
};
} else {
std::array<double, 7> command_torques = computeTrqs(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();
}
}
running.store(false);
pub_thread.join();
sub_thread.join();
} catch (const franka::Exception& ex) {
// print exception
std::cout << ex.what() << std::endl;
running.store(false);
return -1;
}
return 0;
}