Ajout Motion controleur en mode minimal
This commit is contained in:
parent
7224acd615
commit
1b1402c427
|
|
@ -1,3 +1,9 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
|
||||
*
|
||||
* 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
|
||||
|
|
@ -1,42 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include "headers/i2c_slave.h"
|
||||
|
||||
#include <hardware/gpio.h>
|
||||
#include <hardware/i2c.h>
|
||||
#include <pico/i2c_slave.h>
|
||||
#include <headers/robot.h>
|
||||
static struct
|
||||
#include "pico/i2c_slave.h"
|
||||
|
||||
#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];
|
||||
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) {
|
||||
switch(event)
|
||||
{
|
||||
case I2C_SLAVE_RECEIVE: // master has written some data
|
||||
if (!context.mem_address_written) {
|
||||
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
|
||||
// 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++;
|
||||
i2c_buffer.buffer_reg_written = false;
|
||||
break;
|
||||
|
||||
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];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,21 +8,38 @@
|
|||
* Ce Pico est un esclave piloté par le Pico Principal. *
|
||||
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "headers/robot.h"
|
||||
#include <pico/stdlib.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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue