diff --git a/main.c b/main.c index 5c4f9f6..b3019a7 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include "pico/stdlib.h" +#include "pico/time.h" #include "pico/multicore.h" #include "hardware/pwm.h" #include @@ -16,6 +17,9 @@ #define PIN_CONTACTEUR_OUVERT 5 #define PIN_CONTACTEUR_FERMÉ 6 +#define SENS_BAS 0 +#define SENS_HAUT 1 + bool bouton_Presser = false; @@ -25,6 +29,45 @@ float acc_tr_s = 50; const float pas_par_tour = 200; float temps_pas= 0.001; +struct contacteur_t{ + uint gpio; + int pos_valide; + int pos_actuelle; + uint64_t time_new_pos; +}; +struct contacteur_t contacteur_bas, contacteur_fermé, contacteur_ouvert; + +uint64_t get_us_since_boot(){ + return to_us_since_boot(get_absolute_time()); +} + +void contacteur_update(struct contacteur_t * contacteur){ + // Si la position du contacteur a changé, on lance le timer + bool pos_lu = gpio_get(contacteur->gpio); + if(pos_lu != contacteur->pos_actuelle){ + contacteur->pos_actuelle = pos_lu; + if(contacteur->pos_actuelle != contacteur->pos_valide){ + contacteur->time_new_pos = get_us_since_boot(); + } + } + if(contacteur->pos_actuelle != contacteur->pos_valide){ + if(get_us_since_boot() - contacteur->time_new_pos > 100){ + contacteur->pos_valide = contacteur->pos_actuelle; + } + } +} + +int contacteur_init(uint gpio, struct contacteur_t * contacteur){ + contacteur->gpio = gpio; + + gpio_init(contacteur->gpio ); + gpio_pull_down(contacteur->gpio ); + gpio_set_dir(contacteur->gpio, GPIO_IN); + bool pos_lu = gpio_get(contacteur->gpio); + contacteur->pos_actuelle = pos_lu; + contacteur->pos_valide = pos_lu; +} + /// @brief calcule le pas de temps suivant /// @param /// @return @@ -50,28 +93,42 @@ float compute_time_step(float temps_pas_s){ return 0.001; } + + void affiche_pas_de_temps(){ while(1){ - printf(">temps_pas:%f\n", temps_pas * 1000000); + printf(">temps_pas:%f\n", temps_pas * 1000); printf(">v_actuelle_tr_s:%f\n", v_actuelle_tr_s); printf(">v_consigne_tr_s:%f\n", v_consigne_tr_s); + printf(">contacteur_bas:%d\n", contacteur_bas.pos_valide); sleep_ms(1); } } void pince_ouvre(){ gpio_put(PIN_CC_DIR, 1); - pwm_set_chan_level(1, PWM_CHAN_B, 16000); - while(gpio_get(PIN_CONTACTEUR_OUVERT)); + pwm_set_chan_level(1, PWM_CHAN_B, 60000); + while(contacteur_ouvert.pos_valide){ + contacteur_update(&contacteur_ouvert); + } pwm_set_chan_level(1, PWM_CHAN_B, 0); } void pince_ferme(){ gpio_put(PIN_CC_DIR, 0); - pwm_set_chan_level(1, PWM_CHAN_B, 16000); - while(gpio_get(PIN_CONTACTEUR_FERMÉ)); + pwm_set_chan_level(1, PWM_CHAN_B, 60000); + while(contacteur_fermé.pos_valide){ + contacteur_update(&contacteur_fermé); + } pwm_set_chan_level(1, PWM_CHAN_B, 0); } +void pas_a_pas_stop(){ + gpio_put(PIN_DIR, 0); + gpio_put(PIN_ENABLE, 1); + gpio_set_dir(PIN_STEP, GPIO_IN); + v_consigne_tr_s = 0; +} + void main(void) { stdio_init_all(); @@ -88,19 +145,11 @@ void main(void) // Contacteurs - gpio_init(PIN_CONTACTEUR_BAS); - gpio_init(PIN_CONTACTEUR_FERMÉ); - gpio_init(PIN_CONTACTEUR_OUVERT); + contacteur_init(PIN_CONTACTEUR_BAS, &contacteur_bas); + contacteur_init(PIN_CONTACTEUR_FERMÉ, &contacteur_fermé); + contacteur_init(PIN_CONTACTEUR_OUVERT, &contacteur_ouvert); - gpio_pull_down(PIN_CONTACTEUR_BAS); - gpio_pull_down(PIN_CONTACTEUR_FERMÉ); - gpio_pull_down(PIN_CONTACTEUR_OUVERT); - - gpio_set_dir(PIN_CONTACTEUR_BAS, GPIO_IN); - gpio_set_dir(PIN_CONTACTEUR_FERMÉ, GPIO_IN); - gpio_set_dir(PIN_CONTACTEUR_OUVERT, GPIO_IN); - - pince_ferme(); + pince_ouvre(); // Pas à pas @@ -111,25 +160,25 @@ void main(void) gpio_set_dir(PIN_DIR, GPIO_OUT); gpio_set_dir(PIN_ENABLE, GPIO_OUT); gpio_put(PIN_ENABLE, 1); - //multicore_launch_core1(affiche_pas_de_temps); + multicore_launch_core1(affiche_pas_de_temps); float vitesse_nominale_tr_s = 10; //sleep_ms(3000); //printf("kartoffen\n"); const uint32_t my_delay=500; - - while(1){ - printf(">C_ouvert:%d\n", gpio_get(PIN_CONTACTEUR_OUVERT)); - printf(">C_ferme:%d\n", gpio_get(PIN_CONTACTEUR_FERMÉ)); - printf(">C_bas:%d\n", gpio_get(PIN_CONTACTEUR_BAS)); - - sleep_ms(50); - } + uint sens_pas_a_pas=SENS_BAS; while(1){ + contacteur_update(&contacteur_ouvert); + contacteur_update(&contacteur_fermé); + contacteur_update(&contacteur_bas); + + if(sens_pas_a_pas==SENS_BAS && contacteur_bas.pos_valide == 1){ + pas_a_pas_stop(); + } + temps_pas = compute_time_step(temps_pas); - //printf(">t_pas:%f\n", temps_pas); sleep_us(temps_pas * 1000000); gpio_put(PIN_STEP, 1); sleep_us(temps_pas * 1000000); @@ -137,17 +186,19 @@ void main(void) int key = getchar_timeout_us(0); // get any pending key press but don't wait if (key != PICO_ERROR_TIMEOUT) { if(key == 'u' || key == 'U'){ - gpio_put(PIN_DIR, 0); - gpio_put(PIN_ENABLE, 0); - gpio_set_dir(PIN_STEP, GPIO_OUT); - v_consigne_tr_s = vitesse_nominale_tr_s; - - } - if(key == 'd' || key == 'D'){ gpio_put(PIN_DIR, 1); gpio_put(PIN_ENABLE, 0); gpio_set_dir(PIN_STEP, GPIO_OUT); v_consigne_tr_s = vitesse_nominale_tr_s; + sens_pas_a_pas=SENS_HAUT; + + } + if(key == 'd' || key == 'D'){ + gpio_put(PIN_DIR, 0); + gpio_put(PIN_ENABLE, 0); + gpio_set_dir(PIN_STEP, GPIO_OUT); + v_consigne_tr_s = vitesse_nominale_tr_s; + sens_pas_a_pas=SENS_BAS; } if(key == 's' || key == 'S'){ gpio_put(PIN_DIR, 0); @@ -155,6 +206,12 @@ void main(void) gpio_set_dir(PIN_STEP, GPIO_IN); v_consigne_tr_s = 0; } + if(key == 'o' || key == 'O'){ + pince_ouvre(); + } + if(key == 'f' || key == 'F'){ + pince_ferme(); + } //printf("%c %d\n", key,key); }