@@ -27,6 +27,8 @@ static pbio_servo_t *right_servo_ptr = NULL;
2727
2828volatile bool odom_running = false;
2929volatile uint32_t last_odom_time_ms = 0 ;
30+ volatile uint32_t last_pursuit_time_ms = 0 ;
31+
3032volatile float global_x = 0.0f , global_y = 0.0f , global_h = 0.0f ;
3133volatile int32_t last_left_angle = 0 , last_right_angle = 0 ;
3234float odom_deg_to_mm = 1.0f ;
@@ -37,8 +39,32 @@ volatile float p_target_speed = 0.0f;
3739volatile float p_lookahead = 120.0f ;
3840volatile float sp_a = 0.0f , sp_b = 0.0f , sp_c = 0.0f , sp_d = 0.0f , sp_x_end = 0.0f ;
3941
42+ // FPS Counter Variables
43+ volatile uint32_t vm_loop_counter = 0 ;
44+ volatile uint32_t current_fps = 0 ;
45+ volatile uint32_t last_fps_time_ms = 0 ;
46+
4047void pb_background_odometry_update (void ) {
41- if (!odom_running || !left_servo_ptr || !right_servo_ptr ) return ;
48+ if (!odom_running ) return ;
49+
50+ // --- FPS COUNTER LOGIC ---
51+ vm_loop_counter ++ ;
52+ uint32_t now = mp_hal_ticks_ms ();
53+
54+ // Every 1000ms, save the count and reset
55+ if (now - last_fps_time_ms >= 1000 ) {
56+ current_fps = vm_loop_counter ;
57+ vm_loop_counter = 0 ;
58+ last_fps_time_ms = now ;
59+ }
60+ // -------------------------
61+
62+ if (!left_servo_ptr || !right_servo_ptr ) return ;
63+
64+ // --- RATE CAP: 200 Hz (5ms) ---
65+ if (now - last_odom_time_ms < 5 ) return ;
66+ last_odom_time_ms = now ;
67+ // ------------------------------
4268
4369 int32_t cur_l , cur_r , unused_rate ;
4470 pbio_servo_get_state_user (left_servo_ptr , & cur_l , & unused_rate );
@@ -71,6 +97,12 @@ void pb_background_odometry_update(void) {
7197void pb_background_pursuit_update (void ) {
7298 if (!pursuit_running || !left_servo_ptr || !right_servo_ptr ) return ;
7399
100+ // --- RATE CAP: 100 Hz (10ms) ---
101+ uint32_t now = mp_hal_ticks_ms ();
102+ if (now - last_pursuit_time_ms < 10 ) return ;
103+ last_pursuit_time_ms = now ;
104+ // -------------------------------
105+
74106 if (global_x >= sp_x_end ) {
75107 pursuit_running = false;
76108 pbio_servo_stop (left_servo_ptr , PBIO_CONTROL_ON_COMPLETION_BRAKE );
@@ -158,4 +190,8 @@ mp_obj_t experimental_stop_odometry(void) {
158190 return mp_const_none ;
159191}
160192
193+ mp_obj_t experimental_get_fps (void ) {
194+ return mp_obj_new_int_from_uint (current_fps );
195+ }
196+
161197#endif // PYBRICKS_PY_EXPERIMENTAL
0 commit comments