YARP
Yet Another Robot Platform
ImplementImpedanceControl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include <cstdio>
11 
14 #include <yarp/os/LogStream.h>
15 #include <cmath>
16 
17 using namespace yarp::dev;
18 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
19 
22 {
23  iImpedanceRaw=r;
24  helper=nullptr;
25 }
26 
27 bool ImplementImpedanceControl::initialize(int size, const int *amap, const double *enc, const double *zos, const double *nw)
28 {
29  if (helper!=nullptr)
30  return false;
31 
32  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos, nw));
33  yAssert (helper != nullptr);
34 
35  return true;
36 }
37 
39 {
40  uninitialize();
41 }
42 
44 {
45  if (helper!=nullptr)
46  {
47  delete castToMapper(helper);
48  helper=nullptr;
49  }
50 
51  return true;
52 }
53 
55 {
56  return iImpedanceRaw->getAxes(axes);
57 }
58 
59 bool ImplementImpedanceControl::setImpedance(int j, double stiffness, double damping)
60 {
62  int k;
63  double stiff;
64  double damp;
65  castToMapper(helper)->impN2S(stiffness,j,stiff,k);
66  castToMapper(helper)->impN2S(damping,j,damp,k);
67  return iImpedanceRaw->setImpedanceRaw(k, stiff, damp);
68 }
69 
70 bool ImplementImpedanceControl::getImpedance(int j, double *stiffness, double *damping)
71 {
73  int k;
74  k=castToMapper(helper)->toHw(j);
75  bool ret=iImpedanceRaw->getImpedanceRaw(k, stiffness, damping);
76  *stiffness = (castToMapper(helper)->impS2N(*stiffness, k));
77  *damping = (castToMapper(helper)->impS2N(*damping, k));
78  //prevent negative stiffness
79  *stiffness = fabs (*stiffness);
80  *damping = fabs (*damping);
81  return ret;
82 }
83 
85 {
87  int k;
88  double off;
89  castToMapper(helper)->trqN2S(offset,j,off,k);
90  return iImpedanceRaw->setImpedanceOffsetRaw(k, off);
91 }
92 
94 {
96  int k;
97  k=castToMapper(helper)->toHw(j);
98  bool ret = iImpedanceRaw->getImpedanceOffsetRaw(k, offset);
99  *offset = (castToMapper(helper)->trqS2N(*offset,k));
100  return ret;
101 }
102 
103 bool ImplementImpedanceControl::getCurrentImpedanceLimit(int j, double *min_stiff, double *max_stiff, double *min_damp, double *max_damp)
104 {
106  int k;
107  k=castToMapper(helper)->toHw(j);
108  return iImpedanceRaw->getCurrentImpedanceLimitRaw(k, min_stiff, max_stiff, min_damp, max_damp);
109 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:297
void trqN2S(double newtons, int j, double &sens, int &k)
void impN2S(double newtons, int j, double &sens, int &k)
void trqS2N(const double *sens, double *newtons)
void impS2N(const double *sens, double *newtons)
Interface for control boards implementing impedance control.
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
virtual bool getImpedanceOffsetRaw(int j, double *offset)=0
Get current force Offset for a specific joint.
virtual bool setImpedanceOffsetRaw(int j, double offset)=0
Set current force Offset for a specific joint.
virtual bool getImpedanceRaw(int j, double *stiffness, double *damping)=0
Get current impedance parameters (stiffness,damping,offset) for a specific joint.
virtual bool getCurrentImpedanceLimitRaw(int j, double *min_stiff, double *max_stiff, double *min_damp, double *max_damp)=0
Get the current impedandance limits for a specific joint.
virtual bool setImpedanceRaw(int j, double stiffness, double damping)=0
Set current impedance parameters (stiffness,damping) for a specific joint.
bool uninitialize()
Clean up internal data and memory.
bool getImpedance(int j, double *stiffness, double *damping) override
Get current impedance gains (stiffness,damping,offset) for a specific joint.
bool getImpedanceOffset(int j, double *offset) override
Get current force Offset for a specific joint.
bool initialize(int size, const int *amap, const double *enc, const double *zos, const double *nw)
Initialize the internal data and alloc memory.
ImplementImpedanceControl(yarp::dev::IImpedanceControlRaw *y)
yarp::dev::IImpedanceControlRaw * iImpedanceRaw
bool getCurrentImpedanceLimit(int j, double *min_stiff, double *max_stiff, double *min_damp, double *max_damp) override
Get the current impedandance limits for a specific joint.
bool getAxes(int *ax) override
Get the number of controlled axes.
bool setImpedanceOffset(int j, double offset) override
Set current force Offset for a specific joint.
bool setImpedance(int j, double stiffness, double damping) override
Set current impedance gains (stiffness,damping) for a specific joint.
An interface for the device drivers.