2022-09-23 17:43:45 +00:00
|
|
|
#include <stdio.h>
|
2022-11-28 19:49:55 +00:00
|
|
|
#include "pico/multicore.h"
|
2022-09-23 17:43:45 +00:00
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/gpio.h"
|
|
|
|
#include "pico/binary_info.h"
|
|
|
|
|
2022-09-23 20:09:40 +00:00
|
|
|
#include "gyro.h"
|
2022-09-26 19:53:52 +00:00
|
|
|
#include "Temps.h"
|
2022-10-15 19:13:52 +00:00
|
|
|
#include "spi_nb.h"
|
|
|
|
#include "Servomoteur.h"
|
2022-11-20 12:13:14 +00:00
|
|
|
#include "Moteurs.h"
|
2022-11-20 11:53:12 +00:00
|
|
|
#include "QEI.h"
|
2022-11-28 19:49:55 +00:00
|
|
|
#include "Asser_Moteurs.h"
|
2022-09-23 17:43:45 +00:00
|
|
|
|
|
|
|
const uint LED_PIN = 25;
|
2022-11-27 10:35:06 +00:00
|
|
|
const uint LED_PIN_ROUGE = 28;
|
|
|
|
const uint LED_PIN_NE_PAS_UTILISER = 22;
|
|
|
|
|
2022-09-23 17:43:45 +00:00
|
|
|
|
2022-10-15 19:13:52 +00:00
|
|
|
#define V_INIT -999.0
|
2022-11-20 11:53:12 +00:00
|
|
|
#define TEST_TIMEOUT_US 10000000
|
2022-10-15 19:13:52 +00:00
|
|
|
|
2022-11-18 14:44:34 +00:00
|
|
|
int mode_test();
|
|
|
|
int test_moteurs();
|
2022-11-27 10:35:06 +00:00
|
|
|
int test_QIE();
|
2022-11-20 11:53:12 +00:00
|
|
|
int test_vitesse_moteur(enum t_moteur moteur);
|
2022-11-28 19:49:55 +00:00
|
|
|
int test_asser_moteur();
|
2022-11-18 14:44:34 +00:00
|
|
|
|
2022-09-23 17:43:45 +00:00
|
|
|
int main() {
|
|
|
|
bi_decl(bi_program_description("This is a test binary."));
|
|
|
|
bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));
|
2022-10-15 19:13:52 +00:00
|
|
|
double vitesse_filtre_x=V_INIT, vitesse_filtre_y=V_INIT, vitesse_filtre_z=V_INIT;
|
|
|
|
struct t_angle_gyro_double angle_gyro;
|
2022-09-23 17:43:45 +00:00
|
|
|
|
2022-10-15 19:13:52 +00:00
|
|
|
uint32_t temps_ms = 0, temps_ms_old;
|
2022-09-26 19:53:52 +00:00
|
|
|
|
2022-09-23 17:43:45 +00:00
|
|
|
stdio_init_all();
|
|
|
|
|
|
|
|
gpio_init(LED_PIN);
|
|
|
|
gpio_set_dir(LED_PIN, GPIO_OUT);
|
|
|
|
gpio_put(LED_PIN, 1);
|
|
|
|
|
2022-11-27 10:35:06 +00:00
|
|
|
gpio_init(LED_PIN_ROUGE);
|
|
|
|
gpio_set_dir(LED_PIN_ROUGE, GPIO_OUT);
|
|
|
|
gpio_put(LED_PIN_ROUGE, 1);
|
|
|
|
|
|
|
|
// Il fuat neutraliser cettte broche qui pourrait interférer avec
|
|
|
|
// la lecture des codeurs. (problème sur la carte électrique)...
|
|
|
|
gpio_init(LED_PIN_NE_PAS_UTILISER);
|
|
|
|
gpio_set_dir(LED_PIN_NE_PAS_UTILISER, GPIO_IN);
|
|
|
|
|
2022-09-24 09:38:55 +00:00
|
|
|
sleep_ms(3000);
|
2022-10-15 19:13:52 +00:00
|
|
|
Servomoteur_Init();
|
|
|
|
//puts("Debut");
|
|
|
|
//spi_test();
|
2022-09-23 20:09:40 +00:00
|
|
|
|
2022-10-15 19:13:52 +00:00
|
|
|
//while(1);
|
2022-09-26 19:53:52 +00:00
|
|
|
Temps_init();
|
2022-11-20 11:53:12 +00:00
|
|
|
Moteur_Init();
|
2022-11-27 10:35:06 +00:00
|
|
|
QEI_init();
|
2022-11-28 19:49:55 +00:00
|
|
|
AsserMoteur_Init();
|
2022-11-18 14:44:34 +00:00
|
|
|
|
|
|
|
while(mode_test());
|
|
|
|
|
2022-11-20 11:53:12 +00:00
|
|
|
Gyro_Init();
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-26 19:53:52 +00:00
|
|
|
temps_ms = Temps_get_temps_ms();
|
2022-10-15 19:13:52 +00:00
|
|
|
temps_ms_old = temps_ms;
|
2022-09-23 17:43:45 +00:00
|
|
|
while (1) {
|
2022-10-29 11:48:44 +00:00
|
|
|
u_int16_t step_ms = 2;
|
|
|
|
float coef_filtre = 1-0.8;
|
2022-10-15 19:13:52 +00:00
|
|
|
|
2022-10-29 11:48:44 +00:00
|
|
|
while(temps_ms == Temps_get_temps_ms());
|
|
|
|
temps_ms = Temps_get_temps_ms();
|
|
|
|
temps_ms_old = temps_ms;
|
2022-09-26 19:53:52 +00:00
|
|
|
|
|
|
|
// Tous les pas de step_ms
|
2022-10-29 11:48:44 +00:00
|
|
|
if(!(temps_ms % step_ms)){
|
2022-09-26 19:53:52 +00:00
|
|
|
Gyro_Read(step_ms);
|
2022-10-28 21:35:26 +00:00
|
|
|
|
2022-10-15 19:13:52 +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;
|
|
|
|
}
|
|
|
|
|
2022-10-29 13:51:52 +00:00
|
|
|
//printf("%#x, %#x\n", (double)temps_ms_old / 1000, vitesse_filtre_z);
|
|
|
|
|
|
|
|
//printf("%d, %d\n", temps_ms, (int32_t) (vitesse_filtre_z * 1000));
|
2022-10-15 19:13:52 +00:00
|
|
|
//gyro_affiche(angle_gyro, "Vitesse (°/s),");
|
2022-10-28 21:35:26 +00:00
|
|
|
|
2022-10-15 19:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Toutes les 50 ms
|
|
|
|
if((Temps_get_temps_ms() % 50) == 0){
|
2022-10-29 13:51:52 +00:00
|
|
|
struct t_angle_gyro_double m_gyro;
|
|
|
|
m_gyro = gyro_get_angle();
|
|
|
|
printf("%f, %f\n", (double)temps_ms / 1000, m_gyro.rot_z);
|
2022-09-26 19:53:52 +00:00
|
|
|
}
|
2022-09-28 16:46:45 +00:00
|
|
|
|
|
|
|
// Toutes les 500 ms
|
2022-09-26 19:53:52 +00:00
|
|
|
if((Temps_get_temps_ms() % 500) == 0){
|
2022-10-15 19:13:52 +00:00
|
|
|
//gyro_affiche(gyro_get_angle(), "Angle :");
|
|
|
|
}
|
|
|
|
// Toutes les secondes
|
|
|
|
if((Temps_get_temps_ms() % 500) == 0){
|
|
|
|
//gyro_get_temp();
|
2022-09-26 19:53:52 +00:00
|
|
|
}
|
2022-09-23 17:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 14:44:34 +00:00
|
|
|
|
|
|
|
// Mode test : renvoie 0 pour quitter le mode test
|
|
|
|
int mode_test(){
|
2022-11-20 11:53:12 +00:00
|
|
|
static int iteration = 3;
|
2022-11-18 14:44:34 +00:00
|
|
|
printf("Appuyez sur une touche pour entrer en mode test :\n");
|
2022-11-27 10:35:06 +00:00
|
|
|
printf("C - pour les codeurs\n");
|
2022-11-18 14:44:34 +00:00
|
|
|
printf("M - pour les moteurs\n");
|
2022-11-20 11:53:12 +00:00
|
|
|
stdio_flush();
|
|
|
|
int rep = getchar_timeout_us(TEST_TIMEOUT_US);
|
|
|
|
stdio_flush();
|
2022-11-18 14:44:34 +00:00
|
|
|
switch (rep)
|
|
|
|
{
|
2022-11-28 19:49:55 +00:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
while(test_asser_moteur());
|
|
|
|
break;
|
2022-11-27 10:35:06 +00:00
|
|
|
case 'C':
|
|
|
|
case 'c':
|
|
|
|
while(test_QIE());
|
|
|
|
break;
|
2022-11-18 14:44:34 +00:00
|
|
|
case 'M':
|
2022-11-20 11:53:12 +00:00
|
|
|
case 'm':
|
2022-11-18 14:44:34 +00:00
|
|
|
/* code */
|
2022-11-20 11:53:12 +00:00
|
|
|
while(test_moteurs());
|
2022-11-18 14:44:34 +00:00
|
|
|
break;
|
|
|
|
case PICO_ERROR_TIMEOUT:
|
2022-11-20 11:53:12 +00:00
|
|
|
iteration--;
|
|
|
|
if(iteration == 0){
|
|
|
|
printf("Sortie du mode test\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-18 14:44:34 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Commande inconnue\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-11-20 11:53:12 +00:00
|
|
|
return 1;
|
2022-11-18 14:44:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:49:55 +00:00
|
|
|
|
|
|
|
void test_asser_moteur_printf(){
|
|
|
|
int _step_ms = 1;
|
|
|
|
while(1){
|
|
|
|
printf("Vitesse A : %.0f, vitesse B : %.0f, vitesse C : %.0f\n", AsserMoteur_getVitesse_mm_s(MOTEUR_A, _step_ms),
|
|
|
|
AsserMoteur_getVitesse_mm_s(MOTEUR_B, _step_ms), AsserMoteur_getVitesse_mm_s(MOTEUR_C, _step_ms));
|
|
|
|
//sleep_ms(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_asser_moteur(){
|
|
|
|
int lettre;
|
|
|
|
int _step_ms = 1;
|
|
|
|
printf("Asservissement des moteurs :\nAppuyez sur une touche pour quitter\n");
|
|
|
|
AsserMoteur_setConsigne_mm_s(MOTEUR_A, 500);
|
|
|
|
AsserMoteur_setConsigne_mm_s(MOTEUR_B, 500);
|
|
|
|
AsserMoteur_setConsigne_mm_s(MOTEUR_C, 500);
|
|
|
|
multicore_launch_core1(test_asser_moteur_printf);
|
|
|
|
do{
|
|
|
|
QEI_update();
|
|
|
|
AsserMoteur_Gestion(_step_ms);
|
|
|
|
sleep_ms(_step_ms);
|
|
|
|
//printf("Vitesse A : %d, codeur B : %d, codeur C : %d\n", QEI_get(QEI_A_NAME), QEI_get(QEI_B_NAME), QEI_get(QEI_C_NAME));
|
|
|
|
//printf("Vitesse A : %.0f, vitesse B : %.0f, vitesse C : %.0f\n", AsserMoteur_getVitesse_mm_s(MOTEUR_A, _step_ms),
|
|
|
|
// AsserMoteur_getVitesse_mm_s(MOTEUR_B, _step_ms), AsserMoteur_getVitesse_mm_s(MOTEUR_C, _step_ms));
|
|
|
|
lettre = getchar_timeout_us(0);
|
|
|
|
}while(lettre == PICO_ERROR_TIMEOUT);
|
|
|
|
multicore_reset_core1();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-27 10:35:06 +00:00
|
|
|
int test_QIE(){
|
|
|
|
int lettre;
|
|
|
|
printf("Affichage des QEI :\nAppuyez sur une touche pour quitter\n");
|
|
|
|
do{
|
|
|
|
QEI_update();
|
2022-11-28 19:49:55 +00:00
|
|
|
printf("Codeur A : %d, codeur B : %d, codeur C : %d\n", QEI_get(QEI_A_NAME), QEI_get(QEI_B_NAME), QEI_get(QEI_C_NAME));
|
2022-11-27 10:35:06 +00:00
|
|
|
sleep_ms(100);
|
|
|
|
|
|
|
|
lettre = getchar_timeout_us(0);
|
|
|
|
}while(lettre == PICO_ERROR_TIMEOUT);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-18 14:44:34 +00:00
|
|
|
int test_moteurs(){
|
2022-11-20 11:53:12 +00:00
|
|
|
int lettre_moteur;
|
|
|
|
|
2022-11-18 14:44:34 +00:00
|
|
|
printf("Indiquez le moteurs à tester (A, B ou C):\n");
|
2022-11-20 11:53:12 +00:00
|
|
|
do{
|
|
|
|
lettre_moteur = getchar_timeout_us(TEST_TIMEOUT_US);
|
2022-11-27 10:35:06 +00:00
|
|
|
stdio_flush();
|
2022-11-20 11:53:12 +00:00
|
|
|
}while(lettre_moteur == PICO_ERROR_TIMEOUT);
|
|
|
|
printf("Moteur choisi : %c %d %x\n", lettre_moteur, lettre_moteur, lettre_moteur);
|
|
|
|
|
|
|
|
switch (lettre_moteur)
|
|
|
|
{
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
while(test_vitesse_moteur(MOTEUR_A));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
case 'b':
|
|
|
|
while(test_vitesse_moteur(MOTEUR_B));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
case 'c':
|
|
|
|
while(test_vitesse_moteur(MOTEUR_C));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Q':
|
|
|
|
case 'q':
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_vitesse_moteur(enum t_moteur moteur){
|
|
|
|
printf("Vitesse souhaitée :\n0 - 0%%\n1 - 10%%\n2 - 20%%\n...\n9 - 90%%\nA - 100%%\n");
|
|
|
|
|
|
|
|
int vitesse_moteur;
|
|
|
|
do{
|
|
|
|
vitesse_moteur = getchar_timeout_us(TEST_TIMEOUT_US);
|
2022-11-27 10:35:06 +00:00
|
|
|
stdio_flush();
|
2022-11-20 11:53:12 +00:00
|
|
|
}while(vitesse_moteur == PICO_ERROR_TIMEOUT);
|
|
|
|
|
|
|
|
switch (vitesse_moteur)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
printf("Vitesse choisie : %c0%%\n", vitesse_moteur);
|
|
|
|
Moteur_SetVitesse(moteur, (vitesse_moteur - '0') * 32767.0 / 10.);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
printf("Vitesse choisie : 100%%\n");
|
|
|
|
Moteur_SetVitesse(moteur, (int16_t) 32766.0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
2022-11-18 14:44:34 +00:00
|
|
|
}
|