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