Telecommande_2024/main.c

236 lines
4.4 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"
// Pins
#define PIN_BUTTON_CLIC_JOYSTICK 1 // Unused
#define PIN_BUTTON_ELEVATOR 2
#define PIN_BUTTON_LEFT_ROTATION 5
#define PIN_BUTTON_POT 6
#define PIN_BUTTON_RIGHT_ROTATION 9
#define PIN_BUTTON_CLAW 10
#define PIN_BUTTON_MID_ELEVATOR 13
#define PIN_LED_VERTE 25
// Différents états de la pince
2024-01-31 20:55:47 +00:00
#define PINCE_OUVERTE 0
#define PINCE_PLANTE 1
#define PINCE_POT 2
2024-01-31 20:55:47 +00:00
// Différent états de l'ascenseur
2024-01-31 20:55:47 +00:00
#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;
int pince;
int ascenseur;
2024-01-12 16:26:55 +00:00
void init_bouton(uint8_t bouton)
2023-12-15 17:33:44 +00:00
{
gpio_init(bouton);
gpio_set_dir(bouton, GPIO_IN);
gpio_pull_up(bouton);
2023-10-22 16:48:14 +00:00
}
2024-11-16 11:00:45 +00:00
2024-01-12 17:53:51 +00:00
int Adc_Init()
{
adc_init();
adc_gpio_init(26);
adc_gpio_init(27);
}
2024-11-16 11:00:45 +00:00
int Adc_Read(short input)
2024-11-16 11:00:45 +00:00
{
adc_select_input(input);
uint16_t result_adc = adc_read();
return result_adc;
2024-01-31 20:55:47 +00:00
}
bool bouton_appui(uint8_t button)
2024-11-16 11:00:45 +00:00
{
2024-01-31 20:55:47 +00:00
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};
2024-11-16 11:00:45 +00:00
bool is_button_pressed = gpio_get(button);
etat_bouton[button] = is_button_pressed;
2023-12-15 17:33:44 +00:00
return is_button_pressed && is_button_pressed != etat_bouton[button];
2024-01-31 20:55:47 +00:00
}
2023-12-15 17:33:44 +00:00
void main()
{
stdio_init_all();
// Communication
char message [256];
communication_init();
2024-01-12 17:53:51 +00:00
// ADC
Adc_Init();
2024-01-31 20:55:47 +00:00
// Boutons
2024-12-07 21:22:48 +00:00
init_bouton(PIN_BUTTON_ELEVATOR);
init_bouton(PIN_BUTTON_LEFT_ROTATION);
init_bouton(PIN_BUTTON_POT);
init_bouton(PIN_BUTTON_RIGHT_ROTATION);
init_bouton(PIN_BUTTON_CLAW);
init_bouton(PIN_BUTTON_MID_ELEVATOR);
2024-01-31 20:55:47 +00:00
ascenseur = ASCENSEUR_BAS;
2024-01-31 20:55:47 +00:00
pince = PINCE_OUVERTE;
gpio_init(PIN_LED_VERTE);
gpio_set_dir(PIN_LED_VERTE, GPIO_OUT);
gpio_put(PIN_LED_VERTE, true);
while(true)
2024-01-12 17:53:51 +00:00
{
// Voie X
result1 = Adc_Read(1) / 16;
// Voie Y
result0 = Adc_Read(0) / 16;
message[0] = result0;
message[1] = 255 - result1;
// Set rotation speed according to DA rotations buttons
message[2] = 128 - 95 * (gpio_get(PIN_BUTTON_LEFT_ROTATION) - gpio_get(PIN_BUTTON_RIGHT_ROTATION));
/*
// Voie X
result1 = AdcRead(1) / 16;
// Voie Y
result0 = AdcRead(0) / 16;
2024-01-31 20:55:47 +00:00
message[0] = result0;
message[1] = 255 - result1;
//clic sur le joystic
joystic_clicker = gpio_get(PIN_BUTTON_CLIC_JOYSTICK);
if (!joystic_clicker)
{
2024-11-16 11:00:45 +00:00
if(result1 > 135)
{
2024-01-31 20:55:47 +00:00
message[2] = 30;
2024-11-16 11:00:45 +00:00
}
else if (result1 < 122)
{
message[2] = 220;
2024-11-16 11:00:45 +00:00
}else
{
2024-01-31 20:55:47 +00:00
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)){
2024-11-16 11:00:45 +00:00
if(pince == PINCE_OUVERTE)
{
pince = PINCE_POT;
2024-11-16 11:00:45 +00:00
}
else
{
2024-01-31 20:55:47 +00:00
pince = PINCE_OUVERTE;
}
}
2024-11-16 11:00:45 +00:00
2024-01-31 20:55:47 +00:00
// Bouton Pot
2024-11-16 11:00:45 +00:00
if(bouton_appui(10))
{
if(pince == PINCE_OUVERTE)
{
pince = PINCE_PLANTE;
2024-11-16 11:00:45 +00:00
}
else
{
2024-01-31 20:55:47 +00:00
pince = PINCE_OUVERTE;
}
}
2024-01-12 16:26:55 +00:00
2024-01-31 20:55:47 +00:00
// Ascenseur
// Commande simple
2024-11-16 11:00:45 +00:00
if(bouton_appui(2))
{
if(ascenseur == ASCENSEUR_HAUT)
2024-11-16 11:00:45 +00:00
{
ascenseur = ASCENSEUR_BAS;
2024-11-16 11:00:45 +00:00
}
else
{
ascenseur = ASCENSEUR_HAUT;
2024-01-31 20:55:47 +00:00
}
2024-11-16 11:00:45 +00:00
}
2024-01-31 20:55:47 +00:00
// Commande évoluée
2024-11-16 11:00:45 +00:00
if(bouton_appui(14))
{
if(ascenseur == ASCENSEUR_HAUT)
2024-11-16 11:00:45 +00:00
{
if(pince == PINCE_PLANTE)
{
ascenseur = ASCENSEUR_LACHE_PLANTE;
2024-01-31 20:55:47 +00:00
}
2024-11-16 11:00:45 +00:00
if(pince == PINCE_POT)
{
ascenseur = ASCENSEUR_LACHE_POT_JARDINIERE;
2024-01-31 20:55:47 +00:00
}
2024-11-16 11:00:45 +00:00
// Optimisation : lorsque la pince est fermée et l'ascenseur au niveau milieu, la pince s'ouvre, l'ascenseur monte et le robot recule
}
else if((ascenseur == ASCENSEUR_LACHE_POT_JARDINIERE && pince == PINCE_POT) ||
(ascenseur == ASCENSEUR_LACHE_PLANTE && pince == PINCE_PLANTE))
{
2024-11-16 11:00:45 +00:00
pince = PINCE_OUVERTE;
message [3] = pince;
communication_envoyer_message(message, 254);
sleep_ms(500);
ascenseur = ASCENSEUR_HAUT;
message[4] = ascenseur;
2024-11-16 11:00:45 +00:00
communication_envoyer_message(message, 254);
sleep_ms(500);
message[0] = 20;
communication_envoyer_message(message, 254);
sleep_ms(1000);
2024-01-31 20:55:47 +00:00
}
else
{
ascenseur = ASCENSEUR_HAUT;
2024-11-16 11:00:45 +00:00
}
}
2024-01-12 16:26:55 +00:00
2024-12-07 21:22:48 +00:00
message[3] = pince;
message[4] = ascenseur;
2024-01-12 16:26:55 +00:00
2024-12-07 21:22:48 +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(">ascenseur:%d\n", message[4]);
2024-12-07 21:22:48 +00:00
//printf(">result0:%d\n", result0);
communication_envoyer_message(message, 254);
2024-12-07 21:22:48 +00:00
sleep_ms(25);
2024-01-12 17:53:51 +00:00
}
}