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
29 VOR::calc()
30 {
31  std::scoped_lock lock(dataMutex);
32 
33  //Check if all sensor values have at least been reported once to avoid segfaults
34  if (!(reportedSensorValues && initialOrientation && reportedJointAnglesBool))
35  {
36  return;
37  }
38 
39  if (velocityBased && !(reportedJointVelocitiesBool))
40  {
41  return;
42  }
43 
44  Eigen::Quaternionf delta = initialQuaternion.inverse() * currentOrientation;
45  Eigen::Vector3f rpy = quaternionToRPY(delta);
46 
47  NameValueMap reportedJointAngles = this->reportedJointAngles;
48  NameValueMap reportedJointVelocities = this->reportedJointVelocities;
49 
50  reportedJointAnglesBool = false;
51  reportedJointVelocitiesBool = false;
52 
53  //lock.unlock();
54 
55  std::map<std::string, float> headJoints;
56 
57  if (velocityBased)
58  {
59  std::vector<float> error = std::vector<float>(3);
60  // error[0] = rotationVelocity[0] + reportedJointVelocities[neck_roll];
61  // error[1] = rotationVelocity[1] + reportedJointVelocities[eye_pitch_left];
62  // error[2] = (-rotationVelocity[2]) + reportedJointVelocities[eye_yaw_left];
63 
64  //ARMARX_LOG_S << deactivateSpam(3) << "rotationVelocity: (" << rotationVelocity[0] << " / " << rotationVelocity[1] << " / " << rotationVelocity[2] << ")";
65 
66  // error[0] = rpy[0];
67  // error[1] = (rpy[1] - reportedJointAngles[eye_pitch_left]);
68  // error[2] = (rpy[2] - reportedJointAngles[eye_yaw_left]);
69 
70  //ARMARX_LOG_S << "RPY: " << rpy[0] << " " << rpy[1] << " " << rpy[2];
71 
72  //std::vector<float> y = pid(error, {kp, kp, kp}, {ki, ki, ki}, {kd, kd, kd});
73 
74  //std::vector<float> y = pid(error, {kp, kp, kp}, {0, 0, 0}, {0, 0, 0});
75 
76  //ARMARX_LOG_S << "RPY PID: " << y[0] << " " << y[1] << " " << y[2];
77 
78  // headJoints[eye_pitch_left] = -1.0 * (reportedJointVelocities[eye_pitch_left] + y[1]);
79  // if (armar4)
80  // {
81  // headJoints[eye_pitch_right] = (reportedJointVelocities[eye_pitch_left] + y[1]);
82  // }
83  // headJoints[eye_yaw_left] = -1 * (reportedJointVelocities[eye_yaw_left] + y[2]);
84  // headJoints[eye_yaw_right] = -1 * (reportedJointVelocities[eye_yaw_left] + y[2]);
85  //headJoints[neck_roll] = -1 * (reportedJointVelocities[neck_roll] + y[0]);
86 
87 
88  // headJoints[eye_pitch_left] = (reportedJointVelocities[eye_pitch_left] + y[1]);
89  // if (armar4)
90  // {
91  // headJoints[eye_pitch_right] = (reportedJointVelocities[eye_pitch_left] - y[1]);
92  // }
93  // headJoints[eye_yaw_left] = (reportedJointVelocities[eye_yaw_left] + y[2]);
94  // headJoints[eye_yaw_right] = (reportedJointVelocities[eye_yaw_left] + y[2]);
95 
96  headJoints[eye_pitch_left] =
97  -1 * rotationVelocity[1] + kp * (rpy[1] - reportedJointAngles[eye_pitch_left]);
98  headJoints[eye_pitch_left] = 0.;
99 
100  headJoints[eye_yaw_left] =
101  rotationVelocity[2] + kp * (rpy[2] - reportedJointAngles[eye_yaw_left]);
102  headJoints[eye_yaw_right] =
103  rotationVelocity[2] + kp * (rpy[2] - reportedJointAngles[eye_yaw_right]);
104  //headJoints[neck_roll] = (reportedJointVelocities[neck_roll] + y[0]);
105  }
106  else
107  {
108  // todo assume that the axis is parallel to the IMU.
109  headJoints[eye_pitch_left] = -1.0 * rpy[1];
110  if (armar4)
111  {
112  headJoints[eye_pitch_right] = rpy[1];
113  rpy[0] = -rpy[0];
114  }
115  headJoints[eye_yaw_left] = 1 * rpy[2];
116  headJoints[eye_yaw_right] = 1 * rpy[2];
117  //headJoints[neck_roll] = 1 * (rpy[0] + reportedJointAngles[neck_roll]);
118  }
119 
120  std::scoped_lock lock2(mutex);
121 
122  jointAngles.swap(headJoints);
123 }
124 
125 void
127 {
128  std::scoped_lock lock(dataMutex);
129 
130  std::vector<float> q = values.orientationQuaternion;
131 
132  currentOrientation = Eigen::Quaternionf(q[0], q[1], q[2], q[3]);
133 
134  rotationVelocity.clear();
135 
136  rotationVelocity = values.gyroscopeRotation;
137 
138  reportedSensorValues = true;
139 
140  if (!initialOrientation)
141  {
142  initialOrientation = true;
143 
144  initialQuaternion = Eigen::Quaternionf(q[0], q[1], q[2], q[3]);
145  }
146 }
147 
148 void
149 VOR::setJointNames(std::string eye_pitch_left,
150  std::string eye_pitch_right,
151  std::string eye_yaw_left,
152  std::string eye_yaw_right,
153  std::string neck_roll)
154 {
155  std::scoped_lock lock(mutex);
156 
157  this->eye_pitch_left = eye_pitch_left;
158  this->eye_pitch_right = eye_pitch_right;
159  this->eye_yaw_left = eye_yaw_left;
160  this->eye_yaw_right = eye_yaw_right;
161  this->neck_roll = neck_roll;
162 }
163 
164 void
165 VOR::setPIDValues(float kp, float ki, float kd)
166 {
167  std::scoped_lock lock(mutex);
168 
169  this->kp = kp;
170  this->ki = ki;
171  this->kd = kd;
172 }
173 
174 void
175 VOR::setBools(bool armar4, bool velocityBased)
176 {
177  std::scoped_lock lock(mutex);
178 
179  this->armar4 = armar4;
180  this->velocityBased = velocityBased;
181 }
182 
183 std::vector<float>
184 VOR::pid(std::vector<float> error,
185  std::vector<float> kp,
186  std::vector<float> ki,
187  std::vector<float> kd)
188 {
189  if (error.size() != kp.size() || kp.size() != ki.size() || ki.size() != kd.size())
190  {
191  ARMARX_WARNING_S << "Different arraysizes in HeadStabilization::pid";
192  return {0.0, 0.0, 0.0};
193  }
194 
195 
196  std::vector<float> y;
197 
198  for (size_t i = 0; i < error.size(); i++)
199  {
200  err_sum[i] += error[i];
201 
202  y.push_back(kp[i] * error[i] + (ki[i] * err_sum[i]) + (kd[i] * (error[i] - err_old[i])));
203 
204  err_old[i] = error[i];
205  }
206 
207  return y;
208 }
209 
210 void
211 armarx::VOR::onStop()
212 {
213  std::scoped_lock lock(dataMutex);
214 
215  reportedSensorValues = false;
216  initialOrientation = false;
217 
218  reportedJointVelocitiesBool = false;
219  reportedJointAnglesBool = false;
220 
221 
222  std::vector<float> err_old(3, 0.0);
223  err_old.swap(this->err_old);
224  std::vector<float> err_sum(3, 0.0);
225  err_sum.swap(this->err_sum);
226 }
227 
228 void
229 armarx::VOR::reportJointAngles(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
230 {
231  std::scoped_lock lock(dataMutex);
232 
233  reportedJointAnglesBool = true;
234  this->reportedJointAngles = values;
235 }
236 
237 void
239  bool valueChanged,
240  const Ice::Current& c)
241 {
242  std::scoped_lock lock(dataMutex);
243 
244  reportedJointVelocitiesBool = true;
245  this->reportedJointVelocities = values;
246 }
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:149
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::Reflex::jointAngles
std::map< std::string, float > jointAngles
Definition: reflex.h:140
armarx::VOR::setPIDValues
void setPIDValues(float kp, float ki, float kd)
Definition: vor.cpp:165
armarx::VOR::setBools
void setBools(bool armar4, bool velocityBased)
Definition: vor.cpp:175
armarx::Reflex::mutex
std::mutex mutex
Definition: reflex.h:138
armarx::VOR::reportJointAngles
void reportJointAngles(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition: vor.cpp:229
armarx::VOR::reportJointVelocities
void reportJointVelocities(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition: vor.cpp:238
Eigen::Quaternionf
Quaternion< float, 0 > Quaternionf
Definition: EigenForwardDeclarations.h:61
q
#define q
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:88
armarx::Reflex::quaternionToRPY
Eigen::Vector3f quaternionToRPY(Eigen::Quaternionf q)
Definition: reflex.h:121
armarx::Quaternion< float, 0 >
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::VOR::reportSensorValues
void reportSensorValues(const IMUData &values)
Definition: vor.cpp:126