-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautopilot.h
More file actions
265 lines (204 loc) · 6.24 KB
/
Copy pathautopilot.h
File metadata and controls
265 lines (204 loc) · 6.24 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
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
261
262
263
264
265
#ifndef _AUTOPILOT_H_
#define _AUTOPILOT_H_
#include <termios.h>
#include <string>
#include <sys/time.h>
#include <unistd.h>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <cmath>
#include <atomic>
#include <fstream>
#include "pixhawk.h"
#include "image.h"
#include "struct.h"
#include "mavlink2/common/mavlink.h" //changed this :- Mollik
typedef unsigned short int usi;
using namespace std;
#define MAVLINK_MSG_SET_POS_TAR_L_NED_POSITION 0b0000110111111000
#define MAVLINK_MSG_SET_POS_TAR_L_NED_POSITION_X 0b0000110111111110
#define MAVLINK_MSG_SET_POS_TAR_L_NED_POSITION_Y 0b0000110111111101
#define MAVLINK_MSG_SET_POS_TAR_L_NED_POSITION_z 0b0000110111111011
#define MAVLINK_MSG_SET_POS_TAR_L_NED_VELOCITY 0b0000110111000111
#define MAVLINK_MSG_SET_POS_TAR_L_NED_VELOCITY_X 0b0000110111110111
#define MAVLINK_MSG_SET_POS_TAR_L_NED_VELOCITY_Y 0b0000110111101111
#define MAVLINK_MSG_SET_POS_TAR_L_NED_VELOCITY_Z 0b0000110111011111
#define MAVLINK_MSG_SET_POS_TAR_L_NED_YAW 0b0000000111111111
#define MAVLINK_MSG_SET_POS_TAR_L_NED_ALL 0b0000110111000000
#define MAVLINK_MSG_SET_POS_TAR_L_NED_TAKEOFF 0x1000
#define MAVLINK_MSG_SET_ATT_EULER_RATE 0b11111000
#define MAVLINK_MSG_SET_ATT_QUATERNION 0b01111111
#define MAVLINK_MSG_SET_ATT_THRUST 0b10111111
#define MODE_OFFBOARD 393216
#define MODE_STABILIZED 458752
extern Buffers buffers;
struct QuadParam{
mavlink_local_position_ned_t iPos;
mavlink_attitude_t iAtt;
mavlink_local_position_ned_t cPos;
uint64_t cPosTimestamp;
mavlink_attitude_t cAtt;
uint64_t cAttTimestamp;
ImgData imgData;
mavlink_local_position_ned_t hPos;
mavlink_local_position_ned_t tPos;
mavlink_local_position_ned_t dPos;
float fSonarDist;
uint64_t fSonarDistTimestamp;
struct{
uint64_t iPos;
uint64_t iAtt;
uint64_t fSonarDist;
uint64_t hPos;
uint64_t tPos;
uint64_t dPos;
}timestamp;
QuadParam(){
fSonarDist=-1;
timestamp.iPos=-1;
timestamp.hPos=-1;
timestamp.tPos=-1;
timestamp.dPos=-1;
timestamp.iAtt=-1;
timestamp.fSonarDist=-1;
}
QuadParam(const QuadParam &obj){
if(this!=&obj){
iPos=obj.iPos;
tPos=obj.tPos;
hPos=obj.hPos;
dPos=obj.dPos;
iAtt=obj.iAtt;
cPos=obj.cPos;
cAtt=obj.cAtt;
imgData=obj.imgData;
fSonarDist=obj.fSonarDist;
timestamp.iPos=obj.timestamp.iPos;
timestamp.iPos=obj.timestamp.hPos;
timestamp.iPos=obj.timestamp.tPos;
timestamp.iPos=obj.timestamp.dPos;
timestamp.iAtt=obj.timestamp.iAtt;
timestamp.fSonarDist=obj.timestamp.fSonarDist;
}
}
QuadParam & operator= (const QuadParam &obj){
if(this!=&obj){
iPos=obj.iPos;
tPos=obj.tPos;
hPos=obj.hPos;
dPos=obj.dPos;
iAtt=obj.iAtt;
cPos=obj.cPos;
cAtt=obj.cAtt;
imgData=obj.imgData;
fSonarDist=obj.fSonarDist;
timestamp.iPos=obj.timestamp.iPos;
timestamp.iPos=obj.timestamp.hPos;
timestamp.iPos=obj.timestamp.tPos;
timestamp.iPos=obj.timestamp.dPos;
timestamp.iAtt=obj.timestamp.iAtt;
timestamp.fSonarDist=obj.timestamp.fSonarDist;
}
return *this;
}
};
class Behaviour{
protected:
string name;
std::thread thread;
std::atomic<bool> threadRunning;
std::atomic<bool> taskCompleted;
std::atomic<bool> stopFlag;
QuadParam ¶m; //Quad parameters
mavlink_set_position_target_local_ned_t &posTarget; //pos target to send to the pixhawk
virtual void ruleBase()=0; //rules that govern the behaviour
public:
Behaviour(QuadParam &q,mavlink_set_position_target_local_ned_t &posT)
:param(q),posTarget(posT){
name="GenericBehaviour";
threadRunning=false;
taskCompleted=false;
stopFlag=false;
}
virtual ~Behaviour(){};
virtual void start()=0; //To start the behaviour
virtual void stop()=0; //To stop the behaviour
bool completed(){return taskCompleted;}
string getName(){return name;}
uint64_t getTimeUsec(){
struct timespec t;
clock_gettime(CLOCK_REALTIME,&t);
return (t.tv_sec)*1e6+(t.tv_nsec)/1e3;
}
protected:
void bodyToNED(float bx, float by, float theta, float &lx, float &ly){
lx=bx*cos(theta)-by*sin(theta);
ly=bx*sin(theta)+by*cos(theta);
}
void NEDTobody(float lx, float ly, float theta, float &bx, float &by){
bx=+lx*cos(theta)+ly*sin(theta);
by=-lx*sin(theta)+ly*cos(theta);
}
};
class Action{
int sysID;
int compID;
Pixhawk &pixhawk;
Arduino &arduino;
Image ℑ
QuadParam param;
vector<Behaviour*> behaviour;
vector<mavlink_set_position_target_local_ned_t*> posVec;
std::thread thread;
bool threadRunning;
bool obFlag;
mavlink_set_position_target_local_ned_t cPosTarget;
mavlink_message_t msg;
enum {SLEEP,TAKEOFF,SEARCHFORTARGET,SEARCHFORPICKUP,SEARCHFORHOMEBASE,AVOIDBOUNDARY,AVOIDOBSTACLE,LAND}state,pState;
void getParameters(){
param.cPos=buffers.messages.localPos;
param.cAtt=buffers.messages.attitude;
param.timestamp.iPos=buffers.messages.timeStamps.localPos;
param.timestamp.iAtt=buffers.messages.timeStamps.attitude;
param.imgData=buffers.imgData;
param.fSonarDist = buffers.fSonarDist;
}
uint64_t getTimeUsec(){
struct timespec t;
clock_gettime(CLOCK_REALTIME,&t);
return (t.tv_sec)*1e6+(t.tv_nsec)/1e3;
}
void bodyToNED(float bx, float by, float theta, float &lx, float &ly){
lx=bx*cos(theta)-by*sin(theta);
ly=bx*sin(theta)+by*cos(theta);
}
void NEDTobody(float lx, float ly, float theta, float &bx, float &by){
bx=+lx*cos(theta)+ly*sin(theta);
by=-lx*sin(theta)+ly*cos(theta);
}
public:
void setBehaviour();
Action(Pixhawk &px, Arduino &ard, Image &img):pixhawk(px),arduino(ard),image(img){
threadRunning=false;
sysID=0;compID=0;
state=SLEEP;
pState=SLEEP;
param.iAtt.yaw=-7; //yaw cannot be greater than 2*pi
cPosTarget.target_system=sysID;
cPosTarget.target_component=compID;
cPosTarget.vx=0;
cPosTarget.vy=0;
cPosTarget.vz=0;
cPosTarget.z=-1;
cPosTarget.type_mask=MAVLINK_MSG_SET_POS_TAR_L_NED_VELOCITY;
param.fSonarDist=buffers.fSonarDist;
obFlag=false;
}
void eventHandler();
void start();
void stop();
int rebootPixhawk();
};
#endif