2010-08-31 12:17:14 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file simulator.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup HITLPlugin HITL Plugin
|
|
|
|
* @{
|
|
|
|
* @brief The Hardware In The Loop plugin
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "simulator.h"
|
|
|
|
#include "qxtlogger.h"
|
|
|
|
#include "extensionsystem/pluginmanager.h"
|
|
|
|
#include "coreplugin/icore.h"
|
|
|
|
#include "coreplugin/threadmanager.h"
|
|
|
|
|
|
|
|
volatile bool Simulator::isStarted = false;
|
|
|
|
|
2011-03-30 17:59:53 +02:00
|
|
|
const float Simulator::GEE = 9.81;
|
2010-12-24 19:35:34 +01:00
|
|
|
const float Simulator::FT2M = 0.3048;
|
|
|
|
const float Simulator::KT2MPS = 0.514444444;
|
|
|
|
const float Simulator::INHG2KPA = 3.386;
|
|
|
|
const float Simulator::FPS2CMPS = 30.48;
|
|
|
|
const float Simulator::DEG2RAD = (M_PI/180.0);
|
2012-08-06 09:50:26 +02:00
|
|
|
const float Simulator::RAD2DEG = (180.0/M_PI);
|
2010-12-24 19:35:34 +01:00
|
|
|
|
|
|
|
|
2010-08-31 12:17:14 +02:00
|
|
|
Simulator::Simulator(const SimulatorSettings& params) :
|
|
|
|
simProcess(NULL),
|
|
|
|
time(NULL),
|
|
|
|
inSocket(NULL),
|
|
|
|
outSocket(NULL),
|
|
|
|
settings(params),
|
2010-11-29 18:05:53 +01:00
|
|
|
updatePeriod(50),
|
2012-05-26 17:27:22 +02:00
|
|
|
simTimeout(8000),
|
2010-08-31 12:17:14 +02:00
|
|
|
autopilotConnectionStatus(false),
|
|
|
|
simConnectionStatus(false),
|
|
|
|
txTimer(NULL),
|
|
|
|
simTimer(NULL),
|
|
|
|
name("")
|
|
|
|
{
|
|
|
|
// move to thread
|
|
|
|
moveToThread(Core::ICore::instance()->threadManager()->getRealTimeThread());
|
2010-11-29 18:05:53 +01:00
|
|
|
connect(this, SIGNAL(myStart()), this, SLOT(onStart()),Qt::QueuedConnection);
|
2010-08-31 12:17:14 +02:00
|
|
|
emit myStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
Simulator::~Simulator()
|
|
|
|
{
|
|
|
|
if(inSocket)
|
|
|
|
{
|
|
|
|
delete inSocket;
|
|
|
|
inSocket = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(outSocket)
|
|
|
|
{
|
|
|
|
delete outSocket;
|
|
|
|
outSocket = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(txTimer)
|
|
|
|
{
|
|
|
|
delete txTimer;
|
|
|
|
txTimer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(simTimer)
|
|
|
|
{
|
|
|
|
delete simTimer;
|
|
|
|
simTimer = NULL;
|
|
|
|
}
|
|
|
|
// NOTE: Does not currently work, may need to send control+c to through the terminal
|
|
|
|
if (simProcess != NULL)
|
|
|
|
{
|
|
|
|
//connect(simProcess,SIGNAL(finished(int, QProcess::ExitStatus)),this,SLOT(onFinished(int, QProcess::ExitStatus)));
|
|
|
|
|
|
|
|
simProcess->disconnect();
|
|
|
|
if(simProcess->state() == QProcess::Running)
|
|
|
|
simProcess->kill();
|
|
|
|
//if(simProcess->waitForFinished())
|
|
|
|
//emit deleteSimProcess();
|
|
|
|
delete simProcess;
|
|
|
|
simProcess = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::onDeleteSimulator(void)
|
|
|
|
{
|
|
|
|
// [1]
|
|
|
|
Simulator::setStarted(false);
|
|
|
|
// [2]
|
|
|
|
Simulator::Instances().removeOne(simulatorId);
|
|
|
|
|
|
|
|
disconnect(this);
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::onStart()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&lock);
|
|
|
|
|
2010-08-31 14:21:06 +02:00
|
|
|
QThread* mainThread = QThread::currentThread();
|
2010-08-31 12:17:14 +02:00
|
|
|
qDebug() << "Simulator Thread: "<< mainThread;
|
|
|
|
|
|
|
|
// Get required UAVObjects
|
|
|
|
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager* objManager = pm->getObject<UAVObjectManager>();
|
|
|
|
actDesired = ActuatorDesired::GetInstance(objManager);
|
2010-11-29 18:05:53 +01:00
|
|
|
manCtrlCommand = ManualControlCommand::GetInstance(objManager);
|
2011-05-08 23:04:26 +02:00
|
|
|
flightStatus = FlightStatus::GetInstance(objManager);
|
2010-10-16 16:30:36 +02:00
|
|
|
posHome = HomeLocation::GetInstance(objManager);
|
2010-10-09 02:59:14 +02:00
|
|
|
velActual = VelocityActual::GetInstance(objManager);
|
2010-10-09 10:52:19 +02:00
|
|
|
posActual = PositionActual::GetInstance(objManager);
|
2010-08-31 14:21:06 +02:00
|
|
|
altActual = BaroAltitude::GetInstance(objManager);
|
2010-08-31 12:17:14 +02:00
|
|
|
attActual = AttitudeActual::GetInstance(objManager);
|
2012-05-26 17:27:22 +02:00
|
|
|
accels = Accels::GetInstance(objManager);
|
|
|
|
gyros = Gyros::GetInstance(objManager);
|
2010-09-26 05:06:27 +02:00
|
|
|
gpsPos = GPSPosition::GetInstance(objManager);
|
2010-08-31 12:17:14 +02:00
|
|
|
telStats = GCSTelemetryStats::GetInstance(objManager);
|
|
|
|
|
|
|
|
// Listen to autopilot connection events
|
|
|
|
TelemetryManager* telMngr = pm->getObject<TelemetryManager>();
|
|
|
|
connect(telMngr, SIGNAL(connected()), this, SLOT(onAutopilotConnect()));
|
|
|
|
connect(telMngr, SIGNAL(disconnected()), this, SLOT(onAutopilotDisconnect()));
|
|
|
|
//connect(telStats, SIGNAL(objectUpdated(UAVObject*)), this, SLOT(telStatsUpdated(UAVObject*)));
|
|
|
|
|
|
|
|
// If already connect setup autopilot
|
|
|
|
GCSTelemetryStats::DataFields stats = telStats->getData();
|
|
|
|
if ( stats.Status == GCSTelemetryStats::STATUS_CONNECTED )
|
|
|
|
onAutopilotConnect();
|
|
|
|
|
|
|
|
inSocket = new QUdpSocket();
|
|
|
|
outSocket = new QUdpSocket();
|
|
|
|
setupUdpPorts(settings.hostAddress,settings.inPort,settings.outPort);
|
|
|
|
|
2010-11-29 18:05:53 +01:00
|
|
|
emit processOutput("\nLocal interface: " + settings.hostAddress + "\n" + \
|
|
|
|
"Remote interface: " + settings.remoteHostAddress + "\n" + \
|
|
|
|
"inputPort: " + QString::number(settings.inPort) + "\n" + \
|
|
|
|
"outputPort: " + QString::number(settings.outPort) + "\n");
|
2010-08-31 12:17:14 +02:00
|
|
|
|
2010-11-29 18:05:53 +01:00
|
|
|
qxtLog->info("\nLocal interface: " + settings.hostAddress + "\n" + \
|
|
|
|
"Remote interface: " + settings.remoteHostAddress + "\n" + \
|
|
|
|
"inputPort: " + QString::number(settings.inPort) + "\n" + \
|
|
|
|
"outputPort: " + QString::number(settings.outPort) + "\n");
|
2010-08-31 12:17:14 +02:00
|
|
|
|
2010-11-29 18:05:53 +01:00
|
|
|
// if(!inSocket->waitForConnected(5000))
|
|
|
|
// emit processOutput(QString("Can't connect to %1 on %2 port!").arg(settings.hostAddress).arg(settings.inPort));
|
|
|
|
// outSocket->connectToHost(settings.hostAddress,settings.outPort); // FG
|
|
|
|
// if(!outSocket->waitForConnected(5000))
|
|
|
|
// emit processOutput(QString("Can't connect to %1 on %2 port!").arg(settings.hostAddress).arg(settings.outPort));
|
2010-08-31 12:17:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
connect(inSocket, SIGNAL(readyRead()), this, SLOT(receiveUpdate()),Qt::DirectConnection);
|
|
|
|
|
|
|
|
// Setup transmit timer
|
|
|
|
txTimer = new QTimer();
|
|
|
|
connect(txTimer, SIGNAL(timeout()), this, SLOT(transmitUpdate()),Qt::DirectConnection);
|
|
|
|
txTimer->setInterval(updatePeriod);
|
|
|
|
txTimer->start();
|
|
|
|
// Setup simulator connection timer
|
|
|
|
simTimer = new QTimer();
|
|
|
|
connect(simTimer, SIGNAL(timeout()), this, SLOT(onSimulatorConnectionTimeout()),Qt::DirectConnection);
|
|
|
|
simTimer->setInterval(simTimeout);
|
|
|
|
simTimer->start();
|
|
|
|
|
|
|
|
// setup time
|
|
|
|
time = new QTime();
|
|
|
|
time->start();
|
|
|
|
current.T=0;
|
2010-10-17 18:55:18 +02:00
|
|
|
current.i=0;
|
2010-11-29 18:05:53 +01:00
|
|
|
|
2010-08-31 12:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::receiveUpdate()
|
|
|
|
{
|
|
|
|
// Update connection timer and status
|
|
|
|
simTimer->setInterval(simTimeout);
|
|
|
|
simTimer->stop();
|
|
|
|
simTimer->start();
|
|
|
|
if ( !simConnectionStatus )
|
|
|
|
{
|
|
|
|
simConnectionStatus = true;
|
|
|
|
emit simulatorConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process data
|
2010-09-19 14:37:41 +02:00
|
|
|
while(inSocket->hasPendingDatagrams()) {
|
2010-08-31 12:17:14 +02:00
|
|
|
// Receive datagram
|
|
|
|
QByteArray datagram;
|
|
|
|
datagram.resize(inSocket->pendingDatagramSize());
|
|
|
|
QHostAddress sender;
|
|
|
|
quint16 senderPort;
|
|
|
|
inSocket->readDatagram(datagram.data(), datagram.size(),
|
|
|
|
&sender, &senderPort);
|
2010-09-23 17:16:45 +02:00
|
|
|
//QString datastr(datagram);
|
2010-08-31 12:17:14 +02:00
|
|
|
// Process incomming data
|
2010-09-23 17:16:45 +02:00
|
|
|
processUpdate(datagram);
|
2010-08-31 12:17:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::setupObjects()
|
|
|
|
{
|
2010-11-06 16:17:33 +01:00
|
|
|
setupInputObject(actDesired, 100);
|
2010-08-31 12:17:14 +02:00
|
|
|
setupOutputObject(altActual, 250);
|
2010-12-21 21:21:00 +01:00
|
|
|
setupOutputObject(attActual, 10);
|
|
|
|
//setupOutputObject(attActual, 100);
|
2010-09-26 05:06:27 +02:00
|
|
|
setupOutputObject(gpsPos, 250);
|
2010-10-16 16:30:36 +02:00
|
|
|
setupOutputObject(posActual, 250);
|
|
|
|
setupOutputObject(velActual, 250);
|
|
|
|
setupOutputObject(posHome, 1000);
|
2012-05-26 17:27:22 +02:00
|
|
|
setupOutputObject(accels, 10);
|
|
|
|
setupOutputObject(gyros, 10);
|
2010-12-21 21:21:00 +01:00
|
|
|
//setupOutputObject(attRaw, 100);
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-31 12:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::setupInputObject(UAVObject* obj, int updatePeriod)
|
|
|
|
{
|
|
|
|
UAVObject::Metadata mdata;
|
|
|
|
mdata = obj->getDefaultMetadata();
|
2012-02-21 02:45:18 +01:00
|
|
|
UAVObject::SetFlightAccess(mdata, UAVObject::ACCESS_READWRITE);
|
|
|
|
UAVObject::SetGcsAccess(mdata, UAVObject::ACCESS_READWRITE);
|
|
|
|
UAVObject::SetFlightTelemetryAcked(mdata, false);
|
|
|
|
UAVObject::SetFlightTelemetryUpdateMode(mdata, UAVObject::UPDATEMODE_PERIODIC);
|
2010-08-31 12:17:14 +02:00
|
|
|
mdata.flightTelemetryUpdatePeriod = updatePeriod;
|
2012-02-21 02:45:18 +01:00
|
|
|
UAVObject::SetGcsTelemetryUpdateMode(mdata, UAVObject::UPDATEMODE_MANUAL);
|
2010-08-31 12:17:14 +02:00
|
|
|
obj->setMetadata(mdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::setupOutputObject(UAVObject* obj, int updatePeriod)
|
|
|
|
{
|
|
|
|
UAVObject::Metadata mdata;
|
|
|
|
mdata = obj->getDefaultMetadata();
|
2012-02-21 02:45:18 +01:00
|
|
|
UAVObject::SetFlightAccess(mdata, UAVObject::ACCESS_READONLY);
|
|
|
|
UAVObject::SetGcsAccess(mdata, UAVObject::ACCESS_READWRITE);
|
2012-04-19 03:47:13 +02:00
|
|
|
UAVObject::SetFlightTelemetryUpdateMode(mdata,UAVObject::UPDATEMODE_MANUAL);
|
2012-02-21 02:45:18 +01:00
|
|
|
UAVObject::SetGcsTelemetryAcked(mdata, false);
|
|
|
|
UAVObject::SetGcsTelemetryUpdateMode(mdata, UAVObject::UPDATEMODE_PERIODIC);
|
2010-08-31 12:17:14 +02:00
|
|
|
mdata.gcsTelemetryUpdatePeriod = updatePeriod;
|
|
|
|
obj->setMetadata(mdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::onAutopilotConnect()
|
|
|
|
{
|
|
|
|
autopilotConnectionStatus = true;
|
|
|
|
setupObjects();
|
|
|
|
emit autopilotConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::onAutopilotDisconnect()
|
|
|
|
{
|
|
|
|
autopilotConnectionStatus = false;
|
|
|
|
emit autopilotDisconnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulator::onSimulatorConnectionTimeout()
|
|
|
|
{
|
|
|
|
if ( simConnectionStatus )
|
|
|
|
{
|
|
|
|
simConnectionStatus = false;
|
|
|
|
emit simulatorDisconnected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Simulator::telStatsUpdated(UAVObject* obj)
|
|
|
|
{
|
|
|
|
GCSTelemetryStats::DataFields stats = telStats->getData();
|
|
|
|
if ( !autopilotConnectionStatus && stats.Status == GCSTelemetryStats::STATUS_CONNECTED )
|
|
|
|
{
|
|
|
|
onAutopilotConnect();
|
|
|
|
}
|
|
|
|
else if ( autopilotConnectionStatus && stats.Status != GCSTelemetryStats::STATUS_CONNECTED )
|
|
|
|
{
|
|
|
|
onAutopilotDisconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|