From 8392148a16d79ce866a02355b33d69a8b765ddeb Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 24 Jan 2025 19:27:19 +0100 Subject: [PATCH] =?UTF-8?q?=C3=87a=20marche=202025?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- CMakeLists.txt | 1 + Servomoteur.c | 88 +++++++++++++++++++++++++++++++++++++++++++ Servomoteur.h | 9 +++++ main.c | 17 +++++---- moteur.c | 72 +++-------------------------------- moteur.h | 7 ---- 7 files changed, 116 insertions(+), 81 deletions(-) create mode 100644 Servomoteur.c create mode 100644 Servomoteur.h diff --git a/.vscode/settings.json b/.vscode/settings.json index a06b057..7bac038 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "stdlib.h": "c", "stdio.h": "c", "i2c_slave.h": "c", - "i2c.h": "c" + "i2c.h": "c", + "moteur.h": "c" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ea233cf..cb23f3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(Mon_Projet i2c_slave.c communication.c moteur.c + Servomoteur.c ) target_include_directories(Mon_Projet PRIVATE Mon_Projet_ULD_API/inc/) diff --git a/Servomoteur.c b/Servomoteur.c new file mode 100644 index 0000000..682685d --- /dev/null +++ b/Servomoteur.c @@ -0,0 +1,88 @@ +#include "moteur.h" + +// Define pins servo +#define SERVO1_PIN 20 +#define SERVO2_PIN 21 + +// S0 : 26 - PWM 5 - Chan A +// S1 : 22 - PWM 3 - Chan A +// S2 : 21 - PWM 2 - Chan B +// S3 : 20 - PWM 2 - Chan A +// S4 : 27 - PWM 5 - Chan B + +void Servomoteur_init(){ + // Set wrap of pwm slice + pwm_set_wrap(2, 25000); + pwm_set_wrap(3, 25000); + pwm_set_wrap(5, 25000); + + // Set clock div for this pwm slice + pwm_set_clkdiv(2, 100); + pwm_set_clkdiv(3, 100); + pwm_set_clkdiv(5, 100); + + // Enable pwm slice + pwm_set_enabled(2, true); + pwm_set_enabled(3, true); + pwm_set_enabled(5, true); + + // Set channel level + pwm_set_chan_level(2, PWM_CHAN_A, 2800); // S3 - Ouvert : 1700 - Fermé : 2800 + pwm_set_chan_level(2, PWM_CHAN_B, 2650); // S2 - Ouvert : 1500 - Fermé : 2650 + pwm_set_chan_level(3, PWM_CHAN_A, 2300); // S1 - 2300 Pousse, 1500 Neutre + pwm_set_chan_level(5, PWM_CHAN_A, 900); // S0 - Dépose 900 - 1750 + pwm_set_chan_level(5, PWM_CHAN_B, 1500); // S4 - Ouvert 2700 - Fermé : 1500 + + + // Init pin for the servos + gpio_init(SERVO1_PIN); + gpio_init(SERVO2_PIN); + gpio_init(22); + gpio_init(26); + gpio_init(27); + gpio_set_function(SERVO1_PIN, GPIO_FUNC_PWM); + gpio_set_function(SERVO2_PIN, GPIO_FUNC_PWM); + gpio_set_function(22, GPIO_FUNC_PWM); + gpio_set_function(26, GPIO_FUNC_PWM); + gpio_set_function(27, GPIO_FUNC_PWM); + + // Edit 17/01/2024 + // Réglage servomoteurs en 25000 * 100 + // Pince + // Pot : 1700 + // Plante : 1970 + // Ouvert : 1000 + // Ascenseur + // Haut : 2550 + // Bas : 1550 + // Lâche plante : 2050 + // Lâche pot jardinière : 1950 +} + +void Servo_pince_tient(){ + pwm_set_chan_level(2, PWM_CHAN_A, 2800); + pwm_set_chan_level(5, PWM_CHAN_B, 1500); + pwm_set_chan_level(2, PWM_CHAN_B, 2650); +} + +void Servo_pince_lache(){ + pwm_set_chan_level(2, PWM_CHAN_A, 1700); + pwm_set_chan_level(5, PWM_CHAN_B, 2700); + pwm_set_chan_level(2, PWM_CHAN_B, 1500); +} + +void Servo_came_pousse(){ + pwm_set_chan_level(3, PWM_CHAN_A, 2300); // S1 - 2300 Pousse, 1500 Neutre +} + +void Servo_came_neutre(){ + pwm_set_chan_level(3, PWM_CHAN_A, 1500); // S1 - 2300 Pousse, 1500 Neutre +} + +void Servo_deplie_banderole(){ + pwm_set_chan_level(5, PWM_CHAN_A, 900); // S0 - Dépose 900 - neutre 1750 +} + +void Servo_plie_banderole(){ + pwm_set_chan_level(5, PWM_CHAN_A, 1750); // S0 - Dépose 900 - neutre 1750 +} diff --git a/Servomoteur.h b/Servomoteur.h new file mode 100644 index 0000000..e8f8b22 --- /dev/null +++ b/Servomoteur.h @@ -0,0 +1,9 @@ +void Servomoteur_init(); +void Servo_pince_tient(); +void Servo_pince_lache(); + +void Servo_came_pousse(); +void Servo_came_neutre(); + +void Servo_deplie_banderole(); +void Servo_plie_banderole(); \ No newline at end of file diff --git a/main.c b/main.c index 3aef47a..e7e621a 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include "pico/stdlib.h" #include "hardware/adc.h" #include "communication.h" +#include "Servomoteur.h" #include #include "moteur.h" @@ -22,6 +23,7 @@ void main(void) // Init "all" stdio_init_all(); Init_motion_motor(); + Servomoteur_init(); communication_init(); gpio_init(PIN_LED_VERTE); @@ -49,16 +51,17 @@ void main(void) vitesse_angle = reception[2] - 128; switch(reception[3]){ - case 0: Servo_pince_ouverte(); break; - case 1: Servo_pince_plante(); break; - case 2: Servo_pince_pot(); break; + case 0: Servo_pince_tient(); break; + case 1: Servo_pince_lache(); break; } switch(reception[4]){ - case 0: Servo_ascenseur_bas(); break; - case 1: Servo_ascenseur_haut(); break; - case 2: Servo_ascenseur_lache_pot_jardiniere(); break; - case 3: Servo_ascenseur_lache_plante(); break; + case 0: Servo_plie_banderole(); break; + case 1: Servo_deplie_banderole(); break; + } + switch(reception[5]){ + case 0: Servo_came_pousse(); break; + case 1: Servo_came_neutre(); break; } } diff --git a/moteur.c b/moteur.c index e3f0cc8..6d26641 100644 --- a/moteur.c +++ b/moteur.c @@ -14,10 +14,6 @@ #define MOTEUR4_PIN_SENS2 9 #define MOTEUR4_PIN_ACTIVATION 3 -// Define pins servo -#define SERVO1_PIN 20 -#define SERVO2_PIN 21 - // Init all motion motors pins void Init_motion_motor(void) { @@ -75,65 +71,9 @@ void Init_motion_motor(void) pwm_set_chan_level(1, PWM_CHAN_A, 0); pwm_set_chan_level(1, PWM_CHAN_B, 0); - // Servo Init + - // Init pin for the servos - gpio_init(SERVO1_PIN); - gpio_init(SERVO2_PIN); - gpio_set_function(SERVO1_PIN, GPIO_FUNC_PWM); - gpio_set_function(SERVO2_PIN, GPIO_FUNC_PWM); - - // Set wrap of pwm slice - pwm_set_wrap(2, 25000); - - // Set clock div for this pwm slice - pwm_set_clkdiv(2, 100); - - // Enable pwm slice - pwm_set_enabled(2, true); - - // Set channel level - pwm_set_chan_level(2, PWM_CHAN_A, 1950); // Ascenseur - pwm_set_chan_level(2, PWM_CHAN_B, 1000); // Pince - // Edit 17/01/2024 - // Réglage servomoteurs en 25000 * 100 - // Pince - // Pot : 1700 - // Plante : 1970 - // Ouvert : 1000 - // Ascenseur - // Haut : 2550 - // Bas : 1550 - // Lâche plante : 2050 - // Lâche pot jardinière : 1950 - - - - - -} - -void Servo_ascenseur_haut(){ - pwm_set_chan_level(2, PWM_CHAN_A, 2550); -} -void Servo_ascenseur_bas(){ - pwm_set_chan_level(2, PWM_CHAN_A, 1550); -} -void Servo_ascenseur_lache_plante(){ - pwm_set_chan_level(2, PWM_CHAN_A, 2050); -} -void Servo_ascenseur_lache_pot_jardiniere(){ - pwm_set_chan_level(2, PWM_CHAN_A, 1950); -} - -void Servo_pince_ouverte(){ - pwm_set_chan_level(2, PWM_CHAN_B, 1000); -} -void Servo_pince_pot(){ - pwm_set_chan_level(2, PWM_CHAN_B, 1700); -} -void Servo_pince_plante(){ - pwm_set_chan_level(2, PWM_CHAN_B, 1950); + } @@ -231,16 +171,16 @@ void Motor3_speed(int speed) void Motor4_forward(int speed) { pwm_set_chan_level(1, PWM_CHAN_B, speed); - gpio_put(MOTEUR4_PIN_SENS1, 0); - gpio_put(MOTEUR4_PIN_SENS2, 1); + gpio_put(MOTEUR4_PIN_SENS1, 1); + gpio_put(MOTEUR4_PIN_SENS2, 0); } // Set motor 4 speed backward void Motor4_backward(int speed) { pwm_set_chan_level(1, PWM_CHAN_B, speed); - gpio_put(MOTEUR4_PIN_SENS1, 1); - gpio_put(MOTEUR4_PIN_SENS2, 0); + gpio_put(MOTEUR4_PIN_SENS1, 0); + gpio_put(MOTEUR4_PIN_SENS2, 1); } // Set motor 4 speed and direction (negative value : backward / positive value : forward) diff --git a/moteur.h b/moteur.h index 8118884..aa13a4b 100644 --- a/moteur.h +++ b/moteur.h @@ -15,10 +15,3 @@ void Motor2_forward(int speed); void Motor3_backward(int speed); void Motor4_speed(int speed); -void Servo_ascenseur_haut(); -void Servo_ascenseur_bas(); -void Servo_ascenseur_lache_plante(); -void Servo_ascenseur_lache_pot_jardiniere(); -void Servo_pince_ouverte(); -void Servo_pince_pot(); -void Servo_pince_plante(); \ No newline at end of file