2026-Actionneurs/main.c

164 lines
3.8 KiB
C

/*****
* Copyright (c) 2023 - Poivron Robotique
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "hardware/pwm.h"
#include <stdio.h>
#define PIN_STEP 0
#define PIN_DIR 1
#define PIN_ENABLE 2
#define PIN_CC_PWM 3
#define PIN_CC_DIR 4
#define PIN_CONTACTEUR_BAS 7
#define PIN_CONTACTEUR_OUVERT 5
#define PIN_CONTACTEUR_FERMÉ 6
bool bouton_Presser = false;
float v_actuelle_tr_s=0;
float v_consigne_tr_s=0;
float acc_tr_s = 50;
const float pas_par_tour = 200;
float temps_pas= 0.001;
/// @brief calcule le pas de temps suivant
/// @param
/// @return
float compute_time_step(float temps_pas_s){
if(v_actuelle_tr_s != v_consigne_tr_s){
// On ajoute l'accélération
v_actuelle_tr_s += acc_tr_s * temps_pas_s;
//printf(">vit:%f\n", v_actuelle_tr_s);
// On vérifie qu'on a pas dépassé la vitesse consigne
if(v_actuelle_tr_s > v_consigne_tr_s){
v_actuelle_tr_s = v_consigne_tr_s;
}
}
//printf(">vit_2:%f\n", v_actuelle_tr_s);
float temps_tr, temps_pas;
if(v_actuelle_tr_s != 0){
temps_tr = 1 / v_actuelle_tr_s; // 100
temps_pas = temps_tr / pas_par_tour; // 5
if(temps_pas > 0.001)
temps_pas = 0.001;
return temps_pas;
}
return 0.001;
}
void affiche_pas_de_temps(){
while(1){
printf(">temps_pas:%f\n", temps_pas * 1000000);
printf(">v_actuelle_tr_s:%f\n", v_actuelle_tr_s);
printf(">v_consigne_tr_s:%f\n", v_consigne_tr_s);
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, 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, 0);
}
void main(void)
{
stdio_init_all();
// Moteur CC
gpio_set_function(PIN_CC_PWM, GPIO_FUNC_PWM);
pwm_set_wrap(1, (uint16_t)65535);
pwm_set_chan_level(1, PWM_CHAN_B, 0);
pwm_set_enabled(1, true);
gpio_init(PIN_CC_DIR);
gpio_set_dir(PIN_CC_DIR, GPIO_OUT);
gpio_put(PIN_CC_DIR, 1);
// Contacteurs
gpio_init(PIN_CONTACTEUR_BAS);
gpio_init(PIN_CONTACTEUR_FERMÉ);
gpio_init(PIN_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();
// Pas à pas
gpio_init(PIN_STEP);
gpio_init(PIN_DIR);
gpio_init(PIN_ENABLE);
gpio_set_dir(PIN_STEP, GPIO_IN);
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);
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);
}
while(1){
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);
gpio_put(PIN_STEP, 0);
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;
}
if(key == 's' || key == 'S'){
gpio_put(PIN_DIR, 0);
gpio_put(PIN_ENABLE, 1);
gpio_set_dir(PIN_STEP, GPIO_IN);
v_consigne_tr_s = 0;
}
//printf("%c %d\n", key,key);
}
}
}