1515 #define ACCEL_RAM __attribute__((section(".data"), noinline))
1616#else
1717 #define IS_CORTEX_M 0
18- #define ACCEL_RAM
18+ #define ACCEL_RAM
1919#endif
2020
2121// Constants
22- static const float PI_F = 3.141592653589793f ;
23- static const float TWO_PI_F = 6.283185307179586f ;
24- static const float HALF_PI_F = 1.570796326794896f ;
25- static const float INV_TWO_PI_F = 0.159154943091895f ;
22+ static const float PI_F = 3.141592653589793f ;
23+ static const float TWO_PI_F = 6.283185307179586f ;
24+ static const float HALF_PI_F = 1.570796326794896f ;
25+ static const float INV_TWO_PI_F = 0.159154943091895f ;
2626
2727// -----------------------------------------------------------------------------
2828// Core Math Engine (Lasse Schlör Absolute Error Optimized)
@@ -31,17 +31,20 @@ ACCEL_RAM static float fast_sin_internal(float theta) {
3131 float x = theta * INV_TWO_PI_F ;
3232 x = theta - (float )((int )(x + (x > 0 ? 0.5f : -0.5f ))) * TWO_PI_F ;
3333
34- if (x > HALF_PI_F ) { x = PI_F - x ; }
35- else if (x < - HALF_PI_F ) { x = - PI_F - x ; }
34+ if (x > HALF_PI_F ) {
35+ x = PI_F - x ;
36+ } else if (x < - HALF_PI_F ) {
37+ x = - PI_F - x ;
38+ }
3639
3740 float x2 = x * x ;
3841 #if IS_CORTEX_M
39- float res = -0.0001848814f ;
40- res = 0.0083119000f + (x2 * res );
41- res = -0.1666555409f + (x2 * res );
42- return x * (0.9999990609f + (x2 * res ));
42+ float res = -0.0001848814f ;
43+ res = 0.0083119000f + (x2 * res );
44+ res = -0.1666555409f + (x2 * res );
45+ return x * (0.9999990609f + (x2 * res ));
4346 #else
44- return x * (0.9999990609f + x2 * (-0.1666555409f + x2 * (0.0083119000f + x2 * -0.0001848814f )));
47+ return x * (0.9999990609f + x2 * (-0.1666555409f + x2 * (0.0083119000f + x2 * -0.0001848814f )));
4548 #endif
4649}
4750
@@ -67,8 +70,8 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
6770 mp_obj_t left_angle_func = args [3 ];
6871 mp_obj_t db_angle_func = args [4 ];
6972
70- float deg_to_mm = wheel_circ / 720.0f ;
71-
73+ float deg_to_mm = wheel_circ / 720.0f ;
74+
7275 // State variables
7376 float robot_x = 0.0f , robot_y = 0.0f ;
7477 float last_linear = 0.0f ;
@@ -78,10 +81,10 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
7881
7982 // 2. The Core High-Speed C Loop
8083 for (int i = 0 ; i < num_iters ; i ++ ) {
81-
84+
8285 // Execute the exact same hardware getters as your Python script
8386 float right_angle = get_float_from_obj (mp_call_function_0 (right_angle_func ));
84- float left_angle = get_float_from_obj (mp_call_function_0 (left_angle_func ));
87+ float left_angle = get_float_from_obj (mp_call_function_0 (left_angle_func ));
8588 float robot_heading = get_float_from_obj (mp_call_function_0 (db_angle_func ));
8689
8790 // Exactly mirroring your Python math logic
@@ -95,13 +98,15 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
9598 if (fabsf (linear ) > 0.0f ) {
9699 float avg_heading_deg = last_heading - (heading_difference / 2.0f );
97100 float avg_heading_rad = avg_heading_deg * 0.0174532925f ; // DEG_TO_RAD
98-
101+
99102 robot_x += linear * fast_sin_internal (avg_heading_rad + HALF_PI_F ); // cos
100103 robot_y += linear * fast_sin_internal (avg_heading_rad ); // sin
101104 }
102105
103106 // Safety: Prevent watchdog timeouts during massive loops
104- if ((i % 1000 ) == 0 ) { mp_handle_pending (true); }
107+ if ((i % 1000 ) == 0 ) {
108+ mp_handle_pending (true);
109+ }
105110 }
106111
107112 uint32_t duration_ms = mp_hal_ticks_ms () - start_time ;
@@ -122,8 +127,8 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(experimental_odometry_benchmark_obj,
122127
123128// Module Globals
124129static const mp_rom_map_elem_t experimental_globals_table [] = {
125- { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_experimental ) },
126- { MP_ROM_QSTR (MP_QSTR_odometry_benchmark ), MP_ROM_PTR (& experimental_odometry_benchmark_obj ) },
130+ { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_experimental ) },
131+ { MP_ROM_QSTR (MP_QSTR_odometry_benchmark ), MP_ROM_PTR (& experimental_odometry_benchmark_obj ) },
127132};
128133static MP_DEFINE_CONST_DICT (pb_module_experimental_globals , experimental_globals_table ) ;
129134
0 commit comments