Motor encoder(Encoder)

Macro definition

The automatic loading value of the timer should not be greater than 65535 because the timer of the F103 is 16 -bit.
#define ENCODER_TIM_PERIOD (u16)(65535)

Calculate the parameters required for speed
#define PI 3.14159265 //Pi circularity
#define Control_Frequency 200.0 //Coder read frequency
#define Diameter_67 67.0 //Wheel diameter 67mm
#define EncoderMultiples 4.0 //Coder multiple frequency frequency
#define Encoder_precision 11.0 //Coder accuracy 11 line
#define Reduction_Ratio 30.0 //Death ratio 30
#define Perimeter 210.4867 //Week length, unit mm

Control_Frequency=200 It means that the value of Read_encoder () is 5ms



ID of left and right wheels
typedef enum {
MOTOR_ID_ML = 0,
MOTOR_ID_MR,
MAX_MOTOR
} Motor_ID;

method

If you want to use the motor MOTOR, make sure that the function of the initialization motor must be called before the initialized encoder timer !!

Encoder_Init_TIM3(void)

Initialize TIM3 timer

Setting project Set value
Prescaler 0
Automatic reinstatement ENCODER_TIM_PERIOD(The specific value needs to be viewed code)
Clock frequency Indomitable (TIM_CKD_DIV1)
Count mode Counting up (TIM_CounterMode_Up)
Enter the capture filter 10
GPIO pin GPIO_Pin_6 和 GPIO_Pin_7
GPIO mode Floating input (GPIO_Mode_IN_FLOATING)
Timer 3 clock enable Clock of the timer 3
PA port clock enable Make the PA port clock
Encoder mode Coder mode 3 (TIM_EncoderMode_TI12)
Extract the catch Rising edge trigger (TIM_ICPolarity_Rising)
TIM update logo clearance Clear Tim's update logo
TIM update interrupt enable enable Make TIM update interruption

Encoder_Init_TIM4(void)

Initialize TIM4 timer

Setting project Set value
Timer structure TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure
Enter the capture structure TIM_ICInitTypeDef TIM_ICInitStructure
GPIO initialization structure GPIO_InitTypeDef GPIO_InitStructure
Timer 4 clock enable Clock with a clock with timer 4 (RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE))
PB port clock enable Make the PB port clock (RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE))
GPIO pin configuration GPIO_Pin_6 | GPIO_Pin_7
GPIO mode Floating input (GPIO_Mode_IN_FLOATING)
GPIO initialization Initialize GPIOB according to the setting parameter (GPIO_Init(GPIOB, &GPIO_InitStructure))
Initialization of timer basic configuration TIM_TimeBaseStructInit(&TIM_TimeBaseStructure)
Prescaler 0x0
The counter automatic reinstallation value ENCODER_TIM_PERIOD
Clock frequency Indomitable (TIM_CKD_DIV1)
Count mode Counting up (TIM_CounterMode_Up)
Timer initialization TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure)
Code interface configuration Use the encoder mode 3 (TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising))
Input capture structure initialization TIM_ICStructInit(&TIM_ICInitStructure)
Enter the capture filter 10
Input capture initialization TIM_ICInit(TIM4, &TIM_ICInitStructure)
TIM update logo clearance Clear Tim's update logo (TIM_ClearFlag(TIM4, TIM_FLAG_Update))
TIM update interrupt enable enable Make TIM update interruption (TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE))

Read_Encoder(Motor_ID MYTIMX)

Read the encoder count of unit time

Read_Encoder() Reading from the last time calling_encoder () to this time calling_encoder (), the number of encoders during the period

In order to calculate the speed, Read_encoder () needs to be called at a fixed time interval

parametertypeAnnotation
MYTIMXMotor_IDChoose a motor to read
Motor_ID Optional parameter MOTOR_ID_ML/MOTOR_ID_MR
Return valuetype
Encoder countint

Get_Velocity_From_Encoder(int encoder_left, int encoder_right);

Return to the speed of the left and right wheels, the unit is mm/s

This function defaults to 5ms as a speed measurement cycle, corresponding to the control_frequency = 200 (t = 1/f) in the definition of the macro definition

parametertypeAnnotation
encoder_leftintLeft motor encoder value
encoder_rightintRight motor encoder value
Return valuetype
velocitiesfloat*
The return value is a speed array
Velocities [0] is the speed of the left motor
Velocities [1] is the right motor speed, the unit is MM/S

TIM3_IRQHandler(void);

Timer 3 interrupt processing function

TIM4_IRQHandler(void);

Timer 4 interrupt processing function

使用示例


//  Introduction part of the code is omitted 
int main() {
    Encoder_Init_TIM3();
    Encoder_Init_TIM4();
    int left_count = 0;
    int right_count = 0;
    while(1) {
        //  Take 5ms as the speed measurement cycle, corresponding to the control_frequency = 200 (t = 1/f) in the definition of macro definition
        delay_ms(5);
        left_count += Read_Encoder(MOTOR_ID_ML);
        right_count += Read_Encoder(MOTOR_ID_MR);
        printf("L: %d, R: %d\n", left_count, right_count);
    }
    return 0;
}