1 /*==================================================================
3 ! mardrone application AR-Drone for MeeGo
5 ! Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 ! Author:Kate Alhola kate.alhola@nokia.com
10 ! GNU Lesser General Public License Usage
11 ! This file may be used under the terms of the GNU Lesser
12 ! General Public License version 2.1 as published by the Free Software
13 ! Foundation and appearing in the file LICENSE.LGPL included in the
14 ! packaging of this file. Please review the following information to
15 ! ensure the GNU Lesser General Public License version 2.1 requirements
16 ! will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20 *===================================================================*/
21 #include "dronecontrol.h"
24 DroneControl::DroneControl():QObject()
26 qDebug() << "DroneControl::DroneControl";
28 // ctlSock=new QTcpSocket();
29 // ctlSock->bind(QHostAddress::Any,5559);
30 // navSock=new QUdpSocket();
31 // navSock->bind(QHostAddress::Any,5554)
32 //connect(navSocket,SIGNAL(readyRead()),SLOT(navDataReady()));
33 droneHost.setAddress("192.168.1.1");
34 droneThread=new DroneThread(this,droneHost);
35 connect(droneThread->navData(),SIGNAL(navDataUpdated()),this,SLOT(navDataUpdated()));
36 connect(droneThread->navData(),SIGNAL(stateUpdated()),this,SLOT(statusUpdated()));
37 droneSettings=new QSettings("katix.org","ardrone");
44 void DroneControl::navDataUpdated()
46 emit navDataChanged();
49 void DroneControl::statusUpdated()
55 void DroneControl::setPitch(float val_)
58 // qDebug() << "setPitch=" << val_;
59 droneThread->setDroneControl(m_pitch,m_roll,m_yaw,m_vv);
61 float DroneControl::pitch() {return m_pitch;};
63 void DroneControl::setRoll(float val_)
66 // qDebug() << "setRoll=" << val_;
67 droneThread->setDroneControl(m_pitch,m_roll,m_yaw,m_vv);
69 float DroneControl::roll() {return m_roll;};
70 void DroneControl::setYaw(float val_) {
72 // qDebug() << "setYaw=" << val_;
73 droneThread->setDroneControl(m_pitch,m_roll,m_yaw,m_vv);
75 float DroneControl::yaw() {return m_yaw;};
76 void DroneControl::setVVelocity(float val_) {
78 // qDebug() << "setVv=" << val_;
79 droneThread->setDroneControl(m_pitch,m_roll,m_yaw,m_vv);
81 float DroneControl::vVelocity() {return m_vv;};
82 void DroneControl::setAltitude(float val_) {
85 float DroneControl::altitude() {return m_altitude;};
86 void DroneControl::setEnabled(int val_) { m_enabled=val_;};
87 int DroneControl::enabled() {return m_enabled;};
88 void DroneControl::setFly(bool val_)
91 qDebug() << "setFly=" << val_;
92 droneThread->setFly(m_fly);
94 bool DroneControl::fly() {return m_fly;};
95 void DroneControl::setEmergency(bool val_)
98 qDebug() << "setEmergency=" << val_;
99 droneThread->setEmergency(m_emergency);
101 bool DroneControl::emergency() {return m_emergency;};
104 // Getters to drone actual valyes sent by drone
105 float DroneControl::droneAltitude()
107 return droneThread->navData()->altitude;
109 float DroneControl::dronePitch()
111 return droneThread->navData()->pitch;
113 float DroneControl::droneRoll()
115 return droneThread->navData()->roll;
117 float DroneControl::droneYaw()
119 return droneThread->navData()->yaw;
121 float DroneControl::droneVBat()
123 return droneThread->navData()->vbat;
125 QString DroneControl::decodedStatus()
127 return droneThread->navData()->decodedState;
129 int DroneControl::pwm_motor1()
131 return droneThread->navData()->pwm_motor1;
133 int DroneControl::pwm_motor2()
135 return droneThread->navData()->pwm_motor2;
137 int DroneControl::pwm_motor3()
139 return droneThread->navData()->pwm_motor3;
141 int DroneControl::pwm_motor4()
143 return droneThread->navData()->pwm_motor4;
150 QString DroneControl::confDroneIp()
152 qDebug() << "confDroneIp:" << droneSettings->value("droneIp","192.168.1.1").toString();
153 return droneSettings->value("droneIp","192.168.1.1").toString();
155 void DroneControl::setConfDroneIp(QString ip)
157 qDebug() << "setConfDroneIp:" << ip;
158 droneSettings->setValue("droneIp",ip);
159 emit configChanged();
161 bool DroneControl::confShowDebug()
163 return droneSettings->value("showDebug",true).toBool();
165 void DroneControl::setConfShowDebug(bool val)
167 droneSettings->setValue("showDebug",val);
168 emit configChanged();
170 bool DroneControl::confShowHorizon()
172 return droneSettings->value("showHorizon",true).toBool();
174 void DroneControl::setConfShowHorizon(bool val)
176 droneSettings->setValue("showHorizon",val);
177 emit configChanged();
179 bool DroneControl::confShowGauges()
181 return droneSettings->value("showGuges",true).toBool();
183 void DroneControl::setConfShowGauges(bool val)
185 droneSettings->setValue("showGauges",val);
186 emit configChanged();
188 bool DroneControl::confUseAccel()
190 return droneSettings->value("useAccel",false).toBool();
192 void DroneControl::setConfUseAccel(bool val)
194 droneSettings->setValue("useAccel",val);
195 emit configChanged();
197 bool DroneControl::confUseJoyStick()
199 return droneSettings->value("useJouStick",false).toBool();
201 void DroneControl::setConfUseJoyStick(bool val)
203 droneSettings->setValue("useJoyStick",val);
204 emit configChanged();
207 bool DroneControl::confFullScreen()
209 return droneSettings->value("fullScreen",true).toBool();
211 void DroneControl::setConfFullScreen(bool val)
213 droneSettings->setValue("fullScreen",val);
214 emit configChanged();
217 float DroneControl::confForwardGain()
219 return droneSettings->value("forwardGain",1.0).toFloat();
221 void DroneControl::setConfForwardGain(float val)
223 droneSettings->setValue("forwardGain",val);
224 emit configChanged();
228 float DroneControl::confBackwardGain()
230 return droneSettings->value("backwardGain",1.0).toFloat();
232 void DroneControl::setConfBackwardGain(float val)
234 droneSettings->setValue("backwardGain",val);
235 emit configChanged();
238 float DroneControl::confLeftGain()
240 return droneSettings->value("leftGain",1.0).toFloat();
242 void DroneControl::setConfLeftGain(float val)
244 droneSettings->setValue("leftGain",val);
245 emit configChanged();
247 float DroneControl::confRightGain()
249 return droneSettings->value("rightGain",1.0).toFloat();
251 void DroneControl::setConfRightGain(float val)
253 droneSettings->setValue("rightGain",val);
254 emit configChanged();
257 /*=================================================
259 DroneThread class starts here
261 ==================================================*/
264 void DroneThread::setFly(bool fly)
269 stateTimer->setInterval(50); // More frequent updates
270 sendCmd(QString("AT*FTRIM=%1\r").arg(seq++));
275 stateTimer->setInterval(200); // Less frequent updates
280 sendCmd(QString("AT*REF=%1,%2\r").arg(seq++).arg((1<<18) + (1<<20) + (1<<22) + (1<<24) +(1<<28) +(state==flying ? 1<<9:0)));
283 void DroneThread::setEmergency(bool emg)
286 sendCmd(QString("AT*REF=%1,%2\r").arg(seq++).arg((1<<18) + (1<<20) + (1<<22) + (1<<24) +(1<<28) + (m_emergency ? 1<<8:0)));
287 // if(m_emergency==1)
290 void DroneThread::setDroneControl(float pitch,float roll,float yaw,float vv)
296 qDebug() << QString("pitch=%1 roll=%2 yaw=%3 vv=%4\r").arg(m_pitch,3,'F',2).arg(m_roll,3,'F',2).arg(m_yaw,3,'F',2).arg(m_vv,3,'F',2);
299 void DroneThread::setDroneGain(float fgain,float bgain,float lgain,float rgain)
307 void DroneThread::sendCmd(QString cmd)
311 if(cmd.contains("%")) seqCmd=cmd.arg(seq++);
312 dgram=seqCmd.toLatin1();
313 cmdSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5556);
314 seqCmd.chop(1); // Remove training cr
315 qDebug() << "DroneThread::sendCmd= " << seqCmd << "to " << droneHost ;
318 void DroneThread::navDataReady()
324 while(navSock->hasPendingDatagrams()) l=navSock->readDatagram(buf,sizeof(buf),&host,&port);
325 // qDebug() << "DroneThread::navDataReady state=" << state <<" l=" << l << "read=" << buf << "from" << host ;
326 nd.parseRawNavData((char *)&buf,l);
327 if(nd.state& (ARDRONE_COM_WATCHDOG_MASK ))
328 sendCmd(QString("AT*COMWDG=%1\r").arg(seq++));
331 sendCmd("AT*CONFIG=%1,\"general:navdata_demo\",\"FALSE\"\r");
335 sendCmd("AT*CTRL=0\r");
343 void DroneThread::sendNav(QString cmd)
346 dgram=cmd.toLatin1();
347 qDebug() << "DroneThread::sendNav= " << cmd+"\n" << "to " << droneHost ;
348 navSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5554);
352 DroneThread::DroneThread(DroneControl *parentp,QHostAddress host)
354 qDebug() << "DroneThread::DroneThread";
356 state=notInitialized;
358 navSock=new QUdpSocket();
359 navSock->bind(QHostAddress::Any,5554);
360 cmdSock=new QUdpSocket();
361 cmdSock->bind(QHostAddress::Any,5556);
377 void DroneThread::run()
379 qDebug() << "DroneThread::DroneThread";
380 stateTimer=new QTimer(this);
381 connect(stateTimer,SIGNAL(timeout()),this,SLOT(timer()));
382 connect(navSock,SIGNAL(readyRead()),this,SLOT(navDataReady()));
383 stateTimer->start(1000);
390 void DroneThread::timer()
392 // qDebug() << "thread Timer";
400 sendCmd(QString("AT*REF=%1,%2\r").arg(seq++).arg((1<<18) + (1<<20) + (1<<22) + (1<<24) +(1<<28)));
403 float_or_int_t _pitch,_roll,_yaw,_vv;
404 int r=(m_pitch!=0.0 || m_roll!=0.0)?1:0;
411 // qDebug() << QString("AT*PCMD=%1,0,%2,%3,%4,%5\r").arg(seq).arg(_roll.f,3,'F',2).arg(_pitch.f,3,'F',2).arg(_vv.f,3,'F',2).arg(_yaw.f,3,'F',2);
412 // qDebug() << QString("AT*PCMD=%1,0,%2,%3,%4,%5\r").arg(seq).arg(_roll.i,8,16).arg(_pitch.i,8,16).arg(_vv.i,8,16).arg(_yaw.i,8,16);
413 sendCmd(QString("AT*COMWDG=%1\r").arg(seq++));
414 sendCmd(QString("AT*PCMD=%1,%2,%3,%4,%5,%6\r").arg(seq++).arg(r).arg(_roll.i).arg(_pitch.i).arg(_vv.i).arg(_yaw.i));