0 / 6 页已完成

进阶:球路 FSM 可选 · 赛季附录Advanced: Ball Path FSM Optional · Season Appendix

这一节是可选内容This Section Is Optional

本节展示 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:

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:

  1. 吸入球Intake the ball
  2. 光学传感器检测颜色Detect color with an optical sensor
  3. 我方球 → 继续送到射球位置Our ball → continue to the scoring position
  4. 对方球 → 反转吐出来Opponent ball → reverse and eject
C++ (VEXcode) — 伪代码
// 简化的颜色分拣逻辑
void store() {
  // 吸取电机一直转
  intakeMotor.spin(forward);

  // 光学传感器检测到球了
  if (colorSensor.isNearObject()) {
    // 判断颜色
    if (colorSensor.hue() > 200) {
      // 蓝色球(假设我方是蓝色)→ 继续送
      indexMotor.spin(forward);
    } else {
      // 红色球(对方)→ 反转吐出
      indexMotor.spin(reverse);
    }
  }
}
C++ (VS Code) — 伪代码
// 简化的颜色分拣逻辑
void store() {
  // 吸取电机一直转
  intakeMotor.spin(forward);

  // 光学传感器检测到球了
  if (colorSensor.isNearObject()) {
    // 判断颜色
    if (colorSensor.hue() > 200) {
      // 蓝色球(假设我方是蓝色)→ 继续送
      indexMotor.spin(forward);
    } else {
      // 红色球(对方)→ 反转吐出
      indexMotor.spin(reverse);
    }
  }
}
C++ (PROS) — 伪代码
// 简化的颜色分拣逻辑
void store() {
  // 吸取电机一直转
  intakeMotor.move(127);

  // 光学传感器检测到球了
  if (colorSensor.get_proximity() > 200) {
    // 判断颜色
    if (colorSensor.get_hue() > 200) {
      // 蓝色球(假设我方是蓝色)→ 继续送
      indexMotor.move(127);
    } else {
      // 红色球(对方)→ 反转吐出
      indexMotor.move(-127);
    }
  }
}
颜色判断的核心:hue(色调)The Key to Color Detection: hue

光学传感器返回的 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:

  1. enum class 列出所有状态enum class lists all states
  2. 每个状态对应一个函数,处理该状态下的具体动作Each state maps to a function that handles the specific action
  3. switch 把状态和函数连接起来switch connects states to functions
  4. 状态切换在别的地方触发(按钮、传感器、自动逻辑)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

想一想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?

哪种更好?为什么?Which is better? Why?

检查点:理解检验Checkpoint: Understanding Check
机器人吸入了一个对方颜色的球,光学传感器检测到颜色后,程序应该:The robot intakes an opponent-colored ball. After the optical sensor detects the color, the program should: