#include "main.h" #include "headfile.h" unsigned int adc_value[5]; int error[2]; float kp = 1.1; float kd = 0.5; unsigned int left_motor_duty; unsigned int right_motor_duty; unsigned int motor_duty_center = 3000; int main(void) { car_init(); while(1) { adc_value[0] = adc_get(ADC0, ADC_CH_10); adc_value[1] = adc_get(ADC0, ADC_CH_11); adc_value[2] = adc_get(ADC0, ADC_CH_12); adc_value[3] = adc_get(ADC0, ADC_CH_13); adc_value[4] = adc_get(ADC0, ADC_CH_15); car_race(); } } void car_race() { error[0] = adc_value[0] - adc_value[4]; int speed_c = (int)(error[0] * kp + (error[0] + error[1]) * kd); left_motor_duty = motor_duty_center - speed_c; right_motor_duty = motor_duty_center + speed_c; error[1] = error[0]; if(left_motor_duty > 10000) { left_motor_duty = 10000; } else if(left_motor_duty < 0) { left_motor_duty = 0; } if(right_motor_duty > 10000) { right_motor_duty = 10000; } else if(right_motor_duty < 0) { right_motor_duty = 0; } motor_forward(left, left_motor_duty); motor_forward(right, right_motor_duty);}修改为使用算数平均值滤波算法并进行归一化处理,电机控制用增量式PID算法,再用位置式PD算法对方向进行控制的能使小车进行循迹的代码,一定能要让小车跑起来
时间: 2024-04-17 07:28:40 浏览: 41
```
#include "main.h"
#include "headfile.h"
unsigned int adc_value[5];
int error[2];
float integral = 0;
float previous_error = 0;
float kp = 1.1;
float ki = 0.2;
float kd = 0.5;
unsigned int left_motor_duty;
unsigned int right_motor_duty;
unsigned int motor_duty_center = 3000;
int main(void)
{
car_init();
while (1)
{
// Read ADC values
adc_value[0] = adc_get(ADC0, ADC_CH_10);
adc_value[1] = adc_get(ADC0, ADC_CH_11);
adc_value[2] = adc_get(ADC0, ADC_CH_12);
adc_value[3] = adc_get(ADC0, ADC_CH_13);
adc_value[4] = adc_get(ADC0, ADC_CH_15);
// Apply moving average filter and normalize
unsigned int filtered_adc_value[5];
for (int i = 0; i < 5; i++)
{
filtered_adc_value[i] = (adc_value[i] + adc_value[i - 1] + adc_value[i - 2]) / 3;
filtered_adc_value[i] = (filtered_adc_value[i] - 2000) / 1000; // Normalize to -1 to 1 range
}
// Update error and perform PID control for speed
error[0] = filtered_adc_value[0] - filtered_adc_value[4];
float derivative = error[0] - previous_error;
float speed_c = error[0] * kp + integral * ki + derivative * kd;
left_motor_duty += speed_c;
right_motor_duty += speed_c;
previous_error = error[0];
integral += error[0];
// Clamp motor duty values
if (left_motor_duty > 10000)
{
left_motor_duty = 10000;
}
else if (left_motor_duty < 0)
{
left_motor_duty = 0;
}
if (right_motor_duty > 10000)
{
right_motor_duty = 10000;
}
else if (right_motor_duty < 0)
{
right_motor_duty = 0;
}
// Perform direction control with PD algorithm
float direction_c = error[0] * kp + derivative * kd;
// Apply direction control to motor duty values
left_motor_duty -= direction_c;
right_motor_duty += direction_c;
// Clamp motor duty values again
if (left_motor_duty > 10000)
{
left_motor_duty = 10000;
}
else if (left_motor_duty < 0)
{
left_motor_duty = 0;
}
if (right_motor_duty > 10000)
{
right_motor_duty = 10000;
}
else if (right_motor_duty < 0)
{
right_motor_duty = 0;
}
// Control the motors for track tracing
motor_forward(left, left_motor_duty);
motor_forward(right, right_motor_duty);
}
}
```
请确保硬件连接正确,并根据实际情况修改代码中的参数和函数调用以适应您的小车。
阅读全文