-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathFollower.cpp
413 lines (332 loc) · 12.7 KB
/
pathFollower.cpp
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
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ros/ros.h>
#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <std_msgs/Int8.h>
#include <std_msgs/Float32.h>
#include <nav_msgs/Path.h>
#include <nav_msgs/Odometry.h>
#include <geometry_msgs/TwistStamped.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/Joy.h>
#include <tf/transform_datatypes.h>
#include <tf/transform_broadcaster.h>
#include <tf/transform_listener.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <Eigen/Dense>
using namespace std;
const double PI = 3.1415926;
double sensorOffsetX = 0;
double sensorOffsetY = 0;
int pubSkipNum = 1;
int pubSkipCount = 0;
bool twoWayDrive = true;
double lookAheadDis = 0.5;
double yawRateGain = 7.5;
double stopYawRateGain = 7.5;
double maxYawRate = 45.0;
double maxSpeed = 1.0;
double maxAccel = 1.0;
double switchTimeThre = 1.0;
double dirDiffThre = 0.1;
double stopDisThre = 0.2;
double slowDwnDisThre = 1.0;
bool useInclRateToSlow = false;
double inclRateThre = 120.0;
double slowRate1 = 0.25;
double slowRate2 = 0.5;
double slowTime1 = 2.0;
double slowTime2 = 2.0;
bool useInclToStop = false;
double inclThre = 45.0;
double stopTime = 5.0;
bool noRotAtStop = false;
bool noRotAtGoal = true;
bool autonomyMode = false;
double autonomySpeed = 1.0;
double joyToSpeedDelay = 2.0;
double currAngle = 0.0;
float joySpeed = 0;
float joySpeedRaw = 0;
float joyYaw = 0;
int safetyStop = 0;
float vehicleX = 0;
float vehicleY = 0;
float vehicleZ = 0;
float vehicleRoll = 0;
float vehiclePitch = 0;
float vehicleYaw = 0;
float vehicleXRec = 0;
float vehicleYRec = 0;
float vehicleZRec = 0;
float vehicleRollRec = 0;
float vehiclePitchRec = 0;
float vehicleYawRec = 0;
float vehicleYawRate = 0;
float vehicleSpeed = 0;
double odomTime = 0;
double joyTime = 0;
double slowInitTime = 0;
double stopInitTime = false;
int pathPointID = 0;
bool pathInit = false;
bool navFwd = true;
double switchTime = 0;
// Calculate the rotation matrix
Eigen::Matrix2d goal2map;
Eigen::Matrix2d map2odom_rotation;
Eigen::Matrix2d goal2odom;
Eigen::Matrix2cd odom2goal;
tf::StampedTransform map2odom;
nav_msgs::Path path;
void odomHandler(const nav_msgs::Odometry::ConstPtr& odomIn)
{
odomTime = odomIn->header.stamp.toSec();
double roll, pitch, yaw;
geometry_msgs::Quaternion geoQuat = odomIn->pose.pose.orientation;
tf::Matrix3x3(tf::Quaternion(geoQuat.x, geoQuat.y, geoQuat.z, geoQuat.w)).getRPY(roll, pitch, yaw);
vehicleRoll = roll;
vehiclePitch = pitch;
vehicleYaw = yaw;
vehicleX = odomIn->pose.pose.position.x - cos(yaw) * sensorOffsetX + sin(yaw) * sensorOffsetY;
vehicleY = odomIn->pose.pose.position.y - sin(yaw) * sensorOffsetX - cos(yaw) * sensorOffsetY;
vehicleZ = odomIn->pose.pose.position.z;
//rad, too steep to move
if ((fabs(roll) > inclThre * PI / 180.0 || fabs(pitch) > inclThre * PI / 180.0) && useInclToStop) {
stopInitTime = odomIn->header.stamp.toSec();
}
if ((fabs(odomIn->twist.twist.angular.x) > inclRateThre * PI / 180.0 || fabs(odomIn->twist.twist.angular.y) > inclRateThre * PI / 180.0) && useInclRateToSlow) {
slowInitTime = odomIn->header.stamp.toSec();
}
}
void pathHandler(const nav_msgs::Path::ConstPtr& pathIn)
{
int pathSize = pathIn->poses.size();
path.poses.resize(pathSize);
for (int i = 0; i < pathSize; i++) {
path.poses[i].pose.position.x = pathIn->poses[i].pose.position.x;
path.poses[i].pose.position.y = pathIn->poses[i].pose.position.y;
path.poses[i].pose.position.z = pathIn->poses[i].pose.position.z;
}
vehicleXRec = vehicleX;
vehicleYRec = vehicleY;
vehicleZRec = vehicleZ;
vehicleRollRec = vehicleRoll;
vehiclePitchRec = vehiclePitch;
vehicleYawRec = vehicleYaw;
pathPointID = 0;
pathInit = true;
}
void joystickHandler(const sensor_msgs::Joy::ConstPtr& joy)
{
joyTime = ros::Time::now().toSec();
joySpeedRaw = sqrt(joy->axes[3] * joy->axes[3] + joy->axes[4] * joy->axes[4]);
joySpeed = joySpeedRaw;
if (joySpeed > 1.0) joySpeed = 1.0;
if (joy->axes[4] == 0) joySpeed = 0;
joyYaw = joy->axes[3];
if (joySpeed == 0 && noRotAtStop) joyYaw = 0;
if (joy->axes[4] < 0 && !twoWayDrive) {
joySpeed = 0;
joyYaw = 0;
}
if (joy->axes[2] > -0.1) {
autonomyMode = false;
} else {
autonomyMode = true;
}
}
void speedHandler(const std_msgs::Float32::ConstPtr& speed)
{
double speedTime = ros::Time::now().toSec();
if (autonomyMode && speedTime - joyTime > joyToSpeedDelay && joySpeedRaw == 0) {
joySpeed = speed->data / maxSpeed;
if (joySpeed < 0) joySpeed = 0;
else if (joySpeed > 1.0) joySpeed = 1.0;
}
}
void stopHandler(const std_msgs::Int8::ConstPtr& stop)
{
safetyStop = stop->data;
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "pathFollower");
ros::NodeHandle nh;
ros::NodeHandle nhPrivate = ros::NodeHandle("~");
nhPrivate.getParam("sensorOffsetX", sensorOffsetX);
nhPrivate.getParam("sensorOffsetY", sensorOffsetY);
nhPrivate.getParam("pubSkipNum", pubSkipNum);
nhPrivate.getParam("twoWayDrive", twoWayDrive);
nhPrivate.getParam("lookAheadDis", lookAheadDis);
nhPrivate.getParam("yawRateGain", yawRateGain);
nhPrivate.getParam("stopYawRateGain", stopYawRateGain);
nhPrivate.getParam("maxYawRate", maxYawRate);
nhPrivate.getParam("maxSpeed", maxSpeed);
nhPrivate.getParam("maxAccel", maxAccel);
nhPrivate.getParam("switchTimeThre", switchTimeThre);
nhPrivate.getParam("dirDiffThre", dirDiffThre);
nhPrivate.getParam("stopDisThre", stopDisThre);
nhPrivate.getParam("slowDwnDisThre", slowDwnDisThre);
nhPrivate.getParam("useInclRateToSlow", useInclRateToSlow);
nhPrivate.getParam("inclRateThre", inclRateThre);
nhPrivate.getParam("slowRate1", slowRate1);
nhPrivate.getParam("slowRate2", slowRate2);
nhPrivate.getParam("slowTime1", slowTime1);
nhPrivate.getParam("slowTime2", slowTime2);
nhPrivate.getParam("useInclToStop", useInclToStop);
nhPrivate.getParam("inclThre", inclThre);
nhPrivate.getParam("stopTime", stopTime);
nhPrivate.getParam("noRotAtStop", noRotAtStop);
nhPrivate.getParam("noRotAtGoal", noRotAtGoal);
nhPrivate.getParam("autonomyMode", autonomyMode);
nhPrivate.getParam("autonomySpeed", autonomySpeed);
nhPrivate.getParam("joyToSpeedDelay", joyToSpeedDelay);
ros::Subscriber subOdom = nh.subscribe<nav_msgs::Odometry> ("/state_estimation", 5, odomHandler);
ros::Subscriber subPath = nh.subscribe<nav_msgs::Path> ("/path", 5, pathHandler);
ros::Subscriber subJoystick = nh.subscribe<sensor_msgs::Joy> ("/joy", 5, joystickHandler);
ros::Subscriber subSpeed = nh.subscribe<std_msgs::Float32> ("/speed", 5, speedHandler);
ros::Subscriber subStop = nh.subscribe<std_msgs::Int8> ("/stop", 5, stopHandler);
ros::Publisher pubSpeed = nh.advertise<geometry_msgs::TwistStamped> ("/cmd_vel", 5);
geometry_msgs::TwistStamped cmd_vel;
tf::TransformListener listener(ros::Duration(60)); // Increase the buffer size if needed
cmd_vel.header.frame_id = "vehicle";
if (autonomyMode) {
joySpeed = autonomySpeed / maxSpeed;
if (joySpeed < 0) joySpeed = 0;
else if (joySpeed > 1.0) joySpeed = 1.0;
}
ros::Rate rate(100);
bool status = ros::ok();
while (status) {
ros::spinOnce();
if (pathInit) {
float vehicleXRel = cos(vehicleYawRec) * (vehicleX - vehicleXRec)
+ sin(vehicleYawRec) * (vehicleY - vehicleYRec);
float vehicleYRel = -sin(vehicleYawRec) * (vehicleX - vehicleXRec)
+ cos(vehicleYawRec) * (vehicleY - vehicleYRec);
int pathSize = path.poses.size();
float endDisX = path.poses[pathSize - 1].pose.position.x - vehicleXRel;
float endDisY = path.poses[pathSize - 1].pose.position.y - vehicleYRel;
float endDis = sqrt(endDisX * endDisX + endDisY * endDisY);
float disX, disY, dis;
while (pathPointID < pathSize - 1) {
disX = path.poses[pathPointID].pose.position.x - vehicleXRel;
disY = path.poses[pathPointID].pose.position.y - vehicleYRel;
dis = sqrt(disX * disX + disY * disY);
if (dis < lookAheadDis) {
pathPointID++;
} else {
break;
}
}
// Get current segmented point, x and y
disX = path.poses[pathPointID].pose.position.x + vehicleXRel;
disY = path.poses[pathPointID].pose.position.y + vehicleYRel;
dis = sqrt(disX * disX + disY * disY);
// Get segmented moving direction
float pathDir = atan2(disY, disX);
pathDir = pathDir * 180.0 / M_PI;
// Set rotation matrix
// TODO: give it to velocity
goal2map << cos(pathDir), -sin(pathDir),
sin(pathDir), cos(pathDir);
goal2map = goal2map.transpose();
try {
// The code where you perform the transform lookup causing the exception
listener.lookupTransform("map", "vehicle", ros::Time(odomTime), map2odom);
// ... (rest of the code)
} catch (tf::TransformException& ex) {
// Handle the exception
ROS_ERROR("Transform lookup failed: %s", ex.what());
// You can add more detailed error handling here if needed
}
// Extract the rotation matrix
tf::Matrix3x3 rotation_matrix = map2odom.getBasis();
double roll, pitch, yaw;
rotation_matrix.getRPY(roll, pitch, yaw);
map2odom_rotation << cos(yaw), -sin(yaw),
sin(yaw), cos(yaw);
ROS_INFO("Current yaw is: %f", yaw);
goal2odom = goal2map * map2odom_rotation;
// TODO: publish velocity directly, using rotation matrix
float dirDiff = vehicleYaw - vehicleYawRec - pathDir;
if (dirDiff > PI) dirDiff -= 2 * PI;
else if (dirDiff < -PI) dirDiff += 2 * PI;
if (dirDiff > PI) dirDiff -= 2 * PI;
else if (dirDiff < -PI) dirDiff += 2 * PI;
if (twoWayDrive) {
double time = ros::Time::now().toSec();
if (fabs(dirDiff) > PI / 2 && navFwd && time - switchTime > switchTimeThre) {
navFwd = false;
switchTime = time;
} else if (fabs(dirDiff) < PI / 2 && !navFwd && time - switchTime > switchTimeThre) {
navFwd = true;
switchTime = time;
}
}
float joySpeed2 = maxSpeed * joySpeed;
// TODO: Do something here
if (!navFwd) {
dirDiff += PI;
if (dirDiff > PI) dirDiff -= 2 * PI;
joySpeed2 *= -1;
}
if (fabs(vehicleSpeed) < 2.0 * maxAccel / 100.0) vehicleYawRate = -stopYawRateGain * dirDiff;
else vehicleYawRate = -yawRateGain * dirDiff;
if (vehicleYawRate > maxYawRate * PI / 180.0) vehicleYawRate = maxYawRate * PI / 180.0;
else if (vehicleYawRate < -maxYawRate * PI / 180.0) vehicleYawRate = -maxYawRate * PI / 180.0;
if (joySpeed2 == 0 && !autonomyMode) {
vehicleYawRate = maxYawRate * joyYaw * PI / 180.0;
} else if (pathSize <= 1 || (dis < stopDisThre && noRotAtGoal)) {
vehicleYawRate = 0;
}
if (pathSize <= 1) {
joySpeed2 = 0;
} else if (endDis / slowDwnDisThre < joySpeed) {
joySpeed2 *= endDis / slowDwnDisThre;
}
float joySpeed3 = joySpeed2;
if (odomTime < slowInitTime + slowTime1 && slowInitTime > 0) joySpeed3 *= slowRate1;
else if (odomTime < slowInitTime + slowTime1 + slowTime2 && slowInitTime > 0) joySpeed3 *= slowRate2;
if (fabs(dirDiff) < dirDiffThre && dis > stopDisThre) {
if (vehicleSpeed < joySpeed3) vehicleSpeed += maxAccel / 100.0;
else if (vehicleSpeed > joySpeed3) vehicleSpeed -= maxAccel / 100.0;
} else {
if (vehicleSpeed > 0) vehicleSpeed -= maxAccel / 100.0;
else if (vehicleSpeed < 0) vehicleSpeed += maxAccel / 100.0;
}
if (odomTime < stopInitTime + stopTime && stopInitTime > 0) {
vehicleSpeed = 0;
vehicleYawRate = 0;
}
if (safetyStop >= 1)
ROS_INFO("Current vehicleSpeed is %f", vehicleSpeed);
pubSkipCount--;
// ROS_INFO("Current pub skit count is: %d", pubSkipCount);
if (pubSkipCount < 0) {
cmd_vel.header.stamp = ros::Time().fromSec(odomTime);
if (fabs(vehicleSpeed) <= maxAccel / 100.0) cmd_vel.twist.linear.x = 0;
else cmd_vel.twist.linear.x = vehicleSpeed;
odom2goal = goal2odom.transpose();
cmd_vel.twist.linear.x = vehicleSpeed * odom2goal(0, 0).real();
cmd_vel.twist.linear.y = vehicleSpeed * odom2goal(1, 0).real();
// cmd_vel.twist.angular.z = vehicleYawRate;
pubSpeed.publish(cmd_vel);
pubSkipCount = pubSkipNum;
}
}
status = ros::ok();
rate.sleep();
}
return 0;
}