55
66namespace rlenvscpp {
77namespace dynamics {
8+
9+
10+
811
912
1013QuadrotorDynamics::QuadrotorDynamics (QuadrotorDynamicsConfig config,
@@ -97,25 +100,25 @@ QuadrotorDynamics::update_rotation_matrix_(){
97100 const auto psi = euler_angles_old[2 ];
98101
99102
100- auto ctheta = std::cos (theta);
101- auto cpsi = std::cos (psi);
102- auto cphi = std::cos (phi);
103+ auto c_theta = std::cos (theta);
104+ auto c_psi = std::cos (psi);
105+ auto c_phi = std::cos (phi);
103106
104- auto stheta = std::sin (theta);
105- auto spsi = std::sin (psi);
106- auto sphi = std::sin (phi);
107+ auto s_theta = std::sin (theta);
108+ auto s_psi = std::sin (psi);
109+ auto s_phi = std::sin (phi);
107110
108- rotation_mat_ (0 ,0 ) = ctheta * cpsi ;
109- rotation_mat_ (0 ,1 ) = spsi * stheta * cpsi - cphi * spsi ;
110- rotation_mat_ (0 ,2 ) = cphi * stheta * cpsi + sphi * spsi ;
111+ rotation_mat_ (0 ,0 ) = c_theta * c_psi ;
112+ rotation_mat_ (0 ,1 ) = s_psi * s_theta * c_psi - c_phi * s_psi ;
113+ rotation_mat_ (0 ,2 ) = c_phi * s_theta * c_psi + s_phi * s_psi ;
111114
112- rotation_mat_ (1 ,0 ) = ctheta * spsi ;
113- rotation_mat_ (1 ,1 ) = sphi * stheta * spsi + cphi * cpsi ;
114- rotation_mat_ (1 ,2 ) = cphi * stheta * spsi - sphi * cpsi ;
115+ rotation_mat_ (1 ,0 ) = c_theta * s_psi ;
116+ rotation_mat_ (1 ,1 ) = s_phi * s_theta * s_psi + c_phi * c_psi ;
117+ rotation_mat_ (1 ,2 ) = c_phi * s_theta * s_psi - s_phi * c_psi ;
115118
116- rotation_mat_ (0 ,0 ) = stheta ;
117- rotation_mat_ (0 ,1 ) = -sphi * ctheta ;
118- rotation_mat_ (0 ,2 ) = -cphi * ctheta ;
119+ rotation_mat_ (2 ,0 ) = s_theta ;
120+ rotation_mat_ (2 ,1 ) = -s_phi * c_theta ;
121+ rotation_mat_ (2 ,2 ) = -c_phi * c_theta ;
119122}
120123
121124void
@@ -179,7 +182,14 @@ QuadrotorDynamics::translational_dynamics(const RealVec& motor_w){
179182 RealColVec3d fg = RealColVec3d::Zero (3 );
180183
181184 if (config_.use_gravity ){
182- fg[2 ] = mass * rlenvscpp::consts::maths::G;
185+
186+ const auto phi_ = phi ();
187+ const auto theta_ = theta ();
188+
189+ // we do not contain the mass constant so don't use it below
190+ fg[0 ] = -rlenvscpp::consts::maths::G * std::sin (theta_);
191+ fg[1 ] = rlenvscpp::consts::maths::G * std::cos (theta_) * std::sin (phi_);
192+ fg[2 ] = rlenvscpp::consts::maths::G * std::cos (theta_) * std::cos (phi_);
183193 }
184194
185195 // compute the total thurst force
@@ -192,8 +202,10 @@ QuadrotorDynamics::translational_dynamics(const RealVec& motor_w){
192202 // form the vector ωb/i × v
193203 RealColVec3d omega_cross_v = old_omega.cross (old_v);
194204
195- // compute the velocity increment
196- RealColVec3d v = dt * omega_cross_v + (1.0 / mass ) * dt * fg - (1.0 / mass ) * dt * ft - old_v;
205+ // compute the velocity increment.
206+ // Note that the mass is dropped when accounting for gravity
207+ // as fg does not contain it
208+ RealColVec3d v = dt * omega_cross_v + dt * fg - (1.0 / mass ) * dt * ft - old_v;
197209
198210 // compute the time velocity derivative
199211 v_dot_ = (v - old_v) / dt;
0 commit comments