1313#include <pbio/tacho.h>
1414#include <pbio/drivebase.h>
1515
16+ // We include the hub-specific headers to let the build system
17+ // resolve the actual structure definitions.
18+ #include "pybricks/pupdevices.h"
19+ #include "pybricks/robotics.h"
20+
1621#if defined(__ARM_ARCH_7M__ ) || defined(__ARM_ARCH_7EM__ )
1722 #define IS_CORTEX_M 1
1823 #define ACCEL_RAM __attribute__((section(".data"), noinline))
2126 #define ACCEL_RAM
2227#endif
2328
24- // Manually defining internal structures.
25- // Note: Added padding to verify the tacho pointer offset.
26- typedef struct _pb_type_Motor_obj_t {
27- mp_obj_base_t base ;
28- char padding [4 ];
29- pbio_tacho_t * tacho ;
30- } pb_type_Motor_obj_t ;
31-
32- typedef struct _pb_type_DriveBase_obj_t {
33- mp_obj_base_t base ;
34- pbio_drivebase_t * db ;
35- } pb_type_DriveBase_obj_t ;
29+ // -----------------------------------------------------------------------------
30+ // Core Math Engine
31+ // -----------------------------------------------------------------------------
32+ ACCEL_RAM static float fast_sin_internal (float theta ) {
33+ float x = theta * 0.159154943f ; // INV_TWO_PI
34+ x = theta - (float )((int )(x + (x > 0 ? 0.5f : -0.5f ))) * 6.2831853f ;
35+ if (x > 1.5707963f ) x = 3.1415926f - x ;
36+ else if (x < -1.5707963f ) x = -3.1415926f - x ;
37+ float x2 = x * x ;
38+ return x * (0.99999906f + x2 * (-0.16665554f + x2 * (0.00831190f + x2 * -0.00018488f )));
39+ }
3640
3741// -----------------------------------------------------------------------------
38- // High-Speed Direct-Tacho Odometry (Diagnostic Build)
42+ // High-Speed Direct-Tacho Odometry
3943// -----------------------------------------------------------------------------
4044static mp_obj_t experimental_odometry_benchmark (size_t n_args , const mp_obj_t * args ) {
4145 int num_iters = mp_obj_get_int (args [0 ]);
4246 float wheel_circ = mp_obj_get_float (args [1 ]);
4347
48+ // Use the official Pybricks object types
4449 pb_type_Motor_obj_t * right_motor = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [2 ]);
4550 pb_type_Motor_obj_t * left_motor = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [3 ]);
4651 pb_type_DriveBase_obj_t * db_obj = (pb_type_DriveBase_obj_t * )MP_OBJ_TO_PTR (args [4 ]);
@@ -51,15 +56,11 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
5156 pbio_angle_t ang_l , ang_r ;
5257 int32_t h_mdeg ;
5358
54- // Initial capture
59+ // Initial state
5560 pbio_tacho_get_angle (left_motor -> tacho , & ang_l );
5661 pbio_tacho_get_angle (right_motor -> tacho , & ang_r );
5762 pbio_drivebase_get_state_user (db_obj -> db , NULL , NULL , & h_mdeg , NULL );
5863
59- // LOG START: Using mp_printf to guarantee it shows up in the Pybricks IDE terminal
60- mp_printf (& mp_plat_print , "C-DEBUG: TachoL=%ld TachoR=%ld Circ=%d\n" ,
61- (long )ang_l .rotations , (long )ang_r .rotations , (int )wheel_circ );
62-
6364 float last_l_mm = ((float )ang_l .rotations * 360.0f + (float )ang_l .millidegrees / 1000.0f ) * deg_to_mm ;
6465 float last_r_mm = ((float )ang_r .rotations * 360.0f + (float )ang_r .millidegrees / 1000.0f ) * deg_to_mm ;
6566 float last_lin = (last_l_mm + last_r_mm ) / 2.0f ;
@@ -80,25 +81,19 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
8081 float linear_delta = cur_lin - last_lin ;
8182 float heading_delta = cur_heading - last_heading ;
8283
83- if (fabsf (linear_delta ) > 0.0f ) {
84+ if (fabsf (linear_delta ) > 0.0001f ) {
8485 float avg_h_rad = (last_heading + (heading_delta / 2.0f )) * 0.01745329f ;
85- robot_x += linear_delta * cosf (avg_h_rad );
86- robot_y += linear_delta * sinf (avg_h_rad );
86+ robot_x += linear_delta * fast_sin_internal (avg_h_rad + 1.5707963f );
87+ robot_y += linear_delta * fast_sin_internal (avg_h_rad );
8788 }
8889
8990 last_lin = cur_lin ;
9091 last_heading = cur_heading ;
9192
92- if ((i % 5000 ) == 0 ) {
93+ if ((i % 2000 ) == 0 ) {
9394 mp_handle_pending (true);
9495 mp_hal_delay_ms (1 );
9596 }
96-
97- // LOG PROGRESS
98- if (i > 0 && (i % 1000000 ) == 0 ) {
99- mp_printf (& mp_plat_print , "C-ITER %d: X=%d Y=%d D=%d\n" ,
100- i , (int )robot_x , (int )robot_y , (int )linear_delta );
101- }
10297 }
10398
10499 uint32_t dur = mp_hal_ticks_ms () - start_time ;
0 commit comments