YARP
Yet Another Robot Platform
ImplementPidControl.cpp
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 
8 #include <yarp/os/LogStream.h>
10 
11 #include <cmath>
12 
13 using namespace yarp::dev;
14 using namespace yarp::os;
15 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
16 
19 helper(nullptr),
20 doubleBuffManager(nullptr),
21 pidBuffManager(nullptr)
22 {
23  iPid= dynamic_cast<IPidControlRaw *> (y);
24 }
25 
27 {
28  uninitialize();
29 }
30 
31 bool ImplementPidControl:: initialize (int size, const int *amap, const double *enc, const double *zos, const double* newtons, const double* amps, const double* dutys)
32 {
33  if (helper != nullptr) {
34  return false;
35  }
36 
37  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos,newtons,amps,nullptr,dutys));
38  yAssert (helper != nullptr);
39 
41  yAssert (doubleBuffManager != nullptr);
42 
44  yAssert (pidBuffManager != nullptr);
45 
46  return true;
47 }
48 
54 {
55  if (helper!=nullptr)
56  {
57  delete castToMapper(helper);
58  helper=nullptr;
59  }
60 
62  {
63  delete doubleBuffManager;
64  doubleBuffManager=nullptr;
65  }
66 
67  if(pidBuffManager)
68  {
69  delete pidBuffManager;
70  pidBuffManager=nullptr;
71  }
72 
73  return true;
74 }
75 
76 bool ImplementPidControl::setPid(const PidControlTypeEnum& pidtype, int j, const Pid &pid)
77 {
79  Pid pid_machine;
80  int k;
82  cb_helper->convert_pid_to_machine(pidtype, pid, j, pid_machine, k);
83  return iPid->setPidRaw(pidtype, k, pid_machine);
84 }
85 
86 bool ImplementPidControl::setPids(const PidControlTypeEnum& pidtype, const Pid *pids)
87 {
89  int nj= cb_helper->axes();
91  for(int j=0;j<nj;j++)
92  {
93  Pid pid_machine;
94  int k;
95  cb_helper->convert_pid_to_machine(pidtype, pids[j], j, pid_machine, k);
96  buffValues[k] = pid_machine;
97  }
98 
99 
100  bool ret = iPid->setPidsRaw(pidtype, buffValues.getData());
101  pidBuffManager->releaseBuffer(buffValues);
102  return ret;
103 }
104 
105 bool ImplementPidControl::setPidReference(const PidControlTypeEnum& pidtype, int j, double ref)
106 {
108  int k=0;
109  double raw;
110  ControlBoardHelper* cb_helper = castToMapper(helper);
111  cb_helper->convert_pidunits_to_machine(pidtype,ref,j,raw,k);
112  return iPid->setPidReferenceRaw(pidtype, k, raw);
113 }
114 
115 bool ImplementPidControl::setPidReferences(const PidControlTypeEnum& pidtype, const double *refs)
116 {
117  ControlBoardHelper* cb_helper = castToMapper(helper);
119  cb_helper->convert_pidunits_to_machine(pidtype,refs,buffValues.getData());
120  bool ret = iPid->setPidReferencesRaw(pidtype, buffValues.getData());
121  doubleBuffManager->releaseBuffer(buffValues);
122  return ret;
123 }
124 
125 bool ImplementPidControl::setPidErrorLimit(const PidControlTypeEnum& pidtype, int j, double limit)
126 {
128  int k;
129  double raw;
130  ControlBoardHelper* cb_helper = castToMapper(helper);
131  cb_helper->convert_pidunits_to_machine(pidtype,limit,j,raw,k);
132  return iPid->setPidErrorLimitRaw(pidtype, k, raw);
133 }
134 
135 bool ImplementPidControl::setPidErrorLimits(const PidControlTypeEnum& pidtype, const double *limits)
136 {
137  ControlBoardHelper* cb_helper = castToMapper(helper);
139  cb_helper->convert_pidunits_to_machine(pidtype,limits,buffValues.getData());
140  bool ret = iPid->setPidErrorLimitsRaw(pidtype, buffValues.getData());
141  doubleBuffManager->releaseBuffer(buffValues);
142  return ret;
143 }
144 
145 
146 bool ImplementPidControl::getPidError(const PidControlTypeEnum& pidtype, int j, double *err)
147 {
149  int k;
150  double raw;
151  ControlBoardHelper* cb_helper = castToMapper(helper);
152  k=castToMapper(helper)->toHw(j);
153 
154  bool ret=iPid->getPidErrorRaw(pidtype, k, &raw);
155 
156  cb_helper->convert_pidunits_to_user(pidtype,raw,err,k);
157  return ret;
158 }
159 
160 bool ImplementPidControl::getPidErrors(const PidControlTypeEnum& pidtype, double *errs)
161 {
162  bool ret;
163  ControlBoardHelper* cb_helper = castToMapper(helper);
165  ret=iPid->getPidErrorsRaw(pidtype, buffValues.getData());
166  cb_helper->convert_pidunits_to_user(pidtype,buffValues.getData(),errs);
167  doubleBuffManager->releaseBuffer(buffValues);
168  return ret;
169 }
170 
171 bool ImplementPidControl::getPidOutput(const PidControlTypeEnum& pidtype, int j, double *out)
172 {
174  bool ret;
175  int k_raw;
176  double raw;
177  k_raw = castToMapper(helper)->toHw(j);
178  ret = iPid->getPidOutputRaw(pidtype, k_raw, &raw);
179  if (ret)
180  {
181  ControlBoardHelper* cb_helper = castToMapper(helper);
182  double output_conversion_units_user2raw = cb_helper->get_pidoutput_conversion_factor_user2raw(pidtype, j);
183  *out = raw / output_conversion_units_user2raw;
184  return true;
185  }
186  return false;
187 }
188 
189 bool ImplementPidControl::getPidOutputs(const PidControlTypeEnum& pidtype, double *outs)
190 {
191  ControlBoardHelper* cb_helper = castToMapper(helper);
192  int nj = cb_helper->axes();
194  bool ret = iPid->getPidOutputsRaw(pidtype, buffValues.getData());
195  if (ret)
196  {
197  castToMapper(cb_helper)->toUser(buffValues.getData(), outs);
198  for (int j = 0; j < nj; j++)
199  {
200  double output_conversion_units_user2raw = cb_helper->get_pidoutput_conversion_factor_user2raw(pidtype, j);
201  outs[j] = outs[j] / output_conversion_units_user2raw;
202  }
203  }
204  doubleBuffManager->releaseBuffer(buffValues);
205  return ret;
206 }
207 
208 bool ImplementPidControl::getPid(const PidControlTypeEnum& pidtype, int j, Pid *pid)
209 {
211  ControlBoardHelper* cb_helper = castToMapper(helper);
212  int k_raw;
213  k_raw=cb_helper->toHw(j);
214  Pid rawPid;
215  bool ret = iPid->getPidRaw(pidtype, k_raw, &rawPid);
216  if (ret)
217  {
218  cb_helper->convert_pid_to_user(pidtype, rawPid, k_raw, *pid, j);
219  return true;
220  }
221  return false;
222 }
223 
225 {
227  if(!iPid->getPidsRaw(pidtype, buffValues.getData()))
228  {
229  pidBuffManager->releaseBuffer(buffValues);
230  return false;
231  }
232 
233  ControlBoardHelper* cb_helper = castToMapper(helper);
234  int nj=cb_helper->axes();
235 
236  for (int k_raw = 0; k_raw < nj; k_raw++)
237  {
238  int j_usr;
239  Pid outpid;
240  cb_helper->convert_pid_to_user(pidtype, buffValues[k_raw], k_raw, outpid, j_usr);
241  pids[j_usr] = outpid;
242  }
243  pidBuffManager->releaseBuffer(buffValues);
244  return true;
245 }
246 
247 bool ImplementPidControl::getPidReference(const PidControlTypeEnum& pidtype, int j, double *ref)
248 {
250  bool ret;
251  int k;
252  double raw;
253  ControlBoardHelper* cb_helper = castToMapper(helper);
254  k=castToMapper(helper)->toHw(j);
255 
256  ret=iPid->getPidReferenceRaw(pidtype, k, &raw);
257 
258  cb_helper->convert_pidunits_to_user(pidtype,raw,ref,k);
259  return ret;
260 }
261 
263 {
264  bool ret;
265  ControlBoardHelper* cb_helper = castToMapper(helper);
267  ret=iPid->getPidReferencesRaw(pidtype, buffValues.getData());
268 
269  cb_helper->convert_pidunits_to_user(pidtype,buffValues.getData(),refs);
270  doubleBuffManager->releaseBuffer(buffValues);
271  return ret;
272 }
273 
274 bool ImplementPidControl::getPidErrorLimit(const PidControlTypeEnum& pidtype, int j, double *ref)
275 {
277  bool ret;
278  int k;
279  double raw;
280  ControlBoardHelper* cb_helper = castToMapper(helper);
281  k=castToMapper(helper)->toHw(j);
282 
283  ret=iPid->getPidErrorLimitRaw(pidtype, k, &raw);
284 
285  cb_helper->convert_pidunits_to_user(pidtype,raw,ref,k);
286  return ret;
287 }
288 
290 {
291  bool ret;
292  ControlBoardHelper* cb_helper = castToMapper(helper);
294  ret=iPid->getPidErrorLimitsRaw(pidtype, buffValues.getData());
295 
296  cb_helper->convert_pidunits_to_user(pidtype,buffValues.getData(),refs);
297  return ret;
298 }
299 
301 {
303  int k=0;
304  k=castToMapper(helper)->toHw(j);
305 
306  return iPid->resetPidRaw(pidtype, k);
307 }
308 
310 {
312  int k=0;
313  k=castToMapper(helper)->toHw(j);
314 
315  return iPid->enablePidRaw(pidtype, k);
316 }
317 
319 {
321  int k=0;
322  k=castToMapper(helper)->toHw(j);
323 
324  return iPid->disablePidRaw(pidtype, k);
325 }
326 
327 bool ImplementPidControl::setPidOffset(const PidControlTypeEnum& pidtype, int j, double off)
328 {
330  int k = 0;
331  double rawoff;
332  ControlBoardHelper* cb_helper = castToMapper(helper);
333  double output_conversion_units_user2raw = cb_helper->get_pidoutput_conversion_factor_user2raw(pidtype,j);
334  rawoff = off * output_conversion_units_user2raw;
335  return iPid->setPidOffsetRaw(pidtype, k, rawoff);
336 }
337 
338 bool ImplementPidControl::isPidEnabled(const PidControlTypeEnum& pidtype, int j, bool* enabled)
339 {
341  int k=0;
342  k=castToMapper(helper)->toHw(j);
343 
344  return iPid->isPidEnabledRaw(pidtype, k, enabled);
345 }
346 
347 bool ImplementPidControl::setConversionUnits(const PidControlTypeEnum& pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units)
348 {
349  castToMapper(helper)->set_pid_conversion_units(pidtype, fbk_conv_units, out_conv_units);
350  return true;
351 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:294
void convert_pid_to_user(const yarp::dev::PidControlTypeEnum &pidtype, const Pid &in_raw, int j_raw, Pid &out_usr, int &k_usr)
void convert_pidunits_to_machine(const yarp::dev::PidControlTypeEnum &pidtype, double userval, int j, double &machineval, int &k)
void convert_pid_to_machine(const yarp::dev::PidControlTypeEnum &pidtype, const Pid &in_usr, int j_usr, Pid &out_raw, int &k_raw)
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 convert_pidunits_to_user(const yarp::dev::PidControlTypeEnum &pidtype, const double machineval, double *userval, int k)
Interface for a generic control board device implementing a PID controller.
Definition: IPidControl.h:29
virtual bool enablePidRaw(const PidControlTypeEnum &pidtype, int j)=0
Enable the pid computation for a joint.
virtual bool setPidRaw(const PidControlTypeEnum &pidtype, int j, const Pid &pid)=0
Set new pid value for a joint axis.
virtual bool getPidOutputRaw(const PidControlTypeEnum &pidtype, int j, double *out)=0
Get the output of the controller (e.g.
virtual bool setPidReferenceRaw(const PidControlTypeEnum &pidtype, int j, double ref)=0
Set the controller reference for a given axis.
virtual bool setPidOffsetRaw(const PidControlTypeEnum &pidtype, int j, double v)=0
Set an offset value on the ourput of pid controller.
virtual bool disablePidRaw(const PidControlTypeEnum &pidtype, int j)=0
Disable the pid computation for a joint.
virtual bool getPidsRaw(const PidControlTypeEnum &pidtype, Pid *pids)=0
Get current pid value for a specific joint.
virtual bool getPidReferencesRaw(const PidControlTypeEnum &pidtype, double *refs)=0
Get the current reference of all pid controllers.
virtual bool getPidErrorsRaw(const PidControlTypeEnum &pidtype, double *errs)=0
Get the error of all joints.
virtual bool getPidRaw(const PidControlTypeEnum &pidtype, int j, Pid *pid)=0
Get current pid value for a specific joint.
virtual bool getPidOutputsRaw(const PidControlTypeEnum &pidtype, double *outs)=0
Get the output of the controllers (e.g.
virtual bool setPidErrorLimitsRaw(const PidControlTypeEnum &pidtype, const double *limits)=0
Get the error limit for the controller on all joints.
virtual bool getPidErrorRaw(const PidControlTypeEnum &pidtype, int j, double *err)=0
Get the current error for a joint.
virtual bool getPidErrorLimitRaw(const PidControlTypeEnum &pidtype, int j, double *limit)=0
Get the error limit for the controller on a specific joint.
virtual bool isPidEnabledRaw(const PidControlTypeEnum &pidtype, int j, bool *enabled)=0
Get the current status (enabled/disabled) of the pid controller.
virtual bool getPidReferenceRaw(const PidControlTypeEnum &pidtype, int j, double *ref)=0
Get the current reference of the pid controller for a specific joint.
virtual bool getPidErrorLimitsRaw(const PidControlTypeEnum &pidtype, double *limits)=0
Get the error limit for all controllers.
virtual bool setPidErrorLimitRaw(const PidControlTypeEnum &pidtype, int j, double limit)=0
Set the error limit for the controller on a specific joint.
virtual bool setPidsRaw(const PidControlTypeEnum &pidtype, const Pid *pids)=0
Set new pid value on multiple axes.
virtual bool setPidReferencesRaw(const PidControlTypeEnum &pidtype, const double *refs)=0
Set the controller reference, multiple axes.
virtual bool resetPidRaw(const PidControlTypeEnum &pidtype, int j)=0
Reset the controller of a given joint, usually sets the current status of the joint as the reference ...
bool resetPid(const PidControlTypeEnum &pidtype, int j) override
Reset the controller of a given joint, usually sets the current status of the joint as the reference ...
bool enablePid(const PidControlTypeEnum &pidtype, int j) override
Enable the pid computation for a joint.
bool setPids(const PidControlTypeEnum &pidtype, const Pid *pids) override
Set new pid value on multiple axes.
bool isPidEnabled(const PidControlTypeEnum &pidtype, int j, bool *enabled) override
Get the current status (enabled/disabled) of the pid.
bool setPidErrorLimit(const PidControlTypeEnum &pidtype, int j, double limit) override
Set the error limit for the controller on a specifi joint.
bool uninitialize()
Clean up internal data and memory.
bool getPidReferences(const PidControlTypeEnum &pidtype, double *refs) override
Get the current reference of all pid controllers.
bool getPidError(const PidControlTypeEnum &pidtype, int j, double *err) override
Get the current error for a joint.
bool getPids(const PidControlTypeEnum &pidtype, Pid *pids) override
Get current pid value for a specific joint.
bool getPid(const PidControlTypeEnum &pidtype, int j, Pid *pid) override
Get current pid value for a specific joint.
bool getPidErrorLimit(const PidControlTypeEnum &pidtype, int j, double *ref) override
Get the error limit for the controller on a specific joint.
bool setPidOffset(const PidControlTypeEnum &pidtype, int j, double v) override
Set offset value for a given controller.
bool initialize(int size, const int *amap, const double *enc, const double *zos, const double *newtons, const double *amps, const double *dutys)
Initialize the internal data and alloc memory.
ImplementPidControl(yarp::dev::IPidControlRaw *y)
bool getPidErrorLimits(const PidControlTypeEnum &pidtype, double *refs) override
Get the error limit for all controllers.
bool getPidErrors(const PidControlTypeEnum &pidtype, double *errs) override
Get the error of all joints.
bool getPidReference(const PidControlTypeEnum &pidtype, int j, double *ref) override
Get the current reference of the pid controller for a specific joint.
bool disablePid(const PidControlTypeEnum &pidtype, int j) override
Disable the pid computation for a joint.
bool getPidOutput(const PidControlTypeEnum &pidtype, int j, double *out) override
Get the output of the controller (e.g.
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
bool setPidErrorLimits(const PidControlTypeEnum &pidtype, const double *limits) override
Get the error limit for the controller on all joints.
yarp::dev::impl::FixedSizeBuffersManager< yarp::dev::Pid > * pidBuffManager
bool setPidReference(const PidControlTypeEnum &pidtype, int j, double ref) override
Set the controller reference for a given axis.
bool setPid(const PidControlTypeEnum &pidtype, int j, const Pid &pid) override
Set new pid value for a joint axis.
bool setPidReferences(const PidControlTypeEnum &pidtype, const double *refs) override
Set the controller reference, multiple axes.
bool getPidOutputs(const PidControlTypeEnum &pidtype, double *outs) override
Get the output of the controllers (e.g.
bool setConversionUnits(const PidControlTypeEnum &pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units)
Contains the parameters for a PID.
Buffer contains info about a buffer of type T and it is used to exchange information with yarp::dev::...
T * getData()
Return the data pointer.
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
An interface for the device drivers.
PidOutputUnitsEnum
Definition: PidEnums.h:34
PidControlTypeEnum
Definition: PidEnums.h:18
PidFeedbackUnitsEnum
Definition: PidEnums.h:28
An interface to the operating system, including Port based communication.