msp432快速入门第十二节之电机闭环

注意:在看这一节之前必须看完这一节之前的三节教程。

(一)PID原理

  PID的是典型的误差反馈控制系统,是古典控制理论中最为常见的一种控制方法,而且至今仍被广泛使用,对于电机的闭环控制,我使用增量式PID进行控制,设定的采集以及计算时间为100ms,电机的输出PWM范围为0-10000,方向使用电平单独控制,在这个控制过程中仅仅使用了PI两项,并没有使用D项,PI控制电机的效果比较理想。

(二)程序编写

(1)h文件

//普通速度环 基本速度控制
//简易增量式PID
typedef struct
{
	int16 exval;
	int16 nowval;
	float kp;
	float ki;
	float kd;
	int16 nowError;
	float increase;
	int16 lastError;
	int16 beforError;
	int16 out;
	float maxout;
	float minout;
}SpeedPIDTypedef;

extern SpeedPIDTypedef SpeedPIDLeft,SpeedPIDRight;


void pid_param_init(void);
int16 SimpleIncremental_PID(SpeedPIDTypedef *pid,int16 _exval,int16 _nowval);

(2)C文件

/* 参数初始化 */
void pid_param_init(void)
{
	SpeedPIDLeft.maxout = 10000;
	SpeedPIDLeft.minout = -10000;
	SpeedPIDLeft.kp = 0.0f;
	SpeedPIDLeft.ki = 0.0f;
	SpeedPIDRight.maxout = 10000;
	SpeedPIDRight.minout = -10000;
	SpeedPIDRight.kp = 0.0f;
	SpeedPIDRight.ki = 0.0f;
}



/*------------------------普通增量式pid-------------------------*/
SpeedPIDTypedef SpeedPIDLeft,SpeedPIDRight;
int16 SimpleIncremental_PID(SpeedPIDTypedef *pid,int16 _exval,int16 _nowval)
{
	//运算
	pid->exval = _exval;
	pid->nowval = _nowval;
	//当前误差
	pid->nowError =  pid->exval - pid->nowval;
	//增量
	pid->increase = 
		pid->kp * (float)(pid->nowError - pid->lastError) +
		pid->ki * (float)pid->nowError +
		pid->kd * (float)(pid->nowError - 2 * pid->lastError + pid->beforError);
	//更新误差
	pid->lastError = pid->nowError;
	pid->beforError = pid->lastError;
	pid->out += (int16)pid->increase;
	//限制幅度
	pid->out = range_protect(pid->out,(int32)pid->minout,(int32)pid->maxout);
	//输出
	return pid->out;
}

(3)100ms定时器中断

/* 速度控制 */
void Speed_Control(void)
{
	/* 读取速度 */
	Read_Encoder();
	/* 右轮速度控制 */
	pwm_r = SimpleIncremental_PID(&SpeedPIDRight,speed_r,encoder1_val);
	/* 左轮速度控制 */
	pwm_l = SimpleIncremental_PID(&SpeedPIDLeft,speed_l,encoder0_val);
	/* 速度写入 */
	SpeedSet(pwm_l,pwm_r);
}

(三)参数调整

调试时首先将各项数值置零,对于增量型PI控制器可以先从I项开始调整,先给I项一个较小的数值,然后逐渐增大,直到开始轻微震荡,然后把数值调整到震荡数值的2/3即可,然后需要调整P项,也是跟I项调整方式差不多,主要是利用I项快速跟随误差,利用P项抑制超调。
放一张调试图:
在这里插入图片描述

克隆空白工程请使用 git , 如果你觉得还不错欢迎点亮 star !

git clone https://github.com/YGZone/Design-a-car-with-MSP432P401R.git

更多资料可以转步我的个人网站 www.eestr.com ( https://www.eestr.com)或者 Github[https://github.com/YGZone], 欢迎访问

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YGZone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值