vor.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "vor.h"
25 
26 using namespace armarx;
27 
28 void VOR::calc()
29 {
30  std::scoped_lock lock(dataMutex);
31 
32  //Check if all sensor values have at least been reported once to avoid segfaults
33  if (!(reportedSensorValues && initialOrientation && reportedJointAnglesBool))
34  {
35  return;
36  }
37 
38  if (velocityBased && !(reportedJointVelocitiesBool))
39  {
40  return;
41  }
42 
43  Eigen::Quaternionf delta = initialQuaternion.inverse() * currentOrientation;
44  Eigen::Vector3f rpy = quaternionToRPY(delta);
45 
46  NameValueMap reportedJointAngles = this->reportedJointAngles;
47  NameValueMap reportedJointVelocities = this->reportedJointVelocities;
48 
49  reportedJointAnglesBool = false;
50  reportedJointVelocitiesBool = false;
51 
52  //lock.unlock();
53 
54  std::map<std::string, float> headJoints;
55 
56  if (velocityBased)
57  {
58  std::vector<float> error = std::vector<float>(3);
59  // error[0] = rotationVelocity[0] + reportedJointVelocities[neck_roll];
60  // error[1] = rotationVelocity[1] + reportedJointVelocities[eye_pitch_left];
61  // error[2] = (-rotationVelocity[2]) + reportedJointVelocities[eye_yaw_left];
62 
63  //ARMARX_LOG_S << deactivateSpam(3) << "rotationVelocity: (" << rotationVelocity[0] << " / " << rotationVelocity[1] << " / " << rotationVelocity[2] << ")";
64 
65  // error[0] = rpy[0];
66  // error[1] = (rpy[1] - reportedJointAngles[eye_pitch_left]);
67  // error[2] = (rpy[2] - reportedJointAngles[eye_yaw_left]);
68 
69  //ARMARX_LOG_S << "RPY: " << rpy[0] << " " << rpy[1] << " " << rpy[2];
70 
71  //std::vector<float> y = pid(error, {kp, kp, kp}, {ki, ki, ki}, {kd, kd, kd});
72 
73  //std::vector<float> y = pid(error, {kp, kp, kp}, {0, 0, 0}, {0, 0, 0});
74 
75  //ARMARX_LOG_S << "RPY PID: " << y[0] << " " << y[1] << " " << y[2];
76 
77  // headJoints[eye_pitch_left] = -1.0 * (reportedJointVelocities[eye_pitch_left] + y[1]);
78  // if (armar4)
79  // {
80  // headJoints[eye_pitch_right] = (reportedJointVelocities[eye_pitch_left] + y[1]);
81  // }
82  // headJoints[eye_yaw_left] = -1 * (reportedJointVelocities[eye_yaw_left] + y[2]);
83  // headJoints[eye_yaw_right] = -1 * (reportedJointVelocities[eye_yaw_left] + y[2]);
84  //headJoints[neck_roll] = -1 * (reportedJointVelocities[neck_roll] + y[0]);
85 
86 
87  // headJoints[eye_pitch_left] = (reportedJointVelocities[eye_pitch_left] + y[1]);
88  // if (armar4)
89  // {
90  // headJoints[eye_pitch_right] = (reportedJointVelocities[eye_pitch_left] - y[1]);
91  // }
92  // headJoints[eye_yaw_left] = (reportedJointVelocities[eye_yaw_left] + y[2]);
93  // headJoints[eye_yaw_right] = (reportedJointVelocities[eye_yaw_left] + y[2]);
94 
95  headJoints[eye_pitch_left] = -1 * rotationVelocity[1] + kp * (rpy[1] - reportedJointAngles[eye_pitch_left]) ;
96  headJoints[eye_pitch_left] = 0.;
97 
98  headJoints[eye_yaw_left] = rotationVelocity[2] + kp * (rpy[2] - reportedJointAngles[eye_yaw_left]);
99  headJoints[eye_yaw_right] = rotationVelocity[2] + kp * (rpy[2] - reportedJointAngles[eye_yaw_right]);
100  //headJoints[neck_roll] = (reportedJointVelocities[neck_roll] + y[0]);
101 
102  }
103  else
104  {
105  // todo assume that the axis is parallel to the IMU.
106  headJoints[eye_pitch_left] = -1.0 * rpy[1];
107  if (armar4)
108  {
109  headJoints[eye_pitch_right] = rpy[1];
110  rpy[0] = -rpy[0];
111  }
112  headJoints[eye_yaw_left] = 1 * rpy[2];
113  headJoints[eye_yaw_right] = 1 * rpy[2];
114  //headJoints[neck_roll] = 1 * (rpy[0] + reportedJointAngles[neck_roll]);
115  }
116 
117  std::scoped_lock lock2(mutex);
118 
119  jointAngles.swap(headJoints);
120 }
121 
122 void VOR::reportSensorValues(const IMUData& values)
123 {
124  std::scoped_lock lock(dataMutex);
125 
126  std::vector<float> q = values.orientationQuaternion;
127 
128  currentOrientation = Eigen::Quaternionf(q[0], q[1], q[2], q[3]);
129 
130  rotationVelocity.clear();
131 
132  rotationVelocity = values.gyroscopeRotation;
133 
134  reportedSensorValues = true;
135 
136  if (!initialOrientation)
137  {
138  initialOrientation = true;
139 
140  initialQuaternion = Eigen::Quaternionf(q[0], q[1], q[2], q[3]);
141  }
142 
143 }
144 
145 void VOR::setJointNames(std::string eye_pitch_left, std::string eye_pitch_right, std::string eye_yaw_left, std::string eye_yaw_right, std::string neck_roll)
146 {
147  std::scoped_lock lock(mutex);
148 
149  this->eye_pitch_left = eye_pitch_left;
150  this->eye_pitch_right = eye_pitch_right;
151  this->eye_yaw_left = eye_yaw_left;
152  this->eye_yaw_right = eye_yaw_right;
153  this->neck_roll = neck_roll;
154 }
155 
156 void VOR::setPIDValues(float kp, float ki, float kd)
157 {
158  std::scoped_lock lock(mutex);
159 
160  this->kp = kp;
161  this->ki = ki;
162  this->kd = kd;
163 }
164 
165 void VOR::setBools(bool armar4, bool velocityBased)
166 {
167  std::scoped_lock lock(mutex);
168 
169  this->armar4 = armar4;
170  this->velocityBased = velocityBased;
171 }
172 
173 
174 
175 std::vector<float> VOR::pid(std::vector<float> error, std::vector<float> kp, std::vector<float> ki, std::vector<float> kd)
176 {
177  if (error.size() != kp.size() || kp.size() != ki.size() || ki.size() != kd.size())
178  {
179  ARMARX_WARNING_S << "Different arraysizes in HeadStabilization::pid";
180  return {0.0, 0.0, 0.0};
181  }
182 
183 
184  std::vector<float> y;
185 
186  for (size_t i = 0; i < error.size(); i++)
187  {
188  err_sum[i] += error[i];
189 
190  y.push_back(kp[i] * error[i] + (ki[i] * err_sum[i]) + (kd[i] * (error[i] - err_old[i])));
191 
192  err_old[i] = error[i];
193  }
194 
195  return y;
196 }
197 
198 
199 void armarx::VOR::onStop()
200 {
201  std::scoped_lock lock(dataMutex);
202 
203  reportedSensorValues = false;
204  initialOrientation = false;
205 
206  reportedJointVelocitiesBool = false;
207  reportedJointAnglesBool = false;
208 
209 
210  std::vector<float> err_old(3, 0.0);
211  err_old.swap(this->err_old);
212  std::vector<float> err_sum(3, 0.0);
213  err_sum.swap(this->err_sum);
214 }
215 
216 
217 void armarx::VOR::reportJointAngles(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
218 {
219  std::scoped_lock lock(dataMutex);
220 
221  reportedJointAnglesBool = true;
222  this->reportedJointAngles = values;
223 
224 }
225 
226 void armarx::VOR::reportJointVelocities(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
227 {
228  std::scoped_lock lock(dataMutex);
229 
230  reportedJointVelocitiesBool = true;
231  this->reportedJointVelocities = values;
232 
233 }
234 
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
vor.h
armarx::VOR::setJointNames
void setJointNames(std::string eye_pitch_left, std::string eye_pitch_right, std::string eye_yaw_left, std::string eye_yaw_right, std::string neck_roll)
Definition: vor.cpp:145
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::navigation::platform_controller::platform_global_trajectory::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformGlobalTrajectoryController.h:93
armarx::Reflex::jointAngles
std::map< std::string, float > jointAngles
Definition: reflex.h:132
armarx::VOR::setPIDValues
void setPIDValues(float kp, float ki, float kd)
Definition: vor.cpp:156
armarx::VOR::setBools
void setBools(bool armar4, bool velocityBased)
Definition: vor.cpp:165
armarx::Reflex::mutex
std::mutex mutex
Definition: reflex.h:130
armarx::VOR::reportJointAngles
void reportJointAngles(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition: vor.cpp:217
armarx::VOR::reportJointVelocities
void reportJointVelocities(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition: vor.cpp:226
Eigen::Quaternionf
Quaternion< float, 0 > Quaternionf
Definition: EigenForwardDeclarations.h:61
q
#define q
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::Reflex::quaternionToRPY
Eigen::Vector3f quaternionToRPY(Eigen::Quaternionf q)
Definition: reflex.h:115
armarx::Quaternion< float, 0 >
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::VOR::reportSensorValues
void reportSensorValues(const IMUData &values)
Definition: vor.cpp:122