mirror of
https://github.com/modelec/MarcelMoteurSTM32.git
synced 2026-03-18 21:50:40 +01:00
feat: quadrature encoder
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-1185679149476359369" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="1547658766405034993" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
@@ -16,7 +16,7 @@
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-1185679149476359369" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="1547658766405034993" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
|
||||
@@ -12,35 +12,37 @@ Mcu.IP1=RCC
|
||||
Mcu.IP2=SYS
|
||||
Mcu.IP3=TIM2
|
||||
Mcu.IP4=TIM3
|
||||
Mcu.IP5=USART2
|
||||
Mcu.IPNb=6
|
||||
Mcu.IP5=TIM21
|
||||
Mcu.IP6=USART2
|
||||
Mcu.IPNb=7
|
||||
Mcu.Name=STM32L073R(B-Z)Tx
|
||||
Mcu.Package=LQFP64
|
||||
Mcu.Pin0=PC13
|
||||
Mcu.Pin1=PC14-OSC32_IN
|
||||
Mcu.Pin10=PC6
|
||||
Mcu.Pin11=PC7
|
||||
Mcu.Pin12=PC8
|
||||
Mcu.Pin13=PC9
|
||||
Mcu.Pin14=PA13
|
||||
Mcu.Pin15=PA14
|
||||
Mcu.Pin16=PC10
|
||||
Mcu.Pin17=VP_SYS_VS_Systick
|
||||
Mcu.Pin18=VP_TIM3_VS_ClockSourceINT
|
||||
Mcu.Pin10=PB14
|
||||
Mcu.Pin11=PC6
|
||||
Mcu.Pin12=PC7
|
||||
Mcu.Pin13=PC8
|
||||
Mcu.Pin14=PC9
|
||||
Mcu.Pin15=PA13
|
||||
Mcu.Pin16=PA14
|
||||
Mcu.Pin17=PC10
|
||||
Mcu.Pin18=VP_SYS_VS_Systick
|
||||
Mcu.Pin19=VP_TIM3_VS_ClockSourceINT
|
||||
Mcu.Pin2=PC15-OSC32_OUT
|
||||
Mcu.Pin3=PH0-OSC_IN
|
||||
Mcu.Pin4=PH1-OSC_OUT
|
||||
Mcu.Pin5=PA0
|
||||
Mcu.Pin6=PA1
|
||||
Mcu.Pin7=PA2
|
||||
Mcu.Pin8=PA3
|
||||
Mcu.Pin9=PA5
|
||||
Mcu.PinsNb=19
|
||||
Mcu.Pin4=PA0
|
||||
Mcu.Pin5=PA1
|
||||
Mcu.Pin6=PA2
|
||||
Mcu.Pin7=PA3
|
||||
Mcu.Pin8=PA5
|
||||
Mcu.Pin9=PB13
|
||||
Mcu.PinsNb=20
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32L073RZTx
|
||||
MxCube.Version=6.13.0
|
||||
MxDb.Version=DB.6.0.130
|
||||
MxCube.Version=6.14.0
|
||||
MxDb.Version=DB.6.0.140
|
||||
NVIC.ForceEnableDMAVector=true
|
||||
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||
@@ -80,6 +82,12 @@ PA5.GPIOParameters=GPIO_Label
|
||||
PA5.GPIO_Label=LD2 [Green Led]
|
||||
PA5.Locked=true
|
||||
PA5.Signal=GPIO_Output
|
||||
PB13.GPIOParameters=GPIO_PuPd
|
||||
PB13.GPIO_PuPd=GPIO_PULLUP
|
||||
PB13.Signal=S_TIM21_CH1
|
||||
PB14.GPIOParameters=GPIO_PuPd
|
||||
PB14.GPIO_PuPd=GPIO_PULLUP
|
||||
PB14.Signal=S_TIM21_CH2
|
||||
PC10.Locked=true
|
||||
PC10.Signal=GPIO_Output
|
||||
PC13.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI
|
||||
@@ -104,13 +112,12 @@ PC9.Signal=S_TIM3_CH4
|
||||
PH0-OSC_IN.GPIOParameters=GPIO_Label
|
||||
PH0-OSC_IN.GPIO_Label=MCO
|
||||
PH0-OSC_IN.Locked=true
|
||||
PH0-OSC_IN.Mode=HSE-External-Oscillator
|
||||
PH0-OSC_IN.Mode=HSE-External-Clock-Source
|
||||
PH0-OSC_IN.Signal=RCC_OSC_IN
|
||||
PH1-OSC_OUT.Mode=HSE-External-Oscillator
|
||||
PH1-OSC_OUT.Signal=RCC_OSC_OUT
|
||||
PinOutPanel.RotationAngle=0
|
||||
ProjectManager.AskForMigrate=true
|
||||
ProjectManager.BackupPrevious=false
|
||||
ProjectManager.CompilerLinker=GCC
|
||||
ProjectManager.CompilerOptimize=6
|
||||
ProjectManager.ComputerToolchain=false
|
||||
ProjectManager.CoupleFile=true
|
||||
@@ -176,6 +183,10 @@ RCC.VCOOutputFreq_Value=48000000
|
||||
RCC.WatchDogFreq_Value=37000
|
||||
SH.GPXTI13.0=GPIO_EXTI13
|
||||
SH.GPXTI13.ConfNb=1
|
||||
SH.S_TIM21_CH1.0=TIM21_CH1,Encoder_Interface
|
||||
SH.S_TIM21_CH1.ConfNb=1
|
||||
SH.S_TIM21_CH2.0=TIM21_CH2,Encoder_Interface
|
||||
SH.S_TIM21_CH2.ConfNb=1
|
||||
SH.S_TIM2_CH1.0=TIM2_CH1,Encoder_Interface
|
||||
SH.S_TIM2_CH1.ConfNb=1
|
||||
SH.S_TIM2_CH2.0=TIM2_CH2,Encoder_Interface
|
||||
@@ -194,6 +205,8 @@ TIM2.IC1Polarity=TIM_ICPOLARITY_RISING
|
||||
TIM2.IC2Filter=0
|
||||
TIM2.IC2Polarity=TIM_ICPOLARITY_RISING
|
||||
TIM2.IPParameters=EncoderMode,IC1Polarity,IC2Polarity,IC2Filter,IC1Filter
|
||||
TIM21.EncoderMode=TIM_ENCODERMODE_TI12
|
||||
TIM21.IPParameters=EncoderMode
|
||||
TIM3.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
|
||||
TIM3.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
|
||||
TIM3.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* encoder.h
|
||||
*
|
||||
* Created on: Mar 8, 2025
|
||||
* Author: allan
|
||||
*/
|
||||
|
||||
|
||||
#ifndef INC_ENCODER_H_
|
||||
#define INC_ENCODER_H_
|
||||
|
||||
#include "stm32l0xx_hal.h"
|
||||
|
||||
typedef enum {
|
||||
EncoderInputUndefined,
|
||||
EncoderInputHigh,
|
||||
EncoderInputLow,
|
||||
} EncoderInputState;
|
||||
|
||||
typedef enum {
|
||||
EncoderDirectionUndefined,
|
||||
EncoderDirectionForward,
|
||||
EncoderDirectionBackward,
|
||||
} EncoderDirection;
|
||||
|
||||
class Encoder {
|
||||
private:
|
||||
int32_t totalDistance;
|
||||
int32_t latestHandledDistance;
|
||||
uint16_t tickDistance;
|
||||
volatile uint32_t* inputAgpioIDRaddr;
|
||||
uint32_t inputAgpioIDRMask;
|
||||
volatile uint32_t* inputBgpioIDRaddr;
|
||||
uint32_t inputBgpioIDRMask;
|
||||
EncoderInputState previousAstate;
|
||||
EncoderInputState previousBstate;
|
||||
void (*handler)(uint16_t totalDistance, uint16_t newDistance, bool direction); // direction = (1 : forward, 0 : backward)
|
||||
|
||||
public:
|
||||
Encoder(uint16_t wheelDiameter, uint16_t encoderResolution, volatile uint32_t* inputAgpioIDRaddr, volatile uint32_t* inputBgpioIDRaddr, uint32_t inputAgpioIDRMask, uint32_t inputBgpioIDRMask, void (*handler)(uint16_t, uint16_t, bool));
|
||||
void trigger();
|
||||
uint32_t getTotalDistance();
|
||||
uint32_t getDistanceSinceLastCall();
|
||||
void resetToZero();
|
||||
virtual ~Encoder();
|
||||
};
|
||||
|
||||
#endif /* INC_ENCODER_H_ */
|
||||
@@ -22,7 +22,7 @@
|
||||
#define __STM32L0xx_IT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
|
||||
@@ -36,12 +36,15 @@ extern TIM_HandleTypeDef htim2;
|
||||
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
|
||||
extern TIM_HandleTypeDef htim21;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_TIM2_Init(void);
|
||||
void MX_TIM3_Init(void);
|
||||
void MX_TIM21_Init(void);
|
||||
|
||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
/*
|
||||
* encoder.cpp
|
||||
*
|
||||
* Created on: Mar 8, 2025
|
||||
* Author: allan
|
||||
*/
|
||||
|
||||
#include "encoder.h"
|
||||
|
||||
Encoder::Encoder(uint16_t wheelDiameter, uint16_t encoderResolution, volatile uint32_t* inputAgpioIDRaddr, volatile uint32_t* inputBgpioIDRaddr, uint32_t inputAgpioIDRMask, uint32_t inputBgpioIDRMask, void(*handler)(uint16_t, uint16_t, bool)) :
|
||||
totalDistance(0),
|
||||
latestHandledDistance(0),
|
||||
inputAgpioIDRaddr(inputAgpioIDRaddr),
|
||||
inputAgpioIDRMask(inputAgpioIDRMask),
|
||||
inputBgpioIDRaddr(inputBgpioIDRaddr),
|
||||
inputBgpioIDRMask(inputBgpioIDRMask),
|
||||
previousAstate(EncoderInputUndefined),
|
||||
previousBstate(EncoderInputUndefined),
|
||||
handler(handler)
|
||||
{
|
||||
this->tickDistance = (uint16_t)(wheelDiameter * 3.14 / encoderResolution);
|
||||
if((*(this->inputAgpioIDRaddr) & inputAgpioIDRMask) == inputAgpioIDRMask){
|
||||
this->previousAstate = EncoderInputHigh;
|
||||
}
|
||||
if((*(this->inputAgpioIDRaddr) & inputAgpioIDRMask) == 0){
|
||||
this->previousAstate = EncoderInputLow;
|
||||
}
|
||||
if((*(this->inputBgpioIDRaddr) & inputBgpioIDRMask) == inputBgpioIDRMask){
|
||||
this->previousBstate = EncoderInputHigh;
|
||||
}
|
||||
if((*(this->inputBgpioIDRaddr) & inputBgpioIDRMask) == 0){
|
||||
this->previousBstate = EncoderInputLow;
|
||||
}
|
||||
}
|
||||
|
||||
void Encoder::trigger()
|
||||
{
|
||||
//On sait qu'on avance d'un tick : on cherche d'abord a determiner quel pin a change de valeur puis on on cherchera a determiner la direction de deplacement
|
||||
EncoderDirection direction = EncoderDirectionUndefined;
|
||||
EncoderInputState pinAstate = EncoderInputUndefined;
|
||||
EncoderInputState pinBstate = EncoderInputUndefined;
|
||||
// Est ce que le IDR et le mask sont bien transmis ? Est ce qu'on arrive à lire dans le registre IDR ?
|
||||
if((*(this->inputAgpioIDRaddr) & inputAgpioIDRMask) == inputAgpioIDRMask){
|
||||
pinAstate = EncoderInputHigh;
|
||||
}
|
||||
if((*(this->inputAgpioIDRaddr) & inputAgpioIDRMask) == 0){
|
||||
pinAstate = EncoderInputLow;
|
||||
}
|
||||
if((*(this->inputBgpioIDRaddr) & inputBgpioIDRMask) == inputBgpioIDRMask){
|
||||
pinBstate = EncoderInputHigh;
|
||||
}
|
||||
if((*(this->inputBgpioIDRaddr) & inputBgpioIDRMask) == 0){
|
||||
pinBstate = EncoderInputLow;
|
||||
}
|
||||
if(pinAstate == EncoderInputUndefined || pinBstate == EncoderInputUndefined){
|
||||
return; // error : on a pas reussi a lire la valeur des pins !
|
||||
}
|
||||
// on considere que les previousXstate sont initialises par le constructeur
|
||||
if(pinAstate != previousAstate){ // Interrupt venant du fil A => quelle direction ?
|
||||
if(pinAstate == EncoderInputHigh){
|
||||
if(pinBstate == EncoderInputHigh){
|
||||
direction = EncoderDirectionBackward;
|
||||
} else if(pinBstate == EncoderInputLow){
|
||||
direction = EncoderDirectionForward;
|
||||
}
|
||||
} else if(pinAstate == EncoderInputLow) {
|
||||
if(pinBstate == EncoderInputHigh){
|
||||
direction = EncoderDirectionForward;
|
||||
} else if(pinBstate == EncoderInputLow) {
|
||||
direction = EncoderDirectionBackward;
|
||||
}
|
||||
}
|
||||
previousAstate = pinAstate;
|
||||
} else if(pinBstate != previousBstate){ //Interrupt venant du fil B => quelle direction ?
|
||||
if(pinAstate == EncoderInputHigh){
|
||||
if(pinBstate == EncoderInputHigh){
|
||||
direction = EncoderDirectionForward;
|
||||
} else if(pinBstate == EncoderInputLow){
|
||||
direction = EncoderDirectionBackward;
|
||||
}
|
||||
} else if(pinAstate == EncoderInputLow) {
|
||||
if(pinBstate == EncoderInputHigh){
|
||||
direction = EncoderDirectionBackward;
|
||||
} else if(pinBstate == EncoderInputLow) {
|
||||
direction = EncoderDirectionForward;
|
||||
}
|
||||
}
|
||||
previousBstate = pinBstate;
|
||||
} else {
|
||||
return; // error : A et B n'ont pas change de valeur => pas de mouvement alors !
|
||||
}
|
||||
if(direction == EncoderDirectionForward){
|
||||
// On ajoute la distance parcourue et on appelle le handler
|
||||
this->totalDistance += (int32_t)this->tickDistance;
|
||||
this->handler(this->totalDistance, this->tickDistance, 1);
|
||||
} else if(direction == EncoderDirectionBackward){
|
||||
// On retire la distance parcourue et on appelle le handler
|
||||
this->totalDistance -= (int32_t)this->tickDistance;
|
||||
this->handler(this->totalDistance, this->tickDistance, 0);
|
||||
} else {
|
||||
return; //error : on a pas l'information de direction (ne devrait jamais arriver)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint32_t Encoder::getTotalDistance()
|
||||
{
|
||||
return this->totalDistance;
|
||||
}
|
||||
|
||||
uint32_t Encoder::getDistanceSinceLastCall()
|
||||
{
|
||||
uint32_t result = this->totalDistance - this->latestHandledDistance;
|
||||
this->latestHandledDistance = this->totalDistance;
|
||||
return result;
|
||||
}
|
||||
|
||||
void Encoder::resetToZero()
|
||||
{
|
||||
this->totalDistance = 0;
|
||||
this->latestHandledDistance = 0;
|
||||
}
|
||||
|
||||
Encoder::~Encoder()
|
||||
{
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
@@ -3,11 +3,9 @@
|
||||
|
||||
#include "functions.h"
|
||||
#include "motors.h"
|
||||
#include "encoder.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
extern Encoder encoder1;
|
||||
extern UART_HandleTypeDef huart2;
|
||||
|
||||
uint32_t lastTick = 0; // Variable pour mémoriser l'heure de la dernière action
|
||||
|
||||
@@ -48,6 +48,7 @@ void MX_GPIO_Init(void)
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
|
||||
|
||||
@@ -47,10 +47,6 @@ void ModelecOdometryLoop();
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
uint32_t rawCounter = 0;
|
||||
uint32_t counter = 0;
|
||||
uint32_t last_counter = 0;
|
||||
uint32_t blink_delay = 200;
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
@@ -96,12 +92,14 @@ int main(void)
|
||||
MX_USART2_UART_Init();
|
||||
MX_TIM3_Init();
|
||||
MX_TIM2_Init();
|
||||
MX_TIM21_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
|
||||
HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
|
||||
HAL_TIM_Encoder_Start(&htim21, TIM_CHANNEL_ALL);
|
||||
//HAL_TIM_Encoder_Start_IT(&htim2, TIM_CHANNEL_ALL);
|
||||
ModelecOdometrySetup();
|
||||
/* USER CODE END 2 */
|
||||
@@ -115,7 +113,6 @@ int main(void)
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
rawCounter = __HAL_TIM_GET_COUNTER(&htim2);
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
@@ -6,21 +6,35 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "encoder.h"
|
||||
#include "motors.h"
|
||||
#include "functions.h"
|
||||
#include "main.h"
|
||||
#include "stm32l0xx_hal.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
extern "C" {
|
||||
|
||||
// Paramètres des roues codeuses
|
||||
#define N 600 // Impulsions par tour (PPR) du codeur
|
||||
#define WHEEL_DIAMETER 0.06 // Diamètre de la roue en mètres (ex: 5 cm)
|
||||
#define WHEEL_BASE 0.15 // Entraxe entre les roues en mètres (ex: 15 cm)
|
||||
#define MAX_COUNT 65535 // Max compteur (16 bits)
|
||||
|
||||
// Position du robot
|
||||
volatile float X = 0.0, Y = 0.0, theta = 0.0;
|
||||
|
||||
// Dernières valeurs des encodeurs
|
||||
volatile int32_t last_pos_right = 0, last_pos_left = 0;
|
||||
|
||||
// Variables globales
|
||||
Motor motor(TIM3);
|
||||
Encoder encoder1(60000, 600, &GPIOC->IDR, &GPIOC->IDR, (1<<0), (1<<1), &handleEncoderProgression);
|
||||
Encoder encoder2(60000, 600, &GPIOC->IDR, &GPIOC->IDR, (1<<2), (1<<3), &handleEncoderProgression);
|
||||
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim21;
|
||||
extern UART_HandleTypeDef huart2;
|
||||
|
||||
// Fonction de setup appelee au debut du main.c
|
||||
@@ -36,13 +50,52 @@ extern "C" {
|
||||
//On actualise toute les 10ms et on effectue tous les controles périodiques
|
||||
if(isDelayPassed(10)) {
|
||||
|
||||
if(encoder1.getTotalDistance() > 10) {
|
||||
motor.stop();
|
||||
}
|
||||
//On met à jour le statut des moteurs
|
||||
motor.update();
|
||||
}
|
||||
}
|
||||
|
||||
// Fonction de mise à jour de l'odométrie
|
||||
void ModelecOdometryUpdate() {
|
||||
// Lire les compteurs des encodeurs
|
||||
int32_t pos_right = __HAL_TIM_GET_COUNTER(&htim2);
|
||||
int32_t pos_left = __HAL_TIM_GET_COUNTER(&htim21);
|
||||
|
||||
// Debug
|
||||
char msg[128];
|
||||
sprintf(msg, "Encodeur droit %d.\r\n", pos_right);
|
||||
sprintf(msg, "Encodeur gauche %d.\r\n", pos_left);
|
||||
HAL_UART_Transmit(&huart2, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
|
||||
|
||||
// Calcul des variations d’impulsions
|
||||
int32_t deltaP_right = pos_right - last_pos_right;
|
||||
int32_t deltaP_left = pos_left - last_pos_left;
|
||||
|
||||
// Gestion du débordement du compteur
|
||||
if (deltaP_right > MAX_COUNT / 2) deltaP_right -= MAX_COUNT;
|
||||
if (deltaP_right < -MAX_COUNT / 2) deltaP_right += MAX_COUNT;
|
||||
if (deltaP_left > MAX_COUNT / 2) deltaP_left -= MAX_COUNT;
|
||||
if (deltaP_left < -MAX_COUNT / 2) deltaP_left += MAX_COUNT;
|
||||
|
||||
// Convertir en distances parcourues
|
||||
float deltaS_right = (deltaP_right / (float)(N * 4)) * M_PI * WHEEL_DIAMETER;
|
||||
float deltaS_left = (deltaP_left / (float)(N * 4)) * M_PI * WHEEL_DIAMETER;
|
||||
|
||||
// Calcul de la distance totale et du changement d’angle
|
||||
float deltaS = (deltaS_right + deltaS_left) / 2.0;
|
||||
float deltaTheta = (deltaS_right - deltaS_left) / WHEEL_BASE;
|
||||
|
||||
// Mise à jour de la position
|
||||
X += deltaS * cos(theta + deltaTheta / 2.0);
|
||||
Y += deltaS * sin(theta + deltaTheta / 2.0);
|
||||
theta += deltaTheta;
|
||||
|
||||
// Conserver les valeurs actuelles pour la prochaine itération
|
||||
last_pos_right = pos_right;
|
||||
last_pos_left = pos_left;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
TIM_HandleTypeDef htim2;
|
||||
TIM_HandleTypeDef htim3;
|
||||
TIM_HandleTypeDef htim21;
|
||||
|
||||
/* TIM2 init function */
|
||||
void MX_TIM2_Init(void)
|
||||
@@ -140,6 +141,50 @@ void MX_TIM3_Init(void)
|
||||
/* USER CODE END TIM3_Init 2 */
|
||||
HAL_TIM_MspPostInit(&htim3);
|
||||
|
||||
}
|
||||
/* TIM21 init function */
|
||||
void MX_TIM21_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM21_Init 0 */
|
||||
|
||||
/* USER CODE END TIM21_Init 0 */
|
||||
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM21_Init 1 */
|
||||
|
||||
/* USER CODE END TIM21_Init 1 */
|
||||
htim21.Instance = TIM21;
|
||||
htim21.Init.Prescaler = 0;
|
||||
htim21.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim21.Init.Period = 65535;
|
||||
htim21.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim21.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
|
||||
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC1Filter = 0;
|
||||
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC2Filter = 0;
|
||||
if (HAL_TIM_Encoder_Init(&htim21, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim21, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM21_Init 2 */
|
||||
|
||||
/* USER CODE END TIM21_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle)
|
||||
@@ -173,6 +218,30 @@ void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle)
|
||||
|
||||
/* USER CODE END TIM2_MspInit 1 */
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM21)
|
||||
{
|
||||
/* USER CODE BEGIN TIM21_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM21_MspInit 0 */
|
||||
/* TIM21 clock enable */
|
||||
__HAL_RCC_TIM21_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**TIM21 GPIO Configuration
|
||||
PB13 ------> TIM21_CH1
|
||||
PB14 ------> TIM21_CH2
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF6_TIM21;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN TIM21_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM21_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
@@ -244,6 +313,24 @@ void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* tim_encoderHandle)
|
||||
|
||||
/* USER CODE END TIM2_MspDeInit 1 */
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM21)
|
||||
{
|
||||
/* USER CODE BEGIN TIM21_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM21_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM21_CLK_DISABLE();
|
||||
|
||||
/**TIM21 GPIO Configuration
|
||||
PB13 ------> TIM21_CH1
|
||||
PB14 ------> TIM21_CH2
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_14);
|
||||
|
||||
/* USER CODE BEGIN TIM21_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM21_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
|
||||
Reference in New Issue
Block a user