00001
00002 #ifndef DDF_ACCELMOTION_HPP
00003 #define DDF_ACCELMOTION_HPP
00004
00005 #include <iostream>
00006 #include <vector>
00007 #include <algorithm>
00008 #include <sstream>
00009
00010 #include "accprofileitem.hpp"
00011 #include "ddf/debugging.hpp"
00012
00013 namespace jafar
00014 {
00015 namespace ddfsimu
00016 {
00017
00018 typedef std::vector<ddfsimu::AccProfileItem> AccelProfile;
00019
00020 typedef struct {
00021 double A_max;
00022 double A_min;
00023 double V_max;
00024 double V_min;
00025 double P_max;
00026 double P_min;
00027 bool computed;
00028 }ExtValStruct;
00029
00034 class AccelMotion
00035 {
00036 protected:
00037 VEC m_vel_init;
00038 VEC m_pos_init;
00039
00040 private:
00041 AccelProfile m_accel_profile;
00042 bool m_profile_sorted;
00043 ExtValStruct m_extval[4];
00044 std::string m_name;
00045
00046 public:
00047 typedef enum{SAMPLE_ACC = 1 << 0, SAMPLE_VEL = 1 << 1, SAMPLE_POS = 1 << 2} SAMPLE_TYPE;
00048 typedef enum{SV_X, SV_VX, SV_AX, SV_Y, SV_VY, SV_AY, SV_Z, SV_VZ, SV_AZ, SV_BAX, SV_BAY, SV_BAZ} SV_IDX_TYPE;
00049
00050 AccelMotion(std::string name);
00051 virtual ~AccelMotion();
00052
00053 std::string const& GetName() const { return m_name; }
00054 void GetName(char *name) const { strcpy(name, m_name.c_str()); }
00055
00056 void * GetInstance() { return (void*) this; }
00057
00058 void SetInitialConditions(VEC const& v0, VEC const& p0) {
00059 m_pos_init = p0;
00060 m_vel_init = v0;
00061 }
00062
00063 void GetInitialPosition(VEC &p) const { p = m_pos_init; }
00064 void GetInitialVelocity(VEC &v) const { v = m_vel_init; }
00065 VEC const& GetInitialPosition() const { return m_pos_init; }
00066 VEC const& GetInitialVelocity() const { return m_vel_init; }
00067 VEC GetFinalPosition() { return ComputePositionAtTimeT(GetMaxTime()); }
00068
00069 unsigned short GetProfileSize() const { return m_accel_profile.size(); }
00070 VEC const& GetAccel(unsigned short idx) const { JFR_PRECOND(idx < m_accel_profile.size(), "index"); return m_accel_profile[idx].GetAccel(); }
00071 AccProfileItem const* GetAccelItem(unsigned short idx) const
00072 {
00073 JFR_PRECOND(idx < m_accel_profile.size(), "index");
00074 return &m_accel_profile[idx];
00075 }
00076
00077 void AddToAccelProfile(AccProfileItem const &item) {
00078 m_accel_profile.push_back(item);
00079 ToggleExtComputedFlag(false); }
00080
00081 void AddToAccelProfile(double t, VEC const& a) {
00082 time ti(t);
00083 AccProfileItem item(ti,a);
00084 AddToAccelProfile(item);
00085 }
00086
00087 void SortAccelProfile () {
00088 sort(m_accel_profile.begin(),m_accel_profile.end());
00089 JFR_PRECOND (m_accel_profile[0].GetTimeDbl() == 0.0, "!= 0.0");
00090 m_profile_sorted = true;
00091 }
00092
00093 bool IsProfileSorted() const { return m_profile_sorted; }
00094 void ComputeVelocityAndPos (double time, VEC & velocity, VEC & position) const;
00095
00096 void PrintInitialConditions() const {
00097 std::ostringstream stream;
00098
00099 stream << "\n* Initial conditions for " << GetName() << "\n velocity :" << m_vel_init << "\n position :" << m_pos_init << "\n";
00100 JFR_DEBUG(stream.str());
00101 }
00102
00103 void PrintAccelProfile();
00104 double FillArrayWithSamples(float *pArray, unsigned short samples, SAMPLE_TYPE type, AXIS_TYPE axis);
00105 VEC GetAccelAtTimeT(double t) const;
00106 VEC GetInitialAccel() const { return GetAccelAtTimeT(GetMinTimeSec()); }
00107 VEC ComputePositionAtTimeT(double t) const;
00108
00109 double GetMaxTime() const {
00110 JFR_PRECOND(m_profile_sorted == true, "not sorted yet");
00111 return m_accel_profile[m_accel_profile.size()-1].GetTimeDbl();
00112 }
00113
00114 double GetMinTimeSec() const { JFR_PRECOND(m_profile_sorted == true, "not sorted yet"); return m_accel_profile[0].GetTimeDbl(); }
00115 time GetMinTime() const { JFR_PRECOND(m_profile_sorted == true, "not sorted yet"); return m_accel_profile[0].GetTime(); }
00116 double GetTimeSpan() const { return GetMaxTime() - GetMinTimeSec();}
00117 virtual double GetMinTimeSlice() const;
00118 virtual unsigned short GetMinSamplesNr() const;
00119 void ComputeExtremeValues();
00120 bool IsExtremeComputed();
00121
00122 ExtValStruct GetExtremeValues(AXIS_TYPE type) {
00123 JFR_PRECOND(IsExtremeComputed() == true, "extremal values are not computed");
00124 return m_extval[type];
00125 }
00126
00127 ExtValStruct GetExtremeValues(AccelMotion & r, AXIS_TYPE type = AXIS_TYPE(AXIS_Z+1));
00128 void PrintExtreme(AXIS_TYPE type);
00129 void PrintExtreme(ExtValStruct &extval);
00130
00131 void FillProfile(unsigned short buf_size, double data[][4]);
00132 void ClearProfile();
00133 void GdheTrajCmd(unsigned short nbpoints, std::string *str);
00134 unsigned short FillGdheTrajectory(unsigned short nbpoints, float traj[][3], bool purge = true, double time_span = -1.0);
00135 void FillState(VEC const& pos, VEC const& vel, VEC const& acc, VEC & state);
00136 void FillState(time const& t, VEC & state);
00137 void FillStateVelocity(time const& t, VEC & state);
00138
00139 private:
00140 void ComputeVelAndConstAcc(double const time, VEC const& a, VEC const& v_init, VEC const& p_init, VEC *vel, VEC *pos = NULL);
00141 VEC ComputeVelocityAtTimeT (AccProfileItem const& acc_t, AccProfileItem const& acc_t1, VEC const& v0, double T) const;
00142 VEC ComputePositionAtTimeT (AccProfileItem const& acc_t, AccProfileItem const& acc_t1, VEC const& v0, VEC const& x0, double T) const;
00143 void SetVarVECtor(SAMPLE_TYPE type, double x, double y, double z);
00144 void ToggleExtComputedFlag(bool val) { for (int i = 0; i < 4; i++) m_extval[i].computed = val; }
00145 void UpdateMinMax(ExtValStruct *pextval, VEC &accel, VEC &vel, VEC &pos);
00146 };
00147
00148 }
00149 }
00150 #endif