DESERT 3.5.1
Loading...
Searching...
No Matches
uwsmposition.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
40#include "uwsmposition.h"
41#include <iostream>
42
43/* ======================================================================
44 TCL Hooks for the simulator
45 ====================================================================== */
46static class UWSMPositionClass : public TclClass
47{
48public:
50 : TclClass("Position/UWSM")
51 {
52 }
53 TclObject *
54 create(int, const char *const *)
55 {
56 return (new UWSMPosition());
57 }
59
61 : Position()
62 , trgTime_(-1)
63 , Xsorg_(0)
64 , Ysorg_(0)
65 , Zsorg_(0)
66 , Xdest_(0)
67 , Ydest_(0)
68 , Zdest_(0)
69 , speed_(0)
70 , lastUpdateTime_(0)
71{
72 bind("debug_", &debug_);
73}
74
78
79int
80UWSMPosition::command(int argc, const char *const *argv)
81{
82 Tcl &tcl = Tcl::instance();
83 if (argc == 6) {
84 if (strcasecmp(argv[1], "setdest") == 0) {
85 if (debug_)
86 cerr << NOW << "UWSMPosition::command(setdest, " << argv[2]
87 << ", " << argv[3] << ", " << argv[4] << ", " << argv[5]
88 << ")" << endl;
89 setdest(atof(argv[2]), atof(argv[3]), atof(argv[4]), atof(argv[5]));
90 return TCL_OK;
91 }
92 } else if (argc == 5) {
93 if (strcasecmp(argv[1], "setdest") == 0) {
94 if (debug_)
95 cerr << NOW << "UWSMPosition::command(setdest, " << argv[2]
96 << ", " << argv[3] << ", " << argv[4] << ")" << endl;
97 setdest(atof(argv[2]), atof(argv[3]), atof(argv[4]));
98 return TCL_OK;
99 }
100 } else if (argc == 2) {
101 if (strcasecmp(argv[1], "update") == 0) {
102 double now = Scheduler::instance().clock();
103 update(now);
104 return TCL_OK;
105 }
106 }
107 return Position::command(argc, argv);
108}
109
110void
112 double x_dest, double y_dest, double z_dest, double spead_setted)
113{
114 double now = Scheduler::instance().clock();
115 if ((trgTime_ >= 0.) && (now > lastUpdateTime_ + 1e-6))
116 update(now);
117 trgTime_ = now;
118 if (trgTime_ < 0.0)
119 cerr << "Warning: calling set dest at time < 0 will not work" << endl;
120 Xdest_ = x_dest;
121 Ydest_ = y_dest;
122 Zdest_ = z_dest;
123 speed_ = spead_setted;
124 Xsorg_ = x_;
125 Ysorg_ = y_;
126 Zsorg_ = z_;
127 if (debug_)
128 printf("UWSMPosition::setdest pos (%f,%f,%f), dest(%f,%f,%f), "
129 "source(%f,%f,%f), speed %f\n",
130 x_,
131 y_,
132 z_,
133 Xdest_,
134 Ydest_,
135 Zdest_,
136 Xsorg_,
137 Ysorg_,
138 Zsorg_,
139 speed_);
140}
141
142void
143UWSMPosition::setdest(double x_dest, double y_dest, double z_dest)
144{
145 double now = Scheduler::instance().clock();
146 if ((trgTime_ >= 0.) && (now > lastUpdateTime_ + 1e-6))
147 update(now);
148 trgTime_ = now;
149 if (trgTime_ < 0.0)
150 cerr << "Warning: calling set dest at time < 0 will not work" << endl;
151 Xdest_ = x_dest;
152 Ydest_ = y_dest;
153 Zdest_ = z_dest;
154 Xsorg_ = x_;
155 Ysorg_ = y_;
156 Zsorg_ = z_;
157 if (debug_)
158 printf("UWSMPosition::setdest pos (%f,%f,%f), dest(%f,%f,%f), "
159 "source(%f,%f,%f), speed %f\n",
160 x_,
161 y_,
162 z_,
163 Xdest_,
164 Ydest_,
165 Zdest_,
166 Xsorg_,
167 Ysorg_,
168 Zsorg_,
169 speed_);
170}
171
172void
174{
175 if ((trgTime_ < 0.) || (now < lastUpdateTime_ + 1e-6))
176 return;
177 double gamma;
178 double theta;
179 double theta_den = sqrt(pow(Ydest_ - Ysorg_, 2.0) +
180 pow(Xdest_ - Xsorg_, 2.0) + pow(Zdest_ - Zsorg_, 2.0));
181 if (theta_den == 0) {
182 x_ = Xsorg_;
183 y_ = Ysorg_;
184 z_ = Zsorg_;
185 } else {
186 theta = acos((Zdest_ - Zsorg_) / theta_den);
187 if (Xdest_ - Xsorg_ == 0)
188 gamma = pi / 2 * sgn(Ydest_ - Ysorg_);
189 else
190 gamma = atan((Ydest_ - Ysorg_) / (Xdest_ - Xsorg_));
191 if ((Xdest_ - Xsorg_) < 0.0)
192 gamma += (Ysorg_ - Ydest_) >= 0.0 ? pi : -pi;
193 x_ = Xsorg_ + (speed_ * (now - trgTime_)) * sin(theta) * cos(gamma);
194 y_ = Ysorg_ + (speed_ * (now - trgTime_)) * sin(theta) * sin(gamma);
195 z_ = Zsorg_ + (speed_ * (now - trgTime_)) * cos(theta);
196 if (pow(Ydest_ - Ysorg_, 2.0) + pow(Xdest_ - Xsorg_, 2.0) +
197 pow(Zdest_ - Zsorg_, 2.0) <
198 pow(x_ - Xsorg_, 2.0) + pow(y_ - Ysorg_, 2.0) +
199 pow(z_ - Zsorg_, 2.0)) {
200 x_ = Xdest_;
201 y_ = Ydest_;
202 z_ = Zdest_;
203 }
204 if (debug_)
205 printf("New pos (%f,%f,%f), dest(%f,%f,%f), source(%f,%f,%f), "
206 "speed %f sen(%f)=%f\n",
207 x_,
208 y_,
209 z_,
210 Xdest_,
211 Ydest_,
212 Zdest_,
213 Xsorg_,
214 Ysorg_,
215 Zsorg_,
216 speed_,
217 gamma,
218 sin(gamma));
219 }
220
221 lastUpdateTime_ = now;
222}
223
224bool
226{
227 double dist = std::sqrt(pow(Xdest_ - x_, 2.0) + pow(Ydest_ - y_, 2.0) +
228 pow(Zdest_ - z_, 2.0));
229
230 if (std::fabs(dist) < 1e-6)
231 return true;
232
233 return false;
234}
235
236double
238{
239 double now = Scheduler::instance().clock();
240 if ((trgTime_ >= 0.) && (now > lastUpdateTime_ + 1e-6))
241 update(now);
242 return (x_);
243}
244
245void
247{
248 x_ = x;
249 Xdest_ = x;
250 Xsorg_ = x;
251}
252void
254{
255 y_ = y;
256 Ydest_ = y;
257 Ysorg_ = y;
258}
259void
261{
262 z_ = z;
263 Zdest_ = z;
264 Zsorg_ = z;
265}
266double
268{
269 double now = Scheduler::instance().clock();
270 if ((trgTime_ >= 0.) && (now > lastUpdateTime_ + 1e-6))
271 update(now);
272 return (y_);
273}
274
275double
277{
278 double now = Scheduler::instance().clock();
279 if ((trgTime_ >= 0.) && (now > lastUpdateTime_ + 1e-6))
280 update(now);
281 return (z_);
282}
283
284double
286{
287 return Xdest_;
288}
289
290double
292{
293 return Ydest_;
294}
295
296double
298{
299 return Zdest_;
300}
301
302double
304{
305 return speed_;
306}
TclObject * create(int, const char *const *)
virtual void setY(double y)
double lastUpdateTime_
time in which the TCL command setdest is invoked
virtual ~UWSMPosition()
Destructor.
int debug_
speed of the node
virtual double getY()
Method that return the current projection of the node on the y-axis.
virtual double getX()
Method that return the current projection of the node on the x-axis.
virtual bool isDestReached() const
virtual int command(int argc, const char *const *argv)
TCL command interpreter setdest <integer value>integer value>integer value>: set the movement pattern...
virtual void setZ(double z)
UWSMPosition()
Constructor.
virtual void update(double now)
Method that updates both the position coordinates.
double getSpeed() const
Method that return the actual speed.
virtual double getZdest() const
Method that return the z cooridnate of the destination point.
virtual double getYdest() const
Method that return the y cooridnate of the destination point.
double speed_
position on the z-axis of the starting point (when the TCL command setdest is invoked)
virtual double getZ()
Method that return the current projection of the node on the z-axis.
double Zdest_
position on the y-axis of the destination point
virtual void setX(double x)
virtual double getXdest() const
Method that return the x cooridnate of the destination point.
double Xdest_
time last updated of the coordinates was computed
double Ydest_
position on the x-axis of the destination point
double Xsorg_
position on the z-axis of the destination point
double Zsorg_
position on the y-axis of the starting point (when the TCL command setdest is invoked)
virtual void setdest(double x_dest, double y_dest, double z_dest, double spead)
double Ysorg_
position on the x-axis of the starting point (when the TCL command setdest is invoked)
UWSMPositionClass class_uwsmposition
Provides the definition of the class UWSMPosition.
#define pi
#define sgn(x)