Robot_Riombotique/main.c

136 lines
3.3 KiB
C
Raw Normal View History

2023-10-22 16:48:14 +00:00
/*****
* Copyright (c) 2023 - Poivron Robotique
*
* SPDX-License-Identifier: BSD-3-Clause
*/
2023-12-22 17:21:19 +00:00
// Include libraries
2023-10-22 16:48:14 +00:00
#include "pico/stdlib.h"
2023-12-16 11:12:55 +00:00
#include "hardware/pwm.h"
#include "hardware/adc.h"
2023-10-22 16:48:14 +00:00
#include <stdio.h>
2023-12-22 17:21:19 +00:00
// Define pins
#define MOTEUR1_PIN_SENS1 4
#define MOTEUR1_PIN_SENS2 5
2023-12-16 11:12:55 +00:00
#define MOTEUR1_PIN_ACTIVATION 0
2023-12-22 17:21:19 +00:00
#define MOTEUR2_PIN_SENS1 6
#define MOTEUR2_PIN_SENS2 7
2023-12-16 11:12:55 +00:00
#define MOTEUR2_PIN_ACTIVATION 1
2023-12-22 17:21:19 +00:00
#define MOTEUR3_PIN_SENS1 8
#define MOTEUR3_PIN_SENS2 9
#define MOTEUR3_PIN_ACTIVATION 2
#define MOTEUR4_PIN_SENS1 10
#define MOTEUR4_PIN_SENS2 11
#define MOTEUR4_PIN_ACTIVATION 3
// Init all motion motors pins
void Init_motion_motor(void);
// Set motor 1 speed forward
void Motor1_forward(int speed);
// Set motor 1 speed backward
void Motor1_backward(int speed);
// Set motor 1 speed and direction (negative value : backward / positive value : forward)
void Motor1_speed(int speed);
2023-12-16 11:12:55 +00:00
2023-10-22 16:48:14 +00:00
void main(void)
{
stdio_init_all();
2023-12-22 17:21:19 +00:00
Init_motion_motor();
while(1)
{
Motor1_speed(-200);
sleep_ms(3000);
Motor1_speed(500);
sleep_ms(3000);
}
}
// Init all motion motors pins
void Init_motion_motor(void)
{
// Init 1/0 pin for control motion motors
2023-12-16 11:12:55 +00:00
gpio_init(MOTEUR1_PIN_SENS1);
gpio_init(MOTEUR1_PIN_SENS2);
gpio_init(MOTEUR2_PIN_SENS1);
gpio_init(MOTEUR2_PIN_SENS2);
2023-12-22 17:21:19 +00:00
gpio_init(MOTEUR3_PIN_SENS1);
gpio_init(MOTEUR3_PIN_SENS2);
gpio_init(MOTEUR4_PIN_SENS1);
gpio_init(MOTEUR4_PIN_SENS2);
gpio_set_dir(MOTEUR1_PIN_SENS1, GPIO_OUT);
gpio_set_dir(MOTEUR1_PIN_SENS2, GPIO_OUT);
gpio_set_dir(MOTEUR2_PIN_SENS1, GPIO_OUT);
2023-12-16 11:12:55 +00:00
gpio_set_dir(MOTEUR2_PIN_SENS2, GPIO_OUT);
2023-12-22 17:21:19 +00:00
gpio_set_dir(MOTEUR3_PIN_SENS1, GPIO_OUT);
gpio_set_dir(MOTEUR3_PIN_SENS2, GPIO_OUT);
gpio_set_dir(MOTEUR4_PIN_SENS1, GPIO_OUT);
gpio_set_dir(MOTEUR4_PIN_SENS2, GPIO_OUT);
// Set direction to 0 (disactivate)
gpio_put(MOTEUR1_PIN_SENS1, 0);
gpio_put(MOTEUR1_PIN_SENS2, 0);
gpio_put(MOTEUR2_PIN_SENS1, 0);
gpio_put(MOTEUR2_PIN_SENS2, 0);
gpio_put(MOTEUR3_PIN_SENS1, 0);
gpio_put(MOTEUR3_PIN_SENS2, 0);
gpio_put(MOTEUR4_PIN_SENS1, 0);
gpio_put(MOTEUR4_PIN_SENS2, 0);
// Init pwm pins for motion motors
2023-12-16 11:12:55 +00:00
gpio_init(MOTEUR1_PIN_ACTIVATION);
gpio_init(MOTEUR2_PIN_ACTIVATION);
2023-12-22 17:21:19 +00:00
gpio_init(MOTEUR3_PIN_ACTIVATION);
gpio_init(MOTEUR4_PIN_ACTIVATION);
2023-12-16 11:12:55 +00:00
gpio_set_function(MOTEUR1_PIN_ACTIVATION, GPIO_FUNC_PWM);
gpio_set_function(MOTEUR2_PIN_ACTIVATION, GPIO_FUNC_PWM);
2023-12-22 17:21:19 +00:00
gpio_set_function(MOTEUR3_PIN_ACTIVATION, GPIO_FUNC_PWM);
gpio_set_function(MOTEUR4_PIN_ACTIVATION, GPIO_FUNC_PWM);
2023-12-16 11:12:55 +00:00
2023-12-22 17:21:19 +00:00
// Set wrap of pwm slices
2023-12-16 11:12:55 +00:00
pwm_set_wrap(0, 1000);
2023-12-22 17:21:19 +00:00
pwm_set_wrap(1, 1000);
2023-12-16 11:12:55 +00:00
2023-12-22 17:21:19 +00:00
// Active all pwm slices
2023-12-16 11:12:55 +00:00
pwm_set_enabled(0, true);
2023-12-22 17:21:19 +00:00
pwm_set_enabled(1, true);
2023-12-16 11:12:55 +00:00
2023-12-22 17:21:19 +00:00
// Set speed to 0
pwm_set_chan_level(0, PWM_CHAN_A, 0);
pwm_set_chan_level(0, PWM_CHAN_B, 0);
pwm_set_chan_level(1, PWM_CHAN_A, 0);
pwm_set_chan_level(1, PWM_CHAN_B, 0);
2023-12-16 11:12:55 +00:00
2023-12-22 17:21:19 +00:00
}
2023-12-16 11:12:55 +00:00
2023-12-22 17:21:19 +00:00
// Set motor 1 speed forward
void Motor1_forward(int speed)
{
pwm_set_chan_level(0, PWM_CHAN_A, speed);
gpio_put(MOTEUR1_PIN_SENS1, 0);
gpio_put(MOTEUR1_PIN_SENS2, 1);
}
// Set motor 1 speed backward
void Motor1_backward(int speed)
{
pwm_set_chan_level(0, PWM_CHAN_A, speed);
gpio_put(MOTEUR1_PIN_SENS1, 1);
gpio_put(MOTEUR1_PIN_SENS2, 0);
2023-10-22 16:48:14 +00:00
}
2023-12-16 11:12:55 +00:00
2023-12-22 17:21:19 +00:00
// Set motor 1 speed and direction (negative value : backward / positive value : forward)
void Motor1_speed(int speed)
2023-12-16 11:12:55 +00:00
{
2023-12-22 17:21:19 +00:00
if(speed < 0)
2023-12-16 11:12:55 +00:00
{
2023-12-22 17:21:19 +00:00
speed = -speed;
Motor1_backward(speed);
2023-12-16 11:12:55 +00:00
}
2023-12-22 17:21:19 +00:00
else
2023-12-16 11:12:55 +00:00
{
2023-12-22 17:21:19 +00:00
Motor1_forward(speed);
2023-12-16 11:12:55 +00:00
}
}