236 lines
4.4 KiB
C
236 lines
4.4 KiB
C
/*****
|
|
* Copyright (c) 2023 - Poivron Robotique
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
#include "communication.h"
|
|
#include "pico/stdlib.h"
|
|
#include "hardware/pwm.h"
|
|
#include <stdio.h>
|
|
#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
|
|
#define PINCE_OUVERTE 0
|
|
#define PINCE_PLANTE 1
|
|
#define PINCE_POT 2
|
|
|
|
// Différent états de l'ascenseur
|
|
#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;
|
|
|
|
void init_bouton(uint8_t bouton)
|
|
{
|
|
gpio_init(bouton);
|
|
gpio_set_dir(bouton, GPIO_IN);
|
|
gpio_pull_up(bouton);
|
|
}
|
|
|
|
int Adc_Init()
|
|
{
|
|
adc_init();
|
|
adc_gpio_init(26);
|
|
adc_gpio_init(27);
|
|
}
|
|
|
|
int Adc_Read(short input)
|
|
{
|
|
adc_select_input(input);
|
|
uint16_t result_adc = adc_read();
|
|
return result_adc;
|
|
}
|
|
|
|
bool bouton_appui(uint8_t button)
|
|
{
|
|
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};
|
|
|
|
bool is_button_pressed = gpio_get(button);
|
|
etat_bouton[button] = is_button_pressed;
|
|
|
|
return is_button_pressed && is_button_pressed != etat_bouton[button];
|
|
}
|
|
|
|
void main()
|
|
{
|
|
stdio_init_all();
|
|
|
|
// Communication
|
|
char message [256];
|
|
communication_init();
|
|
|
|
// ADC
|
|
Adc_Init();
|
|
|
|
// Boutons
|
|
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);
|
|
|
|
ascenseur = ASCENSEUR_BAS;
|
|
pince = PINCE_OUVERTE;
|
|
|
|
gpio_init(PIN_LED_VERTE);
|
|
gpio_set_dir(PIN_LED_VERTE, GPIO_OUT);
|
|
gpio_put(PIN_LED_VERTE, true);
|
|
|
|
while(true)
|
|
{
|
|
// 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;
|
|
|
|
message[0] = result0;
|
|
message[1] = 255 - result1;
|
|
|
|
//clic sur le joystic
|
|
joystic_clicker = gpio_get(PIN_BUTTON_CLIC_JOYSTICK);
|
|
|
|
if (!joystic_clicker)
|
|
{
|
|
if(result1 > 135)
|
|
{
|
|
message[2] = 30;
|
|
}
|
|
else if (result1 < 122)
|
|
{
|
|
message[2] = 220;
|
|
}else
|
|
{
|
|
message[2] = 128;
|
|
}
|
|
message[1] = 128;
|
|
message[0] = 128;
|
|
}
|
|
else
|
|
{
|
|
message[2] = 128;
|
|
}
|
|
*/
|
|
|
|
// Pince
|
|
// Bouton plante
|
|
if(bouton_appui(6)){
|
|
if(pince == PINCE_OUVERTE)
|
|
{
|
|
pince = PINCE_POT;
|
|
}
|
|
else
|
|
{
|
|
pince = PINCE_OUVERTE;
|
|
}
|
|
}
|
|
|
|
// Bouton Pot
|
|
if(bouton_appui(10))
|
|
{
|
|
if(pince == PINCE_OUVERTE)
|
|
{
|
|
pince = PINCE_PLANTE;
|
|
}
|
|
else
|
|
{
|
|
pince = PINCE_OUVERTE;
|
|
}
|
|
}
|
|
|
|
// Ascenseur
|
|
// Commande simple
|
|
if(bouton_appui(2))
|
|
{
|
|
if(ascenseur == ASCENSEUR_HAUT)
|
|
{
|
|
ascenseur = ASCENSEUR_BAS;
|
|
}
|
|
else
|
|
{
|
|
ascenseur = ASCENSEUR_HAUT;
|
|
}
|
|
}
|
|
|
|
// Commande évoluée
|
|
if(bouton_appui(14))
|
|
{
|
|
if(ascenseur == ASCENSEUR_HAUT)
|
|
{
|
|
if(pince == PINCE_PLANTE)
|
|
{
|
|
ascenseur = ASCENSEUR_LACHE_PLANTE;
|
|
}
|
|
if(pince == PINCE_POT)
|
|
{
|
|
ascenseur = ASCENSEUR_LACHE_POT_JARDINIERE;
|
|
}
|
|
// 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))
|
|
{
|
|
pince = PINCE_OUVERTE;
|
|
message [3] = pince;
|
|
communication_envoyer_message(message, 254);
|
|
|
|
sleep_ms(500);
|
|
|
|
ascenseur = ASCENSEUR_HAUT;
|
|
message[4] = ascenseur;
|
|
communication_envoyer_message(message, 254);
|
|
|
|
sleep_ms(500);
|
|
|
|
message[0] = 20;
|
|
communication_envoyer_message(message, 254);
|
|
sleep_ms(1000);
|
|
}
|
|
else
|
|
{
|
|
ascenseur = ASCENSEUR_HAUT;
|
|
}
|
|
}
|
|
|
|
message[3] = pince;
|
|
message[4] = ascenseur;
|
|
|
|
//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]);
|
|
//printf(">result0:%d\n", result0);
|
|
|
|
communication_envoyer_message(message, 254);
|
|
|
|
sleep_ms(25);
|
|
}
|
|
}
|