IMUSimulation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2013-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 ArmarXSimulation::ArmarXObjects::IMUSimulation
19  * @author Markus Grotz ( markus dot grotz at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "IMUSimulation.h"
26 
27 
28 using namespace armarx;
29 
32 {
33  defineOptionalProperty<std::string>("nodeName", "HeadIMU", "");
34  defineOptionalProperty<std::string>(
35  "SimulatorName", "Simulator", "Name of the simulator component that should be used");
36  defineOptionalProperty<std::string>("RobotStateComponentName",
37  "RobotStateComponent",
38  "Name of the robot state component that should be used");
39 }
40 
41 void
43 {
44  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
45 
46  nodeName = getProperty<std::string>("nodeName").getValue();
47 
48  sensorTask =
49  new PeriodicTask<IMUSimulation>(this, &IMUSimulation::frameAcquisitionTaskLoop, 50);
50 
51  usingProxy(getProperty<std::string>("SimulatorName").getValue());
52 }
53 
54 void
56 {
57  robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(
58  getProperty<std::string>("RobotStateComponentName").getValue());
59 
60  simulator =
61  getProxy<SimulatorInterfacePrx>(getProperty<std::string>("SimulatorName").getValue());
62 
63  timestamp = 0;
64 
65  sensorTask->start();
66 }
67 
68 void
70 {
71  sensorTask->stop();
72 }
73 
74 void
75 IMUSimulation::frameAcquisitionTaskLoop()
76 {
78 
79  auto robotLinearVelocityAsync =
80  simulator->begin_getRobotLinearVelocity(robotStateComponent->getRobotName(), nodeName);
81  auto robotAngularVelocityAsync =
82  simulator->begin_getRobotAngularVelocity(robotStateComponent->getRobotName(), nodeName);
83 
84  SharedRobotNodeInterfacePrx robotNode =
85  robotStateComponent->getSynchronizedRobot()->getRobotNode(nodeName);
86  FramedPoseBasePtr poseInRootFrame = robotNode->getGlobalPose();
87 
88  Eigen::Quaternionf currentOrientation =
89  QuaternionPtr::dynamicCast(poseInRootFrame->orientation)->toEigenQuaternion();
90  Eigen::Vector3f currentPosition =
91  Vector3Ptr::dynamicCast(poseInRootFrame->position)->toEigen() / 1000.0;
92 
93  float deltaTime = (now->timestamp - timestamp) * std::pow(10, -6);
94 
95  Vector3BasePtr robotLinearVelocity =
96  simulator->end_getRobotLinearVelocity(robotLinearVelocityAsync);
97  Vector3BasePtr robotAngularVelocity =
98  simulator->end_getRobotAngularVelocity(robotAngularVelocityAsync);
99 
100 
101  // convert to SI units
102  Eigen::Vector3f currentLinearVelocity =
103  orientation.toRotationMatrix().transpose() *
104  Vector3Ptr::dynamicCast(robotLinearVelocity)->toEigen() / 1000.0f;
105  Eigen::Vector3f angularVelocity = orientation.toRotationMatrix().transpose() *
106  Vector3Ptr::dynamicCast(robotAngularVelocity)->toEigen();
107 
108 
109  if (timestamp)
110  {
111  Eigen::Vector3f acceleration = (currentLinearVelocity - linearVelocity) / deltaTime;
112 
113  IMUData data;
114 
115  data.acceleration.push_back(acceleration(0));
116  data.acceleration.push_back(acceleration(1));
117  data.acceleration.push_back(acceleration(2));
118 
119  data.gyroscopeRotation.push_back(angularVelocity(0));
120  data.gyroscopeRotation.push_back(angularVelocity(1));
121  data.gyroscopeRotation.push_back(angularVelocity(2));
122 
123  data.magneticRotation.push_back(0.0);
124  data.magneticRotation.push_back(0.0);
125  data.magneticRotation.push_back(0.0);
126 
127  data.orientationQuaternion.push_back(currentOrientation.w());
128  data.orientationQuaternion.push_back(currentOrientation.x());
129  data.orientationQuaternion.push_back(currentOrientation.y());
130  data.orientationQuaternion.push_back(currentOrientation.z());
131 
132  IMUTopicPrx->reportSensorValues(getName(), "name", data, now);
133  }
134 
135  orientation = currentOrientation;
136  position = currentPosition;
137  timestamp = now->timestamp;
138  linearVelocity = currentLinearVelocity;
139 }
140 
141 std::string
143 {
144  return "IMUSimulation";
145 }
146 
149 {
152 }
armarx::IMUSimulation::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: IMUSimulation.cpp:148
armarx::Quaternion::toEigenQuaternion
Eigen::Quaternionf toEigenQuaternion() const
Definition: Pose.cpp:210
armarx::InertialMeasurementUnit::IMUTopicPrx
InertialMeasurementUnitListenerPrx IMUTopicPrx
Definition: InertialMeasurementUnit.h:103
armarx::IMUSimulationPropertyDefinitions::IMUSimulationPropertyDefinitions
IMUSimulationPropertyDefinitions(std::string prefix)
Definition: IMUSimulation.cpp:30
armarx::IMUSimulationPropertyDefinitions
Definition: IMUSimulation.h:47
armarx::TimestampVariant::nowPtr
static TimestampVariantPtr nowPtr()
Definition: TimestampVariant.h:126
IceInternal::Handle< TimestampVariant >
IMUSimulation.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::IMUSimulation::onStartIMU
void onStartIMU() override
Definition: IMUSimulation.cpp:55
armarx::Quaternion::toEigen
Eigen::Matrix3f toEigen() const
Definition: Pose.cpp:204
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::Quaternion< float, 0 >
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::IMUSimulation::onExitIMU
void onExitIMU() override
Definition: IMUSimulation.cpp:69
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::PeriodicTask
Definition: ArmarXManager.h:70
armarx::IMUSimulation::onInitIMU
void onInitIMU() override
Definition: IMUSimulation.cpp:42
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::IMUSimulation::getDefaultName
std::string getDefaultName() const override
Definition: IMUSimulation.cpp:142
armarx::InertialMeasurementUnitPropertyDefinitions
Definition: InertialMeasurementUnit.h:40
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27