00001
00002 #ifndef DDF_SENSORSIMU_HPP
00003 #define DDF_SENSORSIMU_HPP
00004
00005 #include "ddfsimu/sensor.hpp"
00006 #include "jmath/random.hpp"
00007 #include "ddf/definitions.hpp"
00008
00009 namespace jafar
00010 {
00011 namespace ddfsimu
00012 {
00013
00014 using jafar::jmath::MultiDimNormalDistribution;
00015
00019 class SensorSimuBase : public SensorObs
00020 {
00022 VEC m_simu_var;
00023 long unsigned int m_seed;
00024 MultiDimNormalDistribution *m_norm_dist;
00025 MultiDimNormalDistribution *m_norm_jitter_dist;
00026 time m_offset;
00027 time m_stamp_jitter;
00028
00029 public:
00030 SensorSimuBase(unsigned short x_size, unsigned short z_size):
00031 SensorObs(x_size, z_size), m_simu_var(z_size), m_offset(0,0), m_stamp_jitter(0,0)
00032 {
00033 m_norm_dist = NULL;
00034 m_norm_jitter_dist = NULL;
00035 m_seed = GenSeed(GetID());
00036 m_simu_var.clear();
00037 }
00038
00039 SensorSimuBase(SensorSimuBase const& other) :
00040 SensorObs(other.GetStateSize(),
00041 other.GetMeasureSize()),
00042 m_simu_var(other.GetMeasureSize()),
00043 m_seed(other.m_seed),
00044 m_offset(other.m_offset),
00045 m_stamp_jitter(other.m_stamp_jitter)
00046 {
00047 JFR_PRECOND(GetMeasureSize() == other.GetMeasureSize(), "bad size");
00048 m_simu_var = other.m_simu_var;
00049
00050 VEC nul(GetMeasureSize());
00051 nul.clear();
00052
00053 if (other.m_norm_dist != NULL)
00054 m_norm_dist = new MultiDimNormalDistribution(nul, m_simu_var, m_seed);
00055 else
00056 m_norm_dist = NULL;
00057
00058 if (other.m_norm_jitter_dist != NULL)
00059 GenStampJitterDistrib();
00060 else
00061 m_norm_jitter_dist = NULL;
00062 }
00063 virtual ~SensorSimuBase() { if (m_norm_dist != NULL) delete m_norm_dist; }
00064
00065 void SetTimeOffset(time const& offset) { m_offset = offset; }
00066 time GetTimeOffset() const { return m_offset; }
00067
00068 void SetStampJitterTwoSigma(time const& jitter) { m_stamp_jitter = jitter; GenStampJitterDistrib(); }
00069 time GetStampJitterTwoSigma() { return m_stamp_jitter; }
00070 time ComputeStampJitter() {
00071 if(m_stamp_jitter == time::null()) return time::null();
00072 VEC v(1); v = m_norm_jitter_dist->get();
00073 return time(fabs(v[0]));
00074 }
00075
00076 void SetSeed(unsigned short sens_id) { m_seed = GenSeed(sens_id); }
00077 long unsigned int GetSeed() { return m_seed; }
00078 static long unsigned int GenSeed(unsigned short id) { return id * 3 + 55319; }
00079 void SetSensorSimVariance(VEC const& var)
00080 {
00081 JFR_PRECOND(var.size() == GetMeasureSize(), "bad size");
00082 VEC nul(GetMeasureSize()); nul.clear();
00083 m_simu_var = var;
00084 if (m_norm_dist != NULL) delete m_norm_dist;
00085 m_norm_dist = new MultiDimNormalDistribution(nul, m_simu_var, m_seed);
00086 }
00087
00088 VEC GetSensorSimVariance() const { return m_simu_var; }
00089
00090 void AddSensorNoise(VEC &measure)
00091 {
00092 JFR_PRECOND(measure.size() == GetMeasureSize(), "bad size");
00093 JFR_PRECOND(m_norm_dist != NULL, "ptr null");
00094 measure += m_norm_dist->get();
00095 }
00096
00097 void AddMeasurementWithNoise(time sensed, time available, VEC &data)
00098 {
00099 VEC tmp(data);
00100 AddSensorNoise(tmp);
00101 AddMeasurement(sensed, available, tmp);
00102 }
00103
00105 virtual void Perceive(VEC const& proc_state, time t)=0;
00106
00108 virtual void ComputeInformation(VEC const& z, VEC &i, MSYM &I)=0;
00109
00110 private:
00111 void GenStampJitterDistrib()
00112 {
00113 VEC v(1), v_var(1);
00114 v[0] = 0;
00115 v_var[0] = TWOSIGMA_TO_VAR(m_stamp_jitter.to_double());
00116 if (m_norm_jitter_dist != NULL) delete m_norm_jitter_dist;
00117 m_norm_jitter_dist = new MultiDimNormalDistribution(v, v_var, m_seed);
00118 }
00119 };
00120
00124 template<typename T_OBS>
00125 class SensorSimu : public SensorSimuBase, public T_OBS
00126 {
00127
00128 public:
00129
00130 SensorSimu(unsigned short x_size, unsigned short z_size): SensorSimuBase(x_size,z_size), T_OBS(x_size, z_size) { }
00131 virtual ~SensorSimu() { }
00132
00133 friend std::ostream& operator << (std::ostream &stream, SensorSimu<T_OBS> const& pd)
00134 {
00135 stream << static_cast<SensorBase> (pd) << " Simulation variance: " << pd.m_var;
00136 return stream;
00137 }
00138 };
00139
00140 }
00141 }
00142 #endif