-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5angle.cpp
145 lines (130 loc) · 3.19 KB
/
5angle.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
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Pose.h>
#include <iostream>
#include <queue>
using namespace std;
int i = 0;
float x, y, theta, v, vt;
int state = 0, rate = 30;
float dist;
float target_angle;
float target_distance;
double pi = 3.1415926535;
ros::Publisher pub;
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 handleStateRotate()
{
if (abs(target_angle - theta) > 0.01)
{
if (abs(target_angle - theta) > 1 / rate)
pub.publish(getMessage(0, target_angle - theta));
else
{
pub.publish(getMessage(0, rate * (target_angle - theta)));
}
}
else
{
pub.publish(getMessage(0, 0));
state = 0;
}
}
void handleStateStraightForward()
{
if (target_distance <= 0)
{
state = 0;
pub.publish(getMessage(0, 0));
}
else
{
if (target_distance > 1.0 / rate)
pub.publish(getMessage(1, 0));
else
pub.publish(getMessage(target_distance * rate, 0));
}
}
void poseCallback(const turtlesim::Pose::ConstPtr &msg)
{
static bool firstCall = true;
float prevx = x, prevy = y;
x = msg->x, y = msg->y, theta = msg->theta,
v = msg->linear_velocity, vt = msg->angular_velocity;
dist = sqrt((x - prevx) * (x - prevx) + (y - prevy) * (y - prevy));
if (!firstCall)
target_distance -= dist;
firstCall = false;
}
void rotate(float angle)
{
state = 1;
target_angle = angle;
}
void straight_forward(float distance)
{
state = 2;
target_distance = distance;
}
struct Action
{
int type;
float target_angle;
float target_distance;
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "myturtle_control");
ros::NodeHandle h;
pub = h.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000);
ros::Subscriber sub =
h.subscribe("/turtle1/pose", 1000, poseCallback);
ros::Rate loopRate(rate);
//float tx = 8.54444444, ty = 7.544444;
queue<Action> q;
q.push({2, 0, 3});
q.push({1, 72 * pi / 180, 0});
q.push({2, 0, 3});
q.push({1, pi - 36 * pi / 180, 0});
q.push({2, 0, 3});
q.push({1, 36 * pi / 180 + pi, 0});
q.push({2, 0, 3});
q.push({1, 3 * pi / 2 + 18 * pi / 180, 0});
q.push({2, 0, 3});
bool in_action = false;
while (ros::ok())
{
//pub.publish(getMessage(linear_x, 5.0));
//linear_x += 1.0;
if (state == 0 && !in_action)
{
if (q.size() > 0)
{
Action a = q.front();
q.pop();
if (a.type == 1)
rotate(a.target_angle);
else if (a.type == 2)
straight_forward(a.target_distance);
in_action = true;
}
}
else if (state == 0 && in_action)
{
in_action = false;
}
else if (state == 1)
handleStateRotate();
else if (state == 2)
handleStateStraightForward();
loopRate.sleep();
ros::spinOnce();
}
return 0;
}