Ajout Motion controleur en mode minimal

This commit is contained in:
Samuel 2026-02-25 21:49:35 +01:00
parent 7224acd615
commit 1b1402c427
4 changed files with 70 additions and 60 deletions

View File

@ -1,3 +1,9 @@
/*
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
*
* SPDX-License-Identifier: MIT
*/
#ifndef I2C_SLAVE_H #ifndef I2C_SLAVE_H
#define I2C_SLAVE_H #define I2C_SLAVE_H
@ -9,15 +15,7 @@
#define I2C_SLAVE_ADDRESS 0x09 #define I2C_SLAVE_ADDRESS 0x09
typedef struct i2c_buffer_t { typedef struct i2c_buffer_t {
union { uint8_t buffer[256];
struct {
uint8_t motor1_speed;
uint8_t motor2_speed;
} hard;
uint8_t raw[256];
} buffer;
uint8_t buffer_reg; uint8_t buffer_reg;
bool buffer_reg_written; bool buffer_reg_written;
} i2c_buffer_t; } i2c_buffer_t;
@ -30,6 +28,4 @@ void deinit_i2c_slave(void);
uint8_t get_vitesse_moteur_1(void); uint8_t get_vitesse_moteur_1(void);
uint8_t get_vitesse_moteur_2(void); uint8_t get_vitesse_moteur_2(void);
extern int nb_messages;
#endif // I2C_SLAVE_H #endif // I2C_SLAVE_H

View File

@ -1,43 +1,49 @@
/*
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
*
* SPDX-License-Identifier: MIT
*/
#include "headers/i2c_slave.h" #include "headers/i2c_slave.h"
#include <hardware/gpio.h> #include <hardware/gpio.h>
#include <hardware/i2c.h> #include <hardware/i2c.h>
#include <pico/i2c_slave.h> #include "pico/i2c_slave.h"
#include <headers/robot.h>
static struct #include <stdio.h>
extern i2c_buffer_t i2c_buffer;
void i2c_slave_buffer_handler(i2c_inst_t *i2c, i2c_slave_event_t event)
{ {
uint8_t mem[256]; switch(event)
uint8_t mem_address; {
bool mem_address_written; case I2C_SLAVE_RECEIVE: // master has written some data
} context; if(!i2c_buffer.buffer_reg_written)
{
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) {
// writes always start with the memory address // writes always start with the memory address
context.mem_address = i2c_read_byte_raw(i2c); i2c_buffer.buffer_reg = i2c_read_byte_raw(I2C_SLAVE_INSTANCE);
context.mem_address_written = true; i2c_buffer.buffer_reg_written = true;
} else { }
else
{
// save into memory // save into memory
context.mem[context.mem_address] = i2c_read_byte_raw(i2c); i2c_buffer.buffer[i2c_buffer.buffer_reg] = i2c_read_byte_raw(I2C_SLAVE_INSTANCE);
context.mem_address++; i2c_buffer.buffer_reg++;
} }
break; break;
case I2C_SLAVE_REQUEST: // master is requesting data
case I2C_SLAVE_REQUEST: // master is requesting data
// load from memory // load from memory
i2c_write_byte_raw(i2c, context.mem[context.mem_address]); i2c_write_byte_raw(I2C_SLAVE_INSTANCE, i2c_buffer.buffer[i2c_buffer.buffer_reg]);
context.mem_address++; i2c_buffer.buffer_reg++;
break; break;
case I2C_SLAVE_FINISH: // master has signalled Stop / Restart
context.mem_address_written = false; case I2C_SLAVE_FINISH: // master has signalled Stop / Restart
nb_messages++; i2c_buffer.buffer_reg_written = false;
break; break;
default:
default:
break; break;
} }
} }
@ -52,7 +58,7 @@ void init_i2c_slave(void)
i2c_init(I2C_SLAVE_INSTANCE, 0); i2c_init(I2C_SLAVE_INSTANCE, 0);
// New SDK method to init i2c slave // 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) void deinit_i2c_slave(void)
@ -66,9 +72,9 @@ void deinit_i2c_slave(void)
} }
uint8_t get_vitesse_moteur_1(void){ uint8_t get_vitesse_moteur_1(void){
return context.mem[0]; return i2c_buffer.buffer[0];
} }
uint8_t get_vitesse_moteur_2(void){ uint8_t get_vitesse_moteur_2(void){
return context.mem[1]; return i2c_buffer.buffer[1];
} }

View File

@ -8,21 +8,38 @@
* Ce Pico est un esclave piloté par le Pico Principal. * * Ce Pico est un esclave piloté par le Pico Principal. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdbool.h> #include <pico/stdlib.h>
#include "headers/robot.h" #include <stdio.h>
#include "i2c/headers/i2c_slave.h"
#include "headers/motors.h"
robot_t robot;
i2c_buffer_t i2c_buffer;
int main(void) 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; return 0;
} }

View File

@ -17,7 +17,7 @@ void robot_init(void)
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
gpio_put(PICO_DEFAULT_LED_PIN, true); gpio_put(PICO_DEFAULT_LED_PIN, true);
motors_init(); //motors_init();
init_i2c_slave(); init_i2c_slave();
@ -42,15 +42,6 @@ static inline void update_time(void)
static double elapsed_time = 0.0; static double elapsed_time = 0.0;
elapsed_time += robot.delta_time_ms; 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) void robot_handle_inputs_outputs(void)