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 
30 
31 
34 {
35  defineOptionalProperty<std::string>("nodeName", "HeadIMU", "");
36  defineOptionalProperty<std::string>("SimulatorName", "Simulator", "Name of the simulator component that should be used");
37  defineOptionalProperty<std::string>("RobotStateComponentName", "RobotStateComponent", "Name of the robot state component that should be used");
38 }
39 
40 
42 {
43  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
44 
45  nodeName = getProperty<std::string>("nodeName").getValue();
46 
47  sensorTask = new PeriodicTask<IMUSimulation>(this, &IMUSimulation::frameAcquisitionTaskLoop, 50);
48 
49  usingProxy(getProperty<std::string>("SimulatorName").getValue());
50 }
51 
52 
54 {
55  robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(getProperty<std::string>("RobotStateComponentName").getValue());
56 
57  simulator = getProxy<SimulatorInterfacePrx>(getProperty<std::string>("SimulatorName").getValue());
58 
59  timestamp = 0;
60 
61  sensorTask->start();
62 }
63 
64 
66 {
67  sensorTask->stop();
68 }
69 
70 
71 void IMUSimulation::frameAcquisitionTaskLoop()
72 {
74 
75  auto robotLinearVelocityAsync = simulator->begin_getRobotLinearVelocity(robotStateComponent->getRobotName(), nodeName);
76  auto robotAngularVelocityAsync = simulator->begin_getRobotAngularVelocity(robotStateComponent->getRobotName(), nodeName);
77 
78  SharedRobotNodeInterfacePrx robotNode = robotStateComponent->getSynchronizedRobot()->getRobotNode(nodeName);
79  FramedPoseBasePtr poseInRootFrame = robotNode->getGlobalPose();
80 
81  Eigen::Quaternionf currentOrientation = QuaternionPtr::dynamicCast(poseInRootFrame->orientation)->toEigenQuaternion();
82  Eigen::Vector3f currentPosition = Vector3Ptr::dynamicCast(poseInRootFrame->position)->toEigen() / 1000.0;
83 
84  float deltaTime = (now->timestamp - timestamp) * std::pow(10, -6);
85 
86  Vector3BasePtr robotLinearVelocity = simulator->end_getRobotLinearVelocity(robotLinearVelocityAsync);
87  Vector3BasePtr robotAngularVelocity = simulator->end_getRobotAngularVelocity(robotAngularVelocityAsync);
88 
89 
90  // convert to SI units
91  Eigen::Vector3f currentLinearVelocity = orientation.toRotationMatrix().transpose() * Vector3Ptr::dynamicCast(robotLinearVelocity)->toEigen() / 1000.0f;
92  Eigen::Vector3f angularVelocity = orientation.toRotationMatrix().transpose() * Vector3Ptr::dynamicCast(robotAngularVelocity)->toEigen();
93 
94 
95  if (timestamp)
96  {
97  Eigen::Vector3f acceleration = (currentLinearVelocity - linearVelocity) / deltaTime;
98 
99  IMUData data;
100 
101  data.acceleration.push_back(acceleration(0));
102  data.acceleration.push_back(acceleration(1));
103  data.acceleration.push_back(acceleration(2));
104 
105  data.gyroscopeRotation.push_back(angularVelocity(0));
106  data.gyroscopeRotation.push_back(angularVelocity(1));
107  data.gyroscopeRotation.push_back(angularVelocity(2));
108 
109  data.magneticRotation.push_back(0.0);
110  data.magneticRotation.push_back(0.0);
111  data.magneticRotation.push_back(0.0);
112 
113  data.orientationQuaternion.push_back(currentOrientation.w());
114  data.orientationQuaternion.push_back(currentOrientation.x());
115  data.orientationQuaternion.push_back(currentOrientation.y());
116  data.orientationQuaternion.push_back(currentOrientation.z());
117 
118  IMUTopicPrx->reportSensorValues(getName(), "name", data, now);
119  }
120 
121  orientation = currentOrientation;
122  position = currentPosition;
123  timestamp = now->timestamp;
124  linearVelocity = currentLinearVelocity;
125 }
126 
127 
128 std::string IMUSimulation::getDefaultName() const
129 {
130  return "IMUSimulation";
131 }
132 
133 
135 {
137 }
138 
armarx::IMUSimulation::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: IMUSimulation.cpp:134
armarx::Quaternion::toEigenQuaternion
Eigen::Quaternionf toEigenQuaternion() const
Definition: Pose.cpp:210
armarx::InertialMeasurementUnit::IMUTopicPrx
InertialMeasurementUnitListenerPrx IMUTopicPrx
Definition: InertialMeasurementUnit.h:101
armarx::IMUSimulationPropertyDefinitions::IMUSimulationPropertyDefinitions
IMUSimulationPropertyDefinitions(std::string prefix)
Definition: IMUSimulation.cpp:32
armarx::IMUSimulationPropertyDefinitions
Definition: IMUSimulation.h:49
armarx::TimestampVariant::nowPtr
static TimestampVariantPtr nowPtr()
Definition: TimestampVariant.h:111
IceInternal::Handle< TimestampVariant >
IMUSimulation.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::IMUSimulation::onStartIMU
void onStartIMU() override
Definition: IMUSimulation.cpp:53
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:74
armarx::Quaternion< float, 0 >
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::IMUSimulation::onExitIMU
void onExitIMU() override
Definition: IMUSimulation.cpp:65
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::PeriodicTask
Definition: ArmarXManager.h:70
armarx::IMUSimulation::onInitIMU
void onInitIMU() override
Definition: IMUSimulation.cpp:41
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::IMUSimulation::getDefaultName
std::string getDefaultName() const override
Definition: IMUSimulation.cpp:128
armarx::InertialMeasurementUnitPropertyDefinitions
Definition: InertialMeasurementUnit.h:39
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:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28