MOOS 0.2375
|
00001 00002 // 00003 // MOOS - Mission Oriented Operating Suite 00004 // 00005 // A suit of Applications and Libraries for Mobile Robotics Research 00006 // Copyright (C) 2001-2005 Massachusetts Institute of Technology and 00007 // Oxford University. 00008 // 00009 // This software was written by Paul Newman and others 00010 // at MIT 2001-2002 and Oxford University 2003-2005. 00011 // email: pnewman@robots.ox.ac.uk. 00012 // 00013 // This file is part of a MOOS Instrument. 00014 // 00015 // This program is free software; you can redistribute it and/or 00016 // modify it under the terms of the GNU General Public License as 00017 // published by the Free Software Foundation; either version 2 of the 00018 // License, or (at your option) any later version. 00019 // 00020 // This program is distributed in the hope that it will be useful, 00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00023 // General Public License for more details. 00024 // 00025 // You should have received a copy of the GNU General Public License 00026 // along with this program; if not, write to the Free Software 00027 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00028 // 02111-1307, USA. 00029 // 00031 // CompassInstrument.cpp: implementation of the CCompassInstrument class. 00032 // 00034 #include <MOOSLIB/MOOSLib.h> 00035 00036 00037 #include <iostream> 00038 using namespace std; 00039 #include "CompassInstrument.h" 00040 00042 // Construction/Destruction 00044 00045 CCompassInstrument::CCompassInstrument() 00046 { 00047 m_dfMagneticOffset = 0; 00048 } 00049 00050 CCompassInstrument::~CCompassInstrument() 00051 { 00052 00053 } 00054 00055 00058 bool CCompassInstrument::Iterate() 00059 { 00060 if(GetData()) 00061 { 00062 PublishData(); 00063 } 00064 00065 return true; 00066 } 00067 00069 // tell the world 00070 bool CCompassInstrument::PublishData() 00071 { 00072 return PublishFreshMOOSVariables(); 00073 00074 } 00075 00076 00077 bool CCompassInstrument::OnStartUp() 00078 { 00079 CMOOSInstrument::OnStartUp(); 00080 00081 //here we make the variables that we are managing 00082 double dfHeadingPeriod = 0.5; 00083 00084 //Compass update @ 2Hz 00085 AddMOOSVariable("Heading", "SIM_HEADING", "COMPASS_HEADING", dfHeadingPeriod); 00086 AddMOOSVariable("Yaw", "", "COMPASS_YAW", dfHeadingPeriod); 00087 AddMOOSVariable("Raw", "", "COMPASS_RAW", dfHeadingPeriod); 00088 00089 GetMagneticOffset(); 00090 00091 if(IsSimulateMode()) 00092 { 00093 //not much to do... 00094 RegisterMOOSVariables(); 00095 } 00096 else 00097 { 00098 //try to open 00099 if(!SetupPort()) 00100 { 00101 return false; 00102 } 00103 00104 //try 10 times to initialise sensor 00105 if(!InitialiseSensorN(10,"COMPASS")) 00106 { 00107 return false; 00108 } 00109 } 00110 00111 00112 return true; 00113 } 00114 00115 00116 00117 bool CCompassInstrument::OnNewMail(MOOSMSG_LIST &NewMail) 00118 { 00119 00120 CMOOSMsg Msg; 00121 00122 if(m_Comms.PeekMail(NewMail,"SIM_HEADING",Msg,true)) 00123 { 00124 return UpdateWithMagneticDegrees(Msg.m_dfVal); 00125 } 00126 else 00127 { 00128 return UpdateMOOSVariables(NewMail); 00129 } 00130 return true; 00131 } 00132 00133 00134 00135 00136 bool CCompassInstrument::OnConnectToServer() 00137 { 00138 if(IsSimulateMode()) 00139 { 00140 //not much to do... 00141 return RegisterMOOSVariables(); 00142 } 00143 else 00144 { 00145 00146 } 00147 return true; 00148 } 00149 00150 00152 // here we initialise the sensor, giving it start up values 00153 bool CCompassInstrument::InitialiseSensor() 00154 { 00155 00156 return true; 00157 00158 } 00159 00160 bool CCompassInstrument::GetData() 00161 { 00162 00163 if(!IsSimulateMode()) 00164 { 00165 //here we actually access serial ports etc 00166 00167 string sWhat; 00168 00169 double dfWhen; 00170 00171 if(m_Port.IsStreaming()) 00172 { 00173 if(!m_Port.GetLatest(sWhat,dfWhen)) 00174 { 00175 return false; 00176 } 00177 } 00178 else 00179 { 00180 if(!m_Port.GetTelegram(sWhat,0.5)) 00181 { 00182 return false; 00183 } 00184 } 00185 00186 if(PublishRaw()) 00187 { 00188 SetMOOSVar("Raw",sWhat,MOOSTime()); 00189 } 00190 00191 ParseCompassData(sWhat); 00192 00193 00194 00195 } 00196 else 00197 { 00198 //in simulated mode there is nothing to do..all data 00199 //arrives via comms. 00200 } 00201 00202 return true; 00203 00204 } 00205 00206 00207 bool CCompassInstrument::ParseCompassData(string &sWhat) 00208 { 00209 MOOSChomp(sWhat,"%"); 00210 if(!sWhat.empty()) 00211 { 00212 double dfAngle = atof(sWhat.c_str()); 00213 dfAngle/=10; 00214 00215 UpdateWithMagneticDegrees(dfAngle); 00216 00217 } 00218 00219 return true; 00220 } 00221 00222 00223 00224 double CCompassInstrument::Magnetic2True(double dfMagnetic) 00225 { 00226 return dfMagnetic+m_dfMagneticOffset; 00227 } 00228 00229 double CCompassInstrument::True2Yaw(double dfTrueHeading) 00230 { 00231 return -dfTrueHeading; 00232 } 00233 00234 bool CCompassInstrument::UpdateWithMagneticDegrees(double dfMagDegrees) 00235 { 00236 //convert to true north 00237 double dfAngle=Magnetic2True(dfMagDegrees); 00238 00239 //publish this in degrees 00240 SetMOOSVar("Heading",dfAngle,MOOSTime()); 00241 00242 //publish in yaw domain 00243 dfAngle = True2Yaw(dfAngle); 00244 00245 //and in radians 00246 dfAngle*=PI/180.0; 00247 00248 //wrapped... 00249 dfAngle = MOOS_ANGLE_WRAP(dfAngle); 00250 00251 //publish.. 00252 SetMOOSVar("Yaw",dfAngle,MOOSTime()); 00253 00254 return true; 00255 }