-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrect_other.cpp
More file actions
224 lines (205 loc) · 7.79 KB
/
Copy pathrect_other.cpp
File metadata and controls
224 lines (205 loc) · 7.79 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
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
// 订阅的无人机当前位置数据
geometry_msgs::PoseStamped local_pos;
void local_pos_cb(const geometry_msgs::PoseStamped::ConstPtr& msg){
local_pos = *msg;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "offb_cfx");
ros::NodeHandle nh;
// 订阅无人机当前状态
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
("mavros/state", 10, state_cb);
// 订阅无人机当前位置(反馈消息)
ros::Subscriber local_pos_sub = nh.subscribe<geometry_msgs::PoseStamped>
("mavros/local_position/pose", 10, local_pos_cb);
// 发布无人机本地位置(控制)
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
("mavros/setpoint_position/local", 10);
// 服务的客户端(设定无人机的模式、状态)
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
("mavros/cmd/arming");
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
("mavros/set_mode");
//the setpoint publishing rate MUST be faster than 2Hz
ros::Rate rate(20.0);
// wait for FCU connection
while(ros::ok() && !current_state.connected){
ros::spinOnce();
rate.sleep();
}
geometry_msgs::PoseStamped pose;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 2;
//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
// 设定无人机工作模式 offboard
mavros_msgs::SetMode offb_set_mode;
offb_set_mode.request.custom_mode = "OFFBOARD";
// 无人机解锁
mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;
// 记录当前时间
ros::Time last_request = ros::Time::now();
// 用于走圈的变量
int step = 0;
int sametimes = 0;
while(ros::ok()){
// 无人机状态设定与判断
// 进入while循环后,先循环5s,然后再向客户端发送无人机状态设置的消息
// set_mode_client.call arming_client.call
if( current_state.mode != "OFFBOARD" &&
(ros::Time::now() - last_request > ros::Duration(5.0))){
if( set_mode_client.call(offb_set_mode) &&
offb_set_mode.response.mode_sent){
ROS_INFO("Offboard enabled");
}
last_request = ros::Time::now();
} else {
if( !current_state.armed &&
(ros::Time::now() - last_request > ros::Duration(5.0))){
if( arming_client.call(arm_cmd) &&
arm_cmd.response.success){
ROS_INFO("Vehicle armed");
}
last_request = ros::Time::now();
}
else // 无人机 Offboard enabled && Vehicle armed 后
{ // 无人机走矩形 每到达一个点停一会
// z: 0-->10 10-->10 10-->10 10-->10 10-->10 10-->0
// x: 0-->0 0-->40 40-->40 40-->0 0-->0 0-->0
// y: 0-->0 0-->0 0-->20 20-->20 20-->0 0-->0
// local_pos_pub.publish(pose);
switch (step)
{
case 0:
//take off to 2m 位置点控制
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 2;
//
if (local_pos.pose.position.z > 1.9 && local_pos.pose.position.z < 2.1)
{
if (sametimes > 20)
{
sametimes = 0;
step = 1;
pose.pose.position.x = 40;
pose.pose.position.y = 0;
pose.pose.position.z = 2;
}
else
sametimes++;
}
else
{
sametimes = 0;
}
local_pos_pub.publish(pose);
break;
case 1:
if (local_pos.pose.position.x > 39.9 && local_pos.pose.position.x < 40.1)
{
if (sametimes > 20)
{
step = 2;
pose.pose.position.x = 40;
pose.pose.position.y = 20;
pose.pose.position.z = 2;
}
else
sametimes++;
}
else
{
sametimes = 0;
}
break;
case 2:
if (local_pos.pose.position.y > 19.9 && local_pos.pose.position.y < 20.1)
{
if (sametimes > 20)
{
step = 3;
pose.pose.position.x = 0;
pose.pose.position.y = 20;
pose.pose.position.z = 2;
}
else
sametimes++;
}
else
{
sametimes = 0;
}
break;
case 3:
if (local_pos.pose.position.x > -0.1 && local_pos.pose.position.x < 0.1)
{
if (sametimes > 20)
{
step = 4;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 2;
}
else
sametimes++;
}
else
{
sametimes = 0;
}
break;
case 4:
if (local_pos.pose.position.y > -0.1 && local_pos.pose.position.y < 0.1)
{
if (sametimes > 20)
{
step = 5;
}
else
sametimes++;
}
else
{
sametimes = 0;
}
break;
case 5: // 准备降落
offb_set_mode.request.custom_mode = "AUTO.LAND";
if (current_state.mode != "AUTO.LAND" && (ros::Time::now() - last_request > ros::Duration(5.0)))
{
if (set_mode_client.call(offb_set_mode) && offb_set_mode.response.mode_sent)
{
ROS_INFO("AUTO.LAND enabled");
}
last_request = ros::Time::now();
}
break;
default:
break;
}
}
}
// 发布位置控制信息
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep(); // 影响消息发布与更新的周期
}
return 0;
}