Cheetah Software  1.0
test_cheetah3_model.cpp
Go to the documentation of this file.
1 
8 #include "Dynamics/Quadruped.h"
9 #include "Utilities/utilities.h"
10 //#include "DynamicsSimulator.h"
12 
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15 
16 using namespace spatial;
17 
18 #include <stdio.h>
19 using namespace std;
20 
25 TEST(Cheetah3, simulatorDynamicsDoesntCrashCheetah3) {
26  FloatingBaseModel<double> cheetah = buildCheetah3<double>().buildModel();
27  DVec<double> tau(12);
29  _dstate.qdd = DVec<double>::Zero(cheetah._nDof - 6);
30 
31  cheetah.forwardKinematics();
32  cheetah.runABA(tau, _dstate);
33 }
34 
40 TEST(Cheetah3, simulatorContactInertiaCheetah3) {
41  FloatingBaseModel<double> cheetahModel = buildCheetah3<double>().buildModel();
42 
43  RotMat<double> rBody = coordinateRotation(CoordinateAxis::X, .123) *
44  coordinateRotation(CoordinateAxis::Z, .232) *
45  coordinateRotation(CoordinateAxis::Y, .111);
46  SVec<double> bodyVel;
47  bodyVel << 1, 2, 3, 4, 5, 6;
49  DVec<double> q(12);
50  DVec<double> dq(12);
51  DVec<double> tauref(12);
52  for (size_t i = 0; i < 12; i++) {
53  q[i] = i + 1;
54  dq[i] = (i + 1) * 2;
55  tauref[i] = (i + 1) * -30.;
56  }
57 
58  // set state
59  x.bodyOrientation = rotationMatrixToQuaternion(rBody.transpose());
60  x.bodyVelocity = bodyVel;
61  x.bodyPosition = Vec3<double>(6, 7, 8);
62  x.q = q;
63  x.qd = dq;
64 
65  // do aba
66  cheetahModel.setState(x);
67 
68  cheetahModel.contactJacobians();
69  DMat<double> H = cheetahModel.massMatrix();
70  D3Mat<double> J0 = cheetahModel._Jc[15];
71  DMat<double> LambdaInv1 = J0 * H.colPivHouseholderQr().solve(J0.transpose());
72 
73  D6Mat<double> forceOnly = D6Mat<double>::Zero(6, 3);
74  forceOnly.bottomRows<3>() = Mat3<double>::Identity();
75 
76  DMat<double> LambdaInv2 = cheetahModel.invContactInertia(15, forceOnly);
77 
78  EXPECT_TRUE(almostEqual(LambdaInv1, LambdaInv2, .001));
79 }
80 
86 TEST(Cheetah3, simulatorTestForceCheetah3) {
87  FloatingBaseModel<double> cheetahModel = buildCheetah3<double>().buildModel();
88 
89  RotMat<double> rBody = coordinateRotation(CoordinateAxis::X, .123) *
90  coordinateRotation(CoordinateAxis::Z, .232) *
91  coordinateRotation(CoordinateAxis::Y, .111);
92  SVec<double> bodyVel;
93  bodyVel << 1, 2, 3, 4, 5, 6;
95  DVec<double> q(12);
96  DVec<double> dq(12);
97  DVec<double> tauref(12);
98  for (size_t i = 0; i < 12; i++) {
99  q[i] = i + 1;
100  dq[i] = (i + 1) * 2;
101  tauref[i] = (i + 1) * -30.;
102  }
103 
104  // set state
105  x.bodyOrientation = rotationMatrixToQuaternion(rBody.transpose());
106  x.bodyVelocity = bodyVel;
107  x.bodyPosition = Vec3<double>(6, 7, 8);
108  x.q = q;
109  x.qd = dq;
110 
112  dx.qdd.setZero(12);
113 
114  cheetahModel.setState(x);
115 
116  cheetahModel.contactJacobians();
117  DMat<double> H = cheetahModel.massMatrix();
118  DMat<double> J0 = cheetahModel._Jc[15].bottomRows(1);
119 
120  Vec3<double> zforce;
121  zforce << 0, 0, 1;
122 
123  double foot_accel_in_z = cheetahModel.applyTestForce(15, zforce, dx);
124 
125  DVec<double> qddFull = H.colPivHouseholderQr().solve(J0.transpose());
126  DMat<double> LambdaInv = J0 * qddFull;
127 
128  SVec<double> dBodyVelocity = qddFull.head<6>();
129  DVec<double> qdd = qddFull.tail(12);
130 
131  double foot_accel_2 = LambdaInv(0, 0);
132 
133  double LambdaInv2 = cheetahModel.invContactInertia(15, zforce);
134 
135  EXPECT_TRUE(almostEqual(dBodyVelocity, dx.dBodyVelocity, .001));
136  EXPECT_TRUE(almostEqual(qdd, dx.qdd, 1e-6));
137  EXPECT_TRUE(abs(foot_accel_2 - foot_accel_in_z) < .001);
138  EXPECT_TRUE(abs(LambdaInv2 - foot_accel_in_z) < .001);
139 
140  qdd(0) += 5;
141  EXPECT_FALSE(almostEqual(qdd, dx.qdd, 1e-6));
142 }
Vec3< T > bodyPosition
void runABA(const DVec< T > &tau, FBModelStateDerivative< T > &dstate)
SVec< T > bodyVelocity
typename Eigen::Matrix< T, 6, Eigen::Dynamic > D6Mat
Definition: cppTypes.h:110
Mat3< T > coordinateRotation(CoordinateAxis axis, T theta)
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
Data structure containing parameters for quadruped robot.
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
Utility function to build a Cheetah 3 Quadruped object.
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
typename Eigen::Matrix< T, 3, Eigen::Dynamic > D3Mat
Definition: cppTypes.h:114
typename Eigen::Matrix< T, 6, 1 > SVec
Definition: cppTypes.h:62
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
TEST(Cheetah3, simulatorDynamicsDoesntCrashCheetah3)
void setState(const FBModelState< T > &state)
DMat< T > invContactInertia(const int gc_index, const D6Mat< T > &force_directions)
T applyTestForce(const int gc_index, const Vec3< T > &force_ics_at_contact, FBModelStateDerivative< T > &dstate_out)
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102
vectorAligned< D3Mat< T > > _Jc
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18
Implementation of Rigid Body Floating Base model data structure.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Quat< T > bodyOrientation