基于STM32的位置式PID直流电机简单控制算法

OUT=Kp*Ek+[Kp*(T/Ti)*\sum_{k=0}^{n}*Ek ]+[Kp*(Td/T)*(Ek-Ek_1)]+OUT0

 

 

#include "pid.h"
#include "stm32f10x.h"


PID my_pid;

void PID_init(u8 sv,u8 kp,u8 t,u8 ti,u8 td,u8 out0)
{
	my_pid.Sv =sv;
	my_pid.Kp =kp;
	my_pid.Td =td;
	my_pid.Ti =ti;
	my_pid.OUT0 =out0;
	
}


u16 MY_PID(u8 sv,u8 pv)
{
	float P,I,D;
	
	my_pid.Sv =sv;
	my_pid.Pv =pv;
	
	my_pid.Ek =(my_pid.Sv -my_pid.Pv );
	
	P=my_pid.Kp*my_pid.Ek;
	
	my_pid.DEk +=my_pid.Ek;
	
	I=my_pid.Kp*(my_pid.T/my_pid.Ti)*my_pid.DEk ;
	
	D=my_pid.Kp*(my_pid.Td/my_pid.T)*(my_pid.Ek-my_pid.Ek_1 )+my_pid.OUT0 ;
	
	my_pid.OUT=P+I+D;
	
	return my_pid.OUT;
	
}

 

你可能感兴趣的:(c程,STM32)