00001 // ---------------------------------------------------------------------------- 00002 // 00003 // $Id$ 00004 // 00005 // Copyright 2008, 2009, 2010, 2011, 2012 Antonio Franchi and Paolo Stegagno 00006 // 00007 // This file is part of MIP. 00008 // 00009 // MIP is free software: you can redistribute it and/or modify 00010 // it under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 3 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // MIP is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>. 00021 // 00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it 00023 // 00024 // ---------------------------------------------------------------------------- 00025 00026 00030 00031 #ifndef __ODOMETRY3D_H 00032 #define __ODOMETRY3D_H 00033 00034 #ifdef MIP_HOST_APPLE 00035 #include <applePatch.h> 00036 #endif 00037 00038 #include "TrajControl.h" 00039 00040 00041 00043 /* @{ */ 00044 00045 00052 class Odometry3D{ 00053 private: 00054 bool first;//TODO si può omettere se inizializzo tutto nel costruttore? 00055 // long int N_LeftW_prec,N_RightW_prec;//Input di update 00056 long int _leastLeftRead, _leastRightRead; 00057 // Timer _encTimer; 00058 Time _leastLeftTime,_leastRightTime; 00059 public: 00060 00061 bool updateOdometry3D(long int currLeftRead, long int currRightRead, long int encRes, Time currLeftTime, Time currRightTime,TrajectoryState &trajStatus) 00062 { 00063 Decimal dT,dTR,dTL; 00064 Decimal vr, vl; 00065 long N_LeftW, N_RightW; 00066 Decimal vel_tick_r,vel_tick_l; 00067 // Time currLeftTime,currRightTime; 00068 00069 static Decimal Deltang=(2.0*M_PI/encRes); 00070 if(first) 00071 {//TODO verifica 00072 _leastLeftRead = 0;//NOTE origine della lettura? 00073 _leastRightRead = 0;//NOTE origine della lettura? 00074 _leastLeftTime=Time(0,0); 00075 _leastRightTime=Time(0,0); 00076 // _encTimer.reset(); 00077 first = false; 00078 } 00079 00080 // wheelCountersL = getLeftEncoder();//NOTE nuova della lettura? 00081 // double ltime = leggiTempo3(tvl); 00082 // currLeftTime=_encTimer.get(); 00083 // wheelCountersR = getRightEncoder();//NOTE nuova della lettura? 00084 // // double rtime = leggiTempo3(tvr); 00085 // currRightTime=_encTimer.get(); 00086 00087 int tent = 0; 00088 int maxTent =2; 00089 while( ( (currLeftRead - _leastLeftRead > 200) || (currLeftRead - _leastLeftRead < -200) ) && (tent<maxTent)) 00090 {/*provo a filtrare letture fallaci*/ 00091 // wheelCountersL = getLeftEncoder(); 00092 // ltime = leggiTempo3(tvl); 00093 //TODO aggiornare comunque least readings?qui wheelCountersL 00094 tent++; 00095 return false; 00096 } 00097 tent=0; 00098 N_LeftW = currLeftRead - _leastLeftRead; 00099 while( ( (currRightRead - _leastRightRead > 200)|| (currRightRead - _leastRightRead < -200) ) && (tent<maxTent)) 00100 {/*provo a filtrare letture fallaci*/ 00101 // wheelCountersR = getRightEncoder(); 00102 // rtime = leggiTempo3(tvr); 00103 //TODO aggiornare comunque least readings?qui wheelCountersR 00104 tent++; 00105 return false; 00106 } 00107 N_RightW = currRightRead - _leastRightRead; 00109 // if(wheelCountersL > 2765){ 00110 // wheelCountersL = getLeftEncoder(); 00111 // ltime = leggiTempo3(tvl); 00112 // } 00113 // N_LeftW=wheelCountersL-N_LeftW_prec; 00114 // /*!filtraggio per evitare letture incoerenti (maggiori della risoluzione max dell'encoder)*/ 00115 // if(wheelCountersR > 2765) 00116 // { 00117 // wheelCountersR = getRightEncoder(); 00118 // rtime = leggiTempo3(tvr); 00119 // } 00120 // N_RightW=wheelCountersR-N_RightW_prec; 00121 Time dtLeft=currLeftTime-_leastLeftTime; 00122 Time dtRight=currRightTime-_leastRightTime; 00123 dTL = dtLeft.dCast(); 00124 dTR =dtRight.dCast(); 00125 Time dTime=(Time)(dtRight+dtLeft)*0.5; 00126 dT=dTime.dCast(); 00127 00128 //Calcolo di vl, vr, v & omega 00129 vel_tick_l = rint(N_LeftW/dTL); //pulse/s 00130 vel_tick_r = rint(N_RightW/dTR); //pulse/s 00131 00132 _leastLeftTime = currLeftTime; 00133 _leastRightTime = currRightTime; 00134 00135 // TODO decommentare queste righe e far compilare ### 00136 /* vl=(2.0/ (E_d+1.0)) * (N_LeftW * Deltang * (wheel_diameter/2.0)) / dTL; // m/s 00137 vr=(2.0/ ( (1.0/E_d) +1.0) ) * (N_RightW * Deltang * (wheel_diameter/2.0)) / dTR; // m/s*/ 00138 00139 trajStatus.setDtWheel((Position)(dTR,dTL)); 00140 // MState.dTL = dTL; 00141 // MState.dTR = dTR; 00142 trajStatus.setWheelVel((Position)(vr,vl)); 00143 // MState.vl = vl; 00144 // MState.vr = vr; 00145 // TODO decommentare queste righe e far compilare ###0 00146 // trajStatus.setStatusVel(VelVec(0.5*(vr+vl),(vr-vl)/(E_b*axle_length),dTime)); 00147 // MState.V = 0.5*(vr+vl); 00148 // MState.W = (vr-vl)/(E_b*axle_length); 00149 // dT = 0.5*(dTR + dTL); 00150 00151 //Integrazione Runge-Kutta del II ordine 00152 Angle theta=trajStatus.statusPose().ori(); 00153 Decimal V=trajStatus.statusVel().linVel(); 00154 Decimal W=trajStatus.statusVel().angVel(); 00155 // Pose dPose=Pose (cos(theta.dCast() + W*dT*0.5 )*V*dT,sin(theta.dCast() + W*dT*0.5 )*V*dT,(Angle)(normalize(theta.dCast()+(W*dT)))); 00156 Pose dPose=Pose (cos(theta.dCast2Pi() + W*dT*0.5 )*V*dT,sin(theta.dCast2Pi() + W*dT*0.5 )*V*dT,(Angle)(theta.dCast2Pi()+(W*dT))); 00157 trajStatus.setStatusPose(trajStatus.statusPose()+dPose); 00158 00159 // MState.x += cos( MState.theta + MState.W*dT*0.5 )*MState.V*dT.dCast(); 00160 // MState.y += sin( MState.theta + MState.W*dT*0.5 )*MState.V*dT.dCast(); 00161 // MState.theta = normalizza( MState.theta + MState.W * dT); 00162 00163 //Aggiornamento dello stato 00164 _leastLeftRead = currLeftRead; 00165 _leastRightRead = currRightRead; 00166 00167 // if(log_flag){ 00168 // MLogState.x = MState.x; 00169 // MLogState.y = MState.y; 00170 // MLogState.theta = MState.theta; 00171 // MLogState.leftTicks = wheelCountersL; 00172 // MLogState.rightTicks = wheelCountersR; 00173 // MLogState.leftTv = tvl; 00174 // MLogState.rightTv = tvr; 00175 // } 00176 return true; 00177 } 00178 }; 00179 00180 00181 #endif 00182 00183 00184 /* @} */ 00185 00186