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-
2116#if defined(__ARM_ARCH_7M__ ) || defined(__ARM_ARCH_7EM__ )
2217 #define IS_CORTEX_M 1
2318 #define ACCEL_RAM __attribute__((section(".data"), noinline))
2621 #define ACCEL_RAM
2722#endif
2823
24+ // Manually defining internal structures to bypass "Unknown Type" errors.
25+ // This matches the memory layout used in Spike and EV3 firmware.
26+ typedef struct _pb_type_Motor_obj_t {
27+ mp_obj_base_t base ;
28+ pbio_tacho_t * tacho ;
29+ } pb_type_Motor_obj_t ;
30+
31+ typedef struct _pb_type_DriveBase_obj_t {
32+ mp_obj_base_t base ;
33+ pbio_drivebase_t * db ;
34+ } pb_type_DriveBase_obj_t ;
35+
2936// -----------------------------------------------------------------------------
30- // Core Math Engine
37+ // Core Math Engine (MiniMax Polynomial Sine)
3138// -----------------------------------------------------------------------------
3239ACCEL_RAM static float fast_sin_internal (float theta ) {
3340 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 ;
41+ x = theta - (float )((int )(x + (x > 0 ? 0.5f : -0.5f ))) * 6.2831853f ; // TWO_PI
42+ if (x > 1.5707963f ) x = 3.1415926f - x ; // HALF_PI, PI
3643 else if (x < -1.5707963f ) x = -3.1415926f - x ;
3744 float x2 = x * x ;
3845 return x * (0.99999906f + x2 * (-0.16665554f + x2 * (0.00831190f + x2 * -0.00018488f )));
@@ -45,7 +52,7 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
4552 int num_iters = mp_obj_get_int (args [0 ]);
4653 float wheel_circ = mp_obj_get_float (args [1 ]);
4754
48- // Use the official Pybricks object types
55+ // Cast raw Python objects to our manual C-struct definitions
4956 pb_type_Motor_obj_t * right_motor = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [2 ]);
5057 pb_type_Motor_obj_t * left_motor = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [3 ]);
5158 pb_type_DriveBase_obj_t * db_obj = (pb_type_DriveBase_obj_t * )MP_OBJ_TO_PTR (args [4 ]);
@@ -69,6 +76,7 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
6976 uint32_t start_time = mp_hal_ticks_ms ();
7077
7178 for (int i = 0 ; i < num_iters ; i ++ ) {
79+ // Direct Hardware Access
7280 pbio_tacho_get_angle (left_motor -> tacho , & ang_l );
7381 pbio_tacho_get_angle (right_motor -> tacho , & ang_r );
7482 pbio_drivebase_get_state_user (db_obj -> db , NULL , NULL , & h_mdeg , NULL );
@@ -83,13 +91,14 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
8391
8492 if (fabsf (linear_delta ) > 0.0001f ) {
8593 float avg_h_rad = (last_heading + (heading_delta / 2.0f )) * 0.01745329f ;
86- robot_x += linear_delta * fast_sin_internal (avg_h_rad + 1.5707963f );
87- robot_y += linear_delta * fast_sin_internal (avg_h_rad );
94+ robot_x += linear_delta * fast_sin_internal (avg_h_rad + 1.5707963f ); // cos
95+ robot_y += linear_delta * fast_sin_internal (avg_h_rad ); // sin
8896 }
8997
9098 last_lin = cur_lin ;
9199 last_heading = cur_heading ;
92100
101+ // Yield to hub system every 2000 loops
93102 if ((i % 2000 ) == 0 ) {
94103 mp_handle_pending (true);
95104 mp_hal_delay_ms (1 );
0 commit comments