-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3turtle.cpp
More file actions
157 lines (146 loc) · 3.65 KB
/
Copy path3turtle.cpp
File metadata and controls
157 lines (146 loc) · 3.65 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
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Pose.h>
#include <cmath>
#include <iostream>
#include <queue>
using namespace std;
int i = 0;
int tx1,ty1,tx2,ty2,tx3,ty3;
float x1, y1, theta1, v1, vt1;
float x2, y2, theta2, v2, vt2;
float x3, y3, theta3, v3, vt3;
int state = 0, rate = 600;
float a_z1,a_z2,a_z3;
float target, target_angle, t_a;
float target2, target_angle2, t_a2;
float target3, target_angle3, t_a3;
double pi = 3.1415926535;
ros::Publisher pub1,pub2,pub3;
geometry_msgs::Twist getMessage(double linear_x, double angular_z)
{
geometry_msgs::Twist msg;
msg.linear.x = linear_x;
msg.angular.z = angular_z;
return msg;
}
void move2()
{
if (target2 > 0.00001)
{
if (target2 > 0.1)
{
if (abs(t_a2 - theta2) > pi / 4 && abs(t_a2 - theta2) < 2 * pi - pi / 4)
{
pub2.publish(getMessage(0, 2 * a_z2 / abs(a_z2)));
cout << "1" << endl;
}
else
{
pub2.publish(getMessage(2, 1 * a_z2 / abs(a_z2)));
cout << "2" << endl;
}
}
else
{
pub2.publish(getMessage(target2, a_z2));
cout << "3" << endl;
}
}
else
{
pub2.publish(getMessage(0, 0));
state = 0;
}
}
void poseCallback2(const turtlesim::Pose::ConstPtr &msg)
{
x2 = msg->x, y2 = msg->y, theta2 = msg->theta,
v2 = msg->linear_velocity, vt2 = msg->angular_velocity;
// if (abs(t_a - theta) > pi / 4)
// {
// cout << "a=" << t_a << endl
// << "t=" << target_angle << endl
// << "vt=" << vt << endl
// << endl;
// };
if (v2 == 0 && vt2 == 0)
cout << "Stop" << endl;
if (theta2 < 0)
theta2 = 2 * pi + theta2;
target2 = sqrt((tx2 - x2) * (tx2 - x2) + (ty2 - y2) * (ty2 - y2));
target_angle2 = atan2(ty2 - y2, tx2 - x2);
if (i != 0)
{
if (target2) > 0.00001)
state = 1;
else
state = 0;
t_a = target_angle;
if (target_angle2 < 0)
{
t_a2 = 2 * pi + target_angle2;
}
// if (ty < y && tx < x)
// t_a = 2 * pi + target_angle;
// else
// t_a = target_angle;
}
else
{
t_a2 = 0;
target2 = 0.1;
state = 0;
i++;
}
if (t_a2 < pi)
{
if (theta2 < t_a2 + pi)
a_z2 = t_a2 - theta2;
else
a_z2 = theta2 - t_a2;
}
else
{
if (theta2 < t_a2 - pi)
a_z2 = theta2 - t_a2;
else
a_z2 = t_a2 - theta2;
}
tx3 = msg->x, ty3 = msg->y;
}
void poseCallback1(const turtlesim::Pose::ConstPtr &msg)
{
tx = msg->x, ty = msg->y;
}
void move(float x, float y)
{
state = 1;
target = sqrt((tx - x) * (tx - x) + (ty - y) * (ty - y));
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "myturtle_control");
ros::NodeHandle h;
pub1 = h.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000);
pub2 = h.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", 1000);
pub3 = h.advertise<geometry_msgs::Twist>("turtle3/cmd_vel", 1000);
ros::Subscriber sub1 =
h.subscribe("/turtle1/pose", 1000, poseCallback1);
ros::Subscriber sub2 =
h.subscribe("/turtle2/pose", 1000, poseCallback2);
ros::Subscriber sub3 =
h.subscribe("/turtle3/pose", 1000, poseCallback3);
ros::Rate loopRate(rate);
bool in_action = false;
while (ros::ok())
{
if (state == 1)
{
move();
}
loopRate.sleep();
ros::spinOnce();
}
return 0;
}