Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit f9f5338

Browse files
committed
assert in tests works even if NDEBUG is defined. and fabs->std::abs
1 parent bfaeedc commit f9f5338

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

drone_physics/body_physics.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ AccelerationType acceleration_in_body_frame(
126126
double air_v = v - wind.y;
127127
double air_w = w - wind.z;
128128

129-
double dot_u = - g * s_theta - (q*w - r*v) - d1.x/m * (air_u) - d2.x/m * air_u * std::fabs(air_u);
130-
double dot_v = + g * c_theta * s_phi - (r*u - p*w) - d1.y/m * (air_v) - d2.y/m * air_v * std::fabs(air_v);
131-
double dot_w = -T/m + g * c_theta * c_phi - (p*v - q*u) - d1.z/m * (air_w) - d2.z/m * air_w * std::fabs(air_w);
129+
double dot_u = - g * s_theta - (q*w - r*v) - d1.x/m * (air_u) - d2.x/m * air_u * std::abs(air_u);
130+
double dot_v = + g * c_theta * s_phi - (r*u - p*w) - d1.y/m * (air_v) - d2.y/m * air_v * std::abs(air_v);
131+
double dot_w = -T/m + g * c_theta * c_phi - (p*v - q*u) - d1.z/m * (air_w) - d2.z/m * air_w * std::abs(air_w);
132132
/*****************************************************************/
133133

134134
return {dot_u, dot_v, dot_w};
@@ -354,7 +354,7 @@ EulerType euler_from_quaternion(const QuaternionType& quaternion)
354354
* - https://www.docswell.com/s/Kouhei_Ito/KDVNVK-2022-06-15-193343#p3
355355
* -
356356
*/
357-
using std::atan2; using std::asin; using std::cos; using std::fabs;
357+
using std::atan2; using std::asin; using std::cos;
358358
auto [q0, q1, q2, q3] = quaternion; /* [w, x, y, z] aligned name with the book for reviewability */
359359

360360
// First, see theta, asin is nearly 1.0, cos(theta) is nearly 0.0
@@ -367,7 +367,7 @@ EulerType euler_from_quaternion(const QuaternionType& quaternion)
367367
* Here, the gimbal lock happens, and the euler angle is not unique.
368368
* Choose the one with the phi = 0, namely (0, 90, -90).
369369
**/
370-
if (fabs(cos(theta)) < 0.0001) {
370+
if (std::abs(cos(theta)) < 0.0001) {
371371
phi = 0;
372372
psi = atan2(2*(q0*q3 - q1*q2), 2*(q0*q0 + q2*q2) - 1);
373373
} else {

drone_physics/ctest.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "drone_physics_debug.h"
22
#include "drone_physics_osdep.h"
33

4+
#undef NDEBUG // force assert to work even if release bulid.
5+
46
const double PI = M_PI;
57

68
void static test_frame_all_unit_vectors_with_some_angles() {

drone_physics/glmtest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <glm/gtc/matrix_inverse.hpp>
44
#include <cmath>
55

6-
#define assert_almost_equal_dvec4(u, v) assert(std::fabs(u[0]-v[0]) < 0.001 && std::fabs(u[1]-v[1]) < 0.001 && std::fabs(u[2]-v[2]) < 0.001 && std::fabs(u[3]-v[3]) < 0.001 || (out("dvec4 different! u=", u, " "), out("v=", v), false))
6+
#define assert_almost_equal_dvec4(u, v) assert(std::abs(u[0]-v[0]) < 0.001 && std::abs(u[1]-v[1]) < 0.001 && std::abs(u[2]-v[2]) < 0.001 && std::abs(u[3]-v[3]) < 0.001 || (out("dvec4 different! u=", u, " "), out("v=", v), false))
77

88
void out(const char* msg, const glm::dvec4& v, const char* cont = "\n") {
99
std::cout << msg << "(" << v[0] << "," << v[1] << "," << v[2] << "," << v[3] << ")" << cont;
@@ -76,7 +76,7 @@ void mixer_test() {
7676
for (int i = 0; i < ROTOR_NUM; i++) {
7777
double ratio = delta_omega[i]/omega0;
7878

79-
if (std::fabs(ratio) > 0.8) {
79+
if (std::abs(ratio) > 0.8) {
8080
if (debugEnabled) {
8181
std::cout << "WARNING: thrust:" << thrust << " tx: " << torque_x << " ty: " << torque_y << " tz: " << torque_z << std::endl;
8282
std::cout << "WARNING: delta_omega("<< i << ") is too big(" << delta_omega[i]/omega0 << "%)...limited to 80%." << std::endl;

drone_physics/utest.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using namespace hako::drone_physics;
66

7+
#undef NDEBUG // force assert to work even if release bulid.
8+
79
const double PI = M_PI;
810

911
void test_frame_all_unit_vectors_with_angle0() {
@@ -45,18 +47,18 @@ void test_frame_matrix_is_unitary() {
4547
for (int i = -180; i < 180; i+=30) {
4648
VelocityType v2 = ground_vector_from_body(v1, EulerType{i * (PI/180), 0, 0});
4749
double len = length_squared(v2);
48-
assert(fabs(len - 1.0) < 0.0001);
50+
assert(std::abs(len - 1.0) < 0.0001);
4951
}
5052
for (int i = 0; i < 90; i+=30) {
5153
VelocityType v2 = ground_vector_from_body(v1, EulerType{0, i * (PI/180), 0});
5254
double len = length_squared(v2);
53-
assert(fabs(len - 1.0) < 0.0001);
55+
assert(std::abs(len - 1.0) < 0.0001);
5456
}
5557

5658
for (int i = -180; i < 360; i+=30) {
5759
VelocityType v2 = ground_vector_from_body(v1, EulerType{0, 0, i * (PI/180)});
5860
double len = length_squared(v2);
59-
assert(fabs(len - 1.0) < 0.0001);
61+
assert(std::abs(len - 1.0) < 0.0001);
6062
}
6163

6264
// conbinations
@@ -67,16 +69,16 @@ void test_frame_matrix_is_unitary() {
6769
for (int k = -180; k < 180; k+=30) {
6870
VelocityType V = ground_vector_from_body(v1, EulerType{i * (PI/180), j * (PI/180), k * (PI/180)});
6971
double len = length_squared(V);
70-
assert(fabs(len - 1.0) < 0.0001);
72+
assert(std::abs(len - 1.0) < 0.0001);
7173

7274
// bug #89 indicated that need testing (0,1,0) and (0,0,1) vectors.
7375
V = ground_vector_from_body(u1, EulerType{i * (PI/180), j * (PI/180), k * (PI/180)});
7476
len = length_squared(V);
75-
assert(fabs(len - 1.0) < 0.0001);
77+
assert(std::abs(len - 1.0) < 0.0001);
7678

7779
V = ground_vector_from_body(w1, EulerType{i * (PI/180), j * (PI/180), k * (PI/180)});
7880
len = length_squared(V);
79-
assert(fabs(len - 1.0) < 0.0001);
81+
assert(std::abs(len - 1.0) < 0.0001);
8082

8183
}
8284
}
@@ -329,7 +331,7 @@ void test_ground_angular_acceleration()
329331
void test_rotor_omega_acceleration() {
330332
double Kr = 2896, Tr = 4.5e-2, duty_rate = 1, omega = 0;
331333
double a = rotor_omega_acceleration(Kr, Tr, omega, duty_rate);
332-
assert(std::fabs(a - (Kr * duty_rate - omega) / Tr) < 0.0001);
334+
assert(std::abs(a - (Kr * duty_rate - omega) / Tr) < 0.0001);
333335

334336
std::ofstream ofs;
335337
ofs.open("omega_times_series.csv", std::ios::out);
@@ -349,7 +351,7 @@ void test_rotor_omega_acceleration() {
349351
double a = rotor_omega_acceleration(Kr, Tr, omega_a, duty_rate);
350352
double b = rotor_omega_acceleration(Vbat, R, Cq, J, K, D, omega_b, duty_rate);
351353

352-
assert(std::fabs(a - (Kr * duty_rate - omega_a) / Tr) < 0.0001);
354+
assert(std::abs(a - (Kr * duty_rate - omega_a) / Tr) < 0.0001);
353355
omega_a += a*time;
354356
omega_b += b*time;
355357

@@ -358,7 +360,7 @@ void test_rotor_omega_acceleration() {
358360
* if duty_rate is a constant, the solution is
359361
* Omega = Kr * (1 - exp(-t/Tr) * (duty rate))
360362
*/
361-
// assert(std::fabs(omega_a - Kr*(1 - std::exp(-time/Tr)*duty_rate) < 10));
363+
// assert(std::std::abs(omega_a - Kr*(1 - std::exp(-time/Tr)*duty_rate) < 10));
362364
ofs << time << "," << a*time << "," << omega_a << ", " << b*time << ", " << omega_b << std::endl;
363365
}
364366
ofs.close();

0 commit comments

Comments
 (0)