1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

GCS - Changes to the map lib. Preparatory steps

to allow WPs to be defined relative to home.
GPS fed UAV map item is only visible when diagnostics are enabled.
Should make users not be confused by 2 UAVs on screen.
This commit is contained in:
PT_Dreamer 2012-05-17 17:27:08 +01:00
parent 9f1afacdaf
commit bb7e85cf2c
9 changed files with 222 additions and 78 deletions

View File

@ -215,6 +215,13 @@ Point PureProjection::FromLatLngToPixel(const PointLatLng &p,const int &zoom)
Lat /= (PI / 180);
Lng /= (PI / 180);
}
double PureProjection::courseBetweenLatLng(PointLatLng const& p1,PointLatLng const& p2)
{
return fmod(atan2(sin(p1.Lng()-p2.Lng())*cos(p2.Lat()),
cos(p1.Lat())*sin(p2.Lat())-sin(p1.Lat())*cos(p2.Lat())*cos(p1.Lng()-p2.Lng())), 2*PI);
}
double PureProjection::DistanceBetweenLatLng(PointLatLng const& p1,PointLatLng const& p2)
{
double R = 6371; // km
@ -229,4 +236,23 @@ Point PureProjection::FromLatLngToPixel(const PointLatLng &p,const int &zoom)
double d = R * c;
return d;
}
void PureProjection::offSetFromLatLngs(PointLatLng p1,PointLatLng p2,double &dX,double &dY)
{
dX=DistanceBetweenLatLng(p1,p2)*sin(courseBetweenLatLng(p1,p2));
dY=DistanceBetweenLatLng(p1,p2)*sin(courseBetweenLatLng(p1,p2));
}
PointLatLng PureProjection::translate(PointLatLng p1,double dX,double dY)
{
PointLatLng origin=p1;
PointLatLng ret;
double d=sqrt(pow(dX,2)+pow(dY,2));
double tc=atan2(dY,dX);
ret.SetLat(asin(sin(origin.Lat())*cos(d)+cos(origin.Lat())*sin(d)*cos(tc)));
double dlon=atan2(sin(tc)*sin(d)*cos(origin.Lat()),cos(d)-sin(origin.Lat())*sin(p1.Lat()));
ret.SetLng(fmod(origin.Lng()-dlon +PI,2*PI )-PI);
return ret;
}
}

View File

@ -81,6 +81,9 @@ public:
void FromCartesianTGeodetic(const double &X,const double &Y,const double &Z, double &Lat, double &Lng);
static double DistanceBetweenLatLng(PointLatLng const& p1,PointLatLng const& p2);
PointLatLng translate(PointLatLng p1, double dX, double dY);
double courseBetweenLatLng(const PointLatLng &p1, const PointLatLng &p2);
void offSetFromLatLngs(PointLatLng p1, PointLatLng p2, double &dX, double &dY);
protected:
static const double PI;

View File

@ -27,15 +27,20 @@
#include "homeitem.h"
namespace mapcontrol
{
HomeItem::HomeItem(MapGraphicItem* map,OPMapWidget* parent):safe(true),map(map),mapwidget(parent),showsafearea(true),safearea(1000),altitude(0)
HomeItem::HomeItem(MapGraphicItem* map,OPMapWidget* parent):safe(true),map(map),mapwidget(parent),showsafearea(true),safearea(1000),altitude(0),isDragging(false)
{
pic.load(QString::fromUtf8(":/markers/images/home2.svg"));
pic=pic.scaled(30,30,Qt::IgnoreAspectRatio);
this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
this->setFlag(QGraphicsItem::ItemIsMovable,true);
this->setFlag(QGraphicsItem::ItemIsSelectable,true);
localposition=map->FromLatLngToLocal(mapwidget->CurrentPosition());
this->setPos(localposition.X(),localposition.Y());
this->setZValue(4);
coord=internals::PointLatLng(50,50);
setToolTip("AAAA");
qDebug()<<"HomeItem created type:"<<type();
}
void HomeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@ -56,7 +61,7 @@ namespace mapcontrol
}
QRectF HomeItem::boundingRect()const
{
if(!showsafearea)
if(pic.width()>localsafearea*2)
return QRectF(-pic.width()/2,-pic.height()/2,pic.width(),pic.height());
else
return QRectF(-localsafearea,-localsafearea,localsafearea*2,localsafearea*2);
@ -76,5 +81,34 @@ namespace mapcontrol
localsafearea=safearea/map->Projection()->GetGroundResolution(map->ZoomTotal(),coord.Lat());
}
void HomeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
isDragging=true;
}
QGraphicsItem::mousePressEvent(event);
}
void HomeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
isDragging=false;
emit homePositionChanged(coord);
}
QGraphicsItem::mouseReleaseEvent(event);
}
void HomeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(isDragging)
{
coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
emit homePositionChanged(coord);
}
QGraphicsItem::mouseMoveEvent(event);
}
}

