YARP
Yet Another Robot Platform
ControlBoardHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_DEV_CONTROLBOARDHELPER_H
10 #define YARP_DEV_CONTROLBOARDHELPER_H
11 
13 #include <yarp/dev/PidEnums.h>
14 #include <yarp/os/Log.h>
15 
16 #include <cstring> // for memset
17 #include <type_traits>
18 
19  /*
20  * simple helper template to alloc memory.
21  */
22 template <class T,
23  std::enable_if_t<std::is_trivial<T>::value, int> = 0>
24 T* allocAndCheck(int size)
25 {
26  T* t = new T[size];
27  yAssert(t != 0);
28  memset(t, 0, sizeof(T) * size);
29  return t;
30 }
31 
32 template <class T,
33  std::enable_if_t<!std::is_trivial<T>::value, int> = 0>
34 T* allocAndCheck(int size)
35 {
36  T* t = new T[size];
37  yAssert(t != 0);
38  return t;
39 }
40 
41 /*
42 *
43 */
44 template <class T>
45 void checkAndDestroy(T* &p) {
46  if (p != 0) {
47  delete[] p;
48  p = 0;
49  }
50 }
51 
52 namespace yarp {
53  namespace dev {
54  class ControlBoardHelper;
55  }
56 }
57 
60 {
61 public:
62  ControlBoardHelper(int n, const int *aMap, const double *angToEncs = nullptr, const double *zs = nullptr, const double *newtons = nullptr, const double *amps = nullptr, const double *volts = nullptr, const double *dutycycles = nullptr, const double *kbemf = nullptr, const double *ktau = nullptr);
66 
67 public:
68  bool checkAxisId(int id);
69  bool checkAxesIds(const int n_axes, const int* axesList);
70  int toHw(int axis);
71  int toUser(int axis);
72  void toUser(const double *hwData, double *user);
73  void toUser(const int *hwData, int *user);
74  void toHw(const double *usr, double *hwData);
75  void toHw(const int *usr, int *hwData);
76 
77  //***************** position ******************//
78  void posA2E(double ang, int j, double &enc, int &k);
79  double posA2E(double ang, int j);
80  void posE2A(double enc, int j, double &ang, int &k);
81  double posE2A(double enc, int j);
82  void posA2E(const double *ang, double *enc);
83  void posE2A(const double *enc, double *ang);
84 
85  //***************** velocity ******************//
86  void velA2E(double ang, int j, double &enc, int &k);
87  double velA2E(double ang, int j);
88  void velA2E_abs(double ang, int j, double &enc, int &k);
89  void velE2A(double enc, int j, double &ang, int &k);
90  void velE2A_abs(double enc, int j, double &ang, int &k);
91  double velE2A(double enc, int j);
92  double velE2A_abs(double enc, int j);
93  void velA2E(const double *ang, double *enc);
94  void velA2E_abs(const double *ang, double *enc);
95  void velE2A(const double *enc, double *ang);
96  void velE2A_abs(const double *enc, double *ang);
97 
98  //***************** acceleration ******************//
99  void accA2E(double ang, int j, double &enc, int &k);
100  void accA2E_abs(double ang, int j, double &enc, int &k);
101  void accE2A(double enc, int j, double &ang, int &k);
102  void accE2A_abs(double enc, int j, double &ang, int &k);
103  double accE2A(double enc, int j);
104  double accE2A_abs(double enc, int j);
105  void accA2E(const double *ang, double *enc);
106  void accA2E_abs(const double *ang, double *enc);
107  void accE2A(const double *enc, double *ang);
108  void accE2A_abs(const double *enc, double *ang);
109 
110  //***************** torque ******************//
111  void trqN2S(double newtons, int j, double &sens, int &k);
112  double trqN2S(double newtons, int j);
113  void trqN2S(const double *newtons, double *sens);
114  void trqS2N(const double *sens, double *newtons);
115  void trqS2N(double sens, int j, double &newton, int &k);
116  double trqS2N(double sens, int j);
117 
118  //***************** impedance ******************//
119  void impN2S(double newtons, int j, double &sens, int &k);
120  double impN2S(double newtons, int j);
121  void impN2S(const double *newtons, double *sens);
122  void impS2N(const double *sens, double *newtons);
123  void impS2N(double sens, int j, double &newton, int &k);
124  double impS2N(double sens, int j);
125 
126  //***************** current ******************//
127  void ampereA2S(double ampere, int j, double &sens, int &k);
128  double ampereA2S(double ampere, int j);
129  void ampereA2S(const double *ampere, double *sens);
130  void ampereS2A(const double *sens, double *ampere);
131  void ampereS2A(double sens, int j, double &ampere, int &k);
132  double ampereS2A(double sens, int j);
133 
134  //***************** voltage ******************//
135  void voltageV2S(double voltage, int j, double &sens, int &k);
136  double voltageV2S(double voltage, int j);
137  void voltageV2S(const double *voltage, double *sens);
138  void voltageS2V(const double *sens, double *voltage);
139  void voltageS2V(double sens, int j, double &voltage, int &k);
140  double voltageS2V(double sens, int j);
141 
142  //***************** dutycycle ******************//
143  void dutycycle2PWM(double dutycycle, int j, double &pwm, int &k);
144  double dutycycle2PWM(double dutycycle, int j);
145  void dutycycle2PWM(const double *dutycycle, double *sens);
146  void PWM2dutycycle(const double *pwm, double *dutycycle);
147  void PWM2dutycycle(double pwm_raw, int k_raw, double &dutycycle, int &j);
148  double PWM2dutycycle(double pwm_raw, int k_raw);
149 
150  // *******************************************//
151  double bemf_user2raw(double bemf_user, int j);
152  double ktau_user2raw(double ktau_user, int j);
153  void bemf_user2raw(double bemf_user, int j, double &bemf_raw, int &k);
154  void ktau_user2raw(double ktau_user, int j, double &ktau_raw, int &k);
155  void bemf_raw2user(double bemf_raw, int k_raw, double &bemf_user, int &j_user);
156  void ktau_raw2user(double ktau_raw, int k_raw, double &ktau_user, int &j_user);
157 
158  int axes();
159 
160  // *******************************************//
161 public:
162  void convert_pidunits_to_machine(const yarp::dev::PidControlTypeEnum& pidtype, double userval, int j, double &machineval, int &k);
163  void convert_pidunits_to_machine(const yarp::dev::PidControlTypeEnum& pidtype, const double* userval, double* machineval);
164  void convert_pidunits_to_user(const yarp::dev::PidControlTypeEnum& pidtype, const double machineval, double* userval, int k);
165  void convert_pidunits_to_user(const yarp::dev::PidControlTypeEnum& pidtype, const double* machineval, double* userval);
166 
167  void convert_pid_to_user(const yarp::dev::PidControlTypeEnum& pidtype, const Pid &in_raw, int j_raw, Pid &out_usr, int &k_usr);
168  void convert_pid_to_machine(const yarp::dev::PidControlTypeEnum& pidtype, const Pid &in_usr, int j_usr, Pid &out_raw, int &k_raw);
169  Pid convert_pid_to_machine(const yarp::dev::PidControlTypeEnum& pidtype, const Pid &in_usr, int j_usr);
170 
171  void set_pid_conversion_units(const PidControlTypeEnum& pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units);
174 
175 private:
176  class PrivateUnitsHandler;
177  PrivateUnitsHandler* mPriv;
178 };
179 
181 { return static_cast<yarp::dev::ControlBoardHelper *>(p); }
182 
183 #endif // YARP_DEV_CONTROLBOARDHELPER_H
void checkAndDestroy(T *&p)
yarp::dev::ControlBoardHelper * castToMapper(void *p)
T * allocAndCheck(int size)
define control board standard interfaces
float t
#define yAssert(x)
Definition: Log.h:297
double get_pidfeedback_conversion_factor_user2raw(const yarp::dev::PidControlTypeEnum &pidtype, int j)
void accE2A(double enc, int j, double &ang, int &k)
void convert_pid_to_user(const yarp::dev::PidControlTypeEnum &pidtype, const Pid &in_raw, int j_raw, Pid &out_usr, int &k_usr)
void ktau_raw2user(double ktau_raw, int k_raw, double &ktau_user, int &j_user)
void trqN2S(double newtons, int j, double &sens, int &k)
void velE2A_abs(double enc, int j, double &ang, int &k)
void convert_pidunits_to_machine(const yarp::dev::PidControlTypeEnum &pidtype, double userval, int j, double &machineval, int &k)
void bemf_raw2user(double bemf_raw, int k_raw, double &bemf_user, int &j_user)
void voltageS2V(const double *sens, double *voltage)
void convert_pid_to_machine(const yarp::dev::PidControlTypeEnum &pidtype, const Pid &in_usr, int j_usr, Pid &out_raw, int &k_raw)
void ampereA2S(double ampere, int j, double &sens, int &k)
void impN2S(double newtons, int j, double &sens, int &k)
double ktau_user2raw(double ktau_user, int j)
void velE2A(double enc, int j, double &ang, int &k)
void velA2E(double ang, int j, double &enc, int &k)
void dutycycle2PWM(double dutycycle, int j, double &pwm, int &k)
void ampereS2A(const double *sens, double *ampere)
void posE2A(double enc, int j, double &ang, int &k)
void trqS2N(const double *sens, double *newtons)
double get_pidoutput_conversion_factor_user2raw(const yarp::dev::PidControlTypeEnum &pidtype, int j)
void set_pid_conversion_units(const PidControlTypeEnum &pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units)
void toUser(const int *hwData, int *user)
void voltageV2S(double voltage, int j, double &sens, int &k)
ControlBoardHelper & operator=(const ControlBoardHelper &other)
double bemf_user2raw(double bemf_user, int j)
void accA2E_abs(double ang, int j, double &enc, int &k)
void accA2E(double ang, int j, double &enc, int &k)
void accE2A_abs(double enc, int j, double &ang, int &k)
void velA2E_abs(double ang, int j, double &enc, int &k)
ControlBoardHelper(int n, const int *aMap, const double *angToEncs=nullptr, const double *zs=nullptr, const double *newtons=nullptr, const double *amps=nullptr, const double *volts=nullptr, const double *dutycycles=nullptr, const double *kbemf=nullptr, const double *ktau=nullptr)
void convert_pidunits_to_user(const yarp::dev::PidControlTypeEnum &pidtype, const double machineval, double *userval, int k)
void impS2N(const double *sens, double *newtons)
void PWM2dutycycle(const double *pwm, double *dutycycle)
bool checkAxesIds(const int n_axes, const int *axesList)
void posA2E(double ang, int j, double &enc, int &k)
Contains the parameters for a PID.
PidOutputUnitsEnum
Definition: PidEnums.h:37
PidControlTypeEnum
Definition: PidEnums.h:21
PidFeedbackUnitsEnum
Definition: PidEnums.h:31
The main, catch-all namespace for YARP.
Definition: environment.h:18
#define YARP_dev_API
Definition: api.h:19