调试状态空间模型和控制器

检查标志

状态空间控制器错误的最常见原因之一是信号被翻转。例如,WPILib中包含的模型期望正电压会导致正加速度,反之亦然。如果施加正电压不能使该机构向前加速,或者如果“向前”移动会使编码器(或其他传感器读数)减小,则应将其反转,以使正电压输入导致编码器读数为正。例如,如果我对差速器传动系统应用:math:[12,12] ^ T`(左右电动机的全速前进)的:term:`input,则我的车轮应将机器人“向前”推进(沿+ X轴局部),并使我的编码器读取正速度。

重要

The WPILib DifferentialDrive, by default, does not invert any motors. You may need to call the setInverted(true) method on the motor controller object to invert so that positive input creates forward motion.

图的重要性

Reliable data of the system’s states, inputs and outputs over time is important when debugging state-space controllers and observers. One common approach is to send this data over NetworkTables and use tools such as Shuffleboard, which allow us to both graph the data in real-time as well as save it to a CSV file for plotting later with tools such as Google Sheets, Excel or Python.

备注

默认情况下,NetworkTables的更新速率限制为10hz。为了进行测试,可以使用以下代码段绕过它,以最高100hz的速度提交数据。该代码应定期运行以强制发布新数据。

危险

这将通过NetworkTables发送额外的数据(最高100hz),这可能导致用户代码和机械手仪表板的滞后。这也将提高网络利用率。在比赛中禁用此功能通常是个好主意。

@Override
public void robotPeriodic() {
   NetworkTableInstance.getDefault().flush();
}
void RobotPeriodic() {
   NetworkTableInstance::GetDefault().Flush();
}
from ntcore import NetworkTableInstance

def robotPeriodic(self):
   NetworkTableInstance.getDefault().flush()

补偿输入滞后

通常,某些传感器输入数据(即速度读数)可能会由于智能电机控制器倾向于执行的车载过滤而被延迟。默认情况下,LQR的K增益不假定输入延迟,因此引入数十毫秒量级的显着延迟可能会导致不稳定。为了解决这个问题,可以降低LQR的K增益,以牺牲性能为代价。有关如何以数学上严格的方式补偿此延迟的代码示例,请参见 here