Video record function added
[mardrone] / mardrone / dronelib / dronecontrol.cpp
index b0e4a4b..9571a68 100644 (file)
@@ -28,6 +28,7 @@
 #include <QNetworkConfigurationManager>
 #include <QNetworkSession>
 #include <QList>
+#include <QDateTime>
 #include "navdata.h"
 #include "ardrone_api.h"
 
@@ -172,6 +173,33 @@ void DroneControl::setEmergency(bool val_)
     droneThread->setEmergency(m_emergency);
 };
 
+bool  DroneControl::recordLog()
+{
+    return droneThread? droneThread->getRecordLog():false;
+};
+
+void DroneControl::setRecordLog(bool val_)
+{
+   if(droneThread) droneThread->setRecordLog(val_);
+};
+QString DroneControl::logFileName()
+{
+    return droneThread? droneThread->getLogFileName():"";
+};
+void DroneControl::setLogFileName(QString val_)
+{
+   if(droneThread) droneThread->setLogFileName(val_);
+};
+unsigned int   DroneControl::logSeq()
+{
+    return droneThread? droneThread->getLogSeq():0;
+};
+
+void DroneControl::setLogSeq(int val_)
+{
+    if(droneThread) droneThread->setLogSeq(val_);
+};
+
 
 
 // Getters to drone actual valyes sent by drone
@@ -187,6 +215,15 @@ int   DroneControl::pwm_motor2()    { return droneThread->navData()->pwm_motor2;
 int   DroneControl::pwm_motor3()    { return droneThread->navData()->pwm_motor3;};
 int   DroneControl::pwm_motor4()      { return droneThread->navData()->pwm_motor4;};
 
+
+/*
+
+  Setters and Getters for Config variables
+
+  */
+
+
+
 QString DroneControl::confDroneIp()
 {
     qDebug() << "confDroneIp:" << droneSettings->value("droneIp","192.168.1.1").toString();
@@ -342,12 +379,78 @@ void DroneControl::setConfRightGain(float val)
     emit configChanged();
 };
 
+QString DroneControl::errorString()
+{
+    return m_errorString;
+};
+void DroneControl::setErrorString(QString val_)
+{
+    m_errorString=val_;
+};
+
 /*=================================================
 
  DroneThread class starts here
 
 ==================================================*/
 
+DroneThread::DroneThread(DroneControl *parentp,QHostAddress host)
+{
+
+    struct ip_mreq imreq;
+    QString my_ip;
+    qDebug() << "DroneThread::DroneThread";
+    my_ip="192.168.1.2";
+        stopped=false;
+        state=notInitialized;
+        parent=parentp;
+        navSock=new QUdpSocket();
+        QList<QNetworkConfiguration> netconfs=QNetworkConfigurationManager().allConfigurations();
+        foreach (QNetworkConfiguration np,netconfs) {
+            qDebug() << "network Confifuration name " << np.name() << np.bearerName() << np.bearerTypeName();
+            QNetworkSession ns(np);
+            if(ns.interface().addressEntries().empty())
+                qDebug() << "network session " << ns.interface().humanReadableName() << "**NotConfig**";
+            else {
+                qDebug() << "network session " << ns.interface().humanReadableName() <<   ns.interface().addressEntries().first().ip();
+                // Ubuntu may give wlan0 as "Ethernet"
+                if((np.bearerName()==QString("WLAN")) || (ns.interface().humanReadableName()==QString("wlan0")) ) {
+                   my_ip=ns.interface().addressEntries().first().ip().toString();
+                   qDebug() << "My IP is " << my_ip;
+            }
+            }
+        }
+        if(!navSock->bind(QHostAddress::Any,5554))
+            qDebug() << "Cant open any: 5554" << navSock->errorString() ;
+//#if 0
+        // Qt 4.7.x
+        imreq.imr_multiaddr.s_addr=inet_addr("224.1.1.1");
+        imreq.imr_interface.s_addr=inet_addr(my_ip.toAscii());
+        setsockopt(navSock->socketDescriptor(),IPPROTO_IP,IP_ADD_MEMBERSHIP,&imreq,sizeof(imreq));
+//#else
+        // Qt 4.8
+        if(!navSock->joinMulticastGroup(QHostAddress("224.1.1.1")))
+            qDebug() << "can't join multicast Group 224.1.1.1" << navSock->errorString();
+//#endif
+        cmdSock=new QUdpSocket();
+        cmdSock->bind(QHostAddress::Any,5556);
+        droneHost=host;
+        recordLog=false;
+        logFile=NULL;
+        logFileName="";
+        seq=1;
+        m_pitch=0;
+        m_roll=0;
+        m_yaw=0;
+        m_vv=0;
+        m_fgain=1.0;
+        m_bgain=1.0;
+        m_rgain=1.0;
+        m_lgain=1.0;
+        noreply=0;
+        start();
+
+};
 
 void DroneThread::setFly(bool fly)
 {
@@ -368,6 +471,51 @@ void DroneThread::setFly(bool fly)
      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)));
 };
 
