今天调试一个程序,因为Feedback是电流采样值,Setpoint是PWM值,这两个不可能是负值。所以以为Setpoint和Feedback这两个变量都可以设置为u16型(unsigned int),结果悲催了,CPU总是跑飞。导致LED暴亮,差点烧掉。。。

  原因是两个unsigned型数据相减后不能为负值。如: PIdata->Setpoint - PIdata->Feedback

所以,要保证其中有一个是signed 型,系统就默认相减的结果为signed型,程序可以正常运行。

typedef struct {
u16 Kp;
u16 Ki;
u8 OutMax;
signed long Setpoint;
u16 Feedback;
signed long Error;
signed long Integral;
signed long Output;
u8 Saturated;
}tPIParams; tPIParams Current;
void CalcPI(tPIParams *PIdata)
{
PIdata->Error = PIdata->Setpoint - PIdata->Feedback;
// Deadband -- If the magnitude of the error is less than a limit,
// then don't calculate the PI routine at all. This saves
// processor time and avoids oscillation problems.
if((PIdata->Error > ) || (PIdata->Error < -))
{
// Now, calculate the actual PI function here.
PIdata->Output += (PIdata->Error * PIdata->Kp )/; // Perform boundary checks on the PI controller output. If the
// output limits are exceeded, then set output to the limit
// and set flag.
if(PIdata->Output > PIdata->OutMax)
{
PIdata->Output = PIdata->OutMax;
}else if(PIdata->Output < )
{
//PIdata->Saturated = 1;
PIdata->Output = ;
}
}
}

最新文章

  1. 推荐一个实用的css工具
  2. HTML无刷新提交表单
  3. 记一次Web应用CPU偏高
  4. nrf51822-提高nordic ble数据发送速率
  5. SqlServer调用外部程序实现数据同步
  6. hadoop中hbase出现的问题
  7. linux 配置 Samba 服务器实现文件共享
  8. 【USACO 1.4.4】母亲的牛奶
  9. HTML与CSS入门——第十一章  在网页中使用图像
  10. Java数据库编程、XML解析技术
  11. ionic3 懒加载在微信上缓存的问题
  12. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限
  13. 远程连接身份验证错误,又找不到加密Oracle修正
  14. quart-process_bar
  15. bash小技巧1 获取文件当前路径
  16. Mybatis的原理相关
  17. CentOS7用阿里云Docker Yum源在线安装Docker 17.03.2
  18. HDU 3853 LOOPS 期望dp
  19. JVM 理解
  20. My Neural Network isn't working! What should I do?

热门文章

  1. WARNING you have Transparen Huge Pages..
  2. 多线程:InterlockedIncrement
  3. Nginx学习记录(一)
  4. 学习笔记(六): Regularization for Simplicity
  5. 小程序wafer2操作数据库
  6. python爬虫的基本思路
  7. WPF实现QQ群文件列表动画(二)
  8. python项目中输出指定颜色的日志
  9. 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
  10. Docker背后的内核知识(一)