YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
13using namespace yarp::dev;
14using namespace yarp::os;
15#define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
16
19helper(nullptr),
20doubleBuffManager(nullptr),
21pidBuffManager(nullptr)
22{
23 iPid= dynamic_cast<IPidControlRaw *> (y);
24}
25
30
31bool 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
68 {
69 delete pidBuffManager;
70 pidBuffManager=nullptr;
71 }
72
73 return true;
74}
75
77{
80 int k;
82 cb_helper->convert_pid_to_machine(pidtype, pid, j, pid_machine, k);
84}
85
87{
89 int nj= cb_helper->axes();
91 for(int j=0;j<nj;j++)
92 {
94 int k;
95 cb_helper->convert_pid_to_machine(pidtype, pids[j], j, pid_machine, k);
97 }
98
99
100 bool ret = iPid->setPidsRaw(pidtype, buffValues.getData());
102 return ret;
103}
104
106{
108 int k=0;
109 double raw;
111 cb_helper->convert_pidunits_to_machine(pidtype,ref,j,raw,k);
112 return iPid->setPidReferenceRaw(pidtype, k, raw);
113}
114
124
126{
128 int k;
129 double raw;
131 cb_helper->convert_pidunits_to_machine(pidtype,limit,j,raw,k);
132 return iPid->setPidErrorLimitRaw(pidtype, k, raw);
133}
134
144
145
147{
149 int k;
150 double raw;
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
170
172{
174 bool ret;
175 int k_raw;
176 double raw;
179 if (ret)
180 {
182 double output_conversion_units_user2raw = cb_helper->get_pidoutput_conversion_factor_user2raw(pidtype, j);
184 return true;
185 }
186 return false;
187}
188
190{
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 }
205 return ret;
206}
207
209{
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 {
230 return false;
231 }
232
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 }
244 return true;
245}
246
248{
250 bool ret;
251 int k;
252 double raw;
255
257
258 cb_helper->convert_pidunits_to_user(pidtype,raw,ref,k);
259 return ret;
260}
261
273
275{
277 bool ret;
278 int k;
279 double raw;
282
284
285 cb_helper->convert_pidunits_to_user(pidtype,raw,ref,k);
286 return ret;
287}
288
299
301{
303 int k=0;
305
306 return iPid->resetPidRaw(pidtype, k);
307}
308
310{
312 int k=0;
314
315 return iPid->enablePidRaw(pidtype, k);
316}
317
319{
321 int k=0;
323
324 return iPid->disablePidRaw(pidtype, k);
325}
326
328{
330 int k = 0;
331 double rawoff;
333 double output_conversion_units_user2raw = cb_helper->get_pidoutput_conversion_factor_user2raw(pidtype,j);
336}
337
339{
341 int k=0;
343
344 return iPid->isPidEnabledRaw(pidtype, k, enabled);
345}
346
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition Log.h:388
void set_pid_conversion_units(const PidControlTypeEnum &pidtype, const PidFeedbackUnitsEnum fbk_conv_units, const PidOutputUnitsEnum out_conv_units)
Interface for a generic control board device implementing a PID controller.
Definition IPidControl.h:27
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::...
A manager of fixed size buffers in multi-thread environment.
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
A mini-server for performing network communication in the background.
For streams capable of holding different kinds of content, check what they actually have.
PidOutputUnitsEnum
Definition PidEnums.h:31
PidControlTypeEnum
Definition PidEnums.h:15
PidFeedbackUnitsEnum
Definition PidEnums.h:25
An interface to the operating system, including Port based communication.