feedforward.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 "feedforward.h"
25 
26 using namespace armarx;
27 
29 {
30 
31  if (!update_input_fromArmarX(stabilizer->input))
32  {
33  return;
34  }
35 
36  reportedJointVelocitiesBool = false;
37  reportedJointAnglesBool = false;
38 
39  stabilizer->control_loop();
40 
41  optFlow_pred = stabilizer->getOptFlowPred();
42  mean_optFl_pred = stabilizer->getMeanOptFlPred();
43  gyroscopeRotation_pred = stabilizer->getIMUPred();
44 
45  update_output_toArmarX(stabilizer->output);
46 
47 }
48 
49 void FeedforwardReflex::setRobot(std::string nodeSetName, std::string headIKName, RobotStateComponentInterfacePrx robotStateComponent)
50 {
51  std::scoped_lock lock(mutex);
52 
53  this->robotStateComponent = robotStateComponent;
54 
55  globalPos = new FramedPosition();
56 
57  GazeStabOptions* gs_options;
58 
59  if (armar4)
60  {
61  gs_options = new GazeStabOptions(1);
62  }
63  else // armar3
64  {
65  gs_options = new GazeStabOptions(2);
66  }
67 
68  stabilizer = new GazeStabilization(gs_options);
69  stabilizer->init();
70 
71  forward_predictor = new ForwardPredictor();
72 
73  startTime = armarx::TimeUtil::GetTime();
74 
75  if (armar4)
76  {
77  headJointNames =
78  {
79  "Neck_1_joint",
80  "Neck_2_joint",
81  "Neck_3_joint",
82  "Head_1_joint",
83  "Head_2_joint",
84  "EyeR_1_joint",
85  "EyeR_2_joint"
86  };
87  }
88  else // armar3
89  {
90  headJointNames =
91  {
92  "Neck_1_Pitch",
93  "Neck_2_Roll",
94  "Neck_3_Yaw",
95  "Cameras",
96  "Eye_Right"
97  };
98  }
99 
100 }
101 
103 {
104 
105  std::scoped_lock lock(dataMutex);
106 
107  //Check if all sensor values have at least been reported once to avoid segfaults
108  if (!(reportedJointAnglesBool && reportedJointVelocitiesBool)) // TODO:: && globalPos))
109  {
110  ARMARX_WARNING_S << "Try to access gaze stab input that haven't been reported";
111  return false;
112  }
113 
114  // globalPos->changeFrame("Neck_1_joint");
115 
116  if (armar4)
117  {
118  PoseBasePtr globalPose = robotStateComponent->getSynchronizedRobot()->getRobotNode("Torso_Yaw_joint")->getGlobalPose();
119 
120  Eigen::Quaternionf quaternion = QuaternionPtr::dynamicCast(globalPose->orientation)->toEigenQuaternion();
121  Eigen::Vector3f rpy = quaternionToRPY(quaternion);
122 
123 
124  gs_input->q_LB[0] = globalPose->position->x; // should be probably converted from mm to m (TBC)
125  gs_input->q_LB[1] = globalPose->position->y;
126  gs_input->q_LB[2] = globalPose->position->z;
127 
128  gs_input->q_LB[3] = rpy(0);
129  gs_input->q_LB[4] = rpy(1);
130  gs_input->q_LB[5] = rpy(2);
131 
132  gs_input->q_LB[6] = this->reportedJointAngles["Torso_Yaw_joint"];
133  gs_input->q_LB[7] = -1 * this->reportedJointAngles["Torso_Pitch_joint"];
134 
135 
136  // in this test we assume we don´t know the floaring base velocity
137  gs_input->qd_LB_des[0] = 0.;
138  gs_input->qd_LB_des[1] = 0.;
139  gs_input->qd_LB_des[2] = 0.;
140  gs_input->qd_LB_des[3] = 0.;
141  gs_input->qd_LB_des[4] = 0.;
142  gs_input->qd_LB_des[5] = 0.;
143 
144  gs_input->qd_LB_des[6] = -1 * this->reportedJointVelocities["Torso_Yaw_joint"];
145  gs_input->qd_LB_des[7] = this->reportedJointVelocities["Torso_Pitch_joint"];
146  }
147  else // armar3
148  {
149  PoseBasePtr globalPose = robotStateComponent->getSynchronizedRobot()->getGlobalPose();
150 
151  Eigen::Quaternionf quaternion = QuaternionPtr::dynamicCast(globalPose->orientation)->toEigenQuaternion();
152  Eigen::Vector3f rpy = quaternionToRPY(quaternion);
153 
154  gs_input->q_LB[0] = globalPose->position->x / 1000.;
155  gs_input->q_LB[1] = globalPose->position->y / 1000.;
156  gs_input->q_LB[2] = globalPose->position->z / 1000.;
157 
158  gs_input->q_LB[3] = rpy(0);
159  gs_input->q_LB[4] = rpy(1);
160  gs_input->q_LB[5] = rpy(2);
161 
162  gs_input->q_LB[6] = 0.; // yaw platform (unused here...)
163  gs_input->q_LB[7] = this->reportedJointAngles["Hip Pitch"];
164  gs_input->q_LB[8] = this->reportedJointAngles["Hip Roll"];
165  gs_input->q_LB[9] = this->reportedJointAngles["Hip Yaw"];
166 
167  // in this test we assume we don´t know the floating base velocity
168  //gs_inputt->qd_LB_des[0] = 0.; //x
169  //gs_input->qd_LB_des[1] = 0.; //y
170  gs_input->qd_LB_des[2] = 0.;
171  gs_input->qd_LB_des[3] = 0.;
172  gs_input->qd_LB_des[4] = 0.;
173  //gs_input->qd_LB_des[5] = 0.; // theta
174 
175  gs_input->qd_LB_des[6] = 0.; // yaw platform (unused here...)
176  gs_input->qd_LB_des[7] = -1 * this->reportedJointVelocities["Hip Pitch"];
177  gs_input->qd_LB_des[8] = -1 * this->reportedJointVelocities["Hip Roll"];
178  gs_input->qd_LB_des[9] = this->reportedJointVelocities["Hip Yaw"];
179 
180  // for now we don't make any diff between desired and measured velocity (to be corrected).
181  for (int i = 0; i < 10; i++)
182  {
183  gs_input->qd_LB[i] = gs_input->qd_LB_des[i];
184  }
185 
186  }
187 
188  for (size_t i = 0; i < headJointNames.size(); i++)
189  {
190  gs_input->q_UB[i] = this->reportedJointAngles[headJointNames[i]];
191  gs_input->qd_UB[i] = this->reportedJointVelocities[headJointNames[i]];
192  }
193 
194  if (armar4)
195  {
196  gs_input->q_UB[2] += M_PI; // shift between models
197  gs_input->q_UB[0] *= -1; // sense of rotation between models
198  gs_input->q_UB[1] *= -1;
199  gs_input->q_UB[6] *= -1;
200  }
201  else
202  {
203  // gs_input->q_UB[4] *= -1;
204  }
205 
206  gs_input->tsim = (armarx::TimeUtil::GetTime() - startTime).toSecondsDouble();
207 
208  return true;
209 
210 }
211 
213 {
214 
215  std::map<std::string, float> targetJointAngles;
216 
217  for (size_t i = 0; i < headJointNames.size(); i++)
218  {
219  targetJointAngles[headJointNames[i]] = gs_output->qd_UB_des[i];
220  }
221  if (armar4)
222  {
223  targetJointAngles[headJointNames[0]] *= -1; // sense of rotation between models
224  targetJointAngles[headJointNames[1]] *= -1;
225  targetJointAngles[headJointNames[6]] *= -1;
226  }
227  else // armar 3
228  {
229  targetJointAngles["Eye_Left"] = targetJointAngles["Eye_Right"];
230 
231  }
232 
233  std::scoped_lock lock(mutex);
234 
235  jointAngles.swap(targetJointAngles);
236 
237 }
238 
239 void FeedforwardReflex::setBools(bool armar4, bool velocityBased)
240 {
241  std::scoped_lock lock(mutex);
242 
243  this->armar4 = armar4;
244  this->velocityBased = velocityBased;
245 }
246 
247 void FeedforwardReflex::reportJointAngles(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
248 {
249  std::scoped_lock lock(dataMutex);
250 
251  reportedJointAnglesBool = true;
252  this->reportedJointAngles = values;
253 
254 }
255 
256 void FeedforwardReflex::reportJointVelocities(const NameValueMap& values, bool valueChanged, const Ice::Current& c)
257 {
258  std::scoped_lock lock(dataMutex);
259 
260  reportedJointVelocitiesBool = true;
261  this->reportedJointVelocities = values;
262 }
263 
264 void FeedforwardReflex::reportPlatformVelocity(float x, float y, float a)
265 {
266  stabilizer->input->qd_LB_des[0] = x / 1000.;
267  stabilizer->input->qd_LB_des[1] = y / 1000.;
268  stabilizer->input->qd_LB_des[5] = a;
269 }
270 
271 
272 
273 void FeedforwardReflex::reportHeadTargetChanged(const NameValueMap& targetJointAngles, const FramedPositionBasePtr& targetPosition)
274 {
275  std::scoped_lock lock(dataMutex);
276 
277  globalPos = FramedPositionPtr::dynamicCast(targetPosition);
278 
279  Eigen::Vector3f x = globalPos->toEigen() / 1000.0f;
280 
281  stabilizer->input->pos_target = x.cast<double>();
282 
283 }
284 
286 {
287  std::scoped_lock lock(dataMutex);
288 
289  // globalPos = {};
290  reportedJointVelocitiesBool = false;
291  reportedJointAnglesBool = false;
292 
293 
294 }
armarx::FeedforwardReflex::optFlow_pred
std::vector< float > optFlow_pred
Definition: feedforward.h:90
armarx::Quaternion::toEigenQuaternion
Eigen::Quaternionf toEigenQuaternion() const
Definition: Pose.cpp:210
GazeStabOutput
Definition: GazeStabInputOutput.hh:26
armarx::FeedforwardReflex::onStop
void onStop() override
Definition: feedforward.cpp:285
GazeStabilization::output
GazeStabOutput * output
Definition: GazeStabilization.hh:17
GazeStabilization
Definition: GazeStabilization.hh:10
GazeStabInput::pos_target
Eigen::Vector3d pos_target
Definition: GazeStabInputOutput.hh:22
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::FeedforwardReflex::reportPlatformVelocity
void reportPlatformVelocity(float x, float y, float a)
Definition: feedforward.cpp:264
armarx::FeedforwardReflex::update_input_fromArmarX
bool update_input_fromArmarX(GazeStabInput *gs_input)
Definition: feedforward.cpp:102
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
feedforward.h
GazeStabOptions
Definition: GazeStabOptions.hh:3
armarx::FeedforwardReflex::update_output_toArmarX
void update_output_toArmarX(GazeStabOutput *gs_output)
Definition: feedforward.cpp:212
armarx::FeedforwardReflex::setBools
void setBools(bool armar4, bool velocityBased)
Definition: feedforward.cpp:239
GazeStabOutput::qd_UB_des
std::vector< double > qd_UB_des
Definition: GazeStabInputOutput.hh:33
GazeStabilization::input
GazeStabInput * input
Definition: GazeStabilization.hh:16
GazeStabInput::q_UB
std::vector< double > q_UB
Definition: GazeStabInputOutput.hh:15
GazeStabInput::q_LB
std::vector< double > q_LB
Definition: GazeStabInputOutput.hh:14
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
GazeStabilization::getIMUPred
std::vector< float > getIMUPred()
Definition: GazeStabilization.hh:60
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::FeedforwardReflex::reportJointAngles
void reportJointAngles(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition: feedforward.cpp:247
GazeStabInput::tsim
double tsim
Definition: GazeStabInputOutput.hh:13
armarx::Reflex::mutex
std::mutex mutex
Definition: reflex.h:130
GazeStabInput
Definition: GazeStabInputOutput.hh:6
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::FeedforwardReflex::calc
void calc() override
Definition: feedforward.cpp:28
armarx::FeedforwardReflex::reportJointVelocities
void reportJointVelocities(const NameValueMap &values, bool valueChanged, const Ice::Current &c)
Definition: feedforward.cpp:256
GazeStabilization::init
void init()
Definition: GazeStabilization.cc:60
GazeStabInput::qd_LB_des
Eigen::VectorXd qd_LB_des
Definition: GazeStabInputOutput.hh:20
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::Reflex::quaternionToRPY
Eigen::Vector3f quaternionToRPY(Eigen::Quaternionf q)
Definition: reflex.h:115
GazeStabilization::getMeanOptFlPred
double getMeanOptFlPred()
Definition: GazeStabilization.hh:65
armarx::Quaternion< float, 0 >
armarx::FeedforwardReflex::setRobot
void setRobot(std::string nodeSetName, std::string headIKName, RobotStateComponentInterfacePrx robotStateComponent)
Definition: feedforward.cpp:49
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
GazeStabilization::getOptFlowPred
std::vector< float > getOptFlowPred()
Definition: GazeStabilization.hh:55
GazeStabilization::control_loop
void control_loop()
Definition: GazeStabilization.cc:110
armarx::FeedforwardReflex::gyroscopeRotation_pred
std::vector< float > gyroscopeRotation_pred
Definition: feedforward.h:92
armarx::FeedforwardReflex::reportHeadTargetChanged
void reportHeadTargetChanged(const NameValueMap &targetJointAngles, const FramedPositionBasePtr &targetPosition)
Definition: feedforward.cpp:273
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
GazeStabInput::qd_LB
std::vector< double > qd_LB
Definition: GazeStabInputOutput.hh:17
armarx::FeedforwardReflex::mean_optFl_pred
double mean_optFl_pred
Definition: feedforward.h:91
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ForwardPredictor
Definition: forwardPredictor.h:11
GazeStabInput::qd_UB
std::vector< double > qd_UB
Definition: GazeStabInputOutput.hh:18