LP-514 Add DShot150/600/1200 and OneShot42/MultiShot Esc support to Wizard
[librepilot.git] / ground / gcs / src / plugins / setupwizard / connectiondiagram.cpp
bloba694a2d5e59dc942a49450b2b337f5a724f742dc
1 /**
2 ******************************************************************************
4 * @file connectiondiagram.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015-2016.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
7 * @addtogroup
8 * @{
9 * @addtogroup ConnectionDiagram
10 * @{
11 * @brief
12 *****************************************************************************/
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.
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.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <QDebug>
30 #include <QFile>
31 #include <QFileDialog>
32 #include "connectiondiagram.h"
33 #include "ui_connectiondiagram.h"
35 const char *ConnectionDiagram::FILE_NAME = ":/setupwizard/resources/connection-diagrams.svg";
36 const int ConnectionDiagram::IMAGE_PADDING = 10;
38 ConnectionDiagram::ConnectionDiagram(QWidget *parent, VehicleConfigurationSource *configSource) :
39 QDialog(parent), ui(new Ui::ConnectionDiagram), m_configSource(configSource)
41 ui->setupUi(this);
42 setWindowTitle(tr("Connection Diagram"));
43 setupGraphicsScene();
46 ConnectionDiagram::~ConnectionDiagram()
48 delete ui;
51 void ConnectionDiagram::resizeEvent(QResizeEvent *event)
53 QWidget::resizeEvent(event);
55 fitInView();
58 void ConnectionDiagram::showEvent(QShowEvent *event)
60 QWidget::showEvent(event);
62 fitInView();
65 void ConnectionDiagram::fitInView()
67 ui->connectionDiagram->setSceneRect(m_scene->itemsBoundingRect());
68 ui->connectionDiagram->fitInView(
69 m_scene->itemsBoundingRect().adjusted(-IMAGE_PADDING, -IMAGE_PADDING, IMAGE_PADDING, IMAGE_PADDING),
70 Qt::KeepAspectRatio);
73 void ConnectionDiagram::setupGraphicsScene()
75 m_renderer = new QSvgRenderer();
76 if (QFile::exists(QString(FILE_NAME)) &&
77 m_renderer->load(QString(FILE_NAME)) &&
78 m_renderer->isValid()) {
79 m_scene = new QGraphicsScene(this);
80 ui->connectionDiagram->setScene(m_scene);
82 QList<QString> elementsToShow;
84 switch (m_configSource->getControllerType()) {
85 case VehicleConfigurationSource::CONTROLLER_CC:
86 elementsToShow << "controller-cc";
87 break;
88 case VehicleConfigurationSource::CONTROLLER_CC3D:
89 elementsToShow << "controller-cc3d";
90 break;
91 case VehicleConfigurationSource::CONTROLLER_REVO:
92 elementsToShow << "controller-revo";
93 break;
94 case VehicleConfigurationSource::CONTROLLER_NANO:
95 elementsToShow << "controller-nano";
96 break;
97 case VehicleConfigurationSource::CONTROLLER_SPARKY2:
98 elementsToShow << "controller-sparky2";
99 break;
100 case VehicleConfigurationSource::CONTROLLER_OPLINK:
101 default:
102 elementsToShow << "controller-cc";
103 break;
106 switch (m_configSource->getVehicleType()) {
107 case VehicleConfigurationSource::VEHICLE_MULTI:
108 switch (m_configSource->getVehicleSubType()) {
109 case VehicleConfigurationSource::MULTI_ROTOR_TRI_Y:
110 elementsToShow << "tri";
111 break;
112 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_X:
113 elementsToShow << "quad-x";
114 break;
115 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS:
116 elementsToShow << "quad-p";
117 break;
118 case VehicleConfigurationSource::MULTI_ROTOR_HEXA:
119 elementsToShow << "hexa";
120 break;
121 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_X:
122 elementsToShow << "hexa-x";
123 break;
124 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y:
125 elementsToShow << "hexa-y";
126 break;
127 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_H:
128 elementsToShow << "hexa-h";
129 break;
130 default:
131 break;
133 break;
134 case VehicleConfigurationSource::VEHICLE_FIXEDWING:
135 switch (m_configSource->getVehicleSubType()) {
136 case VehicleConfigurationSource::FIXED_WING_DUAL_AILERON:
137 elementsToShow << "aileron";
138 break;
139 case VehicleConfigurationSource::FIXED_WING_AILERON:
140 elementsToShow << "aileron-single";
141 break;
142 case VehicleConfigurationSource::FIXED_WING_ELEVON:
143 elementsToShow << "elevon";
144 break;
145 case VehicleConfigurationSource::FIXED_WING_VTAIL:
146 elementsToShow << "vtail";
147 break;
148 default:
149 break;
151 case VehicleConfigurationSource::VEHICLE_SURFACE:
152 switch (m_configSource->getVehicleSubType()) {
153 case VehicleConfigurationSource::GROUNDVEHICLE_CAR:
154 elementsToShow << "car";
155 break;
156 case VehicleConfigurationSource::GROUNDVEHICLE_DIFFERENTIAL:
157 elementsToShow << "tank";
158 break;
159 case VehicleConfigurationSource::GROUNDVEHICLE_MOTORCYCLE:
160 elementsToShow << "motorbike";
161 break;
162 case VehicleConfigurationSource::GROUNDVEHICLE_BOAT:
163 elementsToShow << "boat";
164 break;
165 case VehicleConfigurationSource::GROUNDVEHICLE_DIFFERENTIAL_BOAT:
166 elementsToShow << "boat_diff";
167 break;
168 default:
169 break;
171 case VehicleConfigurationSource::VEHICLE_HELI:
172 default:
173 break;
176 QString prefix = "";
177 QString suffix = "";
178 switch (m_configSource->getControllerType()) {
179 case VehicleConfigurationSource::CONTROLLER_CC:
180 case VehicleConfigurationSource::CONTROLLER_CC3D:
181 prefix = "cc-";
182 if (m_configSource->getEscType() == VehicleConfigurationSource::ESC_ONESHOT125 ||
183 m_configSource->getEscType() == VehicleConfigurationSource::ESC_ONESHOT42 ||
184 m_configSource->getEscType() == VehicleConfigurationSource::ESC_MULTISHOT ||
185 m_configSource->getEscType() == VehicleConfigurationSource::ESC_SYNCHED) {
186 suffix = "-oneshot";
188 break;
189 case VehicleConfigurationSource::CONTROLLER_REVO:
190 prefix = "revo-";
191 break;
192 case VehicleConfigurationSource::CONTROLLER_NANO:
193 prefix = "nano-";
194 break;
195 case VehicleConfigurationSource::CONTROLLER_SPARKY2:
196 prefix = "sparky2-";
197 break;
198 default:
199 break;
202 switch (m_configSource->getInputType()) {
203 case VehicleConfigurationSource::INPUT_PWM:
204 elementsToShow << QString("%1pwm").arg(prefix);
205 break;
206 case VehicleConfigurationSource::INPUT_PPM:
207 elementsToShow << QString("%1ppm%2").arg(prefix).arg(suffix);
208 break;
209 case VehicleConfigurationSource::INPUT_SBUS:
210 elementsToShow << QString("%1sbus").arg(prefix);
211 break;
212 case VehicleConfigurationSource::INPUT_SRXL:
213 elementsToShow << QString("%1srxl").arg(prefix);
214 break;
215 case VehicleConfigurationSource::INPUT_DSM:
216 elementsToShow << QString("%1satellite").arg(prefix);
217 break;
218 case VehicleConfigurationSource::INPUT_HOTT_SUMD:
219 elementsToShow << QString("%1hott").arg(prefix);
220 break;
221 case VehicleConfigurationSource::INPUT_EXBUS:
222 elementsToShow << QString("%1exbus").arg(prefix);
223 break;
224 case VehicleConfigurationSource::INPUT_IBUS:
225 elementsToShow << QString("%1ibus").arg(prefix);
226 break;
227 default:
228 break;
231 if (m_configSource->getVehicleType() == VehicleConfigurationSource::VEHICLE_FIXEDWING &&
232 m_configSource->getAirspeedType() != VehicleConfigurationSource::AIRSPEED_ESTIMATE) {
233 switch (m_configSource->getAirspeedType()) {
234 case VehicleConfigurationSource::AIRSPEED_EAGLETREE:
235 elementsToShow << QString("%1eagletree-speed-sensor").arg(prefix);
236 break;
237 case VehicleConfigurationSource::AIRSPEED_MS4525:
238 elementsToShow << QString("%1ms4525-speed-sensor").arg(prefix);
239 break;
240 default:
241 break;
245 if ((m_configSource->getInputType() == VehicleConfigurationSource::INPUT_SBUS) &&
246 (m_configSource->getControllerType() != VehicleConfigurationSource::CONTROLLER_SPARKY2)) {
247 prefix = QString("flexi-%1").arg(prefix);
249 switch (m_configSource->getGpsType()) {
250 case VehicleConfigurationSource::GPS_DISABLED:
251 break;
252 case VehicleConfigurationSource::GPS_NMEA:
253 elementsToShow << QString("%1generic-nmea").arg(prefix);
254 break;
255 case VehicleConfigurationSource::GPS_PLATINUM:
256 elementsToShow << QString("%1OPGPS-v9").arg(prefix);
257 break;
258 case VehicleConfigurationSource::GPS_NAZA:
259 elementsToShow << QString("%1NazaGPS").arg(prefix);
260 break;
261 case VehicleConfigurationSource::GPS_UBX:
262 elementsToShow << QString("%1OPGPS-v8-ublox").arg(prefix);
263 break;
264 case VehicleConfigurationSource::GPS_UBX_FLEXI_I2CMAG:
265 elementsToShow << QString("%1generic-ublox-mag").arg(prefix);
266 break;
267 default:
268 break;
271 setupGraphicsSceneItems(elementsToShow);
272 fitInView();
273 qDebug() << "Scene complete";
277 void ConnectionDiagram::setupGraphicsSceneItems(QList<QString> elementsToShow)
279 qreal z = 0;
281 foreach(QString elementId, elementsToShow) {
282 if (m_renderer->elementExists(elementId)) {
283 QGraphicsSvgItem *element = new QGraphicsSvgItem();
284 element->setSharedRenderer(m_renderer);
285 element->setElementId(elementId);
286 element->setZValue(z++);
287 element->setOpacity(1.0);
289 QMatrix matrix = m_renderer->matrixForElement(elementId);
290 QRectF orig = matrix.mapRect(m_renderer->boundsOnElement(elementId));
291 element->setPos(orig.x(), orig.y());
292 m_scene->addItem(element);
293 qDebug() << "Adding " << elementId << " to scene at " << element->pos();
294 } else {
295 qDebug() << "Element with id: " << elementId << " not found.";
300 void ConnectionDiagram::on_saveButton_clicked()
302 QImage image(2200, 1100, QImage::Format_ARGB32);
304 image.fill(Qt::white);
305 QPainter painter(&image);
306 m_scene->render(&painter);
307 QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Images (*.png *.xpm *.jpg)"));
308 if (!fileName.isEmpty()) {
309 image.save(fileName);