UtilityH.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /// \file UtilityH.h
  2. /// \brief General Math and Control utility functions
  3. /// \author Hatem Darweesh
  4. /// \date May 14, 2016
  5. #ifndef UTILITYH_H_
  6. #define UTILITYH_H_
  7. #include <assert.h>
  8. #include <string>
  9. #include <math.h>
  10. namespace UtilityHNS
  11. {
  12. #define DEG2RAD M_PI / 180.
  13. #define RAD2DEG 180. / M_PI
  14. #define SIGN(x) (x > 0) ? 1 : ((x < 0) ? -1 : 0)
  15. #define MIN(x,y) (x <= y ? x : y)
  16. #define MAX(x,y) (x >= y ? x : y)
  17. class UtilityH
  18. {
  19. public:
  20. UtilityH();
  21. virtual ~UtilityH();
  22. static double FixNegativeAngle(const double& a);
  23. static double SplitPositiveAngle(const double& a);
  24. static double InverseAngle(const double& a);
  25. static double AngleBetweenTwoAnglesPositive(const double& a1, const double& a2);
  26. static double GetCircularAngle(const double& prevContAngle, const double& prevAngle, const double& currAngle);
  27. //Time Functions
  28. static void GetTickCount(struct timespec& t);
  29. static std::string GetFilePrefixHourMinuteSeconds();
  30. static double GetTimeDiffNow(const struct timespec& old_t);
  31. static double GetTimeDiff(const struct timespec& old_t,const struct timespec& curr_t);
  32. static std::string GetDateTimeStr();
  33. static int tsCompare (struct timespec time1, struct timespec time2, int micro_tolerance = 10);
  34. static int GetSign(double x);
  35. static std::string GetHomeDirectory();
  36. static double GetMomentumScaleFactor(const double& v);
  37. static timespec GetTimeSpec(const time_t& srcT);
  38. static time_t GetLongTime(const struct timespec& srcT);
  39. };
  40. class PIDController
  41. {
  42. public:
  43. PIDController();
  44. PIDController(const double& kp, const double& ki, const double& kd);
  45. void Init(const double& kp, const double& ki, const double& kd);
  46. void Setlimit(const double& upper,const double& lower);
  47. double getPID(const double& currValue, const double& targetValue);
  48. double getPID(const double& e);
  49. void ResetD();
  50. void ResetI();
  51. std::string ToString();
  52. std::string ToStringHeader();
  53. private:
  54. double kp;
  55. double ki;
  56. double kd;
  57. double kp_v;
  58. double ki_v;
  59. double kd_v;
  60. double pid_v;
  61. double pid_lim;
  62. double upper_limit;
  63. double lower_limit;
  64. bool bEnableLimit;
  65. double accumErr;
  66. double prevErr;
  67. bool bResetD;
  68. bool bResetI;
  69. };
  70. class LowpassFilter
  71. {
  72. public:
  73. LowpassFilter();
  74. virtual ~LowpassFilter();
  75. LowpassFilter(const int& filterOrder, const double& sampleFreq, const double& cutOffFreq);
  76. void Init(const int& filterOrder, const double& sampleFreq, const double& cutOffFreq);
  77. double getFilter(const double& value);
  78. private:
  79. int m;
  80. double sampleF;
  81. double cutOffF;
  82. double A ;
  83. double d1 ;
  84. double d2 ;
  85. double w0 ;
  86. double w1 ;
  87. double w2 ;
  88. };
  89. }
  90. #endif