You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pi-top 4球检测器报错:元组与NoneType间无法使用<=运算符

pi-top 4球检测器坐标判断报错解决

问题代码

if blue_ball.found:
        print(f"Blue ball center: {blue_ball.center}")
        print()   

 if ((30,50) <= blue_ball.center <= (60, 80)):
        led.on()
        sleep(0.5)

报错信息

"<=" cannot be inbetween a tuple and a NoneType.

原因分析

  • blue_ball.center 为 NoneType 的核心原因:pitop.processing.algorithms.BallDetector 模块的设计逻辑是,当未检测到目标球时(即 blue_ball.found 为 False),center 属性会被设为 None——因为此时没有有效的坐标数据可以返回。
  • 代码逻辑漏洞:第二个 if 判断没有先校验 blue_ball.found 的状态,当球没被检测到时,直接拿 None 和元组做比较,触发类型错误。
  • 补充:元组范围比较的格式 (30,50) <= blue_ball.center <= (60,80) 本身是合法的(Python会按元组元素顺序逐一比较),但前提是 blue_ball.center 是有效的二元组坐标。

解决方案

方案1:嵌套判断(推荐,逻辑更清晰)

将坐标范围判断嵌套到 blue_ball.found 的分支内,确保只有检测到球时才访问 center 属性:

if blue_ball.found:
        print(f"Blue ball center: {blue_ball.center}")
        print()   
        # 仅在检测到球时判断坐标范围
        if (30, 50) <= blue_ball.center <= (60, 80):
            led.on()
            sleep(0.5)

方案2:合并判断条件

在一个 if 中同时校验检测状态和坐标范围:

if blue_ball.found and (30, 50) <= blue_ball.center <= (60, 80):
        led.on()
        sleep(0.5)

注意事项

  • 不要尝试手动修改 blue_ball.center 的类型,这个属性由 BallDetector 模块自动赋值:检测到球时是包含x、y坐标的二元组,未检测到则为 None。
  • 所有访问 blue_ball.center 的操作,必须先通过 blue_ball.found 确认球已被检测到,这是避免 NoneType 错误的核心原则。

内容的提问来源于stack exchange,提问作者Kai_Q

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:02:16