00001
00002 #ifndef DDF_ROBOTSIMU_HPP
00003 #define DDF_ROBOTSIMU_HPP
00004
00005 #include <string>
00006 #include <set>
00007 #include <algorithm>
00008
00009 #include <BayesFilter/bayesFlt.hpp>
00010 #include <BayesFilter/infFlt.hpp>
00011 #include "ddfsimu/accelmotion.hpp"
00012 #include "ddfsimu/measureitem.hpp"
00013 #include "ddf/definitions.hpp"
00014 #include "ddfsimu/accelerometersimu.hpp"
00015 #include "ddfsimu/eventsimu.hpp"
00016 #include "ddfsimu/gpssimu.hpp"
00017
00018 namespace jafar
00019 {
00020 namespace ddfsimu
00021 {
00022
00027 class RobotSimu : public AccelMotion
00028 {
00029
00030 public:
00031 class SortedBufferItem
00032 {
00033 public:
00034 time m_time;
00035 SensorSimuBase * sensor;
00036 unsigned short m_rob_id;
00037 time m_last_sensed;
00038
00039 bool operator < (const SortedBufferItem & s1) const { return (m_time < s1.m_time); }
00040 };
00041
00042 typedef std::multiset<SortedBufferItem> SortedBuffer;
00043
00044 private:
00045 typedef std::vector<SensorSimuBase *> Sensors;
00046 typedef std::vector<MeasureItem> FilterStateStorage;
00047
00048 Sensors m_sensors;
00049 double m_min_time_slice;
00050 FilterStateStorage m_sv_store;
00051 FilterStateStorage m_iv_store;
00052 FilterStateStorage m_svar_store;
00053 FilterStateStorage m_iv_info_store;
00054 double m_sv_min_slice;
00055 SortedBuffer Sbuf;
00056 unsigned short m_sv_size;
00057 SortedBuffer::iterator m_meas_it;
00058 unsigned short m_nr_meas;
00059 bool m_use_only_vel;
00060 bool m_with_biases;
00061
00062 protected:
00063 unsigned short m_rob_id;
00064
00065 private:
00066 void InfoInitFilter(Bayesian_filter::Information_scheme &info_scheme, unsigned short sv_size, bool with_biases);
00067
00068 public:
00069 typedef enum {SORT_STAMP_SENSED, SORT_STAMP_AVAILABLE} SORT_STAMP_TYPE;
00070
00071 RobotSimu(unsigned short sv_size, std::string name, bool use_only_vel = false, bool use_biases = false);
00072 virtual ~RobotSimu();
00073
00074 unsigned short GetRobID() const { return m_rob_id; }
00075 void Filter(bool with_biases);
00076 void AddSensor(SensorSimuBase * sensor) { m_sensors.push_back(sensor); }
00077 void RemoveSensorByID(unsigned short id);
00078 void RemoveAllSensors();
00079
00080 MeasureItem const& GetStateVector(unsigned short idx) const { return m_sv_store[idx]; }
00081 MeasureItem const& GetInfoVector(unsigned short idx) const { return m_iv_store[idx]; }
00082 MeasureItem const& GetVarVector(unsigned short idx) const { return m_svar_store[idx]; }
00083 MeasureItem const& GetInfoMatVector(unsigned short idx) const { return m_iv_info_store[idx]; }
00084
00085 unsigned short GetSvSize() { return m_sv_size; }
00086 bool HasBias() { return m_with_biases; }
00087 bool UseOnlyVel() { return m_use_only_vel; }
00088 unsigned short GetStatesNr() { JFR_PRECOND(m_sv_store.size() == m_iv_store.size(), "bad sizes"); return m_sv_store.size(); }
00089 void SimulatePropPerception();
00090 void SimulateExtPerception(RobotSimu &other_rob);
00091 void SimulateAll(RobotSimu &other_rob);
00092 void FillUpMeasurementSet(SORT_STAMP_TYPE stmp_type);
00093 void ClearUpMeasurementSet() { Sbuf.clear(); m_nr_meas = 0; }
00094 void ClearAllSensorsMeasurements();
00095 void InitStampJitter(time const& jitter);
00096 short GetCurrentSensorID() const;
00097 SensorSimuBase * GetCurrentSensorPointer() const { return m_meas_it->sensor; }
00098 SortedBuffer::iterator GetCurrentIterator() const { return m_meas_it; }
00099 SENSOR_TYPE GetCurrentSensorType() const;
00100 time GetCurrentLastSensed() const;
00101 int GetCurrentTriggerNodeID();
00102 bool IsCurrentTriggerUpdatePOM();
00103 time GetCurrentRelTime();
00104 time const& GetCurrentSensorPeriod() const;
00105 bool GetCurrentInfo(InfoContainer &meas);
00106 MeasureItem const& GetCurrentMeasure() const;
00107 bool GetNextInfo(InfoContainer &meas);
00108 void ResetCounter() { m_meas_it = Sbuf.begin(); ResetSensorsGetNext(); }
00109 bool IncCounter();
00110 bool EndBuf() { return m_meas_it == Sbuf.end(); }
00111 void DumpAllMeasures(const char * fname);
00112 void DumpTruth(const char* fname);
00113 void DumpSensor(const char* fname, unsigned short rank);
00114 void DumpSensorByID(const char* fname, unsigned short id);
00115 unsigned short GetNrMeas() { return m_nr_meas; }
00116 unsigned short GetSensorID(unsigned short rank_in_array) { return m_sensors[rank_in_array]->GetID(); }
00117 std::string GetSensorName(unsigned short rank_in_array) { return m_sensors[rank_in_array]->GetName(); }
00118 unsigned short GetNumSensors() { return m_sensors.size(); }
00119
00120 void ListAllSensors();
00121
00122 void ListSensorsOfType(SENSOR_TYPE type)
00123 {
00124 std::ostringstream stream;
00125
00126 for (Sensors::iterator it = m_sensors.begin(); it != m_sensors.end(); ++it)
00127 {
00128 if ((*it)->GetTypeName() == type)
00129 stream <<" " << *(*it) << " Time offset: " << (*it)->GetTimeOffset().to_double() << \
00130 " Simu variance: " << (*it)->GetSensorSimVariance()<< " Seed: " << (*it)->GetSeed() << "\n";
00131 }
00132 JFR_DEBUG(stream.str());
00133 }
00134
00135
00136
00137 template<typename T>
00138 T* GetSensorOfType(unsigned short rank)
00139 {
00140 unsigned short idx = 0;
00141 T* sensor;
00142 Sensors::iterator it = m_sensors.begin();
00143
00144 while (it != m_sensors.end())
00145 {
00146 sensor = dynamic_cast<T*> (*it);
00147 if (sensor != NULL) {
00148 if (idx == rank) return sensor;
00149 else idx++;
00150 }
00151 it++;
00152 }
00153 JFR_DEBUG("GetSensorOfType -> Sensor not found!\n");
00154 exit(EXIT_FAILURE);
00155 return NULL;
00156 }
00157
00158 unsigned short GetNumSensorsOfType(SENSOR_TYPE type)
00159 {
00160 unsigned short nb = 0;
00161 for(unsigned short i = 0; i < GetNumSensors(); i++) if (isSensorOfType(m_sensors[i], type)) nb++;
00162 return nb;
00163 }
00164
00165 int GetSensorID(SENSOR_TYPE type, unsigned short rank)
00166 {
00167 int num = GetNumSensorsOfType(type);
00168 unsigned short item = 0;
00169
00170 if (num == 0) return -1;
00171 for(unsigned int i = 0; i < GetNumSensors(); i++)
00172 if (isSensorOfType(m_sensors[i], type) && (item++ == rank)) return m_sensors[i]->GetID();
00173
00174 return -1;
00175 }
00176
00177 void PrintRobot();
00178 void SearchMinTimeSlice();
00179 double GetMinTimeSlice() const {
00180 JFR_PRECOND(m_min_time_slice != std::numeric_limits<double>::max(), "== max double");
00181 return m_min_time_slice;
00182 }
00183
00184 unsigned short GetMinSamplesNr() const;
00185 void PerceiveWithPropSensor(SensorSimuBase &sensor);
00186
00187 SensorBase * GetSensorByID(unsigned short id);
00188
00189 void ResetSensorsGetNext() {
00190 for (int j = 0; j < GetNumSensors(); j++) m_sensors[j]->ResetGetNext();
00191 }
00192
00193 private:
00194 void DumpMeasure(unsigned short nb, MeasureItem const& meas, std::ofstream & stream) const;
00195
00196 bool isSensorOfType(SensorSimuBase *sensor, SENSOR_TYPE type) { if (sensor->GetTypeName() == type) return true; else return false; }
00197 SensorSimuBase * GetSensorPointer(unsigned short rank)
00198 {
00199 JFR_PRECOND(rank < m_sensors.size(), "bad sizes");
00200 return m_sensors[rank];
00201 }
00202
00203 };
00204
00205 }
00206 }
00207 #endif