AccelerationFilter.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
7 #include "AccelerationFilter.h"
8 
10 // Module specification
11 // <rtc-template block="module_spec">
12 static const char* accelerationfilter_spec[] =
13  {
14  "implementation_id", "AccelerationFilter",
15  "type_name", "AccelerationFilter",
16  "description", "Acceleration Filter component",
17  "version", "1.0",
18  "vendor", "JSK",
19  "category", "example",
20  "activity_type", "SPORADIC",
21  "kind", "DataFlowComponent",
22  "max_instance", "10",
23  "language", "C++",
24  "lang_type", "compile",
25  // Configuration variables
26  ""
27  };
28 // </rtc-template>
29 
31  // <rtc-template block="initializer">
32  : RTC::DataFlowComponentBase(manager),
33  m_accInIn("accIn", m_accIn),
34  m_rateInIn("rateIn", m_rateIn),
35  m_rpyInIn("rpyIn", m_rpyIn),
36  m_posInIn("posIn", m_posIn),
37  m_velOutOut("velOut", m_velOut),
38  //m_posOutOut("posOut", m_posOut),
39  m_AccelerationFilterServicePort("AccelerationFilterService"),
40  // </rtc-template>
41  m_use_filter_bool(false)
42 {
43 }
44 
46 {
47 }
48 
49 
51 {
52  // Registration: InPort/OutPort/Service
53  // <rtc-template block="registration">
54  // Set InPort buffers
55  addInPort("accIn", m_accInIn);
56  addInPort("rateIn", m_rateInIn);
57  addInPort("rpyIn", m_rpyInIn);
58  addInPort("posIn", m_posInIn);
59 
60  // Set OutPort buffer
61  addOutPort("velOut", m_velOutOut);
62  //addOutPort("posOut", m_posOutOut);
63 
64  // Set service provider to Ports
65  m_AccelerationFilterServicePort.registerProvider("service0", "AccelerationFilterService", m_service0);
66  m_service0.setInstance(this);
67 
68  // Set service consumers to Ports
69 
70  // Set CORBA Service Ports
72 
73  // </rtc-template>
74 
75  // <rtc-template block="bind_config">
76  // Bind variables and configuration variable
78  if ( ! coil::stringTo(m_dt, prop["dt"].c_str()) ) {
79  std::cerr << "[" << m_profile.instance_name << "] failed to get dt" << std::endl;
80  return RTC::RTC_ERROR;
81  }
82 
83  // read gravity param
84  double param_gravity = 9.80665;
85  if ( ! coil::stringTo(m_gravity, prop["gravity"].c_str()) ) {
86  param_gravity = m_gravity = 9.80665;
87  }
88  std::cerr << "[" << m_profile.instance_name << "] gravity : " << m_gravity << std::endl;
89 
90  // read filter param
91  {
92  coil::vstring filter_str = coil::split(prop["iir_filter_setting"], ",");
93  if (filter_str.size() > 2) {
94  int dim = (filter_str.size() - 1)/2;
95  std::vector<double> bb;
96  std::vector<double> aa;
97  for(int i = 0; i < dim + 1; i++) {
98  double val = -1;
99  coil::stringTo(val, filter_str[i].c_str());
100  bb.push_back(val);
101  }
102  for(int i = 0; i < filter_str.size() - dim - 1; i++) {
103  double val = -1;
104  coil::stringTo(val, filter_str[dim+1+i].c_str());
105  aa.push_back(val);
106  }
107  if (aa.size() > 0 && bb.size() > 0) {
108  m_use_filter_bool = true;
109  std::cerr << "[" << m_profile.instance_name << "] pass filter_param : " << std::endl;
110  std::cerr << "B = [";
111  for(int i = 0; i < bb.size(); i++) {
112  std::cerr << " " << bb[i];
113  }
114  std::cerr << "]" << std::endl;
115  std::cerr << "A = [";
116  for(int i = 0; i < aa.size(); i++) {
117  std::cerr << " " << aa[i];
118  }
119  std::cerr << "]" << std::endl;
120  for (int i = 0; i < 3; i++) {
121  IIRFilterPtr fl(new IIRFilter(std::string(m_profile.instance_name)));
122  fl->setParameter(dim, aa, bb);
123  fl->reset(param_gravity);
124  m_filters.push_back(fl);
125  }
126  }
127  }
128  }
129 
130  // </rtc-template>
131  return RTC::RTC_OK;
132 }
133 
134 
135 /*
136 RTC::ReturnCode_t AccelerationFilter::onFinalize()
137 {
138  return RTC::RTC_OK;
139 }
140 */
141 /*
142 RTC::ReturnCode_t AccelerationFilter::onStartup(RTC::UniqueId ec_id)
143 {
144  return RTC::RTC_OK;
145 }
146 */
147 /*
148 RTC::ReturnCode_t AccelerationFilter::onShutdown(RTC::UniqueId ec_id)
149 {
150  return RTC::RTC_OK;
151 }
152 */
153 
155 {
156  std::cerr << "[" << m_profile.instance_name<< "] onActivated(" << ec_id << ")" << std::endl;
157  return RTC::RTC_OK;
158 }
160 {
161  std::cerr << "[" << m_profile.instance_name<< "] onDeactivated(" << ec_id << ")" << std::endl;
162  // reset filter
163  return RTC::RTC_OK;
164 }
165 
166 
168 {
169  if (m_rpyInIn.isNew()) {
170  m_rpyInIn.read();
171  }
172  if (m_rateInIn.isNew()) {
173  m_rateInIn.read();
174  }
175  // calc expected velocity from AutoBalancer
176  hrp::Vector3 expected_vel;
177  if (m_posInIn.isNew()) {
178  m_posInIn.read();
179  hrp::Vector3 pos(m_posIn.data.x, m_posIn.data.y, m_posIn.data.z);
180  expected_vel = pos - m_previous_pos;
181  expected_vel /= m_dt;
182  m_previous_pos = pos;
183  }
184 
185  //
186  if (m_accInIn.isNew()) {
187  Guard guard(m_mutex);
188 
189  m_accInIn.read();
190  hrp::Vector3 acc(m_accIn.data.ax, m_accIn.data.ay, m_accIn.data.az);
191  hrp::Matrix33 imuR = hrp::rotFromRpy(m_rpyIn.data.r,
192  m_rpyIn.data.p,
193  m_rpyIn.data.y);
194  hrp::Vector3 gravity(0, 0, - m_gravity);
195  hrp::Vector3 acc_wo_g = imuR * acc + gravity;
196 
197  for (int i = 0; i < 3; i++) {
198  if (m_use_filter_bool) {
199  double filtered_acc = m_filters[i]->passFilter(acc_wo_g[i]);
200  m_global_vel[i] += filtered_acc * m_dt;
201  } else {
202  m_global_vel[i] += acc_wo_g[i] * m_dt;
203  }
204  }
205 
206  hrp::Vector3 _result_vel = imuR.inverse() * m_global_vel; // result should be described in sensor coords
207 
208  m_velOut.data.x = _result_vel[0];
209  m_velOut.data.y = _result_vel[1];
210  m_velOut.data.z = _result_vel[2];
211  m_velOutOut.write();
212  }
213 
214  return RTC::RTC_OK;
215 }
216 
217 /*
218 RTC::ReturnCode_t AccelerationFilter::onAborting(RTC::UniqueId ec_id)
219 {
220  return RTC::RTC_OK;
221 }
222 */
223 /*
224 RTC::ReturnCode_t AccelerationFilter::onError(RTC::UniqueId ec_id)
225 {
226  return RTC::RTC_OK;
227 }
228 */
229 /*
230 RTC::ReturnCode_t AccelerationFilter::onReset(RTC::UniqueId ec_id)
231 {
232  return RTC::RTC_OK;
233 }
234 */
235 /*
236 RTC::ReturnCode_t AccelerationFilter::onStateUpdate(RTC::UniqueId ec_id)
237 {
238  return RTC::RTC_OK;
239 }
240 */
241 /*
242 RTC::ReturnCode_t AccelerationFilter::onRateChanged(RTC::UniqueId ec_id)
243 {
244  return RTC::RTC_OK;
245 }
246 */
247 
248 bool AccelerationFilter::resetFilter(const OpenHRP::AccelerationFilterService::ControlMode &mode,
249  const double *vel)
250 {
251  Guard guard(m_mutex);
252  switch(mode) {
253  case OpenHRP::AccelerationFilterService::MODE_ZERO_VELOCITY:
254  m_global_vel[0] = 0;
255  m_global_vel[1] = 0;
256  m_global_vel[2] = 0;
257  break;
258  case OpenHRP::AccelerationFilterService::MODE_RELATIVE_GLOBAL_VELOCITY:
259  m_global_vel[0] += vel[0];
260  m_global_vel[1] += vel[1];
261  m_global_vel[2] += vel[2];
262  break;
263  case OpenHRP::AccelerationFilterService::MODE_ABSOLUTE_GLOBAL_VELOCITY:
264  m_global_vel[0] = vel[0];
265  m_global_vel[1] = vel[1];
266  m_global_vel[2] = vel[2];
267  break;
268  case OpenHRP::AccelerationFilterService::MODE_RELATIVE_LOCAL_VELOCITY:
269  {
270  hrp::Matrix33 imuR = hrp::rotFromRpy(m_rpyIn.data.r,
271  m_rpyIn.data.p,
272  m_rpyIn.data.y);
273  hrp::Vector3 in_vel(vel[0], vel[1], vel[2]);
274  hrp::Vector3 g_vel = imuR * in_vel;
275  m_global_vel += g_vel;
276  }
277  break;
278  case OpenHRP::AccelerationFilterService::MODE_ABSOLUTE_LOCAL_VELOCITY:
279  {
280  hrp::Matrix33 imuR = hrp::rotFromRpy(m_rpyIn.data.r,
281  m_rpyIn.data.p,
282  m_rpyIn.data.y);
283  hrp::Vector3 in_vel(vel[0], vel[1], vel[2]);
284  hrp::Vector3 g_vel = imuR * in_vel;
285  m_global_vel = g_vel;
286  }
287  break;
288  default:
289  break;
290  }
291  return true;
292 }
293 
294 bool AccelerationFilter::setParam(const ::OpenHRP::AccelerationFilterService::AccelerationFilterParam& i_param)
295 {
296  Guard guard(m_mutex);
297  m_gravity = i_param.gravity;
298 
299  if(i_param.filter_param.length() > 1) {
300  int dim;
301  std::vector<double> A;
302  std::vector<double> B;
303  dim = (i_param.filter_param.length() - 1)/2;
304  for(int i = 0; i < dim + 1; i++) {
305  B.push_back(i_param.filter_param[i]);
306  }
307  for(int i = 0; i < i_param.filter_param.length() - dim - 1; i++) {
308  A.push_back(i_param.filter_param[dim+1+i]);
309  }
310  m_filters.resize(0);
311  for(int i = 0; i < 3; i++) {
312  IIRFilterPtr fl(new IIRFilter);
313  fl->setParameter(dim, A, B);
314  m_filters.push_back(fl);
315  }
316  m_use_filter_bool = i_param.use_filter;
317  }
318  return true;
319 }
320 
321 bool AccelerationFilter::getParam(::OpenHRP::AccelerationFilterService::AccelerationFilterParam &i_param)
322 {
323  i_param.gravity = m_gravity;
324  i_param.use_filter = m_use_filter_bool;
325  if(m_filters.size() > 0) {
326  int dim;
327  std::vector<double> A;
328  std::vector<double> B;
329  m_filters[0]->getParameter(dim, A, B);
330  i_param.filter_param.length(2*(dim+1));
331  for(int i = 0; i < dim+1; i++) {
332  i_param.filter_param[i] = B[i];
333  i_param.filter_param[i + dim + 1] = A[i];
334  }
335  }
336  return true;
337 }
338 
339 extern "C"
340 {
342  {
344  manager->registerFactory(profile,
345  RTC::Create<AccelerationFilter>,
346  RTC::Delete<AccelerationFilter>);
347  }
348 };
349 
350 
351 
ComponentProfile m_profile
bool setParam(const ::OpenHRP::AccelerationFilterService::AccelerationFilterParam &i_param)
png_infop png_charpp int png_charpp profile
OutPort< TimedVector3D > m_velOutOut
static const char * accelerationfilter_spec[]
bool stringTo(To &val, const char *str)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
RTC::CorbaPort m_AccelerationFilterServicePort
Acceleration Filter component *.
virtual RTC::ReturnCode_t onInitialize()
TimedAcceleration3D m_accIn
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
png_uint_32 i
bool getParam(::OpenHRP::AccelerationFilterService::AccelerationFilterParam &i_param)
coil::Properties & getProperties()
void AccelerationFilterInit(RTC::Manager *manager)
bool addOutPort(const char *name, OutPortBase &outport)
Eigen::Vector3d Vector3
InPort< TimedAngularVelocity3D > m_rateInIn
Matrix33 rotFromRpy(const Vector3 &rpy)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Eigen::Matrix3d Matrix33
AccelerationFilterService_impl m_service0
std::vector< std::string > vstring
ExecutionContextHandle_t UniqueId
hrp::Vector3 m_previous_pos
int val
TimedOrientation3D m_rpyIn
AccelerationFilter(RTC::Manager *manager)
coil::Guard< coil::Mutex > Guard
prop
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
void setInstance(AccelerationFilter *i_instance)
virtual bool isNew()
InPort< TimedPoint3D > m_posInIn
InPort< TimedAcceleration3D > m_accInIn
bool addPort(PortBase &port)
virtual bool write(DataType &value)
InPort< TimedOrientation3D > m_rpyInIn
bool addInPort(const char *name, InPortBase &inport)
bool resetFilter(const OpenHRP::AccelerationFilterService::ControlMode &mode, const double *vel)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
std::vector< IIRFilterPtr > m_filters


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:49