进阶:球路 FSM 可选 · 赛季附录Advanced: Ball Path FSM Optional · Season Appendix
本节展示 2026 赛季真实比赛中的复杂状态机。目的是阅读理解,不要求你写出来。如果觉得太难,可以跳过直接去下一节。This section shows complex state machines from the 2026 real competition. The goal is reading comprehension — you're not expected to write this yourself. If it feels too hard, skip ahead to the next section.
4.1 为什么需要更复杂的状态机?4.1 Why Do We Need More Complex State Machines?
上一节的 PushBack Block 系统已经有 8 个状态了。但在某些赛季,球路系统还要处理更复杂的情况:The PushBack Block system from the previous section already had 8 states. But in some seasons, the ball path system needs to handle even more complex situations:
- 颜色分拣 — 我方球和对方球颜色不同,要自动识别Color sorting — our balls and opponent balls are different colors, requiring automatic identification
- 多种射球目标 — 高杆、中杆、吐球,每种力度和方向不同Multiple scoring targets — high stake, mid stake, eject — each needs different power and direction
- 球的排队管理 — 球在机器人内部排队等候,先进先出Ball queue management — balls line up inside the robot, first in first out
4.2 颜色检测 + 分拣逻辑4.2 Color Detection + Sorting Logic
2026 赛季 High Stakes 的球分红蓝两色。机器人需要:In the 2026 High Stakes season, balls come in red and blue. The robot needs to:
- 吸入球Intake the ball
- 光学传感器检测颜色Detect color with an optical sensor
- 我方球 → 继续送到射球位置Our ball → continue to the scoring position
- 对方球 → 反转吐出来Opponent ball → reverse and eject
// 简化的颜色分拣逻辑
void store() {
// 吸取电机一直转
intakeMotor.spin(forward);
// 光学传感器检测到球了
if (colorSensor.isNearObject()) {
// 判断颜色
if (colorSensor.hue() > 200) {
// 蓝色球(假设我方是蓝色)→ 继续送
indexMotor.spin(forward);
} else {
// 红色球(对方)→ 反转吐出
indexMotor.spin(reverse);
}
}
}
// 简化的颜色分拣逻辑
void store() {
// 吸取电机一直转
intakeMotor.spin(forward);
// 光学传感器检测到球了
if (colorSensor.isNearObject()) {
// 判断颜色
if (colorSensor.hue() > 200) {
// 蓝色球(假设我方是蓝色)→ 继续送
indexMotor.spin(forward);
} else {
// 红色球(对方)→ 反转吐出
indexMotor.spin(reverse);
}
}
}
// 简化的颜色分拣逻辑
void store() {
// 吸取电机一直转
intakeMotor.move(127);
// 光学传感器检测到球了
if (colorSensor.get_proximity() > 200) {
// 判断颜色
if (colorSensor.get_hue() > 200) {
// 蓝色球(假设我方是蓝色)→ 继续送
indexMotor.move(127);
} else {
// 红色球(对方)→ 反转吐出
indexMotor.move(-127);
}
}
}
光学传感器返回的 hue 值可以区分颜色。红色的 hue 大约在 0-30,蓝色在 200-240。具体阈值需要实际测试调整。The optical sensor returns a hue value that distinguishes colors. Red hue is approximately 0-30, blue is 200-240. The exact thresholds need to be calibrated through testing.
4.3 状态数量爆炸?不怕4.3 Too Many States? No Worries
当你的系统有很多状态时,代码量确实会增加。但只要遵循状态机的模式,结构始终是清晰的:When your system has many states, the code does get longer. But as long as you follow the state machine pattern, the structure stays clear:
- enum class 列出所有状态enum class lists all states
- 每个状态对应一个函数,处理该状态下的具体动作Each state maps to a function that handles the specific action
- switch 把状态和函数连接起来switch connects states to functions
- 状态切换在别的地方触发(按钮、传感器、自动逻辑)State transitions are triggered elsewhere (buttons, sensors, auto logic)
不管是 3 个状态还是 30 个状态,结构都一模一样。区别只是 switch 里的 case 多了几行。Whether you have 3 states or 30, the structure is identical. The only difference is a few more case lines in the switch.
4.4 思考题4.4 Think About It
如果你的机器人要同时控制升降臂和球路系统,你会怎么做?If your robot needs to control both the lift arm and the ball system at the same time, what would you do?
- 方案 A:把升降臂的状态也放进 Block 的 enum 里Option A: Put the lift arm states into the Block enum
- 方案 B:升降臂和球路各自有独立的 enum 和 switchOption B: Give the lift arm and ball system each their own independent enum and switch
哪种更好?为什么?Which is better? Why?