Telecommande_2024/main.c

247 lines
4.1 KiB
C
Raw Normal View History

2023-12-15 17:33:44 +00:00
/*****
2023-10-22 16:48:14 +00:00
* Copyright (c) 2023 - Poivron Robotique
*
* SPDX-License-Identifier: BSD-3-Clause
*/
2024-01-12 16:26:55 +00:00
#include "communication.h"
2023-10-22 16:48:14 +00:00
#include "pico/stdlib.h"
2023-12-15 17:33:44 +00:00
#include "hardware/pwm.h"
2023-10-22 16:48:14 +00:00
#include <stdio.h>
2024-01-12 17:53:51 +00:00
#include "hardware/adc.h"
2023-12-15 17:33:44 +00:00
#define LED_VERTE 25
#define PIN_VITESSE_M1 2
#define PIN_SENS_A_M1 0
#define PIN_SENS_B_M1 1
2024-01-12 17:53:51 +00:00
2024-01-31 20:55:47 +00:00
#define PINCE_OUVERTE 0
#define PINCE_PLANTE 1
#define PINCE_POT 1
#define ASCENSEUR_BAS 0
#define ASCENSEUR_HAUT 1
#define ASCENSEUR_LACHE_POT_JARDINIERE 2
#define ASCENSEUR_LACHE_PLANTE 2
unsigned result0, result1;
int joystic_clicker;
2024-01-31 20:55:47 +00:00
int pince = PINCE_OUVERTE;
int ascenceur = ASCENSEUR_BAS;
2024-01-12 16:26:55 +00:00
2023-12-15 17:33:44 +00:00
int M1_INITIALISE()
2023-10-22 16:48:14 +00:00
{
2023-12-15 17:33:44 +00:00
gpio_init(PIN_VITESSE_M1);
gpio_init(PIN_SENS_A_M1);
gpio_init(PIN_SENS_B_M1);
gpio_set_dir(PIN_VITESSE_M1, GPIO_OUT);
gpio_set_dir(PIN_SENS_A_M1, GPIO_OUT);
gpio_set_dir(PIN_SENS_B_M1, GPIO_OUT);
2024-01-12 17:53:51 +00:00
gpio_set_function(PIN_VITESSE_M1, GPIO_FUNC_PWM);
pwm_set_wrap(PIN_VITESSE_M1, 100);
pwm_set_chan_level(PIN_VITESSE_M1, PWM_CHAN_B, 50);
2023-12-15 17:33:44 +00:00
}
int M1_AVANCE()
{
gpio_put(PIN_VITESSE_M1, 1);
gpio_put(PIN_SENS_A_M1, 1);
gpio_put(PIN_SENS_B_M1, 0);
}
int M1_RECULE()
{
gpio_put(PIN_VITESSE_M1, 1);
gpio_put(PIN_SENS_A_M1, 0);
gpio_put(PIN_SENS_B_M1, 1);
2023-10-22 16:48:14 +00:00
}
2024-01-12 17:53:51 +00:00
int Adc_Init()
{
adc_init();
adc_gpio_init(26);
adc_gpio_init(27);
}
int AdcRead0()
{
adc_select_input(0);
uint16_t resultadc = adc_read();
return resultadc;
}
2023-12-15 17:33:44 +00:00
2024-01-12 17:53:51 +00:00
int AdcRead1()
{
adc_select_input(1);
uint16_t resultadc = adc_read();
2024-01-12 17:53:51 +00:00
return resultadc;
}
2023-12-15 17:33:44 +00:00
2024-01-31 20:55:47 +00:00
void init_bouton(uint8_t bouton){
gpio_init(bouton);
gpio_set_dir(bouton, GPIO_IN);
gpio_pull_up(bouton);
}
int bouton_appui(uint8_t bouton){
static uint8_t etat_bouton[32]={1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1};
if(gpio_get(bouton) == 1 && gpio_get(bouton) != etat_bouton[bouton]){
etat_bouton[bouton] = gpio_get(bouton);
return 1;
}else{
etat_bouton[bouton] = gpio_get(bouton);
}
return 0;
2023-12-15 17:33:44 +00:00
2024-01-31 20:55:47 +00:00
}
2023-12-15 17:33:44 +00:00
void main()
{
2024-01-12 16:26:55 +00:00
char message [256];
2023-12-15 17:33:44 +00:00
stdio_init_all();
// Communication
communication_init();
// Pour envoyer un message
// communication_envoyer_message(message, 254);
2024-01-12 16:26:55 +00:00
2023-12-15 17:33:44 +00:00
// CLignottement LED
gpio_set_function(LED_VERTE, GPIO_FUNC_PWM);
pwm_set_wrap(4, 100);
pwm_set_chan_level(4, PWM_CHAN_B, 25);
pwm_set_enabled(4, true);
2024-01-12 17:53:51 +00:00
// ADC
adc_init();
adc_gpio_init(26);
adc_gpio_init(27);
2024-01-31 20:55:47 +00:00
// Boutons
init_bouton(2);
init_bouton(6);
init_bouton(10);
init_bouton(14);
ascenceur = ASCENSEUR_BAS;
pince = PINCE_OUVERTE;
2024-01-12 17:53:51 +00:00
while (1)
{
// Voie X
2024-01-31 20:55:47 +00:00
result1 = AdcRead1()/16;
// Voie Y
2024-01-31 20:55:47 +00:00
result0 = AdcRead0()/16;
message[0] = 255 - result0;
message[1] = result1;
//clic sur le joystic
gpio_init(1);
gpio_pull_up(1);
gpio_set_dir(1, GPIO_IN);
joystic_clicker = gpio_get(1);
if (!joystic_clicker)
{
2024-01-31 20:55:47 +00:00
if(result1 > 135){
message[2] = 220;
}else if (result1 < 122){
message[2] = 30;
}else{
message[2] = 128;
}
message[1] = 128;
message[0] = 128;
}
else
{
message[2] = 128;
}
2024-01-12 16:26:55 +00:00
2024-01-31 20:55:47 +00:00
// Pince
// Bouton plante
if(bouton_appui(6)){
if(pince == PINCE_OUVERTE){
pince = PINCE_PLANTE;
}else{
pince = PINCE_OUVERTE;
}
}
// Bouton Pot
if(bouton_appui(10)){
if(pince == PINCE_OUVERTE){
pince = PINCE_POT;
}else{
pince = PINCE_OUVERTE;
}
}
2024-01-12 16:26:55 +00:00
2024-01-31 20:55:47 +00:00
// Ascenseur
// Commande simple
if(bouton_appui(2)){
if(ascenceur == ASCENSEUR_BAS){
ascenceur = ASCENSEUR_HAUT;
}else{
ascenceur = ASCENSEUR_BAS;
}
};
// Commande évoluée
if(bouton_appui(14)){
if(ascenceur == ASCENSEUR_HAUT){
if(pince == PINCE_PLANTE){
ascenceur = ASCENSEUR_LACHE_PLANTE;
}
if(pince == PINCE_POT){
ascenceur = ASCENSEUR_LACHE_POT_JARDINIERE;
}
}else{
ascenceur = ASCENSEUR_HAUT;
}
}
2024-01-12 16:26:55 +00:00
message [3] = pince;
message[4] = ascenceur;
2024-01-12 16:26:55 +00:00
printf(">x:%d\n", message[0]);
printf(">Y:%d\n", message[1]);
printf(">Rz:%d\n", message[2]);
printf(">pince:%d\n", message[3]);
printf(">ascenceur:%d\n", message[4]);
2024-01-31 20:55:47 +00:00
printf(">result0:%d\n", result0);
sleep_ms(25);
communication_envoyer_message(message, 254);
2024-01-12 17:53:51 +00:00
}
2023-12-15 17:33:44 +00:00
M1_INITIALISE();
while(1)
{
M1_AVANCE();
2024-01-12 17:53:51 +00:00
sleep_ms(1000);
2023-12-15 17:33:44 +00:00
M1_RECULE();
2024-01-12 17:53:51 +00:00
sleep_ms(1000);
2023-12-15 17:33:44 +00:00
}
}