-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLAM with obstacle avoidance.cpp
More file actions
197 lines (156 loc) · 5.32 KB
/
SLAM with obstacle avoidance.cpp
File metadata and controls
197 lines (156 loc) · 5.32 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
#include<ros/ros.h>
#include<sensor_msgs/LaserScan.h>
#include<geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <tf/tf.h>
#include <fstream>
#include <math.h>
#define PI 3.141592653589
using namespace std;
class MyRobot {
public:
MyRobot();
void Moving();
void laser_callback(const sensor_msgs::LaserScan::ConstPtr &laserMsg);
void odom_callback(const nav_msgs::Odometry::ConstPtr &odomMsg);
void set_velocity(double velocity_x, double velocity_z);
void stop();
void avoid();
double calcAngle(double pointX, double pointY);
double my_x = 0.0;
double my_y = 0.0;
double my_th = 0.0;
double lDist; //left 'antenna'
double rDist; //right 'antenna'
double points[4][2] = {{-1, 2}, {2, 0.5}, {1, -2}, {-2, -0.5}}; //array of waypoints
ofstream myfile;
double my_ls[360];//distance from obstacle on each degree
ros::NodeHandle nh;
ros::Publisher cmdPub;
ros::Subscriber laserSub;
ros::Subscriber odomSub;
geometry_msgs::Twist my_move;
private:
};
MyRobot::MyRobot(){
MyRobot::cmdPub = MyRobot::nh.advertise<geometry_msgs::Twist>("/cmd_vel", 100);
MyRobot::laserSub = MyRobot::nh.subscribe("/scan",1000, &MyRobot::laser_callback,this);
MyRobot::odomSub = MyRobot::nh.subscribe("/odom", 100, &MyRobot::odom_callback,this);
}
void MyRobot::Moving() {
myfile.open("/home/local/CAMPUS/ms19569/M-Drive/turtlebot_ws/src/ce215_pkg/src/myodom.txt");
ros::Rate rate(100);
if (ros::ok) {//MOVEMENT IN HERE
for (int i = 0; i < 4; i++) { //num of points
bool reached = false;
stop();
//set destination coords
double destX = points[i][0];
double destY = points[i][1];
stop(); //reset velocity before starting each
ros::Duration(0.2).sleep();
ros::spinOnce();
while (!reached) {
//update odom and sensor data
ros::spinOnce();
//calculate angle to point
ros::spinOnce();
double angle = calcAngle(destX, destY) - my_th;
//turn toward point
if (!(my_th < angle + (PI / 45) && my_th > angle - (PI / 45))) { //within 4 degree margin
if(angle>0){
set_velocity(0.0, 0.5);
} else {
set_velocity(0.0, -0.5);
}
ros::Duration(abs(angle*2)).sleep();
}
//avoid obstacles
avoid();
//move to point
set_velocity(0.15, 0);
ros::Duration(0.2).sleep();
//if reached, break
ros::spinOnce();
double xDist = destX - my_x;
double yDist = destY - my_y;
if (hypot(xDist, yDist) < 0.3) { //if within 0.1 of destination (radially)
reached = true;
}
rate.sleep();
}
}
}
myfile.close();
}
double MyRobot::calcAngle(double pointX, double pointY){ //calculates angle (from x-axis) from current point to dest. point
double angle, dX, dY;
dY = pointY - this->my_y;
dX = pointX - this->my_x;
angle = atan2(dY,dX);
return angle;
}
void MyRobot::avoid() {
ros::spinOnce();
stop();
if (lDist < rDist) {
while (lDist < 0.4) {//rotate past object
//update
ros::spinOnce();
set_velocity(0, 0.4); //rotate
ros::Duration(0.1).sleep();
}
} else {
while (rDist < 0.4) { //rotate past object
//update
ros::spinOnce();
set_velocity(0, -0.4); //rotate
ros::Duration(0.1).sleep();
}
}
//once obstacle is avoided, stop rotating and move past
set_velocity(0.10, 0);
ros::Duration(1).sleep();
}
void MyRobot::set_velocity(double velocity_x, double velocity_w)
{
my_move.linear.x = velocity_x;
my_move.linear.y = 0;
my_move.angular.z = velocity_w; //angle
cmdPub.publish(my_move);
} //forward
void MyRobot::stop(){
my_move.linear.x = 0;
my_move.linear.y = 0;
my_move.angular.z = 0;
cmdPub.publish(my_move);
}
void MyRobot::laser_callback(const sensor_msgs::LaserScan::ConstPtr& laserMsg) {
for (int i = 0; i < laserMsg->ranges.size(); i++) {
if (laserMsg->ranges[i] > 0.4) MyRobot::my_ls[i] = 0.4; //cap detected distance at 0.4
else MyRobot::my_ls[i] = laserMsg->ranges[i];
}
lDist = this->my_ls[360 - 24]; //left
rDist = this->my_ls[24]; //right
}
void MyRobot::odom_callback(const nav_msgs::Odometry::ConstPtr &odomMsg){
MyRobot::my_x = odomMsg->pose.pose.position.x;
MyRobot::my_y = odomMsg->pose.pose.position.y;
tf::Quaternion q(
odomMsg ->pose.pose.orientation.x,
odomMsg ->pose.pose.orientation.y,
odomMsg ->pose.pose.orientation.z,
odomMsg ->pose.pose.orientation.w);
tf::Matrix3x3 m(q);
double roll, pitch, yaw;
m.getRPY(roll, pitch, yaw);
MyRobot::my_th = yaw;
myfile << this->my_x << "," << this->my_y << "," << endl;
}
int main(int argc,char**argv){
ros::init(argc, argv, "myrobot_node");
MyRobot mr;
mr.Moving();
ROS_INFO("completed!");
return 0;
}