simpson_integrator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef COB_TWIST_CONTROLLER_UTILS_SIMPSON_INTEGRATOR_H
19 #define COB_TWIST_CONTROLLER_UTILS_SIMPSON_INTEGRATOR_H
20 
21 #include <vector>
22 
23 #include <ros/ros.h>
24 #include <kdl/jntarray.hpp>
25 #include <std_msgs/Float64MultiArray.h>
26 
28 
30 {
31  public:
32  explicit SimpsonIntegrator(const uint8_t dof, const double integrator_smoothing = 0.2)
33  : dof_(dof),
34  integrator_smoothing_(integrator_smoothing),
35  last_update_time_(ros::Time(0.0))
36  {
37  for (uint8_t i = 0; i < dof_; i++)
38  {
41  }
42 
48  }
49 
51  {}
52 
54  {
55  // resetting outdated values
56  vel_last_.clear();
57  vel_before_last_.clear();
58 
59  // resetting moving average
60  for (unsigned int i = 0; i < dof_; ++i)
61  {
62  ma_vel_[i]->reset();
63  ma_pos_[i]->reset();
64  }
65  }
66 
67  bool updateIntegration(const KDL::JntArray& q_dot_ik,
68  const KDL::JntArray& current_q,
69  std::vector<double>& pos,
70  std::vector<double>& vel)
71  {
73  ros::Duration period = now - last_update_time_;
74  last_update_time_ = now;
75 
76  bool value_valid = false;
77  pos.clear();
78  vel.clear();
79 
80  // ToDo: Test these conditions and find good thresholds
81  if (period.toSec() > ros::Duration(0.5).toSec()) // missed about 'max_command_silence'
82  {
83  ROS_WARN_STREAM("reset Integration: " << period.toSec());
85  }
86 
87  // smooth incoming velocities
88  KDL::JntArray q_dot_avg(dof_);
91  for (unsigned int i = 0; i < dof_; ++i)
92  {
93  ma_vel_[i]->addElement(q_dot_ik(i));
94  double avg_vel = 0.0;
95  if (ma_vel_[i]->calcMovingAverage(avg_vel))
96  {
97  q_dot_avg(i) = avg_vel;
98  }
99  else
100  {
101  q_dot_avg(i) = q_dot_ik(i);
102  }
103 
106  }
107 
110  if (!vel_before_last_.empty())
111  {
112  for (unsigned int i = 0; i < dof_; ++i)
113  {
114  // Simpson
115  double integration_value = static_cast<double>(period.toSec() / 6.0 * (vel_before_last_[i] + 4.0 * (vel_before_last_[i] + vel_last_[i]) + vel_before_last_[i] + vel_last_[i] + q_dot_avg(i)) + current_q(i));
116 
117  // smooth outgoing positions
118  ma_pos_[i]->addElement(integration_value);
119  double avg_pos = 0.0;
120  if (!ma_pos_[i]->calcMovingAverage(avg_pos))
121  {
122  ROS_ERROR("calcMovingAverage failed");
123  avg_pos = integration_value;
124  }
125 
126  pos.push_back(avg_pos);
127  vel.push_back(q_dot_avg(i));
128 
131  }
132  value_valid = true;
133  }
134 
135  // Continuously shift the vectors for simpson integration
136  vel_before_last_.clear();
137  for (unsigned int i=0; i < vel_last_.size(); ++i)
138  {
139  vel_before_last_.push_back(vel_last_[i]);
140  }
141 
142  vel_last_.clear();
143  for (unsigned int i=0; i < q_dot_avg.rows(); ++i)
144  {
145  vel_last_.push_back(q_dot_avg(i));
146  }
147 
152 
153  return value_valid;
154  }
155 
156  private:
157  std::vector<MovingAvgBase_double_t*> ma_vel_;
158  std::vector<MovingAvgBase_double_t*> ma_pos_;
159  uint8_t dof_;
161  std::vector<double> vel_last_, vel_before_last_;
163 
164  // debug
170 };
171 
172 #endif // COB_TWIST_CONTROLLER_UTILS_SIMPSON_INTEGRATOR_H
MovingAverageExponential< double > MovingAvgExponential_double_t
double now()
unsigned int rows() const
std::vector< double > vel_before_last_
std::vector< double > vel_last_
std::vector< MovingAvgBase_double_t * > ma_vel_
std::vector< MovingAvgBase_double_t * > ma_pos_
bool updateIntegration(const KDL::JntArray &q_dot_ik, const KDL::JntArray &current_q, std::vector< double > &pos, std::vector< double > &vel)
#define ROS_WARN_STREAM(args)
static Time now()
SimpsonIntegrator(const uint8_t dof, const double integrator_smoothing=0.2)
#define ROS_ERROR(...)


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00