stm32f103c8t6五路循迹PID算法代码
时间: 2024-06-01 17:06:54 浏览: 169
STM32F103C8T6是一款常用的单片机,循迹PID算法是一种常用的控制算法,可以用于机器人、小车等自动控制系统中的循迹控制。通常,循迹控制的主要步骤包括采集传感器数据、计算偏差值、根据偏差值计算PID参数、调整控制输出等。
下面是一个简单的五路循迹PID算法代码:
```
#include "stm32f10x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define RIGHT_SENSOR_GPIO_PIN GPIO_Pin_0
#define RIGHT_SENSOR_GPIO_PORT GPIOB
#define RIGHT_SENSOR_ADC_CHANNEL ADC_Channel_8
#define MID_RIGHT_SENSOR_GPIO_PIN GPIO_Pin_1
#define MID_RIGHT_SENSOR_GPIO_PORT GPIOB
#define MID_RIGHT_SENSOR_ADC_CHANNEL ADC_Channel_9
#define MID_SENSOR_GPIO_PIN GPIO_Pin_2
#define MID_SENSOR_GPIO_PORT GPIOB
#define MID_SENSOR_ADC_CHANNEL ADC_Channel_10
#define MID_LEFT_SENSOR_GPIO_PIN GPIO_Pin_10
#define MID_LEFT_SENSOR_GPIO_PORT GPIOB
#define MID_LEFT_SENSOR_ADC_CHANNEL ADC_Channel_15
#define LEFT_SENSOR_GPIO_PIN GPIO_Pin_11
#define LEFT_SENSOR_GPIO_PORT GPIOB
#define LEFT_SENSOR_ADC_CHANNEL ADC_Channel_14
float Kp = 0.5;
float Ki = 0.2;
float Kd = 0.1;
float P, I, D;
float last_error, current_error;
float output;
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_ADC1, ENABLE);
/* Configure ADC Channel8-15 as analog input */
GPIO_InitStructure.GPIO_Pin = RIGHT_SENSOR_GPIO_PIN | MID_RIGHT_SENSOR_GPIO_PIN | MID_SENSOR_GPIO_PIN | MID_LEFT_SENSOR_GPIO_PIN | LEFT_SENSOR_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure and enable ADC1 */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 5;
ADC_Init(ADC1, &ADC_InitStructure);
/* Configure ADC1 Channel8-15 pin */
ADC_RegularChannelConfig(ADC1, RIGHT_SENSOR_ADC_CHANNEL, 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, MID_RIGHT_SENSOR_ADC_CHANNEL, 2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, MID_SENSOR_ADC_CHANNEL, 3, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, MID_LEFT_SENSOR_ADC_CHANNEL, 4, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, LEFT_SENSOR_ADC_CHANNEL, 5, ADC_SampleTime_239Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (1) {
/* Read sensor values */
float right_sensor_value = (float)ADC_GetConversionValue(ADC1);
float mid_right_sensor_value = (float)ADC_GetConversionValue(ADC1);
float mid_sensor_value = (float)ADC_GetConversionValue(ADC1);
float mid_left_sensor_value = (float)ADC_GetConversionValue(ADC1);
float left_sensor_value = (float)ADC_GetConversionValue(ADC1);
/* Calculate error */
float error = (left_sensor_value + mid_left_sensor_value - mid_sensor_value - mid_right_sensor_value - right_sensor_value) / 5.0;
/* PID algorithm */
P = Kp * error;
I += Ki * error;
D = Kd * (error - last_error);
output = P + I + D;
last_error = error;
/* Output control signal */
// code here
/* Delay for a while */
// code here
}
}
```
阅读全文