RPiPico-Holonome2023/test.c

94 lines
2.9 KiB
C
Raw Normal View History

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/binary_info.h"
2022-09-23 20:09:40 +00:00
#include "gyro.h"
#include "Temps.h"
#include "spi_nb.h"
#include "Servomoteur.h"
const uint LED_PIN = 25;
#define V_INIT -999.0
int main() {
bi_decl(bi_program_description("This is a test binary."));
bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));
double vitesse_filtre_x=V_INIT, vitesse_filtre_y=V_INIT, vitesse_filtre_z=V_INIT;
struct t_angle_gyro_double angle_gyro;
uint32_t temps_ms = 0, temps_ms_old;
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 1);
sleep_ms(3000);
Servomoteur_Init();
//puts("Debut");
//spi_test();
2022-09-23 20:09:40 +00:00
//while(1);
Temps_init();
Gyro_Init();
temps_ms = Temps_get_temps_ms();
temps_ms_old = temps_ms;
while (1) {
u_int16_t step_ms = 2;
float coef_filtre = 1-0.8;
while(temps_ms == Temps_get_temps_ms());
temps_ms = Temps_get_temps_ms();
temps_ms_old = temps_ms;
// Tous les pas de step_ms
if(!(temps_ms % step_ms)){
uint64_t _time;
uint32_t time_after_read, time_after_filter, time_after_printf;
_time = time_us_64();
Gyro_Read(step_ms);
time_after_read = time_us_64() - _time;
2022-10-28 21:35:26 +00:00
//gyro_affiche(gyro_get_vitesse(), "Angle :");
// Filtre
angle_gyro = gyro_get_vitesse();
if(vitesse_filtre_x == V_INIT){
vitesse_filtre_x = angle_gyro.rot_x;
vitesse_filtre_y = angle_gyro.rot_y;
vitesse_filtre_z = angle_gyro.rot_z;
}else{
vitesse_filtre_x = vitesse_filtre_x * (1-coef_filtre) + angle_gyro.rot_x * coef_filtre;
vitesse_filtre_y = vitesse_filtre_y * (1-coef_filtre) + angle_gyro.rot_y * coef_filtre;
vitesse_filtre_z = vitesse_filtre_z * (1-coef_filtre) + angle_gyro.rot_z * coef_filtre;
}
time_after_filter = time_us_64() - _time;
//printf("%#x, %#x\n", (double)temps_ms_old / 1000, vitesse_filtre_z);
//printf("%d, %d\n", temps_ms, (int32_t) (vitesse_filtre_z * 1000));
time_after_printf = time_us_64() - _time;
//gyro_affiche(angle_gyro, "Vitesse (°/s),");
2022-10-28 21:35:26 +00:00
}
// Toutes les 50 ms
if((Temps_get_temps_ms() % 50) == 0){
struct t_angle_gyro_double m_gyro;
m_gyro = gyro_get_angle();
printf("%f, %f\n", (double)temps_ms / 1000, m_gyro.rot_z);
}
// Toutes les 500 ms
if((Temps_get_temps_ms() % 500) == 0){
//gyro_affiche(gyro_get_angle(), "Angle :");
}
// Toutes les secondes
if((Temps_get_temps_ms() % 500) == 0){
//gyro_get_temp();
}
}
}