+void DroneThread::setRecordLog(bool log)
+{
+    qDebug() << "DroneThread::setRecordLog" <<  log;
+    QDateTime fileDate=QDateTime::currentDateTime();
+    if(!recordLog && log) {
+        if(logFileName.isEmpty())  logFileName="dronerec"+fileDate.toString("yyyyMMddhhmm");
+        qDebug() << "logFileName=" <<  logFileName;
+        //emit frameSeqChanged();
+    }
+    recordLog=log;
+    if(!recordLog && logFile) { // Stop recording
+        logFile->close();
+        logFile->~QFile();
+        logFile=NULL;
+        logFileName="";
+    }
+
+
+}
+
+bool DroneThread::getRecordLog()
+{
+    return recordLog;
+}
+
+QString DroneThread::getLogFileName()
+{
+    return logFileName;
+}
+
+void DroneThread::setLogFileName(QString name)
+{
+    logFileName=name;
+}
+
+unsigned int DroneThread::getLogSeq()
+{
+    return logSeq;
+}
+
+void DroneThread::setLogSeq(unsigned int val)
+{
+    logSeq=val;
+}
+
 void DroneThread::setEmergency(bool emg)
 {
     m_emergency=emg;
@@ -459,54 +607,6 @@ void DroneThread::sendNav(QString cmd)
 }
 
 
-DroneThread::DroneThread(DroneControl *parentp,QHostAddress host)
-{
-
-    struct ip_mreq imreq;
-    QString my_ip;
-    qDebug() << "DroneThread::DroneThread";
-    my_ip="192.168.1.2";
-        stopped=false;
-        state=notInitialized;
-        parent=parentp;
-        navSock=new QUdpSocket();
-        QList<QNetworkConfiguration> netconfs=QNetworkConfigurationManager().allConfigurations();
-        foreach (QNetworkConfiguration np,netconfs) {
-            qDebug() << "network Confifuration name " << np.name() << np.bearerName() << np.bearerTypeName();
-            QNetworkSession ns(np);
-            if(ns.interface().addressEntries().empty())
-                qDebug() << "network session " << ns.interface().humanReadableName() << "**NotConfig**";
-            else {
-                qDebug() << "network session " << ns.interface().humanReadableName() <<   ns.interface().addressEntries().first().ip();
-                // Ubuntu may give wlan0 as "Ethernet"
-                if((np.bearerName()==QString("WLAN")) || (ns.interface().humanReadableName()==QString("wlan0")) ) {
-                   my_ip=ns.interface().addressEntries().first().ip().toString();
-                   qDebug() << "My IP is " << my_ip;
-            }
-            }
-        }
-        if(!navSock->bind(QHostAddress::Any,5554))
-            qDebug() << "Cant open any: 5554" << navSock->errorString() ;
-        imreq.imr_multiaddr.s_addr=inet_addr("224.1.1.1");
-        imreq.imr_interface.s_addr=inet_addr(my_ip.toAscii());
-        setsockopt(navSock->socketDescriptor(),IPPROTO_IP,IP_ADD_MEMBERSHIP,&imreq,sizeof(imreq));
-        cmdSock=new QUdpSocket();
-        cmdSock->bind(QHostAddress::Any,5556);
-        droneHost=host;
-        seq=1;
-        m_pitch=0;
-        m_roll=0;
-        m_yaw=0;
-        m_vv=0;
-        m_fgain=1.0;
-        m_bgain=1.0;
-        m_rgain=1.0;
-        m_lgain=1.0;
-        noreply=0;
-        start();
-
-
-};
 
 void DroneThread::run()
 {
@@ -529,7 +629,7 @@ void DroneThread::timer()
     if(noreply>50 )  {
         cmd+=QString("AT*COMWDG=%1\r").arg(seq++);
         navData()->setState("**TimeOut**");
-        qDebug("Timeout");
+   //     qDebug("Timeout");
         noreply=0;
     };
     switch(state) {