Compare commits
6 Commits
9966f4745c
...
d8d504fdc1
| Author | SHA1 | Date |
|---|---|---|
|
|
d8d504fdc1 | |
|
|
5a45830183 | |
|
|
66181452c4 | |
|
|
adf03bd9ee | |
|
|
1b1402c427 | |
|
|
7224acd615 |
|
|
@ -7,8 +7,8 @@
|
||||||
#include "headers/motors.h"
|
#include "headers/motors.h"
|
||||||
#include "headers/robot.h"
|
#include "headers/robot.h"
|
||||||
|
|
||||||
#define ANGLE_PER_S 30.0f
|
#define ANGLE_PER_S 90.0f
|
||||||
#define GAIN_KD 10
|
#define GAIN_KP 10
|
||||||
|
|
||||||
#define MSG_LEN sizeof(uint8_t) * 2
|
#define MSG_LEN sizeof(uint8_t) * 2
|
||||||
#define MSG_DELAY_MS 5.0f
|
#define MSG_DELAY_MS 5.0f
|
||||||
|
|
@ -44,7 +44,6 @@ void motion_control_update(void)
|
||||||
while(robot.motion_control_data.target_angle > 180.0f) robot.motion_control_data.target_angle -= 360.0f;
|
while(robot.motion_control_data.target_angle > 180.0f) robot.motion_control_data.target_angle -= 360.0f;
|
||||||
while(robot.motion_control_data.target_angle < -180.0f) robot.motion_control_data.target_angle += 360.0f;
|
while(robot.motion_control_data.target_angle < -180.0f) robot.motion_control_data.target_angle += 360.0f;
|
||||||
|
|
||||||
//printf(">target_angle:%f\n", robot.motion_control_data.target_angle);
|
|
||||||
//printf(">target_speed:%d\n", robot.motion_control_data.target_speed);
|
//printf(">target_speed:%d\n", robot.motion_control_data.target_speed);
|
||||||
|
|
||||||
float target_angle_radian = robot.motion_control_data.target_angle / 180.0f * M_PI;
|
float target_angle_radian = robot.motion_control_data.target_angle / 180.0f * M_PI;
|
||||||
|
|
@ -53,7 +52,18 @@ void motion_control_update(void)
|
||||||
while(error > 180) error -= 360;
|
while(error > 180) error -= 360;
|
||||||
while(error < -180) error += 360;
|
while(error < -180) error += 360;
|
||||||
|
|
||||||
float correction = error * GAIN_KD;
|
float correction = error * GAIN_KP;
|
||||||
|
|
||||||
|
/*printf(">target_angle:%f\n", robot.motion_control_data.target_angle);
|
||||||
|
printf(">current_angle:%f\n", robot.gyro_data.x_angle);
|
||||||
|
printf(">correction:%f\n", correction);*/
|
||||||
|
|
||||||
|
/*if(correction > 20 && correction < 150){
|
||||||
|
correction = 150;
|
||||||
|
}
|
||||||
|
if(correction < -20 && correction > -150){
|
||||||
|
correction = 150;
|
||||||
|
}*/
|
||||||
|
|
||||||
float motor1_speed = (float)robot.motion_control_data.target_speed - correction;
|
float motor1_speed = (float)robot.motion_control_data.target_speed - correction;
|
||||||
float motor2_speed = (float)robot.motion_control_data.target_speed + correction;
|
float motor2_speed = (float)robot.motion_control_data.target_speed + correction;
|
||||||
|
|
@ -73,33 +83,19 @@ void motion_control_update(void)
|
||||||
|
|
||||||
if(elapsed_time_ms >= MSG_DELAY_MS)
|
if(elapsed_time_ms >= MSG_DELAY_MS)
|
||||||
{
|
{
|
||||||
|
elapsed_time_ms = 0;
|
||||||
// Send motors speed via I2C
|
// Send motors speed via I2C
|
||||||
union {
|
uint8_t data[5];
|
||||||
struct {
|
|
||||||
uint8_t motor1_speed;
|
|
||||||
uint8_t motor2_speed;
|
|
||||||
} hard;
|
|
||||||
|
|
||||||
uint8_t raw[MSG_LEN];
|
data[0] = 0x00; /// registre
|
||||||
} data;
|
|
||||||
|
|
||||||
data.hard.motor1_speed = (uint8_t)abs((int)motor1_speed);
|
data[1] = (uint8_t)abs((int)motor1_speed);
|
||||||
data.hard.motor2_speed = (uint8_t)abs((int)motor2_speed);
|
data[2] = (uint8_t)abs((int)motor2_speed);
|
||||||
|
|
||||||
data.hard.motor1_speed = (uint8_t)abs((int)robot.udp_client.data.hard.inputs.joystick_x);
|
|
||||||
data.hard.motor2_speed = (uint8_t)abs((int)robot.udp_client.data.hard.inputs.joystick_x);
|
|
||||||
|
|
||||||
motor_set_dir(MOTOR1, (int16_t)robot.udp_client.data.hard.inputs.joystick_x);
|
|
||||||
motor_set_dir(MOTOR2, (int16_t)robot.udp_client.data.hard.inputs.joystick_x);
|
|
||||||
|
|
||||||
uint8_t reg = 0x00;
|
uint8_t reg = 0x00;
|
||||||
|
|
||||||
if(i2c_write_blocking(I2C_MASTER_INSTANCE, I2C_MOTION_CONTROLLER_ADDRESS, ®, 1, true) == PICO_ERROR_GENERIC)
|
if(i2c_write_blocking(I2C_MASTER_INSTANCE, I2C_MOTION_CONTROLLER_ADDRESS, data, 3, false) == PICO_ERROR_GENERIC)
|
||||||
{
|
|
||||||
puts("Motion controller not reachable");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(i2c_write_blocking(I2C_MASTER_INSTANCE, I2C_MOTION_CONTROLLER_ADDRESS, data.raw, MSG_LEN, false) == PICO_ERROR_GENERIC)
|
|
||||||
{
|
{
|
||||||
puts("Motion controller not reachable");
|
puts("Motion controller not reachable");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <pico/stdlib.h>
|
#include <pico/stdlib.h>
|
||||||
#include <pico/cyw43_arch.h>
|
#include <pico/cyw43_arch.h>
|
||||||
|
#include <pico/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "i2c/headers/i2c_master.h"
|
#include "i2c/headers/i2c_master.h"
|
||||||
#include "i2c/headers/mcp23017.h"
|
#include "i2c/headers/mcp23017.h"
|
||||||
|
|
@ -88,6 +89,8 @@ static inline void update_time(void)
|
||||||
void robot_handle_inputs_outputs(void)
|
void robot_handle_inputs_outputs(void)
|
||||||
{
|
{
|
||||||
static bool led_state=false;
|
static bool led_state=false;
|
||||||
|
static uint32_t temps_ms_old =0, timer_200_ms=0;
|
||||||
|
uint32_t temps_ms;
|
||||||
update_time();
|
update_time();
|
||||||
|
|
||||||
cyw43_arch_poll();
|
cyw43_arch_poll();
|
||||||
|
|
@ -98,11 +101,16 @@ void robot_handle_inputs_outputs(void)
|
||||||
|
|
||||||
mcp23017_update();
|
mcp23017_update();
|
||||||
|
|
||||||
sleep_ms(200);
|
temps_ms = to_ms_since_boot(get_absolute_time());
|
||||||
|
timer_200_ms += temps_ms - temps_ms_old;
|
||||||
|
temps_ms_old = temps_ms;
|
||||||
|
if(timer_200_ms > 200){
|
||||||
|
timer_200_ms = 0;
|
||||||
led_state= !led_state;
|
led_state= !led_state;
|
||||||
uint8_t data[] = {0x02, led_state};
|
uint8_t data[] = {0x02, led_state};
|
||||||
int ret = i2c_write_blocking(I2C_MASTER_INSTANCE, 0x09, data, 2, false);
|
int ret = i2c_write_blocking(I2C_MASTER_INSTANCE, 0x09, data, 2, false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void robot_deinit(void)
|
void robot_deinit(void)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -1,42 +1,48 @@
|
||||||
|
/*
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
switch(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
|
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
|
// 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
|
case I2C_SLAVE_FINISH: // master has signalled Stop / Restart
|
||||||
context.mem_address_written = false;
|
i2c_buffer.buffer_reg_written = false;
|
||||||
nb_messages++;
|
|
||||||
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];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue