00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include<iostream>
00032 #include "bmposition.h"
00033
00034
00035
00036
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
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 }