Skip to content

Commit 03e186e

Browse files
committed
pls work encoder
1 parent 1597274 commit 03e186e

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

pybricks/experimental/pb_module_experimental.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,25 @@
1313
#include <pbio/tacho.h>
1414
#include <pbio/drivebase.h>
1515

16-
// These headers contain the "Getters" we need
17-
#include "pybricks/pupdevices.h"
18-
#include "pybricks/robotics.h"
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+
23+
#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
24+
#define IS_CORTEX_M 1
25+
#define ACCEL_RAM __attribute__((section(".data"), noinline))
26+
#else
27+
#define IS_CORTEX_M 0
28+
#define ACCEL_RAM
29+
#endif
1930

2031
// -----------------------------------------------------------------------------
2132
// Core Math Engine
2233
// -----------------------------------------------------------------------------
23-
static float fast_sin_internal(float theta) {
34+
ACCEL_RAM static float fast_sin_internal(float theta) {
2435
float x = theta * 0.159154943f;
2536
x = theta - (float)((int)(x + (x > 0 ? 0.5f : -0.5f))) * 6.2831853f;
2637
if (x > 1.5707963f) x = 3.1415926f - x;
@@ -36,11 +47,10 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
3647
int num_iters = mp_obj_get_int(args[0]);
3748
float wheel_circ = mp_obj_get_float(args[1]);
3849

39-
// Use the OFFICIAL Pybricks getters to retrieve the C-pointers.
40-
// This is the "Nuclear" fix: it works regardless of struct padding.
41-
pbio_tacho_t *right_tacho = pb_type_Motor_get_tacho(args[2]);
42-
pbio_tacho_t *left_tacho = pb_type_Motor_get_tacho(args[3]);
43-
pbio_drivebase_t *db = pb_type_drivebase_get_drivebase(args[4]);
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]);
4454

4555
float deg_to_mm = wheel_circ / 360.0f;
4656
float robot_x = 0.0f, robot_y = 0.0f;
@@ -82,7 +92,7 @@ static mp_obj_t experimental_odometry_benchmark(size_t n_args, const mp_obj_t *a
8292
last_lin = cur_lin;
8393
last_heading = cur_heading;
8494

85-
// Yield for stability
95+
// Yield for system stability every 2000 loops
8696
if ((i % 2000) == 0) {
8797
mp_handle_pending(true);
8898
mp_hal_delay_ms(1);

0 commit comments

Comments
 (0)