Cheetah Software  1.0
ImuSimulator.cpp
Go to the documentation of this file.
1 
5 #include "Dynamics/spatial.h"
7 #include "Utilities/utilities.h"
8 
9 template <typename T>
11  const FBModelState<T> &robotState,
12  const FBModelStateDerivative<T> &robotStateD, Vec3<float> &acc,
13  std::uniform_real_distribution<float> &dist, const RotMat<float> &R_body) {
14  // accelerometer noise
15  fillEigenWithRandom(acc, _mt, dist);
16 
17  // gravity (should be positive when robot is upright)
18  acc += (R_body * Vec3<float>(0, 0, 9.81));
19 
20  // acceleration
22  robotState.bodyVelocity)
23  .template cast<float>();
24 }
25 
26 
27 template <typename T>
29  const FBModelState<T> &robotState,
30  const FBModelStateDerivative<T> &robotStateD, VectorNavData *data) {
31  // body orientation
33  robotState.bodyOrientation.template cast<float>());
34 
35  Quat<float> ori_quat;
36  // printf("imu ori: %f, %f, %f, %f\n",
37  // robotState.bodyOrientation[0],
38  // robotState.bodyOrientation[1],
39  // robotState.bodyOrientation[2],
40  // robotState.bodyOrientation[3] );
41 
42  // acceleration
43  computeAcceleration(robotState, robotStateD, data->accelerometer,
44  _vectornavAccelerometerDistribution, R_body);
45 
46  // gyro
47  fillEigenWithRandom(data->gyro, _mt, _vectornavGyroDistribution);
48  data->gyro +=
49  robotState.bodyVelocity.template head<3>().template cast<float>();
50 
51  // quaternion
52  if (_vectorNavOrientationNoise) {
53  Vec3<float> omegaNoise;
54  fillEigenWithRandom(omegaNoise, _mt, _vectornavQuatDistribution);
55  Quat<float> floatQuat = robotState.bodyOrientation.template cast<float>();
56  // data->quat = integrateQuat(floatQuat, omegaNoise, 1.0f);
57  ori_quat = integrateQuat(floatQuat, omegaNoise, 1.0f);
58  // printf("imu noised ori: %f, %f, %f, %f\n",
59  // robotState.bodyOrientation[0],
60  // robotState.bodyOrientation[1],
61  // robotState.bodyOrientation[2],
62  // robotState.bodyOrientation[3] );
63  } else {
64  // data->quat = robotState.bodyOrientation.template cast<float>();
65  ori_quat = robotState.bodyOrientation.template cast<float>();
66  }
67  data->quat[3] = ori_quat[0];
68  data->quat[0] = ori_quat[1];
69  data->quat[1] = ori_quat[2];
70  data->quat[2] = ori_quat[3];
71 }
72 
73 template <typename T>
75  const FBModelState<T> &robotState,
76  const FBModelStateDerivative<T> &robotStateD, CheaterState<T> &state) {
78  state.acceleration = (R_body * Vec3<T>(0, 0, 9.81)) +
80  robotStateD.dBodyVelocity, robotState.bodyVelocity);
81  state.orientation = robotState.bodyOrientation;
82  state.position = robotState.bodyPosition;
83  state.omegaBody = robotState.bodyVelocity.template head<3>();
84  state.vBody = robotState.bodyVelocity.template tail<3>();
85 }
86 
87 template class ImuSimulator<float>;
88 template class ImuSimulator<double>;
Vec3< T > bodyPosition
SVec< T > bodyVelocity
Vec3< T > vBody
Definition: IMUTypes.h:25
Simulated IMU.
void updateCheaterState(const FBModelState< T > &robotState, const FBModelStateDerivative< T > &robotStateD, CheaterState< T > &state)
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
auto spatialToLinearAcceleration(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T2 > &v)
Definition: spatial.h:289
Vec3< T > omegaBody
Definition: IMUTypes.h:24
Quat< T > orientation
Definition: IMUTypes.h:22
Vec3< float > accelerometer
Definition: IMUTypes.h:14
Quat< typename T::Scalar > integrateQuat(const Eigen::MatrixBase< T > &quat, const Eigen::MatrixBase< T2 > &omega, T3 dt)
Vec3< T > acceleration
Definition: IMUTypes.h:26
void updateVectornav(const FBModelState< T > &robotState, const FBModelStateDerivative< T > &robotStateD, VectorNavData *data)
Vec3< T > position
Definition: IMUTypes.h:23
Quat< float > quat
Definition: IMUTypes.h:16
Utility functions for 3D rotations.
void computeAcceleration(const FBModelState< T > &robotState, const FBModelStateDerivative< T > &robotStateD, Vec3< float > &acc, std::uniform_real_distribution< float > &dist, const RotMat< float > &R_body)
Mat3< typename T::Scalar > quaternionToRotationMatrix(const Eigen::MatrixBase< T > &q)
Vec3< float > gyro
Definition: IMUTypes.h:15
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18
void fillEigenWithRandom(Eigen::MatrixBase< T > &v, std::mt19937 &gen, std::uniform_real_distribution< typename T::Scalar > &dist)
Definition: utilities.h:75
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Quat< T > bodyOrientation
Utility functions for manipulating spatial quantities.
MX f(const MX &x, const MX &u)