Skip to content

Commit d63db5c

Browse files
committed
please build i need this
1 parent f96d45e commit d63db5c

2 files changed

Lines changed: 69 additions & 14 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "py/mpconfig.h"
2+
#include "py/mphal.h"
3+
#include "py/runtime.h"
4+
#include "pybricks/experimental/platform_math.h"
5+
#include "pybricks/experimental/odometry.h"
6+
7+
// Global visibility for the linker
8+
mp_obj_t calculate_odometry(int num_iters, float wheel_circ, float axle_track, mp_obj_t right_angle_func, mp_obj_t left_angle_func) {
9+
10+
// Constants (mm/deg and 1/track)
11+
float deg_to_mm = wheel_circ * 0.0027777778f;
12+
float inv_axle_track = 1.0f / axle_track;
13+
14+
// Position State
15+
float rx = 0.0f;
16+
float ry = 0.0f;
17+
float rh = 0.0f;
18+
19+
// Initial Grabs
20+
int32_t last_r = mp_obj_get_int(mp_call_function_0(right_angle_func));
21+
int32_t last_l = mp_obj_get_int(mp_call_function_0(left_angle_func));
22+
23+
uint32_t start_time = mp_hal_ticks_ms();
24+
25+
for (int i = 0; i < num_iters; i++) {
26+
// Fetch raw encoders
27+
int32_t cur_r = mp_obj_get_int(mp_call_function_0(right_angle_func));
28+
int32_t cur_l = mp_obj_get_int(mp_call_function_0(left_angle_func));
29+
30+
// Differential logic
31+
float dR = (float)(cur_r - last_r) * deg_to_mm;
32+
float dL = (float)(cur_l - last_l) * deg_to_mm;
33+
float dD = (dR + dL) * 0.5f;
34+
float dH = (dR - dL) * inv_axle_track;
35+
36+
// Update with Midpoint Heading (RK2)
37+
if (dD != 0.0f || dH != 0.0f) {
38+
float avg_h = rh + (dH * 0.5f);
39+
rx += dD * pb_fast_cos(avg_h);
40+
ry += dD * pb_fast_sin(avg_h);
41+
rh += dH;
42+
}
43+
44+
last_r = cur_r;
45+
last_l = cur_l;
46+
47+
// Yield to MicroPython (Every 1024 iters)
48+
if ((i & 0x3FF) == 0) {
49+
mp_handle_pending(true);
50+
}
51+
}
52+
53+
uint32_t dur = mp_hal_ticks_ms() - start_time;
54+
55+
mp_obj_t tuple[5] = {
56+
mp_obj_new_float_from_f((float)dur * 0.001f),
57+
mp_obj_new_int(num_iters),
58+
mp_obj_new_float_from_f((float)num_iters / ((float)dur * 0.001f)),
59+
mp_obj_new_float_from_f(rx),
60+
mp_obj_new_float_from_f(ry)
61+
};
62+
return mp_obj_new_tuple(5, tuple);
63+
}
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
#include "py/mpconfig.h"
22
#include "py/obj.h"
33
#include "py/runtime.h"
4-
#include "py/builtin.h"
54

65
#if PYBRICKS_PY_EXPERIMENTAL
76

87
#include "pybricks/experimental/odometry.h"
98

10-
#ifndef STATIC
11-
#define STATIC static
12-
#endif
13-
14-
// 1. The actual C logic for the benchmark
9+
// The function logic
1510
STATIC mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *args) {
1611
int num_iters = mp_obj_get_int(args[0]);
1712
float wheel_circ = mp_obj_get_float(args[1]);
@@ -21,22 +16,19 @@ STATIC mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
2116

2217
return calculate_odometry(num_iters, wheel_circ, axle_track, right_func, left_func);
2318
}
24-
// 2. Define the function object
2519
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(experimental_odometry_benchmark_obj, 5, 5, experimental_odometry_benchmark);
2620

27-
// 3. Create the globals table
28-
STATIC const mp_rom_map_elem_t experimental_globals_table[] = {
21+
// The dict table. The name must match exactly: pb_module_<filename_without_pb_module>_globals
22+
STATIC const mp_rom_map_elem_t pb_module_experimental_globals_table[] = {
2923
{ MP_ROM_QSTR(MP_QSTR_odometry_benchmark), MP_ROM_PTR(&experimental_odometry_benchmark_obj) },
3024
};
31-
STATIC MP_DEFINE_CONST_DICT(pb_module_experimental_globals, experimental_globals_table);
25+
STATIC MP_DEFINE_CONST_DICT(pb_module_experimental_globals, pb_module_experimental_globals_table);
3226

33-
// 4. THE FIX: Explicitly register the module so the compiler sees the globals are used.
34-
// This matches the name used in your Makefile.
27+
// We define the module struct here so the linker finds it,
28+
// but we do NOT use MP_REGISTER_MODULE because the Makefile does it for us.
3529
const mp_obj_module_t pb_module_experimental = {
3630
.base = { &mp_type_module },
3731
.globals = (mp_obj_dict_t *)&pb_module_experimental_globals,
3832
};
3933

40-
MP_REGISTER_MODULE(MP_QSTR_experimental, pb_module_experimental);
41-
4234
#endif // PYBRICKS_PY_EXPERIMENTAL

0 commit comments

Comments
 (0)