unit_converter.h
Go to the documentation of this file.
1 
2 #ifndef CANOPEN_MOTOR_NODE_UNIT_CONVERTER_H_
3 #define CANOPEN_MOTOR_NODE_UNIT_CONVERTER_H_
4 
5 #include <string>
6 #include <list>
7 #include <boost/function.hpp>
8 #include <boost/shared_ptr.hpp>
9 #include "muParser.h"
10 #include <boost/math/special_functions/fpclassify.hpp> // for isnan
11 
12 namespace canopen {
14 public:
15  typedef boost::function<double * (const std::string &) > get_var_func_type __attribute__((deprecated));
16  typedef boost::function<double * (const std::string &) > GetVarFuncType;
17 
18  UnitConverter(const std::string &expression, GetVarFuncType var_func)
19  : var_func_(var_func)
20  {
21  parser_.SetVarFactory(UnitConverter::createVariable, this);
22 
23  parser_.DefineConst("pi", M_PI);
24  parser_.DefineConst("nan", std::numeric_limits<double>::quiet_NaN());
25 
26  parser_.DefineFun("rad2deg", UnitConverter::rad2deg);
27  parser_.DefineFun("deg2rad", UnitConverter::deg2rad);
28  parser_.DefineFun("norm", UnitConverter::norm);
29  parser_.DefineFun("smooth", UnitConverter::smooth);
30  parser_.DefineFun("avg", UnitConverter::avg);
31 
32  parser_.SetExpr(expression);
33  }
34 
35  void reset(){
36  for(variable_ptr_list::iterator it = var_list_.begin(); it != var_list_.end(); ++it){
37  **it = std::numeric_limits<double>::quiet_NaN();
38  }
39  }
40  double evaluate() { int num; return parser_.Eval(num)[0]; }
41 private:
43  typedef std::list<variable_ptr> variable_ptr_list;
44 
45  static double* createVariable(const char *name, void * userdata) {
46  UnitConverter * uc = static_cast<UnitConverter*>(userdata);
47  double *p = uc->var_func_ ? uc->var_func_(name) : 0;
48  if(!p){
49  p = new double(std::numeric_limits<double>::quiet_NaN());
50  uc->var_list_.push_back(variable_ptr(p));
51  }
52  return p;
53  }
54  variable_ptr_list var_list_;
55  GetVarFuncType var_func_;
56 
57  mu::Parser parser_;
58 
59  static double rad2deg(double r){
60  return r*180.0/M_PI;
61  }
62  static double deg2rad(double d){
63  return d*M_PI/180.0;
64  }
65  static double norm(double val, double min, double max){
66  while(val >= max) val -= (max-min);
67  while(val < min) val += (max-min);
68  return val;
69  }
70  static double smooth(double val, double old_val, double alpha){
71  if(boost::math::isnan(val)) return 0;
72  if(boost::math::isnan(old_val)) return val;
73  return alpha*val + (1.0-alpha)*old_val;
74  }
75  static double avg(const double *vals, int num)
76  {
77  double s = 0.0;
78  int i=0;
79  for (; i<num; ++i){
80  const double &val = vals[i];
81  if(boost::math::isnan(val)) break;
82  s += val;
83  }
84  return s / double(i+1);
85  }
86 };
87 
88 }
89 
90 #endif /* CANOPEN_MOTOR_NODE_UNIT_CONVERTER_H_ */
std::list< variable_ptr > variable_ptr_list
static double deg2rad(double d)
boost::function< double *(const std::string &) > GetVarFuncType
XmlRpcServer s
GetVarFuncType var_func_
static double * createVariable(const char *name, void *userdata)
static double smooth(double val, double old_val, double alpha)
static double rad2deg(double r)
boost::shared_ptr< double > variable_ptr
UnitConverter(const std::string &expression, GetVarFuncType var_func)
static double avg(const double *vals, int num)
static double norm(double val, double min, double max)
boost::function< double *(const std::string &) > get_var_func_type __attribute__((deprecated))
variable_ptr_list var_list_


canopen_motor_node
Author(s): Mathias Lüdtke
autogenerated on Sat May 4 2019 02:40:47