-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPointRobot.cpp
More file actions
392 lines (339 loc) · 11.7 KB
/
PointRobot.cpp
File metadata and controls
392 lines (339 loc) · 11.7 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
#include "PointRobot.h"
#define LOCALIZETHRESHOLD .020
#define THRESHDIFF .005
/**
Constructor
@param fname The filename containing all destination points
@param VARIANCE The amount of error we are willing to allow
*/
PointRobot::PointRobot(char* fname, double SPEED, double VARIANCE, char* mname) {
std::string map_name = mname;
this->MAP_WIDTH = 2000;
this->MAP_HEIGHT = 700;
this->MAP_DATA = new int8_t[this->MAP_WIDTH*this->MAP_HEIGHT];
this->MAP_RESOLUTION = 0.0633;
this->read_image(map_name.c_str());
this->destinations = this->read_file(fname);
this->VARIANCE = VARIANCE;
this->ANGULAR_VELOCITY = 0.0;
this->DEFAULT_SPEED = SPEED;
this->localizer = new Localizer(MAP_DATA, MAP_WIDTH, MAP_HEIGHT, MAP_RESOLUTION);
this->path_planner = new PathPlanner(MAP_DATA, MAP_WIDTH, MAP_HEIGHT, MAP_RESOLUTION);
this->sonar_change = new bool(false);
this->pathCalculated = false;
this->gotoPoints = nav_msgs::Path();
}
void PointRobot::whereAmI() {
suspectedLocation = localizer->update_location(this->pose, this->kinect_data, this->sonar_data, sonar_change);
if(suspectedLocation == NULL){
suspectedLocation = new Particle(0, 0, 0);
suspectedLocation->weight = 0;
}
geometry_msgs::PoseArray arr = localizer->get_particle_poses();
point_cloud_pub.publish(arr);
if(this->destinations.empty()){
printf("no global destinations left\n");
return;
}
if(suspectedLocation->weight > LOCALIZETHRESHOLD && !pathCalculated){
printf("making path\n");
dest d = this->destinations.front();
gotoPoints = path_planner->plan(suspectedLocation->x, suspectedLocation->y, d.x, d.y);
if(gotoPoints.poses.empty()){
printf("Made path but gotopoints is empty\n");
return;
}
printf("going here: %f, %f\n", d.x, d.y);
this->destinations.pop();
path_planning_pub.publish(gotoPoints);
pathCalculated = true;
}
}
/**
The callback function responsible for handling any information
retrieved from /r1/odom
@param msgs The message received from /r1/odom
*/
void PointRobot::odomCallback(nav_msgs::Odometry msgs) {
this->pose = msgs.pose.pose;
}
void PointRobot::sonarCallback(const p2os_msgs::SonarArray msgs) {
this->sonar_data = msgs;
*sonar_change = false;
}
void PointRobot::kinectCallback(const sensor_msgs::LaserScan msgs) {
this->kinect_data = msgs;
}
/**
When called, this function will determine the angular velocity that
the PointRobot instance should take on considering the destination location
as well as the current position and heading of the instance itself.
@return The new angular velocity value
*/
double PointRobot::getAngularVelocity() {
double destination_theta;
double delta_theta;
double x_pos = suspectedLocation->x;
double y_pos = suspectedLocation->y;
double dest_x = this->gotoPoints.poses[gotoPoints.poses.size()-1].pose.position.x;
double dest_y = this->gotoPoints.poses[gotoPoints.poses.size()-1].pose.position.y;
double delta_x = dest_x-x_pos;
double delta_y = dest_y-y_pos;
float angle = atan2(dest_y - y_pos, dest_x - x_pos);
double curangle = suspectedLocation->theta;
double anglediff = angle - curangle;
if(anglediff > PI){
anglediff -= 2*PI;
}
else if(anglediff < -PI){
anglediff += 2*PI;
}
if(fabs(anglediff) <= .1){
ANGULAR_VELOCITY = .08 * DEFAULT_SPEED * anglediff/fabs(anglediff);
}
else{
ANGULAR_VELOCITY = DEFAULT_SPEED * anglediff/fabs(anglediff);
}
return ANGULAR_VELOCITY;
}
/**
When called, this function is responsible for computing the forward velocity
of the PointRobot based on its proximity to the active destination point.
@return The forward velocity of the PointRobot
*/
double PointRobot::getForwardVelocity() {
double x_pos = suspectedLocation->x;
double y_pos = suspectedLocation->y;
double dest_x = this->gotoPoints.poses.back().pose.position.x;
double dest_y = this->gotoPoints.poses.back().pose.position.y;
double EXT_VARIANCE = VARIANCE*5;
if (
(
x_pos >= dest_x-VARIANCE
&& x_pos <= dest_x+VARIANCE
)
&& (
y_pos >= dest_y-VARIANCE
&& y_pos <= dest_y+VARIANCE
)
) {
// If the x_pos and y_pos values are in range of the destination point
// allowing for the specified error, we can stop the PointRobot and pop
// the active destination off of the destinations queue.
printf("DESTINATION REACHED: (%.2f, %.2f)\n", dest_x, dest_y);
gotoPoints.poses.pop_back();
return 0.0;
}
else if (
(
x_pos >= dest_x-EXT_VARIANCE
&& x_pos <= dest_x+EXT_VARIANCE
)
&& (
y_pos >= dest_y-EXT_VARIANCE
&& y_pos <= dest_y+EXT_VARIANCE
)
) {
// If the x_pos and y_pos values are not in range of the destination point
// allowing for specified error, but are at close proximity to that location,
// slow the speed of the PointRobot in order to prepare to stop.
return DEFAULT_SPEED/2;
}
else {
// Otherwise, full steam ahead.
return DEFAULT_SPEED;
}
}
/**
The main ROS loop responsible for combining all the above functions
in order to visit all the destination points specified by the input file.
@param argc The program argc input
@param argv The program argv input
*/
int PointRobot::run(int argc, char** argv, bool run_kinect, bool run_sonar) {
// Initialization
ros::init(argc, argv, "motion");
ros::NodeHandle n;
// Publish a latched value of 1 to the Motor State
ros::Publisher motor_state = n.advertise<p2os_msgs::MotorState>("cmd_motor_state", 1000, true);
p2os_msgs::MotorState state;
state.state = 1;
motor_state.publish(state);
// Publish a latched occupancy grid / map to RVIZ
ros::Publisher static_map = n.advertise<nav_msgs::OccupancyGrid>("static_map", 1000, true);
nav_msgs::OccupancyGrid grid;
std::vector<signed char> data(this->MAP_DATA, this->MAP_DATA+(this->MAP_WIDTH*this->MAP_HEIGHT));
grid.data = data;
grid.info.resolution = MAP_RESOLUTION;
grid.info.width = this->MAP_WIDTH;
grid.info.height = this->MAP_HEIGHT;
grid.info.origin.position.x = 0.0-grid.info.resolution*((double)this->MAP_WIDTH)/2;
grid.info.origin.position.y = 0.0-grid.info.resolution*((double)this->MAP_HEIGHT)/2;
static_map.publish(grid);
// Test the pose.stimation pusblisher with RVIZ, the generation of points will eventually
// be offloaded to Loaclizer.
point_cloud_pub = n.advertise<geometry_msgs::PoseArray>("point_cloud", 1000);
path_planning_pub = n.advertise<nav_msgs::Path>("global_path", 1000);
// Create a odom subscriber so that the robot can tell where it is
ros::Subscriber vel = n.subscribe<nav_msgs::Odometry>("/r1/odom", 1000, &PointRobot::odomCallback, this);
ros::Subscriber kinect;
ros::Subscriber sonar;
if (
run_kinect
) {
kinect = n.subscribe<sensor_msgs::LaserScan>("/r1/kinect_laser/scan", 50, &PointRobot::kinectCallback, this);
}
if (
run_sonar
) {
sonar = n.subscribe<p2os_msgs::SonarArray>("/r1/sonar", 50, &PointRobot::sonarCallback, this);
}
// Create the motion publisher and set the loop rate
ros::Publisher motion = n.advertise<geometry_msgs::Twist>("/r1/cmd_vel", 1000);
ros::Rate loop_rate(10);
// Main event loop
while (ros::ok())
{
geometry_msgs::Twist msg;
// Allow the subscriber callbacks to fire
ros::spinOnce();
this->whereAmI();
// If our destiinations queue is empty, try to find another location to visit
// If we have visited all destinations, we can exit
if (
destinations.empty() && gotoPoints.poses.empty()
) {
motion.publish(msg);
break;
}
if(
gotoPoints.poses.empty()
) {
pathCalculated = false;
msg.linear.x = .2;
motion.publish(msg);
continue;
}
// Get the angular velocity of the PointRobot
double x_pos = suspectedLocation->x;
double y_pos = suspectedLocation->y;
double dest_x = this->gotoPoints.poses.back().pose.position.x;
double dest_y = this->gotoPoints.poses.back().pose.position.y;
float angle = atan2(dest_y - y_pos, dest_x - x_pos);
double curangle = suspectedLocation->theta;
double anglediff = angle - curangle;
if(anglediff > PI){
anglediff -= 2*PI;
}
else if(anglediff < -PI){
anglediff += 2*PI;
}
//Check weight, if it's within a more generous threshold keep executing the path
//Otherwise wander a bit and try to relocalize more confidently
if (suspectedLocation->weight > LOCALIZETHRESHOLD - THRESHDIFF) {
msg.angular.z = this->getAngularVelocity();
// If we are not currently turning, calculate the forward velocity
// of the PointRobot
if (
fabs(anglediff) <= .1
) {
msg.linear.x = this->getForwardVelocity();
}
}
else {
printf("wandering\n");
msg.linear.x = 0.1;
}
// Publish our velocities to /r1/cmd_vel
motion.publish(msg);
// Sleep until next required action
loop_rate.sleep();
}
return 0;
}
/**
The function responsible for reading the input file into memory
and translating it into a queue of destination points.
@param file The name of the file to be read
*/
std::queue<dest> PointRobot::read_file(char *file) {
std::queue<dest> destinations;
std::ifstream read(file);
std::string s;
do {
getline(read, s);
if (
read.eof()
) {
break;
}
dest d;
std::sscanf(s.c_str(), "%lf %lf", &d.x, &d.y);
destinations.push(d);
} while (!read.eof());
return destinations;
}
void PointRobot::read_image(const char *file) {
std::ifstream read(file);
std::string s;
int p_val;
int mapIdx = this->MAP_WIDTH*this->MAP_HEIGHT - this->MAP_WIDTH;
do {
getline(read, s);
if (
read.eof()
) {
break;
}
p_val = atoi(s.c_str());
if (p_val != 0) {
this->MAP_DATA[mapIdx] = 0;
}
else {
this->MAP_DATA[mapIdx] = 100;
}
mapIdx++;
if (mapIdx % this->MAP_WIDTH == 0) {
mapIdx -= 2*this->MAP_WIDTH;
}
} while (!read.eof());
}
/**
usage error message
*/
void usage() {
printf("rosrun hw4 mapper <input_file> <map_file> [sonar] [kinect]\n");
}
/**
main
*/
int main(int argc, char **argv) {
bool run_kinect = false;
bool run_sonar = false;
if (
argc < 4
) {
usage();
return 0;
}
for (int i = 3; i < argc; i++) {
std::string arg(argv[i]);
if (
arg.compare("sonar") == 0
) {
run_sonar = true;
}
else if (
arg.compare("kinect") == 0
) {
run_kinect = true;
}
else {
printf("INVALID ARGUMENT: %s\n", argv[i]);
usage();
}
}
PointRobot robot (argv[1], 0.3, 0.3, argv[2]);
robot.run(argc, argv, run_kinect, run_sonar);
ros::shutdown();
}