-
Notifications
You must be signed in to change notification settings - Fork 6
/
ParameterLoader.cpp
225 lines (193 loc) · 6.82 KB
/
ParameterLoader.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
#include "ParameterLoader.h"
#include "QuadApplication.h"
#include <QSettings>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include "PX4AutopioltPlugins/PX4AutopilotPlugin.h"
#include "PX4FirmwarePlugin.h"
#include <QVariant>
#include "Vehicle.h"
#include "linkmanager.h"
#include "UAS.h"
#include <QThread>
Fact ParameterLoader::_defaultFact;
ParameterLoader::ParameterLoader(Vehicle* vehicle,QObject *parent) :
QObject(parent),
_defaultCompId(PX4AutopilotPlugin::defaultCompId),
_vehicle(vehicle),
_compParamsComplete(false),
_currentParamCount(0),
_parameterMetaData(NULL)
{
_initialParamRequestTimer.setSingleShot(true);
_initialParamRequestTimer.setInterval(5000);
connect(&_initialParamRequestTimer,&QTimer::timeout,this,&ParameterLoader::_initialParamRequestTimeout);
connect(_vehicle->uas(),&UAS::parameterUpdate,this,&ParameterLoader::_parameterUpdate);
_refreshAllParameters();
}
ParameterLoader::~ParameterLoader()
{
}
//check whether params loaded completely, and send param_request_list message to request for all parameters
void ParameterLoader::_refreshAllParameters()
{
qDebug()<<"ParameterLoader: refreshAllParameters";
if(!_compParamsComplete) {
_initialParamRequestTimer.start();
}
mavlink_message_t msg;
mavlink_param_request_list_t param;
param.target_component = 0;
param.target_system =1;
mavlink_msg_param_request_list_encode(DEFAULT_SYSTEM_ID,DEFAULT_COMPONENT_ID,&msg,¶m);
_vehicle->linkManager()->sendMessage(msg);
}
void ParameterLoader::refreshParameters(void)
{
_mapParamName2Variant.clear();
_currentParamCount = 0;
_compParamsComplete = false;
_refreshAllParameters();
}
///if no parameteru updated (no param_value message received) in 6s, send param_request_list message again
void ParameterLoader::_initialParamRequestTimeout()
{
qDebug()<<"Vehicle did not respond to request for parameters, retrying.";
_refreshAllParameters();
_initialParamRequestTimer.start();
}
/// Called whenever a parameter is updated or first seen.
void ParameterLoader::_parameterUpdate(int componentId, QString paramName, int paramCount, int paramId, int type,QVariant value)
{
Q_UNUSED(paramId)
_initialParamRequestTimer.stop();
if(_compParamsComplete) {
return;
}
qDebug()<<QString("_parameterUpdate (compId :%1, "
"name:%2,count:%3,"
"index:%4,"
"mavType:%5,"
"value:%6).").arg(QString::number(componentId)).arg(paramName)
.arg(QString::number(paramCount)).arg(QString::number(paramId)).arg(type).arg(value.toString());
_dataMutex.lock();
_defaultCompId = componentId;
_totalParamCount = paramCount;
if(!_mapParamName2Variant.contains(paramName)) {
//qDebug()<<"Adding new fact";
_currentParamCount++;
FactMetaData::ValueType_t factType;
switch(type) {
case MAV_PARAM_TYPE_UINT8:
factType = FactMetaData::typeUint8;
break;
case MAV_PARAM_TYPE_INT8:
factType = FactMetaData::typeInt8;
break;
case MAV_PARAM_TYPE_UINT16:
factType = FactMetaData::typeUint16;
break;
case MAV_PARAM_TYPE_INT16:
factType = FactMetaData::typeInt16;
break;
case MAV_PARAM_TYPE_UINT32:
factType = FactMetaData::typeUint32;
break;
case MAV_PARAM_TYPE_INT32:
factType = FactMetaData::typeInt32;
break;
case MAV_PARAM_TYPE_REAL32:
factType = FactMetaData::typeFloat;
break;
case MAV_PARAM_TYPE_REAL64:
factType = FactMetaData::typeDouble;
break;
default:
factType = FactMetaData::typeInt32;
qCritical()<<"Unsupported fact type:"<<type;
break;
}
emit paramLoadProgress(_currentParamCount,paramCount);
Fact* fact = new Fact(componentId,paramName,factType,this);
//Returns a QVariant containing a copy of value
_mapParamName2Variant[paramName] = QVariant::fromValue(fact);
// when value changed, send the new value to the parameter manager
connect(fact, &Fact::_containerRawValueChanged, this, &ParameterLoader::_valueUpdated);
}
_dataMutex.unlock();
Q_ASSERT(_mapParamName2Variant.contains(paramName));
Fact* fact = _mapParamName2Variant[paramName].value<Fact*>();
Q_ASSERT(fact);
fact->_containerSetRawValue(value);
if(_compParamsComplete) {
_addMetaDataToDefaultComponent();
}
_checkInitialLoadComplete();
}
void ParameterLoader::_checkInitialLoadComplete(void)
{
if(_compParamsComplete) {
return;
}
if(_currentParamCount == _totalParamCount) {
qDebug()<<QString("%1 ParameterLoader: parameters loading completed").arg(_totalParamCount);
_currentParamCount = 0;
_compParamsComplete = true;
qDebug()<<"parameters ready true";
emit paramsReady(true);
}
else {
_initialParamRequestTimer.start();
}
}
void ParameterLoader::_addMetaDataToDefaultComponent(void)
{
if(_defaultCompId == PX4AutopilotPlugin::defaultCompId) {
return;
}
QString metaDataFile = QString(":\\cal_params.xml");
//_px4MetaData created
_parameterMetaData = _vehicle->firmwarePlugin()->loadParamFactMetaDataFile(metaDataFile);
foreach (const QString&key,_mapParamName2Variant.keys()) {
_vehicle->firmwarePlugin()->addMetaDataToFact(_parameterMetaData,_mapParamName2Variant[key].value<Fact*>());
}
}
/// Connected to Fact::valueUpdated
/// Writes the parameter to mavlink, sets up for write wait
void ParameterLoader::_valueUpdated(const QVariant& value)
{
Fact* fact = qobject_cast<Fact*>(sender());
Q_ASSERT(fact);
_vehicle->uas()->writeParameterRaw(fact->compId(),fact->name(),value);
qDebug()<<"Set parameter (componentId: "<<fact->compId()<<"name: "<<fact->name()<<value;
}
bool ParameterLoader::paramExists(int componentId,const QString& name)
{
bool exist = false;
componentId = _actualCompId(componentId);
if(_mapParamName2Variant.contains(name)) {
exist = true;
}
return exist;
}
Fact* ParameterLoader::getFact(int componentId, const QString& name)
{
componentId = _actualCompId(componentId);
if(!_mapParamName2Variant.contains(name)) {
return &_defaultFact;
}
return _mapParamName2Variant[name].value<Fact*>();
qDebug()<<"ParameterLoader: "<<_mapParamName2Variant[name].value<Fact*>()->name();
}
int ParameterLoader::_actualCompId(int componentId)
{
if(componentId == PX4AutopilotPlugin::defaultCompId) {
componentId = _defaultCompId;
if(componentId == PX4AutopilotPlugin::defaultCompId) {
qWarning()<<"Default component id not set";
}
}
return componentId;
}