View File

@ -67,11 +67,15 @@ namespace mapcontrol
int safearea;
int localsafearea;
int altitude;
bool isDragging;
protected:
void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
void mousePressEvent ( QGraphicsSceneMouseEvent * event );
void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
public slots:
signals:
void homePositionChanged(internals::PointLatLng coord);
};
}
#endif // HOMEITEM_H

View File

@ -40,6 +40,9 @@ namespace mapcontrol
map=new MapGraphicItem(core,config);
mscene.addItem(map);
this->setScene(&mscene);
Home=new HomeItem(map,this);
Home->setParentItem(map);
setStyleSheet("QToolTip {font-size:8pt; color:yellow;background-color : transparent; padding:2px; border-width:2px; border-style:solid; border-radius:4px }");
this->adjustSize();
connect(map,SIGNAL(zoomChanged(double,double,double)),this,SIGNAL(zoomChanged(double,double,double)));
connect(map->core,SIGNAL(OnCurrentPositionChanged(internals::PointLatLng)),this,SIGNAL(OnCurrentPositionChanged(internals::PointLatLng)));
@ -70,12 +73,23 @@ namespace mapcontrol
delete diagTimer;
diagTimer=0;
}
if(GPS!=0)
{
delete GPS;
GPS=0;
}
}
else
{
diagTimer=new QTimer();
connect(diagTimer,SIGNAL(timeout()),this,SLOT(diagRefresh()));
diagTimer->start(500);
if(GPS==0)
{
GPS=new GPSItem(map,this);
GPS->setParentItem(map);
}
}
}
@ -106,37 +120,10 @@ namespace mapcontrol
}
}
if(value && GPS==0)
{
GPS=new GPSItem(map,this);
GPS->setParentItem(map);
}
else if(!value)
{
if(GPS!=0)
{
delete GPS;
GPS=0;
}
}
}
void OPMapWidget::SetShowHome(const bool &value)
{
if(value && Home==0)
{
Home=new HomeItem(map,this);
Home->setParentItem(map);
}
else if(!value)
{
if(Home!=0)
{
delete Home;
Home=0;
}
}
Home->setVisible(value);
}
void OPMapWidget::resizeEvent(QResizeEvent *event)
@ -329,12 +316,12 @@ namespace mapcontrol
compass->setScale(0.1+0.05*(qreal)(this->size().width())/1000*(qreal)(this->size().height())/600);
// compass->setTransformOriginPoint(compass->boundingRect().width(),compass->boundingRect().height());
compass->setFlag(QGraphicsItem::ItemIsMovable,true);
compass->setFlag(QGraphicsItem::ItemIsSelectable,true);
mscene.addItem(compass);
compass->setTransformOriginPoint(compass->boundingRect().width()/2,compass->boundingRect().height()/2);
compass->setPos(55-compass->boundingRect().width()/2,55-compass->boundingRect().height()/2);
compass->setZValue(3);
compass->setOpacity(0.7);
}
if(!value && compass)
{

View File

@ -36,6 +36,8 @@ namespace mapcontrol
pic.load(uavPic);
// Don't scale but trust the image we are given
// pic=pic.scaled(50,33,Qt::IgnoreAspectRatio);
this->setFlag(QGraphicsItem::ItemIsMovable,true);
this->setFlag(QGraphicsItem::ItemIsSelectable,true);
localposition=map->FromLatLngToLocal(mapwidget->CurrentPosition());
this->setPos(localposition.X(),localposition.Y());
this->setZValue(4);
@ -57,14 +59,14 @@ namespace mapcontrol
{
Q_UNUSED(option);
Q_UNUSED(widget);
// painter->rotate(-90);
painter->drawPixmap(-pic.width()/2,-pic.height()/2,pic);
// painter->drawRect(QRectF(-pic.width()/2,-pic.height()/2,pic.width()-1,pic.height()-1));
}
QRectF UAVItem::boundingRect()const
{
return QRectF(-pic.width()/2,-pic.height()/2,pic.width(),pic.height());
}
void UAVItem::SetUAVPos(const internals::PointLatLng &position, const int &altitude)
{
if(coord.IsEmpty())

View File

@ -25,9 +25,11 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "waypointitem.h"
#include "homeitem.h"
namespace mapcontrol
{
WayPointItem::WayPointItem(const internals::PointLatLng &coord,int const& altitude, MapGraphicItem *map):coord(coord),reached(false),description(""),shownumber(true),isDragging(false),altitude(altitude),map(map)
WayPointItem::WayPointItem(const internals::PointLatLng &coord,int const& altitude, MapGraphicItem *map,wptype type):coord(coord),reached(false),description(""),shownumber(true),isDragging(false),altitude(altitude),map(map),myType(type)
{
text=0;
numberI=0;
@ -37,13 +39,31 @@ namespace mapcontrol
this->setFlag(QGraphicsItem::ItemIsMovable,true);
this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
this->setFlag(QGraphicsItem::ItemIsSelectable,true);
// transf.translate(picture.width()/2,picture.height());
// this->setTransform(transf);
SetShowNumber(shownumber);
RefreshToolTip();
RefreshPos();
if(relativeCoord.isNull())
{
HomeItem* home=NULL;
QList<QGraphicsItem *> list=map->childItems();
foreach(QGraphicsItem * obj,list)
{
HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
if(h)
home=h;
}
double X;
double Y;
map->Projection()->offSetFromLatLngs(home->Coord(),coord,X,Y);
relativeCoord.setX(X);
relativeCoord.setY(Y);
}
HomeItem * home=this->parent()->findChild<HomeItem *>();
connect(home,SIGNAL(homePositionChanged(internals::PointLatLng)),this,SLOT(onHomePositionChanged(internals::PointLatLng)));
}
WayPointItem::WayPointItem(const internals::PointLatLng &coord,int const& altitude, const QString &description, MapGraphicItem *map):coord(coord),reached(false),description(description),shownumber(true),isDragging(false),altitude(altitude),map(map)
WayPointItem::WayPointItem(const internals::PointLatLng &coord,int const& altitude, const QString &description, MapGraphicItem *map,wptype type):coord(coord),reached(false),description(description),shownumber(true),isDragging(false),altitude(altitude),map(map),myType(type)
{
text=0;
numberI=0;
@ -53,8 +73,58 @@ namespace mapcontrol
this->setFlag(QGraphicsItem::ItemIsMovable,true);
this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
this->setFlag(QGraphicsItem::ItemIsSelectable,true);
//transf.translate(picture.width()/2,picture.height());
// this->setTransform(transf);
SetShowNumber(shownumber);
RefreshToolTip();
RefreshPos();
if(relativeCoord.isNull())
{
HomeItem* home=NULL;
QList<QGraphicsItem *> list=map->childItems();
foreach(QGraphicsItem * obj,list)
{
HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
if(h)
home=h;
}
double X;
double Y;
map->Projection()->offSetFromLatLngs(home->Coord(),coord,X,Y);
relativeCoord.setX(X);
relativeCoord.setY(Y);
}
HomeItem* home=NULL;
QList<QGraphicsItem *> list=map->childItems();
foreach(QGraphicsItem * obj,list)
{
HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
if(h)
home=h;
}
connect(home,SIGNAL(homePositionChanged(internals::PointLatLng)),this,SLOT(onHomePositionChanged(internals::PointLatLng)));
}
WayPointItem::WayPointItem(const QPoint &relativeCoord, const int &altitude, const QString &description, MapGraphicItem *map):relativeCoord(relativeCoord),reached(false),description(description),shownumber(true),isDragging(false),altitude(altitude),map(map)
{
HomeItem* home=NULL;
QList<QGraphicsItem *> list=map->childItems();
foreach(QGraphicsItem * obj,list)
{
HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
if(h)
home=h;
}
connect(home,SIGNAL(homePositionChanged(internals::PointLatLng)),this,SLOT(onHomePositionChanged(internals::PointLatLng)));
coord=map->Projection()->translate(home->Coord(),relativeCoord.x(),relativeCoord.y());
myType=relative;
text=0;
numberI=0;
picture.load(QString::fromUtf8(":/markers/images/marker.png"));
number=WayPointItem::snumber;
++WayPointItem::snumber;
this->setFlag(QGraphicsItem::ItemIsMovable,true);
this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
this->setFlag(QGraphicsItem::ItemIsSelectable,true);
SetShowNumber(shownumber);
RefreshToolTip();
RefreshPos();
@ -76,21 +146,18 @@ namespace mapcontrol
{
if(event->button()==Qt::LeftButton)
{
text=new QGraphicsSimpleTextItem(this);
text=new QGraphicsSimpleTextItem(this);
textBG=new QGraphicsRectItem(this);
// textBG->setBrush(Qt::white);
// textBG->setOpacity(0.5);
textBG->setBrush(Qt::yellow);
textBG->setBrush(QColor(255, 255, 255, 128));
text->setPen(QPen(Qt::red));
text->setPos(10,-picture.height());
textBG->setPos(10,-picture.height());
text->setZValue(3);
RefreshToolTip();
isDragging=true;
}
text->setPen(QPen(Qt::red));
text->setPos(10,-picture.height());
textBG->setPos(10,-picture.height());
text->setZValue(3);
RefreshToolTip();
isDragging=true;
}
QGraphicsItem::mousePressEvent(event);
}
void WayPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
@ -205,6 +272,14 @@ namespace mapcontrol
this->update();
}
}
void WayPointItem::onHomePositionChanged(internals::PointLatLng homepos)
{
if(myType==relative)
{
coord=map->Projection()->translate(homepos,relativeCoord.x(),relativeCoord.y());
}
}
void WayPointItem::WPRenumbered(const int &oldnumber, const int &newnumber, WayPointItem *waypoint)
{
if (waypoint!=this)

View File

@ -33,6 +33,8 @@
#include "../internals/pointlatlng.h"
#include "mapgraphicitem.h"
#include <QObject>
#include <QPoint>
namespace mapcontrol
{
/**
@ -46,15 +48,16 @@ class WayPointItem:public QObject,public QGraphicsItem
Q_INTERFACES(QGraphicsItem)
public:
enum { Type = UserType + 1 };
enum wptype {absolute,relative};
/**
* @brief Constructer
*
* @param coord coordinates in LatLng of the Waypoint
* @param altitude altitude of the WayPoint
* @param map pointer to map to use
* @return
* @return
*/
WayPointItem(internals::PointLatLng const& coord,int const& altitude,MapGraphicItem* map);
WayPointItem(internals::PointLatLng const& coord,int const& altitude,MapGraphicItem* map,wptype type=absolute);
/**
* @brief Constructer
*
@ -64,7 +67,9 @@ public:
* @param map pointer to map to use
* @return
*/
WayPointItem(internals::PointLatLng const& coord,int const& altitude,QString const& description,MapGraphicItem* map);
WayPointItem(internals::PointLatLng const& coord,int const& altitude,QString const& description,MapGraphicItem* map,wptype type=absolute);
WayPointItem(QPoint const& relativeCoord,int const& altitude,QString const& description,MapGraphicItem* map);
/**
* @brief Returns the WayPoint description
*
@ -149,9 +154,9 @@ protected:
void mousePressEvent ( QGraphicsSceneMouseEvent * event );
void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
private:
internals::PointLatLng coord;//coordinates of this WayPoint
QPoint relativeCoord;
bool reached;
QString description;
bool shownumber;
@ -167,6 +172,8 @@ private:
QGraphicsRectItem* numberIBG;
QTransform transf;
wptype myType;
public slots:
/**
* @brief Called when a WayPoint is deleted
@ -189,6 +196,8 @@ public slots:
* @param waypoint a pointer to the WayPoint inserted
*/
void WPInserted(int const& number,WayPointItem* waypoint);
void onHomePositionChanged(internals::PointLatLng);
signals:
/**
* @brief fires when this WayPoint number changes (not fired if due to a auto-renumbering)

View File

@ -178,13 +178,13 @@ OPMapGadgetWidget::OPMapGadgetWidget(QWidget *parent) : QWidget(parent)
m_map->UAV->SetTrailType(UAVTrailType::ByTimeElapsed);
// m_map->UAV->SetTrailType(UAVTrailType::ByDistance);
if(m_map->GPS)
{
m_map->GPS->SetTrailTime(uav_trail_time_list[0]); // seconds
m_map->GPS->SetTrailDistance(uav_trail_distance_list[1]); // meters
m_map->GPS->SetTrailTime(uav_trail_time_list[0]); // seconds
m_map->GPS->SetTrailDistance(uav_trail_distance_list[1]); // meters
m_map->GPS->SetTrailType(UAVTrailType::ByTimeElapsed);
// m_map->GPS->SetTrailType(UAVTrailType::ByDistance);
m_map->GPS->SetTrailType(UAVTrailType::ByTimeElapsed);
}
// **************
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
@ -269,8 +269,9 @@ OPMapGadgetWidget::OPMapGadgetWidget(QWidget *parent) : QWidget(parent)
m_map->SetCurrentPosition(m_home_position.coord); // set the map position
m_map->Home->SetCoord(m_home_position.coord); // set the HOME position
m_map->UAV->SetUAVPos(m_home_position.coord, 0.0); // set the UAV position
m_map->GPS->SetUAVPos(m_home_position.coord, 0.0); // set the UAV position
if(m_map->GPS)
m_map->GPS->SetUAVPos(m_home_position.coord, 0.0); // set the UAV position
m_map->WPCreate();
// **************
// create various context menu (mouse right click menu) actions
@ -390,22 +391,22 @@ void OPMapGadgetWidget::contextMenuEvent(QContextMenuEvent *event)
QString s;
if (!m_widget || !m_map)
return;
if (!m_widget || !m_map)
return;
if (event->reason() != QContextMenuEvent::Mouse)
return; // not a mouse click event
// current mouse position
QPoint p = m_map->mapFromGlobal(event->globalPos());
m_context_menu_lat_lon = m_map->GetFromLocalToLatLng(p);
m_context_menu_lat_lon = m_map->GetFromLocalToLatLng(p);
// m_context_menu_lat_lon = m_map->currentMousePosition();
if (!m_map->contentsRect().contains(p))
return; // the mouse click was not on the map
// show the mouse position
s = QString::number(m_context_menu_lat_lon.Lat(), 'f', 7) + " " + QString::number(m_context_menu_lat_lon.Lng(), 'f', 7);
s = QString::number(m_context_menu_lat_lon.Lat(), 'f', 7) + " " + QString::number(m_context_menu_lat_lon.Lng(), 'f', 7);
m_widget->labelMousePos->setText(s);
// find out if we have a waypoint under the mouse cursor
@ -434,12 +435,12 @@ void OPMapGadgetWidget::contextMenuEvent(QContextMenuEvent *event)
menu.addSeparator();
QMenu maxUpdateRateSubMenu(tr("&Max Update Rate ") + "(" + QString::number(m_maxUpdateRate) + " ms)", this);
for (int i = 0; i < maxUpdateRateAct.count(); i++)
maxUpdateRateSubMenu.addAction(maxUpdateRateAct.at(i));
menu.addMenu(&maxUpdateRateSubMenu);
QMenu maxUpdateRateSubMenu(tr("&Max Update Rate ") + "(" + QString::number(m_maxUpdateRate) + " ms)", this);
for (int i = 0; i < maxUpdateRateAct.count(); i++)
maxUpdateRateSubMenu.addAction(maxUpdateRateAct.at(i));
menu.addMenu(&maxUpdateRateSubMenu);
menu.addSeparator();
menu.addSeparator();
switch (m_map_mode)
{
@ -700,9 +701,11 @@ void OPMapGadgetWidget::updatePosition()
// *************
// set the GPS icon position on the map
m_map->GPS->SetUAVPos(gps_pos, gps_altitude); // set the maps GPS position
m_map->GPS->SetUAVHeading(gps_heading); // set the maps GPS heading
if(m_map->GPS)
{
m_map->GPS->SetUAVPos(gps_pos, gps_altitude); // set the maps GPS position
m_map->GPS->SetUAVHeading(gps_heading); // set the maps GPS heading
}
// *************
}
@ -1886,7 +1889,8 @@ void OPMapGadgetWidget::onClearUAVtrailAct_triggered()
return;
m_map->UAV->DeleteTrail();
m_map->GPS->DeleteTrail();
if(m_map->GPS)
m_map->GPS->DeleteTrail();
}
void OPMapGadgetWidget::onUAVTrailTimeActGroup_triggered(QAction *action)