Skip to content

Commit ebe69cf

Browse files
committed
fps cap and counter
1 parent 0ef7e4a commit ebe69cf

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

pybricks/experimental/odometry.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static pbio_servo_t *right_servo_ptr = NULL;
2727

2828
volatile bool odom_running = false;
2929
volatile uint32_t last_odom_time_ms = 0;
30+
volatile uint32_t last_pursuit_time_ms = 0;
31+
3032
volatile float global_x = 0.0f, global_y = 0.0f, global_h = 0.0f;
3133
volatile int32_t last_left_angle = 0, last_right_angle = 0;
3234
float odom_deg_to_mm = 1.0f;
@@ -37,8 +39,32 @@ volatile float p_target_speed = 0.0f;
3739
volatile float p_lookahead = 120.0f;
3840
volatile 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+
4047
void 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) {
7197
void 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

pybricks/experimental/pb_module_experimental.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// SPDX-License-Identifier: MIT
12
#include "py/mpconfig.h"
23
#include "py/obj.h"
34
#include "py/runtime.h"
@@ -10,13 +11,18 @@ extern mp_obj_t experimental_get_odometry(void);
1011
extern mp_obj_t experimental_stop_odometry(void);
1112
extern mp_obj_t experimental_start_pursuit(size_t n_args, const mp_obj_t *args);
1213
extern mp_obj_t experimental_stop_pursuit(void);
14+
extern mp_obj_t experimental_get_fps(void);
1315

1416
// Object definitions
1517
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(experimental_start_odometry_obj, 7, 7, experimental_start_odometry);
1618
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_get_odometry_obj, experimental_get_odometry);
1719
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_stop_odometry_obj, experimental_stop_odometry);
18-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(experimental_start_pursuit_obj, 4, 4, experimental_start_pursuit);
20+
21+
// FIXED: Pursuit takes 7 arguments (a, b, c, d, x_end, speed, lookahead), not 4.
22+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(experimental_start_pursuit_obj, 7, 7, experimental_start_pursuit);
23+
1924
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_stop_pursuit_obj, experimental_stop_pursuit);
25+
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_get_fps_obj, experimental_get_fps);
2026

2127
const mp_rom_map_elem_t pb_module_experimental_globals_table[] = {
2228
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_experimental) },
@@ -25,6 +31,7 @@ const mp_rom_map_elem_t pb_module_experimental_globals_table[] = {
2531
{ MP_ROM_QSTR(MP_QSTR_stop_odometry), MP_ROM_PTR(&experimental_stop_odometry_obj) },
2632
{ MP_ROM_QSTR(MP_QSTR_start_pursuit), MP_ROM_PTR(&experimental_start_pursuit_obj) },
2733
{ MP_ROM_QSTR(MP_QSTR_stop_pursuit), MP_ROM_PTR(&experimental_stop_pursuit_obj) },
34+
{ MP_ROM_QSTR(MP_QSTR_get_fps), MP_ROM_PTR(&experimental_get_fps_obj) },
2835
};
2936
MP_DEFINE_CONST_DICT(pb_module_experimental_globals, pb_module_experimental_globals_table);
3037

0 commit comments

Comments
 (0)