Telecommande_2024/main.c

174 lines
2.6 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
int result0, result1;
int joystic_clicker;
int pince;
int ascenceur;
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
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);
while (1)
{
// Voie X
result1 = AdcRead1();
message[0] = result1/16;
// Voie Y
result0 = AdcRead0();
message[1] = result0/16;
//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)
{
message[2] = result1/16;
message[1] = 128;
message[0] = 128;
}
else
{
message[2] = 128;
}
2024-01-12 16:26:55 +00:00
//pince
gpio_init(2);
gpio_set_dir(2, GPIO_IN);
gpio_pull_up(2);
pince = gpio_get(2);
message [3] = pince;
2024-01-12 16:26:55 +00:00
gpio_init(6);
gpio_set_dir(6, GPIO_IN);
gpio_pull_up(6);
ascenceur = gpio_get(6);
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-12 17:53:51 +00:00
sleep_ms(100);
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
}
}