-
Notifications
You must be signed in to change notification settings - Fork 31
/
controller.cpp
executable file
·260 lines (230 loc) · 7.31 KB
/
controller.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
#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <std_srvs/Empty.h>
#include <geometry_msgs/Twist.h>
#include "pid.hpp"
double get(
const ros::NodeHandle& n,
const std::string& name) {
double value;
n.getParam(name, value);
return value;
}
class Controller
{
public:
Controller(
const std::string& worldFrame,
const std::string& frame,
const ros::NodeHandle& n)
: m_worldFrame(worldFrame)
, m_frame(frame)
, m_pubNav()
, m_listener()
, m_pidX(
get(n, "PIDs/X/kp"),
get(n, "PIDs/X/kd"),
get(n, "PIDs/X/ki"),
get(n, "PIDs/X/minOutput"),
get(n, "PIDs/X/maxOutput"),
get(n, "PIDs/X/integratorMin"),
get(n, "PIDs/X/integratorMax"),
"x")
, m_pidY(
get(n, "PIDs/Y/kp"),
get(n, "PIDs/Y/kd"),
get(n, "PIDs/Y/ki"),
get(n, "PIDs/Y/minOutput"),
get(n, "PIDs/Y/maxOutput"),
get(n, "PIDs/Y/integratorMin"),
get(n, "PIDs/Y/integratorMax"),
"y")
, m_pidZ(
get(n, "PIDs/Z/kp"),
get(n, "PIDs/Z/kd"),
get(n, "PIDs/Z/ki"),
get(n, "PIDs/Z/minOutput"),
get(n, "PIDs/Z/maxOutput"),
get(n, "PIDs/Z/integratorMin"),
get(n, "PIDs/Z/integratorMax"),
"z")
, m_pidYaw(
get(n, "PIDs/Yaw/kp"),
get(n, "PIDs/Yaw/kd"),
get(n, "PIDs/Yaw/ki"),
get(n, "PIDs/Yaw/minOutput"),
get(n, "PIDs/Yaw/maxOutput"),
get(n, "PIDs/Yaw/integratorMin"),
get(n, "PIDs/Yaw/integratorMax"),
"yaw")
, m_state(Idle)
, m_goal()
, m_subscribeGoal()
, m_serviceTakeoff()
, m_serviceLand()
, m_thrust(0)
, m_startZ(0)
{
ros::NodeHandle nh;
m_listener.waitForTransform(m_worldFrame, m_frame, ros::Time(0), ros::Duration(10.0));
m_pubNav = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1);
m_subscribeGoal = nh.subscribe("goal", 1, &Controller::goalChanged, this);
m_serviceTakeoff = nh.advertiseService("takeoff", &Controller::takeoff, this);
m_serviceLand = nh.advertiseService("land", &Controller::land, this);
}
void run(double frequency)
{
ros::NodeHandle node;
ros::Timer timer = node.createTimer(ros::Duration(1.0/frequency), &Controller::iteration, this);
ros::spin();
}
private:
void goalChanged(
const geometry_msgs::PoseStamped::ConstPtr& msg)
{
m_goal = *msg;
}
bool takeoff(
std_srvs::Empty::Request& req,
std_srvs::Empty::Response& res)
{
ROS_INFO("Takeoff requested!");
m_state = TakingOff;
tf::StampedTransform transform;
m_listener.lookupTransform(m_worldFrame, m_frame, ros::Time(0), transform);
m_startZ = transform.getOrigin().z();
return true;
}
bool land(
std_srvs::Empty::Request& req,
std_srvs::Empty::Response& res)
{
ROS_INFO("Landing requested!");
m_state = Landing;
return true;
}
void getTransform(
const std::string& sourceFrame,
const std::string& targetFrame,
tf::StampedTransform& result)
{
m_listener.lookupTransform(sourceFrame, targetFrame, ros::Time(0), result);
}
void pidReset()
{
m_pidX.reset();
m_pidY.reset();
m_pidZ.reset();
m_pidYaw.reset();
}
void iteration(const ros::TimerEvent& e)
{
float dt = e.current_real.toSec() - e.last_real.toSec();
switch(m_state)
{
case TakingOff:
{
tf::StampedTransform transform;
m_listener.lookupTransform(m_worldFrame, m_frame, ros::Time(0), transform);
if (transform.getOrigin().z() > m_startZ + 0.05 || m_thrust > 50000)
{
pidReset();
m_pidZ.setIntegral(m_thrust / m_pidZ.ki());
m_state = Automatic;
m_thrust = 0;
}
else
{
m_thrust += 10000 * dt;
geometry_msgs::Twist msg;
msg.linear.z = m_thrust;
m_pubNav.publish(msg);
}
}
break;
case Landing:
{
m_goal.pose.position.z = m_startZ + 0.05;
tf::StampedTransform transform;
m_listener.lookupTransform(m_worldFrame, m_frame, ros::Time(0), transform);
if (transform.getOrigin().z() <= m_startZ + 0.05) {
m_state = Idle;
geometry_msgs::Twist msg;
m_pubNav.publish(msg);
}
}
// intentional fall-thru
case Automatic:
{
tf::StampedTransform transform;
m_listener.lookupTransform(m_worldFrame, m_frame, ros::Time(0), transform);
geometry_msgs::PoseStamped targetWorld;
targetWorld.header.stamp = transform.stamp_;
targetWorld.header.frame_id = m_worldFrame;
targetWorld.pose = m_goal.pose;
geometry_msgs::PoseStamped targetDrone;
m_listener.transformPose(m_frame, targetWorld, targetDrone);
tfScalar roll, pitch, yaw;
tf::Matrix3x3(
tf::Quaternion(
targetDrone.pose.orientation.x,
targetDrone.pose.orientation.y,
targetDrone.pose.orientation.z,
targetDrone.pose.orientation.w
)).getRPY(roll, pitch, yaw);
geometry_msgs::Twist msg;
msg.linear.x = m_pidX.update(0, targetDrone.pose.position.x);
msg.linear.y = m_pidY.update(0.0, targetDrone.pose.position.y);
msg.linear.z = m_pidZ.update(0.0, targetDrone.pose.position.z);
msg.angular.z = m_pidYaw.update(0.0, yaw);
m_pubNav.publish(msg);
}
break;
case Idle:
{
geometry_msgs::Twist msg;
m_pubNav.publish(msg);
}
break;
}
}
private:
enum State
{
Idle = 0,
Automatic = 1,
TakingOff = 2,
Landing = 3,
};
private:
std::string m_worldFrame;
std::string m_frame;
ros::Publisher m_pubNav;
tf::TransformListener m_listener;
PID m_pidX;
PID m_pidY;
PID m_pidZ;
PID m_pidYaw;
State m_state;
geometry_msgs::PoseStamped m_goal;
ros::Subscriber m_subscribeGoal;
ros::ServiceServer m_serviceTakeoff;
ros::ServiceServer m_serviceLand;
float m_thrust;
float m_startZ;
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "controller");
// Read parameters
ros::NodeHandle n("~");
std::string worldFrame;
n.param<std::string>("worldFrame", worldFrame, "/world");
std::string frame;
n.getParam("frame", frame);
double frequency;
n.param("frequency", frequency, 50.0);
Controller controller(worldFrame, frame, n);
controller.run(frequency);
return 0;
}