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