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
28using namespace armarx;
29
32{
33 defineOptionalProperty<std::string>("nodeName", "HeadIMU", "");
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
41void
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
54void
56{
58 getProperty<std::string>("RobotStateComponentName").getValue());
59
60 simulator =
62
63 timestamp = 0;
64
65 sensorTask->start();
66}
67
68void
70{
71 sensorTask->stop();
72}
73
74void
75IMUSimulation::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
141std::string
143{
144 return "IMUSimulation";
145}
146
std::string timestamp()
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
IMUSimulationPropertyDefinitions(std::string prefix)
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onExitIMU() override
void onStartIMU() override
void onInitIMU() override
std::string getDefaultName() const override
InertialMeasurementUnitListenerPrx IMUTopicPrx
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
Eigen::Matrix3f toEigen() const
Definition Pose.cpp:204
static TimestampVariantPtr nowPtr()
Quaternion< float, 0 > Quaternionf
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< TimestampVariant > TimestampVariantPtr