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
yarp::dev::ControlBoardHelper::convert_pidunits_to_machine
void convert_pidunits_to_machine(const yarp::dev::PidControlTypeEnum &pidtype, double userval, int j, double &machineval, int &k)
Definition: ControlBoardHelper.cpp:858
yarp::dev::ControlBoardHelper::velE2A
void velE2A(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:414
yarp::dev::ControlBoardHelper::toHw
int toHw(int axis)
Definition: ControlBoardHelper.cpp:245
yarp::dev::ControlBoardHelper::PrivateUnitsHandler
Definition: ControlBoardHelper.cpp:30
yarp::dev::ControlBoardHelper::convert_pid_to_machine
void convert_pid_to_machine(const yarp::dev::PidControlTypeEnum &pidtype, const Pid &in_usr, int j_usr, Pid &out_raw, int &k_raw)
Definition: ControlBoardHelper.cpp:834
yarp::dev::ControlBoardHelper::posE2A
void posE2A(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:290
yarp::dev::ControlBoardHelper::bemf_user2raw
double bemf_user2raw(double bemf_user, int j)
Definition: ControlBoardHelper.cpp:750
yarp::dev::ControlBoardHelper::get_pidoutput_conversion_factor_user2raw
double get_pidoutput_conversion_factor_user2raw(const yarp::dev::PidControlTypeEnum &pidtype, int j)
Definition: ControlBoardHelper.cpp:767
yarp::dev::ControlBoardHelper::toUser
int toUser(int axis)
Definition: ControlBoardHelper.cpp:248
yarp::dev::ControlBoardHelper::checkAxesIds
bool checkAxesIds(const int n_axes, const int *axesList)
Definition: ControlBoardHelper.cpp:224
yarp::dev::ControlBoardHelper::toUser
void toUser(const int *hwData, int *user)
yarp::dev::ControlBoardHelper::velA2E_abs
void velA2E_abs(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:408
t
float t
Definition: FfmpegWriter.cpp:74
yarp::dev::ControlBoardHelper::operator=
ControlBoardHelper & operator=(const ControlBoardHelper &other)
Definition: ControlBoardHelper.cpp:208
yarp::dev::ControlBoardHelper::dutycycle2PWM
void dutycycle2PWM(double dutycycle, int j, double &pwm, int &k)
Definition: ControlBoardHelper.cpp:680
yarp::dev::ControlBoardHelper::ampereS2A
void ampereS2A(const double *sens, double *ampere)
Definition: ControlBoardHelper.cpp:606
yarp::dev::PidOutputUnitsEnum
PidOutputUnitsEnum
Definition: PidEnums.h:37
yarp::dev::ControlBoardHelper::ampereA2S
void ampereA2S(double ampere, int j, double &sens, int &k)
Definition: ControlBoardHelper.cpp:582
PidEnums.h
Log.h
yarp::dev::ControlBoardHelper::velE2A_abs
void velE2A_abs(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:420
yarp::dev::ControlBoardHelper::checkAxisId
bool checkAxisId(int id)
Definition: ControlBoardHelper.cpp:214
yarp::dev::ControlBoardHelper::ControlBoardHelper
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)
Definition: ControlBoardHelper.cpp:164
yarp::dev::ControlBoardHelper::posA2E
void posA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:279
yarp::dev::PidControlTypeEnum
PidControlTypeEnum
Definition: PidEnums.h:21
yarp::dev::ControlBoardHelper::voltageS2V
void voltageS2V(const double *sens, double *voltage)
Definition: ControlBoardHelper.cpp:655
yarp::dev::ControlBoardHelper::impS2N
void impS2N(const double *sens, double *newtons)
Definition: ControlBoardHelper.cpp:373
yarp::dev::ControlBoardHelper::voltageV2S
void voltageV2S(double voltage, int j, double &sens, int &k)
Definition: ControlBoardHelper.cpp:631
yarp::dev::ControlBoardHelper::ktau_user2raw
double ktau_user2raw(double ktau_user, int j)
Definition: ControlBoardHelper.cpp:755
yarp::dev::ControlBoardHelper::impN2S
void impN2S(double newtons, int j, double &sens, int &k)
Definition: ControlBoardHelper.cpp:304
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
yarp::dev::ControlBoardHelper::accA2E
void accA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:426
yarp::dev::ControlBoardHelper::trqN2S
void trqN2S(double newtons, int j, double &sens, int &k)
Definition: ControlBoardHelper.cpp:326
yarp::dev::ControlBoardHelper::velA2E
void velA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:397
checkAndDestroy
void checkAndDestroy(T *&p)
Definition: ControlBoardHelper.h:45
yarp::dev::ControlBoardHelper::accE2A_abs
void accE2A_abs(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:441
yarp::dev::PidFeedbackUnitsEnum
PidFeedbackUnitsEnum
Definition: PidEnums.h:31
yarp::dev::ControlBoardHelper::accA2E_abs
void accA2E_abs(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:431
ControlBoardPid.h
define control board standard interfaces
yarp::dev::ControlBoardHelper::~ControlBoardHelper
~ControlBoardHelper()
Definition: ControlBoardHelper.cpp:198
castToMapper
yarp::dev::ControlBoardHelper * castToMapper(void *p)
Definition: ControlBoardHelper.h:180
yarp::dev::ControlBoardHelper
Definition: ControlBoardHelper.h:60
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::dev::ControlBoardHelper::convert_pid_to_user
void convert_pid_to_user(const yarp::dev::PidControlTypeEnum &pidtype, const Pid &in_raw, int j_raw, Pid &out_usr, int &k_usr)
Definition: ControlBoardHelper.cpp:802
yarp::dev::ControlBoardHelper::get_pidfeedback_conversion_factor_user2raw
double get_pidfeedback_conversion_factor_user2raw(const yarp::dev::PidControlTypeEnum &pidtype, int j)
Definition: ControlBoardHelper.cpp:783
yarp::dev::ControlBoardHelper::accE2A
void accE2A(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:436
yarp::dev::ControlBoardHelper::trqS2N
void trqS2N(const double *sens, double *newtons)
Definition: ControlBoardHelper.cpp:350
yarp::dev::Pid
Contains the parameters for a PID.
Definition: ControlBoardPid.h:29
yarp::dev::ControlBoardHelper::axes
int axes()
Definition: ControlBoardHelper.cpp:762
yarp::dev::ControlBoardHelper::set_pid_conversion_units
void set_pid_conversion_units(const PidControlTypeEnum &pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units)
Definition: ControlBoardHelper.cpp:958
yarp::dev::ControlBoardHelper::bemf_raw2user
void bemf_raw2user(double bemf_raw, int k_raw, double &bemf_user, int &j_user)
Definition: ControlBoardHelper.cpp:738
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::dev::ControlBoardHelper::ktau_raw2user
void ktau_raw2user(double ktau_raw, int k_raw, double &ktau_user, int &j_user)
Definition: ControlBoardHelper.cpp:744
allocAndCheck
T * allocAndCheck(int size)
Definition: ControlBoardHelper.h:24
yarp::dev::ControlBoardHelper::PWM2dutycycle
void PWM2dutycycle(const double *pwm, double *dutycycle)
Definition: ControlBoardHelper.cpp:702
yarp::dev::ControlBoardHelper::convert_pidunits_to_user
void convert_pidunits_to_user(const yarp::dev::PidControlTypeEnum &pidtype, const double machineval, double *userval, int k)
Definition: ControlBoardHelper.cpp:908