bmposition.cc

00001 /*
00002  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #include<iostream>
00032 #include "bmposition.h"
00033 
00034 
00035 /* ======================================================================
00036    TCL Hooks for the simulator
00037    ====================================================================== */
00038 static class BMPositionClass : public TclClass {
00039 public:
00040         BMPositionClass() : TclClass("Position/BM") {}
00041         TclObject* create(int, const char*const*) {
00042                 return (new BMPosition());
00043         }
00044 } class_bmposition;
00045 
00046 BMPosition::BMPosition() : Position(), trgTime_(-1), Xsorg_(0), Ysorg_(0), Xdest_(0), Ydest_(0), speed_(0), lastUpdateTime_(0)
00047 {
00048         bind("debug_", &debug_);
00049 }
00050 
00051 BMPosition::~BMPosition()
00052 {
00053 }
00054 
00055 int BMPosition::command(int argc, const char*const* argv)
00056 {
00057         Tcl& tcl = Tcl::instance();
00058         if(argc == 5)
00059         {
00060                 if(strcasecmp(argv[1], "setdest") == 0)
00061                 {
00062                   if (debug_ > 10)
00063                     cerr << NOW << "BMPosition::command(setdest, "
00064                          << argv[2] << ", "
00065                          << argv[3] << ", "
00066                          << argv[4] << ")"
00067                          << endl;
00068                       
00069                         trgTime_ = Scheduler::instance().clock();
00070                         if(trgTime_ <= 0.0)
00071                           cerr << "Warning: calling set dest at time <= 0 will not work" << endl;
00072                         Xdest_ = atof(argv[2]);
00073                         Ydest_ = atof(argv[3]);
00074                         speed_ = atof(argv[4]);
00075                         Xsorg_ = x_;
00076                         Ysorg_ = y_;
00077                         return TCL_OK;
00078                 }
00079         }
00080         return Position::command(argc, argv);
00081 }
00082 
00083 
00084 void BMPosition::update(double now)
00085 {
00086         double gamma;
00087         
00088                 if (Xdest_-Xsorg_==0)
00089                         gamma = pi/2*sgn(Ydest_-Ysorg_);
00090                 else 
00091                         gamma = atan((Ydest_-Ysorg_)/(Xdest_-Xsorg_));
00092                 if ((Xdest_-Xsorg_)<0.0) gamma += (Ysorg_-Ydest_)>=0.0?pi:-pi;
00093                 //printf("At %f prec pos (%f,%f)",now,x_,y_);
00094                 x_ = Xsorg_ + (speed_*(now - trgTime_) )*cos(gamma);
00095                 y_ = Ysorg_ + (speed_*(now - trgTime_) )*sin(gamma);
00096                 if (debug_>50)
00097                         printf("New pos (%f,%f), dest(%f,%f), speed %f sen(%f)=%f\n",x_,y_,Xdest_,Ydest_,speed_,gamma, sin(gamma));
00098                 
00099                 
00100                 lastUpdateTime_ = now;
00101 }
00102 
00103 double BMPosition::getX()
00104 {
00105         double now = Scheduler::instance().clock();
00106         if ((trgTime_>0.)&&(now>lastUpdateTime_+1e-6))
00107                 update(now);
00108         return (x_);
00109 }
00110 
00111 double BMPosition::getY()
00112 {
00113         double now = Scheduler::instance().clock();
00114         if ((trgTime_>0.)&&(now>lastUpdateTime_+1e-6))
00115                 update(now);
00116         return (y_);
00117 }

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2