-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQCurrentState.cpp
More file actions
141 lines (123 loc) · 4.24 KB
/
QCurrentState.cpp
File metadata and controls
141 lines (123 loc) · 4.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
#include "QCurrentState.h"
QCurrentState::QCurrentState(QObject *parent) : QObject(parent)
{
m_roll= 0;
m_pitch = 0;
m_yaw = 0;
m_groundspeed = 0;
m_airspeed = 0;
m_batteryVoltage = 0;
m_batteryCurrent = 0;
m_batteryRemaining = 0;
m_altitude = 0;
m_watts = 0;
m_gpsstatus = 0;
m_gpshdop = 0;
m_satcount = 0;
m_wp_dist = 0;
m_ch3percent = 0;
m_timeInAir = 0;
m_DistToHome = 0;
m_distTraveled = 0;
m_AZToMAV = 0;
m_lat = 0;
m_lng = 0;
m_uas = NULL;
m_distUnit = "m";
m_speedUnit = "m/s";
m_CurPos = QPointF(0,0);
m_LastPos = QPointF(0,0);
m_armed = false;
m_timeInAirTimer = new QTimer(this);
connect(m_timeInAirTimer, SIGNAL(timeout()), this, SLOT(updateTimeInAir()));
}
QCurrentState::~QCurrentState()
{
delete m_timeInAirTimer;
}
void QCurrentState::setUAS(UAS *uas)
{
if (m_uas != NULL)
{
disconnect(m_uas,SIGNAL(valueChanged(const int, const QString&, const QString&, const QVariant &,const quint64)),
this,SLOT(onValueChanged(const int, const QString&, const QString&, const QVariant &,const quint64)));
disconnect(m_uas,SIGNAL(armed()),this,SLOT(onArmed()));
disconnect(m_uas,SIGNAL(disarmed()),this,SLOT(onDisArmed()));
disconnect(m_uas,SIGNAL(remoteControlChannelScaledChanged(int, float)),this,SLOT(onRemoteControlChannelScaledChanged(int, float)));
disconnect(m_uas,SIGNAL(globalPositionChanged(UASInterface*, double, double, double, quint64 )),this,SLOT(onGlobalPositionChanged(UASInterface*, double, double, double, quint64 )));
}
m_uas = uas;
if (m_uas != NULL)
{
connect(m_uas,SIGNAL(valueChanged(const int, const QString&, const QString&, const QVariant &,const quint64)),
this,SLOT(onValueChanged(const int, const QString&, const QString&, const QVariant &,const quint64)));
connect(m_uas,SIGNAL(armed()),this,SLOT(onArmed()));
connect(m_uas,SIGNAL(disarmed()),this,SLOT(onDisarmed()));
connect(m_uas,SIGNAL(remoteControlChannelScaledChanged(int, float)),this,SLOT(onRemoteControlChannelScaledChanged(int, float)));
connect(m_uas,SIGNAL(globalPositionChanged(UASInterface*, double, double, double, quint64 )),this,SLOT(onGlobalPositionChanged(UASInterface*, double, double, double, quint64 )));
}
}
void QCurrentState::onValueChanged(const int uasid, const QString &name, const QString &unit, const QVariant &value, const quint64 msecs)
{
Q_UNUSED(uasid);
Q_UNUSED(unit);
Q_UNUSED(value);
Q_UNUSED(msecs);
if (m_uas != NULL)
{
if (name == "GPS HDOP") setGpshdop(value.toFloat());
else if (name=="distToWaypoint") setWp_dist(value.toFloat());
}
}
void QCurrentState::onArmed()
{
setArmed(true);
// Estimate time in air by arm state
setTimeInAir(0);
setDistToHome(0);
setDistTraveled(0);
m_timeInAirTimer->start(1000);
}
void QCurrentState::onDisarmed()
{
setArmed(false);
m_timeInAirTimer->stop();
}
void QCurrentState::onRemoteControlChannelScaledChanged(int channelId, float normalized)
{
if (channelId == 1)
{
setCh3percent(normalized);
}
}
void QCurrentState::updateTimeInAir()
{
if (m_armed)
{
setTimeInAir(getTimeInAir()+1);
if (!m_CurPos.isNull() && !m_LastPos.isNull()) setDistTraveled(m_distTraveled + GetDistanceBetweenPoints(m_CurPos,m_LastPos));
}
}
void QCurrentState::onGlobalPositionChanged(UASInterface*, double lat, double lon, double alt, quint64 usec)
{
Q_UNUSED(usec);
Q_UNUSED(alt);
m_CurPos.setX(lat);
m_CurPos.setY(lon);
}
double QCurrentState::GetDistanceBetweenPoints(QPointF curPos, QPointF lastPos)
{
double Lat = curPos.x()/1E7;
double Lng = curPos.y()/1E7;
double Lat2 = lastPos.x()/1E7;
double Lng2 = lastPos.y()/1E7;
double d = Lat * 0.017453292519943295;
double num2 = Lng * 0.017453292519943295;
double num3 = Lat2 * 0.017453292519943295;
double num4 = Lng2 * 0.017453292519943295;
double num5 = num4 - num2;
double num6 = num3 - d;
double num7 = pow(sin(num6 / 2.0), 2.0) + ((cos(d) * cos(num3)) * pow(sin(num5 / 2.0), 2.0));
double num8 = 2.0 * atan2(sqrt(num7), sqrt(1.0 - num7));
return (6371 * num8) * 1000.0; // M
}