From 1b1402c42747def2c895f4c543456bb04ccdac48 Mon Sep 17 00:00:00 2001 From: Samuel Date: Wed, 25 Feb 2026 21:49:35 +0100 Subject: [PATCH] Ajout Motion controleur en mode minimal --- .../src/i2c/headers/i2c_slave.h | 18 ++--- program/motion_controller/src/i2c/i2c_slave.c | 68 ++++++++++--------- program/motion_controller/src/main.c | 33 ++++++--- program/motion_controller/src/robot.c | 11 +-- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/program/motion_controller/src/i2c/headers/i2c_slave.h b/program/motion_controller/src/i2c/headers/i2c_slave.h index 20808a4..7414382 100644 --- a/program/motion_controller/src/i2c/headers/i2c_slave.h +++ b/program/motion_controller/src/i2c/headers/i2c_slave.h @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2021 Valentin Milea + * + * SPDX-License-Identifier: MIT + */ + #ifndef I2C_SLAVE_H #define I2C_SLAVE_H @@ -9,15 +15,7 @@ #define I2C_SLAVE_ADDRESS 0x09 typedef struct i2c_buffer_t { - union { - struct { - uint8_t motor1_speed; - uint8_t motor2_speed; - } hard; - - uint8_t raw[256]; - } buffer; - + uint8_t buffer[256]; uint8_t buffer_reg; bool buffer_reg_written; } i2c_buffer_t; @@ -30,6 +28,4 @@ void deinit_i2c_slave(void); uint8_t get_vitesse_moteur_1(void); uint8_t get_vitesse_moteur_2(void); -extern int nb_messages; - #endif // I2C_SLAVE_H \ No newline at end of file diff --git a/program/motion_controller/src/i2c/i2c_slave.c b/program/motion_controller/src/i2c/i2c_slave.c index f810ed8..8b7673a 100644 --- a/program/motion_controller/src/i2c/i2c_slave.c +++ b/program/motion_controller/src/i2c/i2c_slave.c @@ -1,43 +1,49 @@ +/* + * Copyright (c) 2021 Valentin Milea + * + * SPDX-License-Identifier: MIT + */ + #include "headers/i2c_slave.h" #include #include -#include -#include -static struct +#include "pico/i2c_slave.h" + +#include + +extern i2c_buffer_t i2c_buffer; + +void i2c_slave_buffer_handler(i2c_inst_t *i2c, i2c_slave_event_t event) { - uint8_t mem[256]; - uint8_t mem_address; - bool mem_address_written; -} context; - -int nb_messages = 0; - -// Our handler is called from the I2C ISR, so it must complete quickly. Blocking calls / -// printing to stdio may interfere with interrupt handling. -static void i2c_slave_handler(i2c_inst_t *i2c, i2c_slave_event_t event) { - switch (event) { - case I2C_SLAVE_RECEIVE: // master has written some data - if (!context.mem_address_written) { + switch(event) + { + case I2C_SLAVE_RECEIVE: // master has written some data + if(!i2c_buffer.buffer_reg_written) + { // writes always start with the memory address - context.mem_address = i2c_read_byte_raw(i2c); - context.mem_address_written = true; - } else { + i2c_buffer.buffer_reg = i2c_read_byte_raw(I2C_SLAVE_INSTANCE); + i2c_buffer.buffer_reg_written = true; + } + else + { // save into memory - context.mem[context.mem_address] = i2c_read_byte_raw(i2c); - context.mem_address++; + i2c_buffer.buffer[i2c_buffer.buffer_reg] = i2c_read_byte_raw(I2C_SLAVE_INSTANCE); + i2c_buffer.buffer_reg++; } break; - case I2C_SLAVE_REQUEST: // master is requesting data + + case I2C_SLAVE_REQUEST: // master is requesting data // load from memory - i2c_write_byte_raw(i2c, context.mem[context.mem_address]); - context.mem_address++; + i2c_write_byte_raw(I2C_SLAVE_INSTANCE, i2c_buffer.buffer[i2c_buffer.buffer_reg]); + i2c_buffer.buffer_reg++; break; - case I2C_SLAVE_FINISH: // master has signalled Stop / Restart - context.mem_address_written = false; - nb_messages++; + + case I2C_SLAVE_FINISH: // master has signalled Stop / Restart + i2c_buffer.buffer_reg_written = false; break; - default: + + default: break; } } @@ -52,7 +58,7 @@ void init_i2c_slave(void) i2c_init(I2C_SLAVE_INSTANCE, 0); // New SDK method to init i2c slave - i2c_slave_init(I2C_SLAVE_INSTANCE, I2C_SLAVE_ADDRESS, i2c_slave_handler); + i2c_slave_init(I2C_SLAVE_INSTANCE, I2C_SLAVE_ADDRESS, &i2c_slave_buffer_handler); } void deinit_i2c_slave(void) @@ -66,9 +72,9 @@ void deinit_i2c_slave(void) } uint8_t get_vitesse_moteur_1(void){ - return context.mem[0]; + return i2c_buffer.buffer[0]; } uint8_t get_vitesse_moteur_2(void){ - return context.mem[1]; + return i2c_buffer.buffer[1]; } diff --git a/program/motion_controller/src/main.c b/program/motion_controller/src/main.c index 0516502..e890bf2 100644 --- a/program/motion_controller/src/main.c +++ b/program/motion_controller/src/main.c @@ -8,21 +8,38 @@ * Ce Pico est un esclave piloté par le Pico Principal. * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include -#include "headers/robot.h" +#include +#include +#include "i2c/headers/i2c_slave.h" +#include "headers/motors.h" -robot_t robot; + + +i2c_buffer_t i2c_buffer; int main(void) { - robot_init(); + stdio_init_all(); - while(robot.is_running) + sleep_ms(1000); + + puts("STDIO INIT ALL DONE"); + + init_i2c_slave(); + + puts("SLAVE INIT DONE"); + + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + motors_init(); + + while(true) { - robot_handle_inputs_outputs(); + gpio_put(PICO_DEFAULT_LED_PIN, i2c_buffer.buffer[2]); + motors_update(); + sleep_ms(10); } - robot_deinit(); - return 0; } diff --git a/program/motion_controller/src/robot.c b/program/motion_controller/src/robot.c index f7d2079..aa6db4a 100644 --- a/program/motion_controller/src/robot.c +++ b/program/motion_controller/src/robot.c @@ -17,7 +17,7 @@ void robot_init(void) gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); gpio_put(PICO_DEFAULT_LED_PIN, true); - motors_init(); + //motors_init(); init_i2c_slave(); @@ -42,15 +42,6 @@ static inline void update_time(void) static double elapsed_time = 0.0; elapsed_time += robot.delta_time_ms; - - if(nb_messages >= 10) - { - nb_messages = 0; - - gpio_put(PICO_DEFAULT_LED_PIN, led_state); - - led_state = !led_state; - } } void robot_handle_inputs_outputs(void)