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