DESERT 3.5.1
Loading...
Searching...
No Matches
uwPosEstimation.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
38#include "uwPosEstimation.h"
39
40#include <cmath>
41
43 : initialPos()
44 , destPos()
45 , timestamp(0)
46 , speed(1)
47{
48}
49
53
55{
56 return initialPos;
57}
58
60{
61 return destPos;
62}
63
65{
66 return timestamp;
67}
68
70{
71 return speed;
72}
73
74void UwPosEstimation::update(Position newInitPos, Position newDest, double newTime, double newSpeed)
75{
76 if (newTime <= timestamp) {
77 std::cout << "UwPosEstimation::update, new informations are obslete"
78 << std::endl;
79 return;
80 }
81 initialPos.setX(newInitPos.getX());
82 initialPos.setY(newInitPos.getY());
83 initialPos.setZ(newInitPos.getZ());
84 destPos.setX(newDest.getX());
85 destPos.setY(newDest.getY());
86 destPos.setZ(newDest.getZ());
87 timestamp = newTime;
88 speed = newSpeed;
89}
90
92{
93 double x_ROV = initialPos.getX();
94 double y_ROV = initialPos.getY();
95 double z_ROV = initialPos.getZ();
96 double x_wp = destPos.getX();
97 double y_wp = destPos.getY();
98 double z_wp = destPos.getZ();
99 double x = std::abs(x_ROV - x_wp);
100 double y = std::abs(y_ROV - y_wp);
101 double z = std::abs(z_ROV - z_wp);
102 double rho = sqrt(pow(x,2) + pow(y,2) + pow(z,2));
103 double theta = 0; //theta [-pi, pi]
104 double psi = 0; //psi [0, pi]
105 double deltaT = time - timestamp;
106 double dist = nodesDistance(initialPos,destPos);
107 Position estimated_ROV_pos;
108 if (speed == 0 || dist/speed < deltaT) { //waypoint reached
109 estimated_ROV_pos.setX(x_wp);
110 estimated_ROV_pos.setY(y_wp);
111 estimated_ROV_pos.setZ(z_wp);
112 return estimated_ROV_pos;
113 }
114
115 if (x == 0 && y == 0) { //true both for z=0 and z!=0
116 estimated_ROV_pos.setX(x_ROV);
117 estimated_ROV_pos.setY(y_ROV);
118 estimated_ROV_pos.setZ(z_ROV + deltaT * speed);
119 } else {
120 psi = acos(z/rho);
121 if (x == 0) {
122 if (y > 0) {
123 theta = pi/2;
124 } else {
125 theta = -pi/2;
126 }
127 psi = acos(z/rho);
128 } else if (x > 0) {
129 theta = atan(y/x);
130 } else { // x < 0
131 if (y >= 0) {
132 theta = atan(y/x) + pi;
133 } else {
134 theta = atan(y/x) - pi;
135 }
136 }
137 }
138 estimated_ROV_pos.setX(x_ROV + deltaT*speed*sin(psi)*cos(theta));
139 estimated_ROV_pos.setY(y_ROV + deltaT*speed*sin(psi)*sin(theta));
140 estimated_ROV_pos.setZ(z_ROV + deltaT*speed*cos(psi));
141
142 return estimated_ROV_pos;
143}
144
145double UwPosEstimation::nodesDistance(Position& p1, Position& p2)
146{
147 double x1 = p1.getX();
148 double y1 = p1.getY();
149 double z1 = p1.getZ();
150 double x2 = p2.getX();
151 double y2 = p2.getY();
152 double z2 = p2.getZ();
153
154 return sqrt(pow(x2-x1,2) + pow(y2-y1,2) + pow(z2-z1,2));
155}
virtual void update(Position newInitPos, Position newDest, double newTime, double newSpeed)
Update initial position, destination, time of last update.
virtual ~UwPosEstimation()
Destructor of UwPosEstimation class.
virtual Position getDest()
Get destination of last update.
UwPosEstimation()
Constructor of UwPosEstimation class.
double speed
speed of the node.
virtual Position getInitPos()
Get itnitial position of last update.
Position destPos
Destination of last update.
Position initialPos
Initial position related to last update.
virtual double nodesDistance(Position &p1, Position &p2)
Compute absoulute distance between 2 nodes.
virtual Position getEstimatePos(double time)
Get position estimation at a given time.
virtual double getTimestamp()
Get time of last update.
virtual double getSpeed()
Get speed of the node.
double timestamp
time of last update.
Estimate position given initial point, end point and time.
#define pi