dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
vehicleconfig.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
30 #include <uavobjects/uavobject.h>
32 
33 #include <actuatorcommand.h>
34 
35 #include <QDebug>
36 #include <QPointer>
37 
38 const double VehicleConfig::mixerScale = 128.0;
39 const double VehicleConfig::mixerRange = 256.0; /* For now cap to 2x in UI */
40 
42  : ConfigTaskWidget(parent)
43 {
44  // Generate lists of mixerTypeNames, mixerVectorNames, channelNames
45  channelNames << "None";
46  for (int i = 0; i < (int)(ActuatorCommand::CHANNEL_NUMELEM); i++) {
47  mixerTypes << QString("Mixer%1Type").arg(i + 1);
48  mixerVectors << QString("Mixer%1Vector").arg(i + 1);
49  channelNames << QString("Channel%1").arg(i + 1);
50  }
51 
53  MixerSettings::GetInstance(getUAVObjectManager())->getField("Mixer1Type")->getOptions();
54 
55  // This is needed because new style tries to compact things as much as possible in grid
56  // and on OSX the widget sizes of PushButtons is reported incorrectly:
57  // https://bugreports.qt-project.org/browse/QTBUG-14591
58  foreach (QPushButton *btn, findChildren<QPushButton *>()) {
59  btn->setAttribute(Qt::WA_LayoutUsesWidgetRect);
60  }
61 }
62 
64 {
65  // Do nothing
66 }
67 
69 {
70 
71  int i;
72  GUIConfigDataUnion configData;
73 
74  // get an instance of systemsettings
75  SystemSettings *systemSettings = SystemSettings::GetInstance(getUAVObjectManager());
76  Q_ASSERT(systemSettings);
77  SystemSettings::DataFields systemSettingsData = systemSettings->getData();
78 
79  // copy systemsettings -> local configData
80  for (i = 0; i < (int)(SystemSettings::AIRFRAMECATEGORYSPECIFICCONFIGURATION_NUMELEM); i++)
81  configData.UAVObject[i] = systemSettingsData.AirframeCategorySpecificConfiguration[i];
82 
83  // sanity check
84  Q_ASSERT(SystemSettings::AIRFRAMECATEGORYSPECIFICCONFIGURATION_NUMELEM
85  == (sizeof(configData.UAVObject) / sizeof(configData.UAVObject[0])));
86 
87  return configData;
88 }
89 
91  SystemSettings::AirframeTypeOptions vehicle)
92 {
93 
94  int i;
95 
96  // sanity check
97  Q_ASSERT(SystemSettings::AIRFRAMECATEGORYSPECIFICCONFIGURATION_NUMELEM
98  == (sizeof(configData.UAVObject) / sizeof(configData.UAVObject[0])));
99 
100  // get an instance of systemsettings
101  SystemSettings *systemSettings = SystemSettings::GetInstance(getUAVObjectManager());
102  Q_ASSERT(systemSettings);
103  SystemSettings::DataFields systemSettingsData = systemSettings->getData();
104 
105  // copy parameter configData -> systemsettings
106  for (i = 0; i < (int)(SystemSettings::AIRFRAMECATEGORYSPECIFICCONFIGURATION_NUMELEM); i++) {
107  systemSettingsData.AirframeCategorySpecificConfiguration[i] = configData.UAVObject[i];
108  }
109 
110  systemSettingsData.AirframeType = vehicle;
111 
112  systemSettings->setData(systemSettingsData);
113 }
114 
116 {
117  Q_UNUSED(configData);
118 }
119 
125 void VehicleConfig::setComboCurrentIndex(QComboBox *box, int index)
126 {
127  Q_ASSERT(box);
128 
129  if (index >= 0 && index < box->count())
130  box->setCurrentIndex(index);
131 }
132 
137 void VehicleConfig::enableComboBoxes(QWidget *owner, QString boxName, int boxCount, bool enable)
138 {
139  for (int i = 1; i <= boxCount; i++) {
140  QComboBox *box = owner->findChild<QComboBox *>(QString("%0%1").arg(boxName).arg(i));
141  if (box)
142  box->setEnabled(enable);
143  }
144 }
145 QString VehicleConfig::getMixerType(UAVDataObject *mixer, int channel)
146 {
147  Q_ASSERT(mixer);
148 
149  QString mixerType =
150  mixerTypeDescriptions[MixerSettings::MIXER1TYPE_DISABLED]; // default to disabled
151 
152  if (channel >= 0 && channel < mixerTypes.count()) {
153  UAVObjectField *field = mixer->getField(mixerTypes.at(channel));
154  Q_ASSERT(field);
155 
156  if (field)
157  mixerType = field->getValue().toString();
158  }
159 
160  return mixerType;
161 }
162 
164  MixerSettings::Mixer1TypeOptions mixerType)
165 {
166  Q_ASSERT(mixer);
167 
168  if (channel >= 0 && channel < mixerTypes.count()) {
169  UAVObjectField *field = mixer->getField(mixerTypes.at(channel));
170  Q_ASSERT(field);
171 
172  if (field) {
173  if (mixerType >= 0 && mixerType < mixerTypeDescriptions.count()) {
174  field->setValue(mixerTypeDescriptions[mixerType]);
175  }
176  }
177  }
178 }
179 
181 {
182  Q_ASSERT(mixer);
183 
184  if (channel >= 0 && channel < mixerVectors.count()) {
185  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_THROTTLECURVE1, 0);
186  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_THROTTLECURVE2, 0);
187  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_PITCH, 0);
188  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_ROLL, 0);
189  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_YAW, 0);
190  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_ACCESSORY0, 0);
191  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_ACCESSORY1, 0);
192  setMixerVectorValue(mixer, channel, MixerSettings::MIXER1VECTOR_ACCESSORY2, 0);
193  }
194 }
195 
196 // Disable all servo/motor mixers (but keep camera and accessory ones)
198 {
199  for (int channel = 0; channel < (int)ActuatorCommand::CHANNEL_NUMELEM; channel++) {
200  QString type = getMixerType(mixer, channel);
201  if ((type == mixerTypeDescriptions[MixerSettings::MIXER1TYPE_DISABLED])
202  || (type == mixerTypeDescriptions[MixerSettings::MIXER1TYPE_MOTOR])
203  || (type == mixerTypeDescriptions[MixerSettings::MIXER1TYPE_SERVO])) {
204  setMixerType(mixer, channel, MixerSettings::MIXER1TYPE_DISABLED);
205  resetMixerVector(mixer, channel);
206  }
207  }
208 }
209 
211  MixerSettings::Mixer1VectorElem elementName)
212 {
213  Q_ASSERT(mixer);
214 
215  double value = 0;
216 
217  if (channel >= 0 && channel < mixerVectors.count()) {
218  UAVObjectField *field = mixer->getField(mixerVectors.at(channel));
219  Q_ASSERT(field);
220 
221  if (field) {
222  value = field->getDouble(elementName);
223  }
224  }
225  return value;
226 }
227 
229  MixerSettings::Mixer1VectorElem elementName, double value)
230 {
231  Q_ASSERT(mixer);
232 
233  if (channel >= 0 && channel < mixerVectors.count()) {
234  UAVObjectField *field = mixer->getField(mixerVectors.at(channel));
235  Q_ASSERT(field);
236 
237  if (field) {
238  field->setDouble(value, elementName);
239  }
240  }
241 }
242 
243 double VehicleConfig::getMixerValue(UAVDataObject *mixer, QString elementName)
244 {
245  Q_ASSERT(mixer);
246 
247  double value = 0.0;
248 
249  QPointer<UAVObjectField> field = mixer->getField(elementName);
250  if (field) {
251  value = field->getDouble();
252  }
253  return value;
254 }
255 
256 void VehicleConfig::setMixerValue(UAVDataObject *mixer, QString elementName, double value)
257 {
258  Q_ASSERT(mixer);
259 
260  QPointer<UAVObjectField> field = mixer->getField(elementName);
261  if (field) {
262  field->setDouble(value);
263  }
264 }
265 
267  MixerSettings::Mixer1VectorElem curveType, QList<double> curve)
268 {
269  QPointer<UAVObjectField> field;
270  field = mixer->getField(mixer->getField("Mixer1Vector")->getElementNames().at(curveType));
271 
272  if (field && field->getNumElements() == curve.length()) {
273  for (int i = 0; i < curve.length(); i++)
274  field->setValue(curve.at(i), i);
275  }
276 }
277 
279  MixerSettings::Mixer1VectorElem curveType,
280  QList<double> *curve)
281 {
282  Q_ASSERT(mixer);
283  Q_ASSERT(curve);
284 
285  QPointer<UAVObjectField> field;
286  field = mixer->getField(mixer->getField("Mixer1Vector")->getElementNames().at(curveType));
287 
288  if (field) {
289  curve->clear();
290  for (auto i = 0; i < field->getNumElements(); i++) {
291  curve->append(field->getValue(i).toDouble());
292  }
293  }
294 }
295 
297 {
298  Q_ASSERT(curve);
299 
300  if (curve) {
301  for (int i = 0; i < curve->count(); i++) {
302  if (curve->at(i) != 0)
303  return true;
304  }
305  }
306  return false;
307 }
308 
310 {
311  double min = 0;
312  for (int i = 0; i < curve->count(); i++)
313  min = std::min(min, curve->at(i));
314 
315  return min;
316 }
317 
319 {
320  double max = 0;
321  for (int i = 0; i < curve->count(); i++)
322  max = std::max(max, curve->at(i));
323 
324  return max;
325 }
330 {
331  for (auto i = 0; i < field->getNumElements(); i++)
332  field->setValue(0, i);
333 }
334 
339 UAVObjectManager *VehicleConfig::getUAVObjectManager()
340 {
341  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
342  UAVObjectManager *objMngr = pm->getObject<UAVObjectManager>();
343  Q_ASSERT(objMngr);
344  return objMngr;
345 }
346 
354 void VehicleConfig::setLabelText(QWidget *owner, const QString &boxName, const QStringList &labels)
355 {
356  // set the desired labels
357  for (int i = 0; i < labels.length(); i++) {
358  QLabel *lbl = owner->findChild<QLabel *>(QString("%0%1").arg(boxName).arg(i + 1));
359  if (lbl)
360  lbl->setText(labels[i]);
361  }
362  // set the remainder of the labels blank
363  int i = labels.length();
364  while (QLabel *lbl = owner->findChild<QLabel *>(QString("%0%1").arg(boxName).arg(i + 1))) {
365  lbl->setText(QString(""));
366  i++;
367  }
368 }
VehicleConfig(QWidget *parent=nullptr)
QStringList mixerTypes
QStringList mixerTypeDescriptions
QString getMixerType(UAVDataObject *mixer, int channel)
static void SetConfigData(GUIConfigDataUnion configData, SystemSettings::AirframeTypeOptions vehicle)
static GUIConfigDataUnion GetConfigData()
static void resetField(UAVObjectField *field)
double getDouble(int index=0) const
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
static const double mixerRange
QVariant getValue(int index=0) const
int getNumElements() const
for i
Definition: OPPlots.m:140
bool isValidThrottleCurve(QList< double > *curve)
double getMixerVectorValue(UAVDataObject *mixer, int channel, MixerSettings::Mixer1VectorElem elementName)
virtual void ResetActuators(GUIConfigDataUnion *configData)
void setValue(const QVariant &data, int index=0)
void getThrottleCurve(UAVDataObject *mixer, MixerSettings::Mixer1VectorElem curveType, QList< double > *curve)
double getMixerValue(UAVDataObject *mixer, QString elementName)
void resetMixerVector(UAVDataObject *mixer, int channel)
UAVObjectField * getField(const QString &name)
Definition: uavobject.cpp:236
void resetMixers(UAVDataObject *mixer)
void setMixerType(UAVDataObject *mixer, int channel, MixerSettings::Mixer1TypeOptions mixerType)
QStringList mixerVectors
QStringList channelNames
void setMixerVectorValue(UAVDataObject *mixer, int channel, MixerSettings::Mixer1VectorElem elementName, double value)
QStringList getElementNames() const
static void enableComboBoxes(QWidget *owner, QString boxName, int boxCount, bool enable)
void setThrottleCurve(UAVDataObject *mixer, MixerSettings::Mixer1VectorElem curveType, QList< double > curve)
double getCurveMax(QList< double > *curve)
void setDouble(double value, int index=0)
void setMixerValue(UAVDataObject *mixer, QString elementName, double value)
void setLabelText(QWidget *owner, const QString &boxName, const QStringList &labels)
static const double mixerScale
double getCurveMin(QList< double > *curve)
static void setComboCurrentIndex(QComboBox *box, int index)