add encoders, to be tested on real hw, do not break immediately
This commit is contained in:
@@ -6,4 +6,8 @@ DMA_HandleTypeDef hdma_usart3_rx;
|
||||
DMA_HandleTypeDef hdma_usart3_tx;
|
||||
I2C_HandleTypeDef hi2c2;
|
||||
TIM_HandleTypeDef htim14;
|
||||
TIM_HandleTypeDef htim1;
|
||||
TIM_HandleTypeDef htim2;
|
||||
TIM_HandleTypeDef htim3;
|
||||
TIM_HandleTypeDef htim4;
|
||||
UART_HandleTypeDef huart3;
|
||||
|
||||
@@ -12,6 +12,7 @@ set(KPIROVER_COMPONENTS
|
||||
Database
|
||||
Logger
|
||||
Motors
|
||||
Encoders
|
||||
#Storage
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
set(KPI_Rover_Encoders_Inc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
set(KPI_Rover_Encoders_Src
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/drvEncoder.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ulEncoder.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/hwTIM.c
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Encoders STATIC)
|
||||
|
||||
target_include_directories(KPI_Rover_Encoders PRIVATE
|
||||
${KPI_Rover_Encoders_Inc}
|
||||
)
|
||||
|
||||
target_sources(KPI_Rover_Encoders PRIVATE
|
||||
${KPI_Rover_Encoders_Src}
|
||||
)
|
||||
|
||||
target_link_libraries(KPI_Rover_Encoders PRIVATE
|
||||
FREERTOS_H
|
||||
HAL_H
|
||||
KPI_Rover_Database_H
|
||||
KPI_Rover_Logger_H
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Encoders_H INTERFACE)
|
||||
target_include_directories(KPI_Rover_Encoders_H INTERFACE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "drvEncoder.h"
|
||||
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
|
||||
void drvEncoder_Init(void) {
|
||||
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);
|
||||
HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
|
||||
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
|
||||
HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_ALL);
|
||||
}
|
||||
|
||||
uint32_t drvEncoder_Read(uint8_t channel) {
|
||||
switch (channel) {
|
||||
case 0: return TIM1->CNT;
|
||||
case 1: return TIM2->CNT;
|
||||
case 2: return TIM3->CNT;
|
||||
case 3: return TIM4->CNT;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef ENCODERS_DRVENCODER_H_
|
||||
#define ENCODERS_DRVENCODER_H_
|
||||
|
||||
#include "main.h"
|
||||
|
||||
void drvEncoder_Init(void);
|
||||
uint32_t drvEncoder_Read(uint8_t channel);
|
||||
|
||||
#endif /* ENCODERS_DRVENCODER_H_ */
|
||||
@@ -0,0 +1,215 @@
|
||||
#include "main.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
void MX_TIM1_Init(void)
|
||||
{
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 65535;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
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(&htim1, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void MX_TIM2_Init(void)
|
||||
{
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
htim2.Instance = TIM2;
|
||||
htim2.Init.Prescaler = 0;
|
||||
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim2.Init.Period = 0xffff;
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
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(&htim2, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void MX_TIM3_Init(void)
|
||||
{
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
htim3.Instance = TIM3;
|
||||
htim3.Init.Prescaler = 0;
|
||||
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim3.Init.Period = 65535;
|
||||
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
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(&htim3, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void MX_TIM4_Init(void)
|
||||
{
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
htim4.Instance = TIM4;
|
||||
htim4.Init.Prescaler = 0;
|
||||
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim4.Init.Period = 65535;
|
||||
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
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(&htim4, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(tim_encoderHandle->Instance==TIM1)
|
||||
{
|
||||
__HAL_RCC_TIM1_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_11;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
||||
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM2)
|
||||
{
|
||||
__HAL_RCC_TIM2_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM3)
|
||||
{
|
||||
__HAL_RCC_TIM3_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM4)
|
||||
{
|
||||
__HAL_RCC_TIM4_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
|
||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* tim_encoderHandle)
|
||||
{
|
||||
|
||||
if(tim_encoderHandle->Instance==TIM1)
|
||||
{
|
||||
__HAL_RCC_TIM1_CLK_DISABLE();
|
||||
HAL_GPIO_DeInit(GPIOE, GPIO_PIN_9|GPIO_PIN_11);
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM2)
|
||||
{
|
||||
__HAL_RCC_TIM2_CLK_DISABLE();
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1);
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM3)
|
||||
{
|
||||
__HAL_RCC_TIM3_CLK_DISABLE();
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6|GPIO_PIN_7);
|
||||
}
|
||||
else if(tim_encoderHandle->Instance==TIM4)
|
||||
{
|
||||
__HAL_RCC_TIM4_CLK_DISABLE();
|
||||
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_12|GPIO_PIN_13);
|
||||
}
|
||||
}
|
||||
|
||||
void HW_TIM_init(void)
|
||||
{
|
||||
MX_TIM1_Init();
|
||||
MX_TIM2_Init();
|
||||
MX_TIM3_Init();
|
||||
MX_TIM4_Init();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#include "cmsis_os2.h"
|
||||
#include "drvEncoder.h"
|
||||
#include "ulDatabase.h"
|
||||
#include "ulEncoder.h"
|
||||
#include "ulog.h"
|
||||
|
||||
|
||||
#define MOTORS_COUNT 4
|
||||
|
||||
|
||||
static uint16_t lastTicks_RPM[MOTORS_COUNT];
|
||||
static uint16_t lastTicks_ROS[MOTORS_COUNT];
|
||||
|
||||
static uint16_t encoder_period_ms = 5;
|
||||
static float encoder_ticks_per_rev = 820.0f;
|
||||
|
||||
static osTimerId_t encoderTimerHandle;
|
||||
|
||||
|
||||
static inline int32_t ulEncoder_CalculateDiff(uint16_t current, uint16_t last) {
|
||||
return (int16_t)(current - last);
|
||||
}
|
||||
|
||||
static void ulEncoder_TimerCallback(void *argument) {
|
||||
|
||||
for (int i = 0; i < MOTORS_COUNT; i++) {
|
||||
uint32_t currentTicks = drvEncoder_Read(i);
|
||||
uint32_t lastTicks = lastTicks_RPM[i];
|
||||
|
||||
int32_t diff = ulEncoder_CalculateDiff(currentTicks, lastTicks);
|
||||
|
||||
float ticks_per_sec = (float) diff * (1000.0f / encoder_period_ms);
|
||||
float rpm = ticks_per_sec * 60.0f / encoder_ticks_per_rev;
|
||||
|
||||
int32_t rpm_to_db = (int32_t)rpm;
|
||||
ulDatabase_setInt32(MOTOR_FL_RPM + i, rpm_to_db);
|
||||
|
||||
lastTicks_RPM[i] = currentTicks;
|
||||
}
|
||||
}
|
||||
|
||||
void ulEncoder_Init(void) {
|
||||
|
||||
drvEncoder_Init();
|
||||
|
||||
if (!ulDatabase_getUint16(ENCODER_CONTROL_PERIOD_MS, &encoder_period_ms) || encoder_period_ms == 0) {
|
||||
encoder_period_ms = 5;
|
||||
}
|
||||
|
||||
if (!ulDatabase_getFloat(ENCODER_TICKS_PER_REVOLUTION, &encoder_ticks_per_rev) || encoder_ticks_per_rev < 1.0f) {
|
||||
encoder_ticks_per_rev = 820.0f;
|
||||
}
|
||||
|
||||
for (int i = 0; i < MOTORS_COUNT; i++) {
|
||||
uint32_t initialTicks = drvEncoder_Read(i);
|
||||
lastTicks_RPM[i] = initialTicks;
|
||||
lastTicks_ROS[i] = initialTicks;
|
||||
}
|
||||
|
||||
const osTimerAttr_t encoderTimer_attributes = {
|
||||
.name = "EncoderTimer",
|
||||
};
|
||||
|
||||
encoderTimerHandle = osTimerNew(ulEncoder_TimerCallback, osTimerPeriodic, NULL, &encoderTimer_attributes);
|
||||
|
||||
if (!encoderTimerHandle) {
|
||||
ULOG_ERROR("Failed to create Encoder timer");
|
||||
osThreadExit();
|
||||
}
|
||||
|
||||
if (osTimerStart(encoderTimerHandle, encoder_period_ms) != osOK) {
|
||||
ULOG_ERROR("Failed to start Encoder timer");
|
||||
osThreadExit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ulEncoder_GetDiffForROS(int32_t *diffOutput) {
|
||||
|
||||
for (int i = 0; i < MOTORS_COUNT; i++) {
|
||||
|
||||
uint32_t currentTicks = drvEncoder_Read(i);
|
||||
uint32_t lastTicks = lastTicks_ROS[i];
|
||||
|
||||
int32_t diff = ulEncoder_CalculateDiff(currentTicks, lastTicks);
|
||||
|
||||
diffOutput[i] = diff;
|
||||
lastTicks_ROS[i] = currentTicks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef ENCODERS_ULENCODER_H_
|
||||
#define ENCODERS_ULENCODER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void ulEncoder_Init(void);
|
||||
void ulEncoder_GetDiffForEthernet(int32_t *diffOutput);
|
||||
|
||||
#endif /* ENCODERS_ULENCODER_H_ */
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "ulAdc.h"
|
||||
#include "ProtocolHandler.h"
|
||||
#include "ulDatabase.h"
|
||||
//#include "Encoders/ulEncoder.h"
|
||||
#include "ulEncoder.h"
|
||||
//#include "IMU/ulImu.h"
|
||||
#include "ulMotorsController.h"
|
||||
//#include "Storage/ulStorage.h"
|
||||
@@ -75,6 +75,7 @@ void KPIRover_Init(void) {
|
||||
|
||||
//ulAdc_init();
|
||||
ulMotorsController_init();
|
||||
ulEncoder_Init();
|
||||
#if 0
|
||||
ulStorage_init();
|
||||
ulEncoder_Init();
|
||||
|
||||
Reference in New Issue
Block a user