1313#include <pbio/tacho.h>
1414#include <pbio/drivebase.h>
1515
16- // MANUALLY PROTOTYPE THE FIRMWARE GETTERS
17- // This bypasses the header visibility issues while remaining safe.
18- struct _pbio_tacho_t ;
19- struct _pbio_drivebase_t ;
20- extern struct _pbio_tacho_t * pb_type_Motor_get_tacho (mp_obj_t obj );
21- extern struct _pbio_drivebase_t * pb_type_drivebase_get_drivebase (mp_obj_t obj );
22-
2316#if defined(__ARM_ARCH_7M__ ) || defined(__ARM_ARCH_7EM__ )
2417 #define IS_CORTEX_M 1
2518 #define ACCEL_RAM __attribute__((section(".data"), noinline))
@@ -28,6 +21,18 @@ extern struct _pbio_drivebase_t *pb_type_drivebase_get_drivebase(mp_obj_t obj);
2821 #define ACCEL_RAM
2922#endif
3023
24+ // We define our own version of the objects based on the observed
25+ // memory layout of the Spike Prime 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+
3136// -----------------------------------------------------------------------------
3237// Core Math Engine
3338// -----------------------------------------------------------------------------
@@ -41,27 +46,27 @@ ACCEL_RAM static float fast_sin_internal(float theta) {
4146}
4247
4348// -----------------------------------------------------------------------------
44- // High-Speed API-Based Odometry
49+ // High-Speed Odometry
4550// -----------------------------------------------------------------------------
4651static mp_obj_t experimental_odometry_benchmark (size_t n_args , const mp_obj_t * args ) {
4752 int num_iters = mp_obj_get_int (args [0 ]);
4853 float wheel_circ = mp_obj_get_float (args [1 ]);
4954
50- // Retrieve pointers using the prototyped firmware functions
51- pbio_tacho_t * right_tacho = (pbio_tacho_t * )pb_type_Motor_get_tacho (args [2 ]);
52- pbio_tacho_t * left_tacho = (pbio_tacho_t * )pb_type_Motor_get_tacho (args [3 ]);
53- pbio_drivebase_t * db = (pbio_drivebase_t * )pb_type_drivebase_get_drivebase (args [4 ]);
55+ // Direct casting from the Python object to our defined struct
56+ pb_type_Motor_obj_t * m_right = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [2 ]);
57+ pb_type_Motor_obj_t * m_left = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [3 ]);
58+ pb_type_DriveBase_obj_t * db_obj = (pb_type_DriveBase_obj_t * )MP_OBJ_TO_PTR (args [4 ]);
5459
5560 float deg_to_mm = wheel_circ / 360.0f ;
5661 float robot_x = 0.0f , robot_y = 0.0f ;
5762
5863 pbio_angle_t ang_l , ang_r ;
5964 int32_t h_mdeg ;
6065
61- // Capture initial state
62- pbio_tacho_get_angle (left_tacho , & ang_l );
63- pbio_tacho_get_angle (right_tacho , & ang_r );
64- pbio_drivebase_get_state_user (db , NULL , NULL , & h_mdeg , NULL );
66+ // Initial capture
67+ pbio_tacho_get_angle (m_left -> tacho , & ang_l );
68+ pbio_tacho_get_angle (m_right -> tacho , & ang_r );
69+ pbio_drivebase_get_state_user (db_obj -> db , NULL , NULL , & h_mdeg , NULL );
6570
6671 float last_l_mm = ((float )ang_l .rotations * 360.0f + (float )ang_l .millidegrees / 1000.0f ) * deg_to_mm ;
6772 float last_r_mm = ((float )ang_r .rotations * 360.0f + (float )ang_r .millidegrees / 1000.0f ) * deg_to_mm ;
@@ -71,9 +76,10 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
7176 uint32_t start_time = mp_hal_ticks_ms ();
7277
7378 for (int i = 0 ; i < num_iters ; i ++ ) {
74- pbio_tacho_get_angle (left_tacho , & ang_l );
75- pbio_tacho_get_angle (right_tacho , & ang_r );
76- pbio_drivebase_get_state_user (db , NULL , NULL , & h_mdeg , NULL );
79+ // We use the pointers we grabbed directly from the structs
80+ pbio_tacho_get_angle (m_left -> tacho , & ang_l );
81+ pbio_tacho_get_angle (m_right -> tacho , & ang_r );
82+ pbio_drivebase_get_state_user (db_obj -> db , NULL , NULL , & h_mdeg , NULL );
7783
7884 float cur_l_mm = ((float )ang_l .rotations * 360.0f + (float )ang_l .millidegrees / 1000.0f ) * deg_to_mm ;
7985 float cur_r_mm = ((float )ang_r .rotations * 360.0f + (float )ang_r .millidegrees / 1000.0f ) * deg_to_mm ;
@@ -92,7 +98,6 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
9298 last_lin = cur_lin ;
9399 last_heading = cur_heading ;
94100
95- // Yield for system stability every 2000 loops
96101 if ((i % 2000 ) == 0 ) {
97102 mp_handle_pending (true);
98103 mp_hal_delay_ms (1 );
0 commit comments