stm32f103c8t6用pid算法控制五路循迹小车(无需测速,用直流电机控制)
时间: 2023-10-05 21:08:07 浏览: 113
好的,下面是一个简单的基于STM32F103C8T6的PID算法控制五路循迹小车的示例代码:
```c
#include "stm32f10x.h"
// 定义电机控制引脚
#define MOTOR_1_A_Pin GPIO_Pin_0
#define MOTOR_1_B_Pin GPIO_Pin_1
#define MOTOR_2_A_Pin GPIO_Pin_2
#define MOTOR_2_B_Pin GPIO_Pin_3
#define MOTOR_3_A_Pin GPIO_Pin_4
#define MOTOR_3_B_Pin GPIO_Pin_5
#define MOTOR_4_A_Pin GPIO_Pin_6
#define MOTOR_4_B_Pin GPIO_Pin_7
#define MOTOR_5_A_Pin GPIO_Pin_8
#define MOTOR_5_B_Pin GPIO_Pin_9
// 定义循迹传感器引脚
#define SENSOR_1_Pin GPIO_Pin_10
#define SENSOR_2_Pin GPIO_Pin_11
#define SENSOR_3_Pin GPIO_Pin_12
#define SENSOR_4_Pin GPIO_Pin_13
#define SENSOR_5_Pin GPIO_Pin_14
// PID参数
double kp = 0.5;
double ki = 0.1;
double kd = 0.1;
// 循迹传感器阈值
int threshold = 500;
// 当前偏差
int currentError = 0;
// 上一次偏差
int lastError = 0;
// 积分项
double integral = 0;
// 微分项
double derivative = 0;
// 目标速度(PWM占空比)
int targetSpeed = 100;
// 左右电机PWM值
int pwmLeft = 0;
int pwmRight = 0;
// 初始化GPIO
void initGPIO()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = MOTOR_1_A_Pin | MOTOR_1_B_Pin | MOTOR_2_A_Pin | MOTOR_2_B_Pin | MOTOR_3_A_Pin | MOTOR_3_B_Pin | MOTOR_4_A_Pin | MOTOR_4_B_Pin | MOTOR_5_A_Pin | MOTOR_5_B_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = SENSOR_1_Pin | SENSOR_2_Pin | SENSOR_3_Pin | SENSOR_4_Pin | SENSOR_5_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
// 读取循迹传感器值
void readSensors(int *sensorValues)
{
sensorValues[0] = ADC_GetConversionValue(ADC1);
sensorValues[1] = ADC_GetConversionValue(ADC2);
sensorValues[2] = ADC_GetConversionValue(ADC3);
sensorValues[3] = ADC_GetConversionValue(ADC4);
sensorValues[4] = ADC_GetConversionValue(ADC5);
}
// 控制电机
void controlMotors(int pwmLeft, int pwmRight)
{
if (pwmLeft > 0)
{
GPIO_SetBits(GPIOA, MOTOR_1_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_1_B_Pin);
TIM_SetCompare1(TIM1, pwmLeft);
}
else if (pwmLeft < 0)
{
GPIO_ResetBits(GPIOA, MOTOR_1_A_Pin);
GPIO_SetBits(GPIOA, MOTOR_1_B_Pin);
TIM_SetCompare1(TIM1, -pwmLeft);
}
else
{
GPIO_ResetBits(GPIOA, MOTOR_1_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_1_B_Pin);
TIM_SetCompare1(TIM1, 0);
}
if (pwmRight > 0)
{
GPIO_SetBits(GPIOA, MOTOR_2_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_2_B_Pin);
TIM_SetCompare2(TIM1, pwmRight);
}
else if (pwmRight < 0)
{
GPIO_ResetBits(GPIOA, MOTOR_2_A_Pin);
GPIO_SetBits(GPIOA, MOTOR_2_B_Pin);
TIM_SetCompare2(TIM1, -pwmRight);
}
else
{
GPIO_ResetBits(GPIOA, MOTOR_2_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_2_B_Pin);
TIM_SetCompare2(TIM1, 0);
}
if (pwmLeft > 0)
{
GPIO_SetBits(GPIOA, MOTOR_3_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_3_B_Pin);
TIM_SetCompare3(TIM1, pwmLeft);
}
else if (pwmLeft < 0)
{
GPIO_ResetBits(GPIOA, MOTOR_3_A_Pin);
GPIO_SetBits(GPIOA, MOTOR_3_B_Pin);
TIM_SetCompare3(TIM1, -pwmLeft);
}
else
{
GPIO_ResetBits(GPIOA, MOTOR_3_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_3_B_Pin);
TIM_SetCompare3(TIM1, 0);
}
if (pwmRight > 0)
{
GPIO_SetBits(GPIOA, MOTOR_4_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_4_B_Pin);
TIM_SetCompare4(TIM1, pwmRight);
}
else if (pwmRight < 0)
{
GPIO_ResetBits(GPIOA, MOTOR_4_A_Pin);
GPIO_SetBits(GPIOA, MOTOR_4_B_Pin);
TIM_SetCompare4(TIM1, -pwmRight);
}
else
{
GPIO_ResetBits(GPIOA, MOTOR_4_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_4_B_Pin);
TIM_SetCompare4(TIM1, 0);
}
if (pwmLeft > 0)
{
GPIO_SetBits(GPIOA, MOTOR_5_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_5_B_Pin);
}
else if (pwmLeft < 0)
{
GPIO_ResetBits(GPIOA, MOTOR_5_A_Pin);
GPIO_SetBits(GPIOA, MOTOR_5_B_Pin);
}
else
{
GPIO_ResetBits(GPIOA, MOTOR_5_A_Pin);
GPIO_ResetBits(GPIOA, MOTOR_5_B_Pin);
}
}
// 计算PID控制量
void calculatePID(int *sensorValues)
{
currentError = 0;
int sum = 0;
for (int i = 0; i < 5; i++)
{
if (sensorValues[i] > threshold)
{
currentError += (i - 2) * sensorValues[i];
sum += sensorValues[i];
}
}
if (sum == 0)
{
currentError = 0;
}
else
{
currentError /= sum;
}
integral += currentError;
derivative = currentError - lastError;
lastError = currentError;
pwmLeft = targetSpeed + kp * currentError + ki * integral + kd * derivative;
pwmRight = targetSpeed - kp * currentError - ki * integral - kd * derivative;
}
int main(void)
{
// 初始化GPIO
initGPIO();
// 初始化ADC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3 | RCC_APB2Periph_ADC4 | RCC_APB2Periph_ADC5, ENABLE);
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
ADC_InitStruct.ADC_ScanConvMode = ENABLE;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_NbrOfChannel = 5;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_Init(ADC2, &ADC_InitStruct);
ADC_Init(ADC3, &ADC_InitStruct);
ADC_Init(ADC4, &ADC_InitStruct);
ADC_Init(ADC5, &ADC_InitStruct);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC4, ADC_Channel_13, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC5, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
ADC_Cmd(ADC3, ENABLE);
ADC_Cmd(ADC4, ENABLE);
ADC_Cmd(ADC5, ENABLE);
ADC_ResetCalibration(ADC1);
ADC_ResetCalibration(ADC2);
ADC_ResetCalibration(ADC3);
ADC_ResetCalibration(ADC4);
ADC_ResetCalibration(ADC5);
while (ADC_GetResetCalibrationStatus(ADC1) || ADC_GetResetCalibrationStatus(ADC2) || ADC_GetResetCalibrationStatus(ADC3) || ADC_GetResetCalibrationStatus(ADC4) || ADC_GetResetCalibrationStatus(ADC5));
ADC_StartCalibration(ADC1);
ADC_StartCalibration(ADC2);
ADC_StartCalibration(ADC3);
ADC_StartCalibration(ADC4);
ADC_StartCalibration(ADC5);
while (ADC_GetCalibrationStatus(ADC1) || ADC_GetCalibrationStatus(ADC2) || ADC_GetCalibrationStatus(ADC3) || ADC_GetCalibrationStatus(ADC4) || ADC_GetCalibrationStatus(ADC5));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
ADC_SoftwareStartConvCmd(ADC2, ENABLE);
ADC_SoftwareStartConvCmd(ADC3, ENABLE);
ADC_SoftwareStartConvCmd(ADC4, ENABLE);
ADC_SoftwareStartConvCmd(ADC5, ENABLE);
// 初始化定时器
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_TimeBaseStruct.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStruct.TIM_Period = 1000 - 1;
TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);
TIM_Cmd(TIM1, ENABLE);
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct.TIM_Pulse = 0;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStruct);
TIM_OC2Init(TIM1, &TIM_OCInitStruct);
TIM_OC3Init(TIM1, &TIM_OCInitStruct);
TIM_OC4Init(TIM1, &TIM_OCInitStruct);
while (1)
{
int sensorValues[5];
readSensors(sensorValues);
calculatePID(sensorValues);
controlMotors(pwmLeft, pwmRight);
}
}
```
这个代码中,我们使用了STM32F103C8T6的定时器和PWM功能来控制电机的转速,使用了STM32F103C8T6的ADC功能来读取循迹传感器的值,并通过PID算法计算出左右电机的PWM值,从而实现对五路循迹小车的控制。
阅读全文