-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixhawk.h
More file actions
227 lines (170 loc) · 5.88 KB
/
Copy pathpixhawk.h
File metadata and controls
227 lines (170 loc) · 5.88 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
//Defines the class that sends data to the pixhawk over USB
#ifndef PIXHAWK_H
#define PIXHAWK_H
#include <termios.h>
#include <string>
#include <sys/time.h>
#include <iostream>
#include <mutex>
#include <thread>
#include "serial.h"
#include "mavlink2/common/mavlink.h" //changed this :- Mollik
#include "common.h"
#include "struct.h"
struct mavlinkMsgsDefined{
mavlink_heartbeat_t heartbeat;
mavlink_sys_status_t sysStatus;
mavlink_system_time_t sysTime;
mavlink_timesync_t timesync;
mavlink_battery_status_t batteryStatus;
mavlink_radio_status_t radioStatus;
mavlink_local_position_ned_t localPos;
mavlink_position_target_local_ned_t localPosTarget;
mavlink_highres_imu_t imuData;
mavlink_attitude_t attitude;
mavlink_attitude_target_t attitudeTarget;
mavlink_optical_flow_rad_t opticalFlow;
};
class Mavlink_Messages{
Mavlink_Messages(const Mavlink_Messages&);
Mavlink_Messages& operator=(const Mavlink_Messages&);
int64_t timeSkew;
std::mutex muTimeSkew;
public:
int sysID;
int compID;
int64_t getTimeSkew(){
int64_t t;
muTimeSkew.lock();
t = timeSkew;
muTimeSkew.unlock();
return t;
}
void setTimeSkew(int64_t skew){
muTimeSkew.lock();
timeSkew = skew;
muTimeSkew.unlock();
}
// wrapper to provide mutex functionality
template <class T>
class msgWrapper{
T msg;
uint64_t timestamp;
std::mutex mu;
public:
void getData(T &dst, uint64_t &t){
mu.lock();
dst = msg;
t = timestamp;
mu.unlock();
}
void getData(uint64_t &t){
mu.lock();
t = timestamp;
mu.unlock();
}
void getData(T &dst){
mu.lock();
dst = msg;
mu.unlock();
}
void setData(const T &src, const uint64_t &t){
mu.lock();
msg = src;
timestamp = t;
mu.unlock();
}
msgWrapper(){
timestamp = 0;
}
};
//Messages we care about
//Make sure they match mavlinkMessagesDefined class
msgWrapper<mavlink_heartbeat_t> heartbeat;
msgWrapper<mavlink_sys_status_t> sysStatus;
msgWrapper<mavlink_system_time_t> sysTime;
msgWrapper<mavlink_battery_status_t> batteryStatus;
msgWrapper<mavlink_radio_status_t> radioStatus;
msgWrapper<mavlink_local_position_ned_t> localPos;
msgWrapper<mavlink_position_target_local_ned_t> localPosTarget;
msgWrapper<mavlink_highres_imu_t> imuData;
msgWrapper<mavlink_attitude_t> attitude;
msgWrapper<mavlink_attitude_target_t> attitudeTarget;
msgWrapper<mavlink_optical_flow_rad_t> opticalFlow;
Mavlink_Messages(){
sysID=-1;
compID=-1;
}
void printMessages(){
uint64_t t;
mavlinkMsgsDefined msgs;
std::cout<<"\n\n--------------------------------------------------";
std::cout<<"\nReceived buffers.messages: ";
heartbeat.getData(msgs.heartbeat, t);
std::cout<<"\n\nGot heartbeat at "<<t;
std::cout<<"\n\nautopilot: "<<msgs.heartbeat.autopilot;
std::cout<<"\n\tb: "<<msgs.heartbeat.base_mode;
std::cout<<"\tc: "<<msgs.heartbeat.custom_mode;
std::cout<<"\tstatus: "<<msgs.heartbeat.system_status;
sysTime.getData(msgs.sysTime, t);
std::cout<<"\n\nGot system time at "<<t;
std::cout<<"\nEpoch: "<<msgs.sysTime.time_unix_usec<<"\tBoot: "<<msgs.sysTime.time_boot_ms<<"\tSkew: "<<msgs.sysTime.time_unix_usec - t;
sysStatus.getData(msgs.sysStatus, t);
std::cout<<"\nGot system status at "<<t;
//This doesn't exist? cout<<"\n\tMode: "<<buffers.messages.sysStatus.mode<<"\t"<<buffers.messages.sysStatus.nav_mode;
//This doesn't exist? cout<<"\n\tStatus: "<<buffers.messages.sysStatus.status;
std::cout<<"\n\tBat vol: "<<msgs.sysStatus.voltage_battery;
batteryStatus.getData(msgs.batteryStatus, t);
std::cout<<"\nGot battery status at "<<t;
radioStatus.getData(msgs.radioStatus, t);
std::cout<<"\nGot radio status at "<<t;
localPos.getData(msgs.localPos, t);
std::cout<<"\nGot local position at "<<t;
std::cout<<"\n\tPos: "<<msgs.localPos.x<<","<<msgs.localPos.y<<","<<msgs.localPos.z;
std::cout<<"\n\tVel: "<<msgs.localPos.vx<<","<<msgs.localPos.vy<<","<<msgs.localPos.vz;
//std::cout<<"\nGot global position at "<<timeStamps.globalPos;
localPosTarget.getData(msgs.localPosTarget, t);
std::cout<<"\nGot local position target at "<<t;
std::cout<<"\n\tPos Tar: "<<msgs.localPosTarget.x<<","<<msgs.localPosTarget.y<<","<<msgs.localPosTarget.z;
std::cout<<"\n\tVel Tar: "<<msgs.localPosTarget.vx<<","<<msgs.localPosTarget.vy<<","<<msgs.localPosTarget.vz;
//std::cout<<"\n\nGot global position target at "<<buffers.messages.timeStamps.globalPosTarget;
imuData.getData(msgs.imuData, t);
std::cout<<"\n\nGot imu data at "<<t;
//opticalFlow.getData(msgs.opticalFlow, t);
//std::cout<<"\n\nGot distance data from optical flow at "<<timeStamps.opticalFlow;
//std::cout<<"\n\tDistance: "<<opticalFlow.distance;
attitude.getData(msgs.attitude, t);
std::cout<<"\n\nGot attitude at "<<t;
std::cout<<"\n\tRoll: "<<msgs.attitude.roll<<"\tPitch: "<<msgs.attitude.pitch<<"\tYaw: "<<msgs.attitude.yaw;
std::cout<<"\n--------------------------------------------------"<<std::endl;
}
};
class Pixhawk: private Serial{
private:
//USB connection
Serial device;
int baudRate;
std::string devName;
//pixhawk parameters
Mavlink_Messages *px4StateMsgs; //Where to store received msgs
CircularBuffer<mavlink_message_t> *px4TelemBuffer; //to store orig telem msgs
//threading
std::thread recvThread;
bool recvThreadActive;
bool msgWaitingToSend;
void getMessages();
void startReceivingMsgs();
void stopReceivingMsgs();
void printMessages();
public:
Pixhawk(int bR, std::string dN, Mavlink_Messages *m,
CircularBuffer<mavlink_message_t> *telem);
//initialize the USB connection. Needs to be called after obj creations
int init();
int sendMessage(mavlink_message_t &msg);
void deInit();
~Pixhawk(){
stopReceivingMsgs();
}
};
#endif