Newer
Older
Lionel Heng
committed
///*=====================================================================
//
//QGroundControl Open Source Ground Control Station
//
//(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
//
//This file is part of the QGROUNDCONTROL project
//
// QGROUNDCONTROL 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.
//
// QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
//
//======================================================================*/
/**
* @file
* @brief Definition of the class Pixhawk3DWidget.
*
* @author Lionel Heng <hengli@student.ethz.ch>
*
*/
#include "Pixhawk3DWidget.h"
#include <sstream>
#include <osg/Geode>
#include <osg/Image>
Lionel Heng
committed
#include <osg/LineWidth>
#include <osg/ShapeDrawable>
#include "PixhawkCheetahGeode.h"
#include "UASManager.h"
Lionel Heng
committed
Lionel Heng
committed
#include "QGC.h"
Pixhawk3DWidget::Pixhawk3DWidget(QWidget* parent)
: Q3DWidget(parent)
, uas(NULL)
, mode(DEFAULT_MODE)
, selectedWpIndex(-1)
Lionel Heng
committed
, displayGrid(true)
, displayTrail(false)
Lionel Heng
committed
, displayImagery(true)
Lionel Heng
committed
, displayWaypoints(true)
, displayRGBD2D(false)
, displayRGBD3D(false)
, enableRGBDColor(true)
hengli
committed
, followCamera(true)
hengli
committed
, enableFreenect(false)
Lionel Heng
committed
, frame(MAV_FRAME_GLOBAL)
, lastRobotX(0.0f)
, lastRobotY(0.0f)
, lastRobotZ(0.0f)
Lionel Heng
committed
{
setCameraParams(2.0f, 30.0f, 0.01f, 10000.0f);
Lionel Heng
committed
init(15.0f);
// generate Pixhawk Cheetah model
vehicleModel = PixhawkCheetahGeode::instance();
egocentricMap->addChild(vehicleModel);
Lionel Heng
committed
// generate grid model
gridNode = createGrid();
rollingMap->addChild(gridNode);
// generate empty trail model
trailNode = createTrail();
rollingMap->addChild(trailNode);
// generate map model
mapNode = createMap();
rollingMap->addChild(mapNode);
Lionel Heng
committed
// generate waypoint model
Lionel Heng
committed
waypointGroupNode = new WaypointGroupNode;
waypointGroupNode->init();
rollingMap->addChild(waypointGroupNode);
Lionel Heng
committed
#ifdef QGC_LIBFREENECT_ENABLED
freenect.reset(new Freenect());
hengli
committed
enableFreenect = freenect->init();
#endif
hengli
committed
if (enableFreenect)
{
rgbd3DNode = createRGBD3D();
egocentricMap->addChild(rgbd3DNode);
}
Lionel Heng
committed
setupHUD();
// find available vehicle models in models folder
vehicleModels = findVehicleModels();
Lionel Heng
committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
buildLayout();
connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)),
this, SLOT(setActiveUAS(UASInterface*)));
}
Pixhawk3DWidget::~Pixhawk3DWidget()
{
}
/**
*
* @param uas the UAS/MAV to monitor/display with the HUD
*/
void Pixhawk3DWidget::setActiveUAS(UASInterface* uas)
{
if (this->uas != NULL && this->uas != uas)
{
// Disconnect any previously connected active MAV
//disconnect(uas, SIGNAL(valueChanged(UASInterface*,QString,double,quint64)), this, SLOT(updateValue(UASInterface*,QString,double,quint64)));
}
this->uas = uas;
}
Lionel Heng
committed
void
Pixhawk3DWidget::selectFrame(QString text)
{
if (text.compare("Global") == 0)
{
frame = MAV_FRAME_GLOBAL;
}
else if (text.compare("Local") == 0)
{
frame = MAV_FRAME_LOCAL;
}
getPosition(lastRobotX, lastRobotY, lastRobotZ);
recenter();
}
Lionel Heng
committed
void
Pixhawk3DWidget::showGrid(int32_t state)
{
if (state == Qt::Checked)
{
displayGrid = true;
}
else
{
displayGrid = false;
}
}
void
Pixhawk3DWidget::showTrail(int32_t state)
{
if (state == Qt::Checked)
{
if (!displayTrail)
{
Lionel Heng
committed
}
displayTrail = true;
}
else
{
displayTrail = false;
}
}
void
Pixhawk3DWidget::showWaypoints(int state)
{
if (state == Qt::Checked)
{
displayWaypoints = true;
}
else
{
displayWaypoints = false;
}
}
Lionel Heng
committed
void
Pixhawk3DWidget::selectMapSource(int index)
{
mapNode->setImageryType(static_cast<Imagery::ImageryType>(index));
hengli
committed
if (mapNode->getImageryType() == Imagery::BLANK_MAP)
{
displayImagery = false;
}
else
{
displayImagery = true;
}
Lionel Heng
committed
}
void
Pixhawk3DWidget::selectVehicleModel(int index)
{
vehicleModel = vehicleModels.at(index);
Lionel Heng
committed
void
Pixhawk3DWidget::recenter(void)
Lionel Heng
committed
{
hengli
committed
double robotX = 0.0f, robotY = 0.0f, robotZ = 0.0f;
Lionel Heng
committed
getPosition(robotX, robotY, robotZ);
hengli
committed
recenterCamera(robotY, robotX, -robotZ);
Lionel Heng
committed
}
void
hengli
committed
Pixhawk3DWidget::toggleFollowCamera(int32_t state)
Lionel Heng
committed
{
if (state == Qt::Checked)
{
hengli
committed
followCamera = true;
Lionel Heng
committed
}
else
{
hengli
committed
followCamera = false;
Lionel Heng
committed
}
}
Lionel Heng
committed
void
Pixhawk3DWidget::insertWaypoint(void)
{
if (uas)
{
Lionel Heng
committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Waypoint* wp;
if (frame == MAV_FRAME_GLOBAL)
{
double latitude = uas->getLatitude();
double longitude = uas->getLongitude();
double altitude = uas->getAltitude();
double x, y;
QString utmZone;
Imagery::LLtoUTM(latitude, longitude, x, y, utmZone);
std::pair<double,double> cursorWorldCoords =
getGlobalCursorPosition(getMouseX(), getMouseY(), altitude);
Imagery::UTMtoLL(cursorWorldCoords.first, cursorWorldCoords.second, utmZone,
latitude, longitude);
wp = new Waypoint(0, longitude, latitude, altitude);
}
else if (frame == MAV_FRAME_LOCAL)
{
double z = uas->getLocalZ();
std::pair<double,double> cursorWorldCoords =
getGlobalCursorPosition(getMouseX(), getMouseY(), -z);
wp = new Waypoint(0, cursorWorldCoords.first,
cursorWorldCoords.second, z);
}
if (wp)
{
wp->setFrame(frame);
uas->getWaypointManager().addWaypoint(wp);
}
}
}
void
Pixhawk3DWidget::moveWaypoint(void)
{
mode = MOVE_WAYPOINT_MODE;
}
void
Pixhawk3DWidget::setWaypoint(void)
{
if (uas)
{
const QVector<Waypoint *> waypoints =
uas->getWaypointManager().getWaypointList();
Waypoint* waypoint = waypoints.at(selectedWpIndex);
Lionel Heng
committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
if (frame == MAV_FRAME_GLOBAL)
{
double latitude = uas->getLatitude();
double longitude = uas->getLongitude();
double altitude = uas->getAltitude();
double x, y;
QString utmZone;
Imagery::LLtoUTM(latitude, longitude, x, y, utmZone);
std::pair<double,double> cursorWorldCoords =
getGlobalCursorPosition(getMouseX(), getMouseY(), altitude);
Imagery::UTMtoLL(cursorWorldCoords.first, cursorWorldCoords.second, utmZone,
latitude, longitude);
waypoint->setX(longitude);
waypoint->setY(latitude);
waypoint->setZ(altitude);
}
else if (frame == MAV_FRAME_LOCAL)
{
double z = uas->getLocalZ();
std::pair<double,double> cursorWorldCoords =
getGlobalCursorPosition(getMouseX(), getMouseY(), -z);
waypoint->setX(cursorWorldCoords.first);
waypoint->setY(cursorWorldCoords.second);
waypoint->setZ(z);
}
}
}
void
Pixhawk3DWidget::deleteWaypoint(void)
{
if (uas)
{
uas->getWaypointManager().removeWaypoint(selectedWpIndex);
}
}
void
Pixhawk3DWidget::setWaypointAltitude(void)
{
if (uas)
{
const QVector<Waypoint *> waypoints =
uas->getWaypointManager().getWaypointList();
Waypoint* waypoint = waypoints.at(selectedWpIndex);
Lionel Heng
committed
double altitude = waypoint->getZ();
if (frame == MAV_FRAME_LOCAL)
{
altitude = -altitude;
}
double newAltitude =
QInputDialog::getDouble(this, tr("Set altitude of waypoint %1").arg(selectedWpIndex),
tr("Altitude (m):"), waypoint->getZ(), -1000.0, 1000.0, 1, &ok);
Lionel Heng
committed
if (frame == MAV_FRAME_GLOBAL)
{
waypoint->setZ(newAltitude);
}
else if (frame == MAV_FRAME_LOCAL)
{
waypoint->setZ(-newAltitude);
}
}
}
void
Pixhawk3DWidget::clearAllWaypoints(void)
{
if (uas)
{
const QVector<Waypoint *> waypoints =
uas->getWaypointManager().getWaypointList();
for (int i = waypoints.size() - 1; i >= 0; --i)
{
uas->getWaypointManager().removeWaypoint(i);
}
QVector< osg::ref_ptr<osg::Node> >
Pixhawk3DWidget::findVehicleModels(void)
{
QDir directory("models");
QStringList files = directory.entryList(QStringList("*.osg"), QDir::Files);
QVector< osg::ref_ptr<osg::Node> > nodes;
// add Pixhawk Bravo model
nodes.push_back(PixhawkCheetahGeode::instance());
for (int i = 0; i < files.size(); ++i)
{
osg::ref_ptr<osg::Node> node =
osgDB::readNodeFile(directory.absoluteFilePath(files[i]).toStdString().c_str());
nodes.push_back(node);
printf("%s\n", QString("ERROR: Could not load file " + directory.absoluteFilePath(files[i]) + "\n").toStdString().c_str());
// QStringList dirs = directory.entryList(QDir::Dirs);
// // Add models in subfolders
// for (int i = 0; i < dirs.size(); ++i)
// {
// // Handling the current directory
// QStringList currFiles = QDir(dirs[i]).entryList(QStringList("*.ac"), QDir::Files);
// // Load the file
// osg::ref_ptr<osg::Node> node =
// osgDB::readNodeFile(directory.absoluteFilePath(currFiles.first()).toStdString().c_str());
// if (node)
// {
// nodes.push_back(node);
// }
// else
// {
// printf(QString("ERROR: Could not load file " + directory.absoluteFilePath(files[i]) + "\n").toStdString().c_str());
// }
// }
return nodes;
}
void
Pixhawk3DWidget::buildLayout(void)
{
Lionel Heng
committed
QComboBox* frameComboBox = new QComboBox(this);
frameComboBox->addItem("Global");
frameComboBox->addItem("Local");
frameComboBox->setFixedWidth(70);
QCheckBox* gridCheckBox = new QCheckBox(this);
gridCheckBox->setText("Grid");
gridCheckBox->setChecked(displayGrid);
QCheckBox* trailCheckBox = new QCheckBox(this);
trailCheckBox->setText("Trail");
trailCheckBox->setChecked(displayTrail);
QCheckBox* waypointsCheckBox = new QCheckBox(this);
waypointsCheckBox->setText("Waypoints");
waypointsCheckBox->setChecked(displayWaypoints);
Lionel Heng
committed
QLabel* mapLabel = new QLabel("Map", this);
QComboBox* mapComboBox = new QComboBox(this);
mapComboBox->addItem("None");
mapComboBox->addItem("Map (Google)");
mapComboBox->addItem("Satellite (Google)");
QLabel* modelLabel = new QLabel("Vehicle", this);
QComboBox* modelComboBox = new QComboBox(this);
for (int i = 0; i < vehicleModels.size(); ++i)
{
modelComboBox->addItem(vehicleModels[i]->getName().c_str());
}
QPushButton* recenterButton = new QPushButton(this);
recenterButton->setText("Recenter Camera");
QCheckBox* followCameraCheckBox = new QCheckBox(this);
followCameraCheckBox->setText("Follow Camera");
followCameraCheckBox->setChecked(followCamera);
QGridLayout* layout = new QGridLayout(this);
layout->setMargin(0);
layout->setSpacing(2);
Lionel Heng
committed
layout->addWidget(frameComboBox, 0, 10);
layout->addWidget(gridCheckBox, 2, 0);
layout->addWidget(trailCheckBox, 2, 1);
layout->addWidget(waypointsCheckBox, 2, 2);
layout->addItem(new QSpacerItem(10, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 2, 3);
layout->addWidget(mapLabel, 2, 4);
layout->addWidget(mapComboBox, 2, 5);
layout->addWidget(modelLabel, 2, 6);
layout->addWidget(modelComboBox, 2, 7);
layout->addItem(new QSpacerItem(10, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 2, 8);
layout->addWidget(recenterButton, 2, 9);
layout->addWidget(followCameraCheckBox, 2, 10);
layout->setRowStretch(0, 1);
layout->setRowStretch(1, 100);
layout->setRowStretch(2, 1);
Lionel Heng
committed
connect(frameComboBox, SIGNAL(currentIndexChanged(QString)),
this, SLOT(selectFrame(QString)));
connect(gridCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(showGrid(int)));
connect(trailCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(showTrail(int)));
connect(waypointsCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(showWaypoints(int)));
Lionel Heng
committed
connect(mapComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(selectMapSource(int)));
connect(modelComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(selectVehicleModel(int)));
connect(recenterButton, SIGNAL(clicked()), this, SLOT(recenter()));
connect(followCameraCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(toggleFollowCamera(int)));
}
Lionel Heng
committed
hengli
committed
void
Pixhawk3DWidget::display(void)
{
hengli
committed
{
Lionel Heng
committed
double robotX, robotY, robotZ, robotRoll, robotPitch, robotYaw;
QString utmZone;
Lionel Heng
committed
getPose(robotX, robotY, robotZ, robotRoll, robotPitch, robotYaw, utmZone);
if (lastRobotX == 0.0f && lastRobotY == 0.0f && lastRobotZ == 0.0f)
{
lastRobotX = robotX;
lastRobotY = robotY;
lastRobotZ = robotZ;
recenterCamera(robotY, robotX, -robotZ);
return;
hengli
committed
}
hengli
committed
double dx = robotY - lastRobotY;
double dy = robotX - lastRobotX;
double dz = lastRobotZ - robotZ;
moveCamera(dx, dy, dz);
hengli
committed
robotPosition->setPosition(osg::Vec3d(robotY, robotX, -robotZ));
robotAttitude->setAttitude(osg::Quat(-robotYaw, osg::Vec3d(0.0f, 0.0f, 1.0f),
robotPitch, osg::Vec3d(1.0f, 0.0f, 0.0f),
robotRoll, osg::Vec3d(0.0f, 1.0f, 0.0f)));
hengli
committed
Lionel Heng
committed
if (displayTrail)
{
updateTrail(robotX, robotY, robotZ);
}
Lionel Heng
committed
if (frame == MAV_FRAME_GLOBAL && displayImagery)
Lionel Heng
committed
{
updateImagery(robotX, robotY, robotZ, utmZone);
Lionel Heng
committed
}
if (displayWaypoints)
{
updateWaypoints();
}
#ifdef QGC_LIBFREENECT_ENABLED
hengli
committed
if (enableFreenect && (displayRGBD2D || displayRGBD3D))
Lionel Heng
committed
{
updateRGBD();
}
Lionel Heng
committed
updateHUD(robotX, robotY, robotZ, robotRoll, robotPitch, robotYaw, utmZone);
hengli
committed
// set node visibility
Lionel Heng
committed
hengli
committed
rollingMap->setChildValue(gridNode, displayGrid);
rollingMap->setChildValue(trailNode, displayTrail);
rollingMap->setChildValue(mapNode, displayImagery);
Lionel Heng
committed
rollingMap->setChildValue(waypointGroupNode, displayWaypoints);
hengli
committed
if (enableFreenect)
{
egocentricMap->setChildValue(rgbd3DNode, displayRGBD3D);
}
hudGroup->setChildValue(rgb2DGeode, displayRGBD2D);
hudGroup->setChildValue(depth2DGeode, displayRGBD2D);
lastRobotX = robotX;
lastRobotY = robotY;
lastRobotZ = robotZ;
hengli
committed
}
void
Pixhawk3DWidget::keyPressEvent(QKeyEvent* event)
{
if (!event->text().isEmpty())
{
switch (*(event->text().toAscii().data()))
{
case '1':
displayRGBD2D = !displayRGBD2D;
break;
case '2':
displayRGBD3D = !displayRGBD3D;
break;
case 'c': case 'C':
enableRGBDColor = !enableRGBDColor;
break;
}
}
Q3DWidget::keyPressEvent(event);
}
hengli
committed
void
Pixhawk3DWidget::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
hengli
committed
{
if (mode == MOVE_WAYPOINT_MODE)
{
setWaypoint();
mode = DEFAULT_MODE;
return;
}
if (event->modifiers() == Qt::ShiftModifier)
{
selectedWpIndex = findWaypoint(event->x(), event->y());
if (selectedWpIndex == -1)
{
showInsertWaypointMenu(event->globalPos());
}
else
{
showEditWaypointMenu(event->globalPos());
}
return;
}
hengli
committed
}
Q3DWidget::mousePressEvent(event);
}
Lionel Heng
committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
void
Pixhawk3DWidget::getPose(double& x, double& y, double& z,
double& roll, double& pitch, double& yaw,
QString& utmZone)
{
if (uas)
{
if (frame == MAV_FRAME_GLOBAL)
{
double latitude = uas->getLatitude();
double longitude = uas->getLongitude();
double altitude = uas->getAltitude();
Imagery::LLtoUTM(latitude, longitude, x, y, utmZone);
z = -altitude;
}
else if (frame == MAV_FRAME_LOCAL)
{
x = uas->getLocalX();
y = uas->getLocalY();
z = uas->getLocalZ();
}
roll = uas->getRoll();
pitch = uas->getPitch();
yaw = uas->getYaw();
}
}
void
Pixhawk3DWidget::getPose(double& x, double& y, double& z,
double& roll, double& pitch, double& yaw)
{
QString utmZone;
getPose(x, y, z, roll, pitch, yaw);
}
void
Pixhawk3DWidget::getPosition(double& x, double& y, double& z,
QString& utmZone)
{
if (uas)
{
if (frame == MAV_FRAME_GLOBAL)
{
double latitude = uas->getLatitude();
double longitude = uas->getLongitude();
double altitude = uas->getAltitude();
Imagery::LLtoUTM(latitude, longitude, x, y, utmZone);
z = -altitude;
}
else if (frame == MAV_FRAME_LOCAL)
{
x = uas->getLocalX();
y = uas->getLocalY();
z = uas->getLocalZ();
}
}
}
void
Pixhawk3DWidget::getPosition(double& x, double& y, double& z)
{
QString utmZone;
getPosition(x, y, z, utmZone);
}
Lionel Heng
committed
osg::ref_ptr<osg::Geode>
Pixhawk3DWidget::createGrid(void)
{
osg::ref_ptr<osg::Geode> geode(new osg::Geode());
osg::ref_ptr<osg::Geometry> fineGeometry(new osg::Geometry());
osg::ref_ptr<osg::Geometry> coarseGeometry(new osg::Geometry());
geode->addDrawable(fineGeometry);
geode->addDrawable(coarseGeometry);
Lionel Heng
committed
float radius = 10.0f;
float resolution = 0.25f;
osg::ref_ptr<osg::Vec3Array> fineCoords(new osg::Vec3Array);
osg::ref_ptr<osg::Vec3Array> coarseCoords(new osg::Vec3Array);
Lionel Heng
committed
// draw a 20m x 20m grid with 0.25m resolution
for (float i = -radius; i <= radius; i += resolution)
{
{
coarseCoords->push_back(osg::Vec3(i, -radius, 0.0f));
coarseCoords->push_back(osg::Vec3(i, radius, 0.0f));
coarseCoords->push_back(osg::Vec3(-radius, i, 0.0f));
coarseCoords->push_back(osg::Vec3(radius, i, 0.0f));
}
else
{
fineCoords->push_back(osg::Vec3(i, -radius, 0.0f));
fineCoords->push_back(osg::Vec3(i, radius, 0.0f));
fineCoords->push_back(osg::Vec3(-radius, i, 0.0f));
fineCoords->push_back(osg::Vec3(radius, i, 0.0f));
}
Lionel Heng
committed
}
fineGeometry->setVertexArray(fineCoords);
coarseGeometry->setVertexArray(coarseCoords);
Lionel Heng
committed
osg::ref_ptr<osg::Vec4Array> color(new osg::Vec4Array);
color->push_back(osg::Vec4(0.5f, 0.5f, 0.5f, 1.0f));
fineGeometry->setColorArray(color);
coarseGeometry->setColorArray(color);
fineGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
coarseGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
fineGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,
0, fineCoords->size()));
coarseGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0,
coarseCoords->size()));
osg::ref_ptr<osg::StateSet> fineStateset(new osg::StateSet);
osg::ref_ptr<osg::LineWidth> fineLinewidth(new osg::LineWidth());
fineLinewidth->setWidth(0.25f);
fineStateset->setAttributeAndModes(fineLinewidth, osg::StateAttribute::ON);
fineStateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
fineGeometry->setStateSet(fineStateset);
osg::ref_ptr<osg::StateSet> coarseStateset(new osg::StateSet);
osg::ref_ptr<osg::LineWidth> coarseLinewidth(new osg::LineWidth());
coarseLinewidth->setWidth(2.0f);
coarseStateset->setAttributeAndModes(coarseLinewidth, osg::StateAttribute::ON);
coarseStateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
coarseGeometry->setStateSet(coarseStateset);
Lionel Heng
committed
return geode;
}
osg::ref_ptr<osg::Geode>
Pixhawk3DWidget::createTrail(void)
{
osg::ref_ptr<osg::Geode> geode(new osg::Geode());
trailGeometry = new osg::Geometry();
trailGeometry->setUseDisplayList(false);
geode->addDrawable(trailGeometry.get());
hengli
committed
trailVertices = new osg::Vec3dArray;
Lionel Heng
committed
trailGeometry->setVertexArray(trailVertices);
trailDrawArrays = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP);
trailGeometry->addPrimitiveSet(trailDrawArrays);
osg::ref_ptr<osg::Vec4Array> color(new osg::Vec4Array);
color->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
trailGeometry->setColorArray(color);
trailGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
osg::ref_ptr<osg::StateSet> stateset(new osg::StateSet);
osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth());
linewidth->setWidth(1.0f);
stateset->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
trailGeometry->setStateSet(stateset);
return geode;
}
Lionel Heng
committed
osg::ref_ptr<Imagery>
Pixhawk3DWidget::createMap(void)
{
Lionel Heng
committed
return osg::ref_ptr<Imagery>(new Imagery());
osg::ref_ptr<osg::Geode>
Pixhawk3DWidget::createRGBD3D(void)
int frameSize = 640 * 480;
osg::ref_ptr<osg::Geode> geode(new osg::Geode);
osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
osg::ref_ptr<osg::Vec3Array> vertices(new osg::Vec3Array(frameSize));
geometry->setVertexArray(vertices);
osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array(frameSize));
geometry->setColorArray(colors);
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
geometry->setUseDisplayList(false);
geode->addDrawable(geometry);
return geode;
Lionel Heng
committed
void
Pixhawk3DWidget::setupHUD(void)
{
osg::ref_ptr<osg::Vec4Array> hudColors(new osg::Vec4Array);
Lionel Heng
committed
hudColors->push_back(osg::Vec4(0.0f, 0.0f, 0.0f, 0.5f));
hudColors->push_back(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
Lionel Heng
committed
hudBackgroundGeometry = new osg::Geometry;
hudBackgroundGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,
0, 4));
hudBackgroundGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,
4, 4));
Lionel Heng
committed
hudBackgroundGeometry->setColorArray(hudColors);
Lionel Heng
committed
hudBackgroundGeometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
hudBackgroundGeometry->setUseDisplayList(false);
Lionel Heng
committed
statusText = new osgText::Text;
statusText->setCharacterSize(11);
statusText->setFont("images/Vera.ttf");
statusText->setAxisAlignment(osgText::Text::SCREEN);
statusText->setColor(osg::Vec4(255, 255, 255, 1));
resizeHUD();
osg::ref_ptr<osg::Geode> statusGeode = new osg::Geode;
statusGeode->addDrawable(hudBackgroundGeometry);
statusGeode->addDrawable(statusText);
hudGroup->addChild(statusGeode);
rgbImage = new osg::Image;
rgb2DGeode = new ImageWindowGeode("RGB Image",
osg::Vec4(0.0f, 0.0f, 0.1f, 1.0f),
rgbImage);
hudGroup->addChild(rgb2DGeode);
depthImage = new osg::Image;
depth2DGeode = new ImageWindowGeode("Depth Image",
osg::Vec4(0.0f, 0.0f, 0.1f, 1.0f),
depthImage);
hudGroup->addChild(depth2DGeode);
scaleGeode = new HUDScaleGeode;
scaleGeode->init();
hudGroup->addChild(scaleGeode);
Lionel Heng
committed
}
void
Pixhawk3DWidget::resizeHUD(void)
Lionel Heng
committed
{
Lionel Heng
committed
int topHUDHeight = 25;
int bottomHUDHeight = 25;
osg::Vec3Array* vertices = static_cast<osg::Vec3Array*>(hudBackgroundGeometry->getVertexArray());
if (vertices == NULL || vertices->size() != 8)
{
osg::ref_ptr<osg::Vec3Array> newVertices = new osg::Vec3Array(8);
hudBackgroundGeometry->setVertexArray(newVertices);
vertices = static_cast<osg::Vec3Array*>(hudBackgroundGeometry->getVertexArray());
}
(*vertices)[0] = osg::Vec3(0, height(), -1);
(*vertices)[1] = osg::Vec3(width(), height(), -1);
(*vertices)[2] = osg::Vec3(width(), height() - topHUDHeight, -1);
(*vertices)[3] = osg::Vec3(0, height() - topHUDHeight, -1);
(*vertices)[4] = osg::Vec3(0, 0, -1);
(*vertices)[5] = osg::Vec3(width(), 0, -1);
(*vertices)[6] = osg::Vec3(width(), bottomHUDHeight, -1);
(*vertices)[7] = osg::Vec3(0, bottomHUDHeight, -1);
Lionel Heng
committed
Lionel Heng
committed
statusText->setPosition(osg::Vec3(10, height() - 15, -1.5));
Lionel Heng
committed
if (rgb2DGeode.valid() && depth2DGeode.valid())
{
int windowWidth = (width() - 20) / 2;
int windowHeight = 3 * windowWidth / 4;
rgb2DGeode->setAttributes(10, (height() - windowHeight) / 2,
windowWidth, windowHeight);
depth2DGeode->setAttributes(width() / 2, (height() - windowHeight) / 2,
windowWidth, windowHeight);
}
}
void
hengli
committed
Pixhawk3DWidget::updateHUD(double robotX, double robotY, double robotZ,
Lionel Heng
committed
double robotRoll, double robotPitch, double robotYaw,
const QString& utmZone)
{
resizeHUD();
std::pair<double,double> cursorPosition =
getGlobalCursorPosition(getMouseX(), getMouseY(), -robotZ);
Lionel Heng
committed
std::ostringstream oss;
oss.setf(std::ios::fixed, std::ios::floatfield);
oss.precision(2);
Lionel Heng
committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
if (frame == MAV_FRAME_GLOBAL)
{
double latitude, longitude;
Imagery::UTMtoLL(robotX, robotY, utmZone, latitude, longitude);
double cursorLatitude, cursorLongitude;
Imagery::UTMtoLL(cursorPosition.first, cursorPosition.second,
utmZone, cursorLatitude, cursorLongitude);
oss.precision(6);
oss << " Lat = " << latitude <<
" Lon = " << longitude;
oss.precision(2);
oss << " Altitude = " << -robotZ <<
" r = " << robotRoll <<
" p = " << robotPitch <<
" y = " << robotYaw;
oss.precision(6);
oss << " Cursor [" << cursorLatitude <<
" " << cursorLongitude << "]";
}
else if (frame == MAV_FRAME_LOCAL)
{
oss << " x = " << robotX <<
" y = " << robotY <<
" z = " << robotZ <<
" r = " << robotRoll <<
" p = " << robotPitch <<
" y = " << robotYaw <<
" Cursor [" << cursorPosition.first <<
" " << cursorPosition.second << "]";
}
Lionel Heng
committed
statusText->setText(oss.str());
bool darkBackground = true;
if (mapNode->getImageryType() == Imagery::GOOGLE_MAP)
{
darkBackground = false;
}
scaleGeode->update(height(), cameraParams.cameraFov,
cameraManipulator->getDistance(), darkBackground);
if (!rgb.isNull())
{
rgbImage->setImage(640, 480, 1,
GL_RGB, GL_RGB, GL_UNSIGNED_BYTE,
reinterpret_cast<unsigned char *>(rgb->data()),
osg::Image::NO_DELETE);
rgbImage->dirty();
depthImage->setImage(640, 480, 1,
GL_RGB, GL_RGB, GL_UNSIGNED_BYTE,
reinterpret_cast<unsigned char *>(coloredDepth->data()),
osg::Image::NO_DELETE);
depthImage->dirty();
}
Lionel Heng
committed
}
void
hengli
committed
Pixhawk3DWidget::updateTrail(double robotX, double robotY, double robotZ)
Lionel Heng
committed
{
if (robotX == 0.0f || robotY == 0.0f || robotZ == 0.0f)
{
return;