2022-09-23 20:09:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/gpio.h"
|
|
|
|
#include "hardware/spi.h"
|
2022-09-24 09:38:55 +00:00
|
|
|
#include "hardware/structs/spi.h"
|
2023-04-28 21:51:43 +00:00
|
|
|
#include "Geometrie.h"
|
2023-05-18 20:26:34 +00:00
|
|
|
#include "Monitoring.h"
|
2022-09-24 09:38:55 +00:00
|
|
|
#include "spi_nb.h"
|
2022-09-26 17:28:45 +00:00
|
|
|
#include "Temps.h"
|
2022-09-26 19:53:52 +00:00
|
|
|
#include "gyro.h"
|
2023-03-29 21:11:04 +00:00
|
|
|
#include "math.h"
|
2022-09-23 20:09:40 +00:00
|
|
|
|
2023-03-29 21:11:04 +00:00
|
|
|
#define RADIAN_VERS_DEGRES (180. / M_PI)
|
2022-10-27 17:49:16 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
|
|
|
|
#ifdef GYRO_L3GD20H
|
|
|
|
#include "gyro_L3GD20H.h"
|
|
|
|
#else
|
|
|
|
#ifdef GYRO_ADXRS453
|
|
|
|
#include "gyro_ADXRS453.h"
|
|
|
|
#else
|
|
|
|
#error "Choissisez un gyroscope"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-09-26 19:53:52 +00:00
|
|
|
|
|
|
|
/// @brief structure d'échange des angles du gyrocope
|
2022-10-16 08:26:44 +00:00
|
|
|
struct t_angle_gyro _vitesse_calibration;
|
|
|
|
struct t_angle_gyro *vitesse_calibration;
|
2023-04-28 21:51:43 +00:00
|
|
|
struct t_angle_gyro_float _vitesse_angulaire;
|
|
|
|
struct t_angle_gyro_float *vitesse_angulaire;
|
2022-09-26 17:28:45 +00:00
|
|
|
|
2022-09-23 20:09:40 +00:00
|
|
|
int gyro_read_register_blocking(uint8_t registrer, uint8_t *tampon, uint8_t nb_a_lire);
|
2022-09-26 17:28:45 +00:00
|
|
|
void gyro_calibration(void);
|
|
|
|
|
|
|
|
uint32_t rot_x_zero, rot_y_zero, rot_z_zero;
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-28 21:51:43 +00:00
|
|
|
struct t_angle_gyro_float angle_gyro, vitesse_gyro;
|
2022-09-26 19:53:52 +00:00
|
|
|
|
2023-04-28 21:51:43 +00:00
|
|
|
void gyro_set_angle_radian(float angle_radian){
|
2023-03-29 21:11:04 +00:00
|
|
|
angle_gyro.rot_z = angle_radian * RADIAN_VERS_DEGRES;
|
|
|
|
}
|
2022-09-26 19:53:52 +00:00
|
|
|
|
2023-04-28 21:51:43 +00:00
|
|
|
struct t_angle_gyro_float gyro_get_angle_degres(void){
|
2022-09-26 19:53:52 +00:00
|
|
|
return angle_gyro;
|
|
|
|
}
|
2023-04-28 21:51:43 +00:00
|
|
|
struct t_angle_gyro_float gyro_get_vitesse(void){
|
2022-10-15 19:13:52 +00:00
|
|
|
return vitesse_gyro;
|
|
|
|
}
|
2022-09-23 20:09:40 +00:00
|
|
|
|
|
|
|
void Gyro_Init(void){
|
|
|
|
//
|
2022-11-03 21:46:41 +00:00
|
|
|
gpio_set_function(0, GPIO_FUNC_SPI); // SDI (ancien : 16)
|
|
|
|
gpio_set_function(2, GPIO_FUNC_SPI); // SCK (ancien : 18)
|
|
|
|
gpio_set_function(3, GPIO_FUNC_SPI); // SDO (ancien : 19)
|
2022-09-23 20:09:40 +00:00
|
|
|
gpio_set_function(PIN_CS, GPIO_OUT); // CSn
|
|
|
|
|
|
|
|
gpio_init(PIN_CS);
|
|
|
|
gpio_set_dir(PIN_CS, GPIO_OUT);
|
|
|
|
cs_deselect();
|
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
vitesse_calibration = NULL;
|
|
|
|
vitesse_angulaire = &_vitesse_angulaire;
|
2022-10-15 19:13:52 +00:00
|
|
|
|
2022-12-11 14:39:30 +00:00
|
|
|
//uint speed = spi_init(spi0, 10 * 1000); // SPI init @ 10 kHz
|
2022-09-28 16:46:45 +00:00
|
|
|
uint speed = spi_init(spi0, 2 * 1000 * 1000); // SPI init @ 2 MHz
|
2022-12-11 14:39:30 +00:00
|
|
|
|
2023-02-19 16:56:45 +00:00
|
|
|
//printf("vitesse SPI : %d\n", speed);
|
2022-09-23 20:09:40 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
|
2022-10-27 17:49:16 +00:00
|
|
|
spi_set_format(spi0, 8, SPI_CPHA_0, SPI_CPOL_0, SPI_MSB_FIRST);
|
2022-09-23 20:09:40 +00:00
|
|
|
|
|
|
|
// Test de la présence du gyroscope :
|
|
|
|
if(gyro_init_check()){
|
2023-05-18 20:26:34 +00:00
|
|
|
Monitoring_Error("Gyroscope non trouve");
|
|
|
|
while(1){
|
|
|
|
puts("Gyroscope non trouve");
|
|
|
|
}; // On s'arrête là !
|
2022-09-23 20:09:40 +00:00
|
|
|
}else{
|
2023-02-19 16:56:45 +00:00
|
|
|
//puts("Gyroscope trouve");
|
2022-10-16 08:26:44 +00:00
|
|
|
if(!gyro_config()){
|
2023-02-19 16:56:45 +00:00
|
|
|
//puts("gyro_config ok !");
|
2022-10-16 08:26:44 +00:00
|
|
|
}else{
|
2023-05-18 20:26:34 +00:00
|
|
|
Monitoring_Error("gyro_config FAILED !");
|
|
|
|
while(1){
|
|
|
|
puts("gyro_config FAILED !");
|
|
|
|
}; // On s'arrête là !
|
2022-10-16 08:26:44 +00:00
|
|
|
|
|
|
|
}
|
2022-09-23 20:09:40 +00:00
|
|
|
}
|
2022-10-15 19:13:52 +00:00
|
|
|
sleep_ms(150); // Temps d'init du gyroscope
|
2022-10-29 13:51:52 +00:00
|
|
|
|
|
|
|
gyro_calibration();
|
|
|
|
|
2022-09-23 20:09:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 16:55:09 +00:00
|
|
|
void Gyro_Read(uint16_t step_ms){
|
2022-10-16 08:26:44 +00:00
|
|
|
struct t_angle_gyro * _vitesse_angulaire_brute;
|
|
|
|
struct t_angle_gyro m_vitesse_angulaire_brute;
|
2022-09-23 20:09:40 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
_vitesse_angulaire_brute = &m_vitesse_angulaire_brute;
|
2022-09-23 20:09:40 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
// Acquisition des valeurs
|
|
|
|
gyro_get_vitesse_brute(_vitesse_angulaire_brute, vitesse_calibration);
|
2022-09-28 16:46:45 +00:00
|
|
|
//gyro_get_angles(&vitesse_angulaire, NULL);
|
2022-09-24 16:55:09 +00:00
|
|
|
|
2022-10-28 21:35:26 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
// conversion de la vitesse angulaire en degré/seconde
|
|
|
|
gyro_get_vitesse_normalisee(_vitesse_angulaire_brute, vitesse_angulaire);
|
2022-09-28 16:46:45 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
vitesse_gyro = *vitesse_angulaire;
|
2022-09-24 18:54:10 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
// Intégration en fonction du pas de temps
|
|
|
|
angle_gyro.rot_x = angle_gyro.rot_x + vitesse_angulaire->rot_x * step_ms * 0.001;
|
|
|
|
angle_gyro.rot_y = angle_gyro.rot_y + vitesse_angulaire->rot_y * step_ms * 0.001;
|
|
|
|
angle_gyro.rot_z = angle_gyro.rot_z + vitesse_angulaire->rot_z * step_ms * 0.001;
|
2022-10-28 21:35:26 +00:00
|
|
|
|
2022-09-26 17:28:45 +00:00
|
|
|
}
|
2022-09-24 16:55:09 +00:00
|
|
|
|
2022-10-15 19:13:52 +00:00
|
|
|
int16_t gyro_get_temp(void){
|
|
|
|
int8_t tampon[3]="\0\0";
|
|
|
|
int16_t temperature;
|
|
|
|
spi_read_register(spi0, 0x26, tampon, 6);
|
|
|
|
temperature = -tampon[1];
|
|
|
|
printf("temperature %d\n",temperature);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:51:43 +00:00
|
|
|
void gyro_affiche(struct t_angle_gyro_float angle_gyro, char * titre){
|
2022-09-26 19:53:52 +00:00
|
|
|
if(titre != NULL){
|
|
|
|
printf("%s ",titre);
|
|
|
|
}
|
2022-10-15 19:13:52 +00:00
|
|
|
printf("angle, %f, %f, %f\n", angle_gyro.rot_x, angle_gyro.rot_y, angle_gyro.rot_z);
|
2022-09-26 17:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gyro_calibration(void){
|
2022-10-29 13:51:52 +00:00
|
|
|
uint32_t t_calibration_ms = 10000;
|
|
|
|
uint32_t nb_ech = t_calibration_ms/2;
|
2022-09-26 17:28:45 +00:00
|
|
|
uint32_t m_temps_ms = Temps_get_temps_ms();
|
2022-10-15 19:13:52 +00:00
|
|
|
uint32_t temps_500ms = m_temps_ms;
|
|
|
|
int16_t temperature;
|
2022-10-16 08:26:44 +00:00
|
|
|
struct t_angle_gyro vitesse_grute_gyro;
|
2022-09-26 17:28:45 +00:00
|
|
|
|
|
|
|
printf("Calibration...\n");
|
2022-10-16 08:26:44 +00:00
|
|
|
vitesse_calibration = &_vitesse_calibration;
|
2022-09-24 18:54:10 +00:00
|
|
|
|
2022-10-16 08:26:44 +00:00
|
|
|
vitesse_calibration->rot_x = 0;
|
|
|
|
vitesse_calibration->rot_y = 0;
|
|
|
|
vitesse_calibration->rot_z = 0;
|
2022-09-26 17:28:45 +00:00
|
|
|
|
2022-10-15 19:13:52 +00:00
|
|
|
// Acquisition des échantillons, 1 par milliseconde (1 ms, c'est trop court on dirait !)
|
2022-09-26 19:53:52 +00:00
|
|
|
for(uint32_t i=0; i<nb_ech; i++){
|
2022-09-26 17:28:45 +00:00
|
|
|
while(m_temps_ms == Temps_get_temps_ms());
|
2022-09-26 19:53:52 +00:00
|
|
|
m_temps_ms = Temps_get_temps_ms();
|
2022-10-16 08:26:44 +00:00
|
|
|
gyro_get_vitesse_brute(&vitesse_grute_gyro, NULL);
|
|
|
|
vitesse_calibration->rot_x += vitesse_grute_gyro.rot_x;
|
|
|
|
vitesse_calibration->rot_y += vitesse_grute_gyro.rot_y;
|
|
|
|
vitesse_calibration->rot_z += vitesse_grute_gyro.rot_z;
|
2022-10-29 13:51:52 +00:00
|
|
|
/*if(m_temps_ms > temps_500ms){
|
2022-10-15 19:13:52 +00:00
|
|
|
printf(".");
|
|
|
|
gyro_get_temp();
|
|
|
|
temps_500ms += 500;
|
2022-10-29 13:51:52 +00:00
|
|
|
}*/
|
|
|
|
sleep_ms(2);
|
2022-09-26 17:28:45 +00:00
|
|
|
}
|
2022-10-16 08:26:44 +00:00
|
|
|
vitesse_calibration->rot_x = vitesse_calibration->rot_x / (int32_t)nb_ech;
|
|
|
|
vitesse_calibration->rot_y = vitesse_calibration->rot_y / (int32_t)nb_ech;
|
|
|
|
vitesse_calibration->rot_z = vitesse_calibration->rot_z / (int32_t)nb_ech;
|
2022-10-29 13:51:52 +00:00
|
|
|
//temperature = gyro_get_temp();
|
2022-09-24 16:55:09 +00:00
|
|
|
|
2022-10-29 13:51:52 +00:00
|
|
|
//printf("fin calibration, %d, %d, %d, %d\n", vitesse_calibration->rot_x, vitesse_calibration->rot_y ,vitesse_calibration->rot_z, temperature);
|
|
|
|
printf("fin calibration, %d\n", vitesse_calibration->rot_z);
|
2022-09-26 17:28:45 +00:00
|
|
|
|
|
|
|
}
|
2022-09-26 19:53:52 +00:00
|
|
|
|
|
|
|
|