99#include "py/obj.h"
1010#include "py/runtime.h"
1111#include <math.h>
12- #include <stdio.h> // For printf debugging
1312
1413#include <pbio/tacho.h>
1514#include <pbio/drivebase.h>
1615
17- // Manually defining internal structures with padding to ensure we hit the right offset
16+ #if defined(__ARM_ARCH_7M__ ) || defined(__ARM_ARCH_7EM__ )
17+ #define IS_CORTEX_M 1
18+ #define ACCEL_RAM __attribute__((section(".data"), noinline))
19+ #else
20+ #define IS_CORTEX_M 0
21+ #define ACCEL_RAM
22+ #endif
23+
24+ // Manually defining internal structures.
25+ // Note: Added padding to verify the tacho pointer offset.
1826typedef struct _pb_type_Motor_obj_t {
1927 mp_obj_base_t base ;
20- char padding [4 ]; // Safety padding for different firmware versions
28+ char padding [4 ];
2129 pbio_tacho_t * tacho ;
2230} pb_type_Motor_obj_t ;
2331
@@ -26,20 +34,13 @@ typedef struct _pb_type_DriveBase_obj_t {
2634 pbio_drivebase_t * db ;
2735} pb_type_DriveBase_obj_t ;
2836
29- // Constants
30- static const float PI_F = 3.141592653589793f ;
31- static const float HALF_PI_F = 1.570796326794896f ;
32-
33- // Minimal Sin for testing
34- static float fast_sin_internal (float x ) {
35- return sinf (x ); // Using standard math for debug version to rule out poly errors
36- }
37-
37+ // -----------------------------------------------------------------------------
38+ // High-Speed Direct-Tacho Odometry (Diagnostic Build)
39+ // -----------------------------------------------------------------------------
3840static mp_obj_t experimental_odometry_benchmark (size_t n_args , const mp_obj_t * args ) {
3941 int num_iters = mp_obj_get_int (args [0 ]);
4042 float wheel_circ = mp_obj_get_float (args [1 ]);
4143
42- // Re-verify object pointers
4344 pb_type_Motor_obj_t * right_motor = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [2 ]);
4445 pb_type_Motor_obj_t * left_motor = (pb_type_Motor_obj_t * )MP_OBJ_TO_PTR (args [3 ]);
4546 pb_type_DriveBase_obj_t * db_obj = (pb_type_DriveBase_obj_t * )MP_OBJ_TO_PTR (args [4 ]);
@@ -50,16 +51,18 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
5051 pbio_angle_t ang_l , ang_r ;
5152 int32_t h_mdeg ;
5253
53- // DEBUG: Print initial values to see if C can even talk to the hardware
54+ // Initial capture
5455 pbio_tacho_get_angle (left_motor -> tacho , & ang_l );
5556 pbio_tacho_get_angle (right_motor -> tacho , & ang_r );
56- printf ("DEBUG START: L_Rot=%ld, R_Rot=%ld, Circ=%f\n" , (long )ang_l .rotations , (long )ang_r .rotations , (double )wheel_circ );
57+ pbio_drivebase_get_state_user (db_obj -> db , NULL , NULL , & h_mdeg , NULL );
58+
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 );
5762
5863 float last_l_mm = ((float )ang_l .rotations * 360.0f + (float )ang_l .millidegrees / 1000.0f ) * deg_to_mm ;
5964 float last_r_mm = ((float )ang_r .rotations * 360.0f + (float )ang_r .millidegrees / 1000.0f ) * deg_to_mm ;
6065 float last_lin = (last_l_mm + last_r_mm ) / 2.0f ;
61-
62- pbio_drivebase_get_state_user (db_obj -> db , NULL , NULL , & h_mdeg , NULL );
6366 float last_heading = (float )h_mdeg / 1000.0f ;
6467
6568 uint32_t start_time = mp_hal_ticks_ms ();
@@ -79,8 +82,8 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
7982
8083 if (fabsf (linear_delta ) > 0.0f ) {
8184 float avg_h_rad = (last_heading + (heading_delta / 2.0f )) * 0.01745329f ;
82- robot_x += linear_delta * cosf (avg_h_rad ); // Using standard cosf for debug
83- robot_y += linear_delta * sinf (avg_h_rad ); // Using standard sinf for debug
85+ robot_x += linear_delta * cosf (avg_h_rad );
86+ robot_y += linear_delta * sinf (avg_h_rad );
8487 }
8588
8689 last_lin = cur_lin ;
@@ -91,9 +94,10 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
9194 mp_hal_delay_ms (1 );
9295 }
9396
94- // DEBUG: Print every 1M iterations
97+ // LOG PROGRESS
9598 if (i > 0 && (i % 1000000 ) == 0 ) {
96- printf ("ITER %d: X=%.2f, Y=%.2f, LinDelta=%f\n" , i , (double )robot_x , (double )robot_y , (double )linear_delta );
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 );
97101 }
98102 }
99103
0 commit comments