Compare commits
3 Commits
62ce3126a3
...
fd7e278b84
| Author | SHA1 | Date |
|---|---|---|
|
|
fd7e278b84 | |
|
|
a4735a13f9 | |
|
|
83e00fe885 |
|
|
@ -1,2 +1,3 @@
|
|||
program/main_controller/build
|
||||
program/main_controller/src/wifi/headers/wifi_operator.h
|
||||
program/motion_controller/build
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ typedef struct motion_control_data_t {
|
|||
} motion_control_data_t;
|
||||
|
||||
// Init values for motion control
|
||||
void init_motion_control(void);
|
||||
void motion_control_init(void);
|
||||
// Update motion control buffer from motion control data and gyro data
|
||||
void i2c_update_motion_control(void);
|
||||
// Update servo motors from motion control data
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@
|
|||
#include "i2c/headers/mcp23017.h"
|
||||
#include "i2c/headers/gyro.h"
|
||||
#include "motion_control.h"
|
||||
#include "wifi/headers/udp_client.h"
|
||||
|
||||
typedef struct robot_t {
|
||||
udp_client_t udp_client;
|
||||
mcp23017_data_t mcp23017_data;
|
||||
gyro_data_t gyro_data;
|
||||
motion_control_data_t motion_control_data;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef GYRO_H
|
||||
#define GYRO_H
|
||||
|
||||
#define I2C_GYRO_ADDRESS 0x6b
|
||||
#define I2C_GYRO_ADDRESS 0x6B
|
||||
|
||||
typedef struct gyro_data_t {
|
||||
float x_offset, y_offset, z_offset;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#define I2C_MCP23017_ADDRESS 0x20
|
||||
|
||||
|
||||
typedef struct mcp23017_data_t {
|
||||
uint8_t gpio_state[2];
|
||||
} mcp23017_data_t;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#define GAIN_KD 10
|
||||
|
||||
void init_motion_control(void)
|
||||
void motion_control_init(void)
|
||||
{
|
||||
robot.motion_control_data.angle = 0;
|
||||
robot.motion_control_data.x_axis_speed = 0;
|
||||
|
|
|
|||
|
|
@ -3,14 +3,11 @@
|
|||
#include <pico/stdlib.h>
|
||||
#include <pico/cyw43_arch.h>
|
||||
#include <time.h>
|
||||
#include <pico/mutex.h>
|
||||
#include "i2c/headers/i2c_master.h"
|
||||
#include "i2c/headers/mcp23017.h"
|
||||
#include "wifi/headers/udp_client.h"
|
||||
#include "wifi/headers/wifi_operator.h"
|
||||
|
||||
auto_init_mutex(wifi_mutex);
|
||||
|
||||
void robot_init(void)
|
||||
{
|
||||
robot.is_running = true;
|
||||
|
|
@ -20,29 +17,27 @@ void robot_init(void)
|
|||
if(cyw43_arch_init())
|
||||
robot.is_running = false;
|
||||
|
||||
mutex_enter_blocking(&wifi_mutex);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true);
|
||||
mutex_exit(&wifi_mutex);
|
||||
|
||||
//i2c_master_init();
|
||||
i2c_master_init();
|
||||
|
||||
//mcp23017_init();
|
||||
mcp23017_init();
|
||||
|
||||
//gyro_init();
|
||||
gyro_init();
|
||||
|
||||
//gyro_calibrate();
|
||||
|
||||
//init_motion_control();
|
||||
//motion_control_init();
|
||||
|
||||
init_wifi_operator();
|
||||
udp_client_init();
|
||||
wifi_operator_init();
|
||||
|
||||
if(udp_client_init())
|
||||
robot.is_running = false;
|
||||
|
||||
// Initialisation ended
|
||||
for(uint i = 0, led_state = true; i < 5; i++)
|
||||
{
|
||||
mutex_enter_blocking(&wifi_mutex);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_state);
|
||||
mutex_exit(&wifi_mutex);
|
||||
|
||||
sleep_ms(100);
|
||||
|
||||
|
|
@ -65,9 +60,7 @@ static inline void update_time(void)
|
|||
{
|
||||
elapsed_time = 0.0f;
|
||||
|
||||
mutex_enter_blocking(&wifi_mutex);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_state);
|
||||
mutex_exit(&wifi_mutex);
|
||||
|
||||
led_state = !led_state;
|
||||
}
|
||||
|
|
@ -75,9 +68,7 @@ static inline void update_time(void)
|
|||
|
||||
void robot_handle_inputs_outputs(void)
|
||||
{
|
||||
mutex_enter_blocking(&wifi_mutex);
|
||||
cyw43_arch_poll();
|
||||
mutex_exit(&wifi_mutex);
|
||||
|
||||
update_time();
|
||||
|
||||
|
|
@ -95,5 +86,6 @@ void robot_handle_inputs_outputs(void)
|
|||
void robot_deinit(void)
|
||||
{
|
||||
udp_client_deinit();
|
||||
//i2c_master_deinit();
|
||||
cyw43_arch_deinit();
|
||||
i2c_master_deinit();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,22 +6,15 @@
|
|||
|
||||
#define UDP_CLIENT_PORT 4243
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
typedef void (*message_callback_t)(uint8_t *payload, uint16_t len);
|
||||
|
||||
// Message callback deffinition
|
||||
typedef void (*message_callback_t)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, uint16_t port);
|
||||
|
||||
// Data in here is used by the SDK
|
||||
typedef struct udp_client_t {
|
||||
struct udp_pcb *pcb; // like this
|
||||
ip_addr_t local_addr; // or this...
|
||||
uint16_t local_port; // So don't remove them, even if they are not used explicitely in the program
|
||||
uint8_t recv_buffer[BUFFER_SIZE]; // Please (Do not even change their position)
|
||||
struct udp_pcb *pcb;
|
||||
message_callback_t message_callback;
|
||||
} udp_client_t;
|
||||
|
||||
// Init udp client, set callback to NULL for the default callback
|
||||
void udp_client_init(void);
|
||||
// Init udp client
|
||||
int udp_client_init(void);
|
||||
// Exit udp client
|
||||
void udp_client_deinit(void);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,11 +5,6 @@
|
|||
#include <stdint.h>
|
||||
#include <lwip/udp.h>
|
||||
|
||||
#define UDP_PAYLOAD_ANGLE_L_BYTE 0x00
|
||||
#define UDP_PAYLOAD_ANGLE_H_BYTE 0x01
|
||||
#define UDP_PAYLOAD_X_AXIS_SPEED_BYTE 0x02
|
||||
#define UDP_PAYLOAD_Y_AXIS_SPEED_BYTE 0x03
|
||||
|
||||
// Callback function for writing data when udp package received
|
||||
void __not_in_flash_func(udp_client_message_handler)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, u16_t port);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
#ifndef WIFI_OPERATOR_H
|
||||
#define WIFI_OPERATOR_H
|
||||
|
||||
#define WIFI_OPERATOR_SSID "RiombotiqueAP"
|
||||
#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz"
|
||||
//#define WIFI_OPERATOR_SSID "RiombotiqueAP"
|
||||
//#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz"
|
||||
#define WIFI_OPERATOR_SSID "thinkpad-T440p"
|
||||
#define WIFI_OPERATOR_PASSWORD "CDuKaka2000!"
|
||||
|
||||
void init_wifi_operator(void);
|
||||
void wifi_operator_init(void);
|
||||
|
||||
#endif // WIFI_OPERATOR_H
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
#ifndef WIFI_OPERATOR_H
|
||||
#define WIFI_OPERATOR_H
|
||||
|
||||
#define WIFI_OPERATOR_SSID "RiombotiqueAP"
|
||||
#define WIFI_OPERATOR_PASSWORD "x4ptSLpPuJFcpzbLEhDoZ5J7dz"
|
||||
#define WIFI_OPERATOR_SSID "CoolAccessPointName"
|
||||
#define WIFI_OPERATOR_PASSWORD "TheBestPasswordEver"
|
||||
|
||||
void init_wifi_operator(void);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,70 +2,54 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include "headers/udp_payload.h"
|
||||
#include "headers/robot.h"
|
||||
|
||||
udp_client_t udp_client;
|
||||
|
||||
static inline void handle_receive(struct pbuf *p, const ip_addr_t *addr, u16_t port)
|
||||
// Default callback func
|
||||
static void __not_in_flash_func(default_message_callback)(uint8_t *payload, uint16_t len)
|
||||
{
|
||||
if(p->len >= 2)
|
||||
{
|
||||
uint8_t *payload = (uint8_t *)p->payload;
|
||||
uint16_t len = p->len;
|
||||
for(uint i = 0; i < len; i++)
|
||||
printf("payload[%d]=%d | ", i, payload[i]);
|
||||
|
||||
udp_client.message_callback(payload, len, addr, port);
|
||||
}
|
||||
|
||||
pbuf_free(p);
|
||||
puts("\n");
|
||||
}
|
||||
|
||||
static void __not_in_flash_func(udp_receive_callback)(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
|
||||
{
|
||||
udp_client_t *udp_client_received_data = (udp_client_t *)arg;
|
||||
handle_receive(p, addr, port);
|
||||
robot.udp_client.message_callback((uint8_t *)p->payload, p->len);
|
||||
pbuf_free(p);
|
||||
}
|
||||
|
||||
// Default callback func
|
||||
static void __not_in_flash_func(default_message_callback)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, uint16_t port)
|
||||
{
|
||||
printf("Received: len=%d from %s:%d\n", len, ipaddr_ntoa(addr), port);
|
||||
|
||||
for(uint i = 0; i < len; i++) printf("payload[%d]=%d | ", i, payload[i]);
|
||||
puts("\n");
|
||||
|
||||
//printf(">Robot angle : %d\n", (int16_t)((payload[UDP_PAYLOAD_ANGLE_H_BYTE] << 8) | payload[UDP_PAYLOAD_ANGLE_L_BYTE]));
|
||||
//printf(">Robot x speed : %d\n", (int8_t)payload[UDP_PAYLOAD_X_AXIS_SPEED_BYTE]);
|
||||
//printf(">Robot y speed : %d\n", (int8_t)payload[UDP_PAYLOAD_Y_AXIS_SPEED_BYTE]);
|
||||
}
|
||||
|
||||
void udp_client_init(void)
|
||||
int udp_client_init(void)
|
||||
{
|
||||
//udp_client.message_callback = udp_client_message_handler;
|
||||
udp_client.message_callback = default_message_callback;
|
||||
robot.udp_client.message_callback = default_message_callback;
|
||||
|
||||
udp_client.pcb = udp_new();
|
||||
if(udp_client.pcb == NULL)
|
||||
robot.udp_client.pcb = udp_new();
|
||||
if(robot.udp_client.pcb == NULL)
|
||||
{
|
||||
puts("Error creating UDP client");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
udp_recv(udp_client.pcb, udp_receive_callback, &udp_client);
|
||||
udp_recv(robot.udp_client.pcb, udp_receive_callback, &robot.udp_client);
|
||||
|
||||
err_t err = udp_bind(udp_client.pcb, IP_ADDR_ANY, UDP_CLIENT_PORT);
|
||||
if(err != ERR_OK)
|
||||
err_t err = udp_bind(robot.udp_client.pcb, IP_ADDR_ANY, UDP_CLIENT_PORT);
|
||||
if(err)
|
||||
{
|
||||
printf("Error bind UDP client: %d\n", err);
|
||||
return;
|
||||
printf("Error binding UDP client: %d\n", err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("UDP client started on port %d\n", UDP_CLIENT_PORT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void udp_client_deinit(void)
|
||||
{
|
||||
if(udp_client.pcb)
|
||||
if(robot.udp_client.pcb)
|
||||
{
|
||||
udp_remove(udp_client.pcb);
|
||||
udp_client.pcb = NULL;
|
||||
udp_remove(robot.udp_client.pcb);
|
||||
robot.udp_client.pcb = NULL;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,11 @@
|
|||
#include "i2c/headers/motors.h"
|
||||
#include "headers/robot.h"
|
||||
|
||||
#define UDP_PAYLOAD_ANGLE_L_BYTE 0x00
|
||||
#define UDP_PAYLOAD_ANGLE_H_BYTE 0x01
|
||||
#define UDP_PAYLOAD_X_AXIS_SPEED_BYTE 0x02
|
||||
#define UDP_PAYLOAD_Y_AXIS_SPEED_BYTE 0x03
|
||||
|
||||
void __not_in_flash_func(udp_client_message_handler)(uint8_t *payload, uint16_t len, const ip_addr_t *addr, u16_t port)
|
||||
{
|
||||
if(len != 16) return;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include <lwip/netif.h>
|
||||
#include <lwip/ip4_addr.h>
|
||||
|
||||
void init_wifi_operator(void)
|
||||
void wifi_operator_init(void)
|
||||
{
|
||||
// Mode client
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
|
@ -13,62 +13,35 @@ void init_wifi_operator(void)
|
|||
// Désactiver le mode d'économie d'énergie
|
||||
cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE);
|
||||
|
||||
// Configuration IP
|
||||
ip4_addr_t ip, netmask, gateway;
|
||||
IP4_ADDR(&ip, 192, 168, 128, 2);
|
||||
IP4_ADDR(&netmask, 255, 255, 255, 0);
|
||||
IP4_ADDR(&gateway, 192, 168, 128, 1);
|
||||
|
||||
// Configuration réseau avant connexion
|
||||
netif_set_up(netif_default);
|
||||
netif_set_addr(netif_default, &ip, &netmask, &gateway);
|
||||
|
||||
puts("IP config done");
|
||||
sleep_ms(300); // Wait for wifi to be initialized
|
||||
|
||||
// Tentativs de connexion
|
||||
for(int error_code; !error_code;)
|
||||
for(int error_code = 1; error_code; )
|
||||
{
|
||||
// Afficher les paramètres de connexion
|
||||
printf("Trying to connect to '%s'\n", WIFI_OPERATOR_SSID);
|
||||
puts("WiFi connection attempt");
|
||||
|
||||
error_code = cyw43_arch_wifi_connect_timeout_ms(WIFI_OPERATOR_SSID, WIFI_OPERATOR_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 10000);
|
||||
|
||||
if(error_code)
|
||||
{
|
||||
const char *error_description;
|
||||
const char *error_message;
|
||||
|
||||
switch(error_code)
|
||||
{
|
||||
case -1:
|
||||
error_description = "Error Generic";
|
||||
case PICO_ERROR_TIMEOUT:
|
||||
error_message = "Timeout reached";
|
||||
break;
|
||||
|
||||
case -2:
|
||||
error_description = "Acces point not found";
|
||||
case PICO_ERROR_BADAUTH:
|
||||
error_message = "Bad password";
|
||||
break;
|
||||
|
||||
case -3:
|
||||
error_description = "Incorrect password";
|
||||
case PICO_ERROR_CONNECT_FAILED:
|
||||
error_message = "Some reason";
|
||||
break;
|
||||
|
||||
default:
|
||||
error_description = "Unknow error";
|
||||
}
|
||||
|
||||
printf("Error: WiFi can't be connected - Error code: %d - %s\n", error_code, error_description);
|
||||
printf("Connection failed : %s\n", error_message);
|
||||
}
|
||||
}
|
||||
|
||||
// Configuration de l'interface réseau
|
||||
if(netif_default == NULL)
|
||||
{
|
||||
puts("Error: WiFi interface isn't accessible");
|
||||
return;
|
||||
}
|
||||
|
||||
netif_set_up(netif_default);
|
||||
netif_set_link_up(netif_default);
|
||||
netif_set_addr(netif_default, &ip, &netmask, &gateway);
|
||||
|
||||
puts("Connexion successfully etablished !");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue