dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
gcscontrol.cpp
Go to the documentation of this file.
1 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, see <http://www.gnu.org/licenses/>
24  */
25 
26 #include "gcscontrol.h"
27 #include <QDebug>
28 #include <QtPlugin>
30 #if defined(USE_SDL)
31 #include "sdlgamepad/sdlgamepad.h"
32 #endif
33 
34 #define CHANNEL_MAX 9000
35 #define CHANNEL_NEUTRAL 5000
36 #define CHANNEL_THROTNEUTRAL 1800
37 #define CHANNEL_MIN 1000
38 
39 bool GCSControl::firstInstance = true;
40 
41 GCSControl::GCSControl()
42  : hasControl(false)
43 {
44  Q_ASSERT(firstInstance); // There should only be one instance of this class
45  firstInstance = false;
46  receiverActivity.setInterval(100);
47  connect(&receiverActivity, &QTimer::timeout, this, &GCSControl::receiverActivitySlot);
48 }
49 
50 void GCSControl::extensionsInitialized()
51 {
52  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
53  UAVObjectManager *objMngr = pm->getObject<UAVObjectManager>();
54  Q_ASSERT(objMngr);
55 
56  manControlSettingsUAVO = ManualControlSettings::GetInstance(objMngr);
57  Q_ASSERT(manControlSettingsUAVO);
58 
59  m_gcsReceiver = UAVTalkReceiver::GetInstance(objMngr);
60  Q_ASSERT(m_gcsReceiver);
61 }
62 
63 GCSControl::~GCSControl()
64 {
65  // Delete this plugin from plugin manager
66  removeObject(this);
67 }
68 
69 bool GCSControl::initialize(const QStringList &arguments, QString *errorString)
70 {
71  Q_UNUSED(arguments);
72  Q_UNUSED(errorString);
73 
74 #if defined(USE_SDL)
75  sdlGamepad = new SDLGamepad();
76  if (sdlGamepad->init()) {
77  sdlGamepad->start();
78  qRegisterMetaType<QListInt16>("QListInt16");
79  qRegisterMetaType<ButtonNumber>("ButtonNumber");
80  }
81 #endif
82 
83  mf = new GCSControlGadgetFactory(this);
85 
86  // Add this plugin to plugin manager
87  addObject(this);
88  return true;
89 }
90 
91 void GCSControl::shutdown()
92 {
93 }
94 
100 {
101  if (hasControl)
102  return false;
103  dataBackup = manControlSettingsUAVO->getData();
104  metaBackup = manControlSettingsUAVO->getMetadata();
105  ManualControlSettings::Metadata meta = manControlSettingsUAVO->getDefaultMetadata();
107 
108  // No need to set flight mode, we leave that at none and directly change
109  // the setting
110  manControlSettingsUAVO->setChannelGroups(ManualControlSettings::CHANNELGROUPS_FLIGHTMODE,
111  ManualControlSettings::CHANNELGROUPS_NONE);
112 
113  // Only throttle, roll, pitch, yaw, and arming need to be configured for this
114  // mode to work
115  const int NUM_CHANNELS = 5;
116  const quint8 channels[NUM_CHANNELS] = { ManualControlSettings::CHANNELGROUPS_THROTTLE,
117  ManualControlSettings::CHANNELGROUPS_ROLL,
118  ManualControlSettings::CHANNELGROUPS_PITCH,
119  ManualControlSettings::CHANNELGROUPS_YAW,
120  ManualControlSettings::CHANNELGROUPS_ARMING };
121 
122  for (quint8 i = 0; i < NUM_CHANNELS; ++i) {
123  quint8 x = channels[i];
124 
125  inverseMapping[x] = i;
126 
127  // Assign this channel to GCS control
128  manControlSettingsUAVO->setChannelGroups(x, ManualControlSettings::CHANNELGROUPS_UAVTALK);
129 
130  // Set the ranges to match what the widget produces
131  manControlSettingsUAVO->setChannelNumber(x, i + 1);
132  manControlSettingsUAVO->setChannelMax(x, CHANNEL_MAX);
133 
134  if (x == ManualControlSettings::CHANNELGROUPS_THROTTLE) {
135  manControlSettingsUAVO->setChannelNeutral(x, CHANNEL_THROTNEUTRAL);
136  } else {
137  manControlSettingsUAVO->setChannelNeutral(x, CHANNEL_NEUTRAL);
138  }
139 
140  manControlSettingsUAVO->setChannelMin(x, CHANNEL_MIN);
141  }
142 
143  manControlSettingsUAVO->setDeadband(0);
144  manControlSettingsUAVO->setFlightModeNumber(1);
145  manControlSettingsUAVO->setFlightModePosition(
146  0, ManualControlSettings::FLIGHTMODEPOSITION_STABILIZED1);
147  manControlSettingsUAVO->updated();
148  connect(manControlSettingsUAVO, &UAVObject::objectUpdated, this, &GCSControl::objectsUpdated);
149  hasControl = true;
150 
151  for (quint8 x = 0; x < NUM_CHANNELS; ++x) {
152  setChannel(channels[x], 0);
153  }
154 
155  setChannel(ManualControlSettings::CHANNELGROUPS_ARMING, -1);
156  receiverActivity.start();
157  return true;
158 }
159 
164 {
165  if (!hasControl)
166  return false;
167  disconnect(manControlSettingsUAVO, &UAVObject::objectUpdated, this,
168  &GCSControl::objectsUpdated);
169  manControlSettingsUAVO->setData(dataBackup);
170  manControlSettingsUAVO->setMetadata(metaBackup);
171  manControlSettingsUAVO->updated();
172  hasControl = false;
173  receiverActivity.stop();
174  return true;
175 }
176 
177 bool GCSControl::setFlightMode(ManualControlSettings::FlightModePositionOptions flightMode)
178 {
179  if (!hasControl)
180  return false;
181  manControlSettingsUAVO->setFlightModePosition(0, flightMode);
182  manControlSettingsUAVO->updated();
183  m_gcsReceiver->setChannel(ManualControlSettings::CHANNELGROUPS_FLIGHTMODE, CHANNEL_MIN);
184  m_gcsReceiver->updated();
185  return true;
186 }
187 
188 bool GCSControl::setThrottle(float value)
189 {
190  return setChannel(ManualControlSettings::CHANNELGROUPS_THROTTLE, value);
191 }
192 
193 bool GCSControl::setRoll(float value)
194 {
195  return setChannel(ManualControlSettings::CHANNELGROUPS_ROLL, value);
196 }
197 
198 bool GCSControl::setPitch(float value)
199 {
200  return setChannel(ManualControlSettings::CHANNELGROUPS_PITCH, value);
201 }
202 
203 bool GCSControl::setYaw(float value)
204 {
205  return setChannel(ManualControlSettings::CHANNELGROUPS_YAW, value);
206 }
207 
208 bool GCSControl::setArming(float value)
209 {
210  return setChannel(ManualControlSettings::CHANNELGROUPS_ARMING, value);
211 }
212 
213 bool GCSControl::setChannel(quint8 channeltype, float value)
214 {
215  if (channeltype >= ManualControlSettings::CHANNELGROUPS_NUMELEM)
216  return false;
217 
218  uint8_t channel = inverseMapping[channeltype];
219 
220  if (value > 1 || value < -1 || channel >= UAVTalkReceiver::CHANNEL_NUMELEM || !hasControl)
221  return false;
222  quint16 pwmValue;
223  if (value >= 0)
224  pwmValue = (value * (float)(CHANNEL_MAX - CHANNEL_NEUTRAL)) + (float)CHANNEL_NEUTRAL;
225  else
226  pwmValue = (value * (float)(CHANNEL_NEUTRAL - CHANNEL_MIN)) + (float)CHANNEL_NEUTRAL;
227  m_gcsReceiver->setChannel(channel, pwmValue);
228  m_gcsReceiver->updated();
229  return true;
230 }
231 
232 void GCSControl::objectsUpdated(UAVObject *obj)
233 {
234  qDebug() << "GCSControl::objectsUpdated"
235  << "Object" << obj->getName() << "changed outside this class";
236 }
237 
238 void GCSControl::receiverActivitySlot()
239 {
240  if (m_gcsReceiver)
241  m_gcsReceiver->updated();
242 }
void addObject(QObject *obj)
Definition: iplugin.cpp:291
bool setYaw(float value)
Definition: gcscontrol.cpp:203
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
bool setRoll(float value)
Definition: gcscontrol.cpp:193
bool setThrottle(float value)
Definition: gcscontrol.cpp:188
for i
Definition: OPPlots.m:140
bool setFlightMode(ManualControlSettings::FlightModePositionOptions flightMode)
Definition: gcscontrol.cpp:177
bool setChannel(quint8 channel, float value)
Definition: gcscontrol.cpp:213
void objectUpdated(UAVObject *obj)
Signal sent whenever any field of the object is updated.
static void SetGcsAccess(Metadata &meta, AccessMode mode)
Definition: uavobject.cpp:407
bool beginGCSControl()
GCSControl::beginGCSControl Configure the FC to use the GCSReceiver control for manual control (cache...
Definition: gcscontrol.cpp:99
bool setPitch(float value)
Definition: gcscontrol.cpp:198
void removeObject(QObject *obj)
Definition: iplugin.cpp:317
bool endGCSControl()
GCSControl::endGCSControl restores the cached ManualControlSettings.
Definition: gcscontrol.cpp:163
QString getName()
Definition: uavobject.cpp:131
void addAutoReleasedObject(QObject *obj)
Definition: iplugin.cpp:306
x
Definition: OPPlots.m:100
bool setArming(float value)
Definition: gcscontrol.cpp:208