gpt4 book ai didi

c - PID在arduino中的实现

转载 作者:行者123 更新时间:2023-11-30 16:24:14 25 4
gpt4 key购买 nike

我在网上发现了一些为arduino实现PID的代码。我对实现感到困惑。我对 PID 的工作原理有基本的了解,但是我的困惑在于为什么 m_prevError 使用十六进制?值 0x80000000L 代表什么以及为什么在计算速度时右移 10?

// ServoLoop Constructor
ServoLoop::ServoLoop(int32_t proportionalGain, int32_t derivativeGain)
{
m_pos = RCS_CENTER_POS;
m_proportionalGain = proportionalGain;
m_derivativeGain = derivativeGain;
m_prevError = 0x80000000L;
}

// ServoLoop Update
// Calculates new output based on the measured
// error and the current state.
void ServoLoop::update(int32_t error)
{
long int velocity;
char buf[32];
if (m_prevError!=0x80000000)
{
velocity = (error*m_proportionalGain + (error - m_prevError)*m_derivativeGain)>>10;

m_pos += velocity;
if (m_pos>RCS_MAX_POS)
{
m_pos = RCS_MAX_POS;
}
else if (m_pos<RCS_MIN_POS)
{
m_pos = RCS_MIN_POS;
}
}
m_prevError = error;
}

最佳答案

将二进制数右移 1 意味着将其对应的十进制值乘以 2。这里移动 10 意味着乘以 2^10,即 1024。作为任何基本控制循环,它可能是速度增益,其中返回值被转换为适合任何其他方法重复使用的值。

此处的L 0x80000000L 将该值声明为long。所以,这个值0x80000000可能是错误的初始值等等。另外,您需要修改完整的程序以查看其工作原理以及为 error 等内容分配什么值。

关于c - PID在arduino中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53697590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com