Fix panda sorting logic in pandad (#32100)

* fix: returns int instead of bool in cmp function

* fix: usb_serial will not be equal

* refactor: stop using cmp function and instead use tuple of keys
pull/32189/head
Miwa / Ensan 1 year ago committed by GitHub
parent 661df357a9
commit 969be5ab9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      selfdrive/boardd/pandad.py

@ -5,7 +5,6 @@ import usb1
import time
import subprocess
from typing import NoReturn
from functools import cmp_to_key
from panda import Panda, PandaDFU, PandaProtocolMismatch, FW_PATH
from openpilot.common.basedir import BASEDIR
@ -62,24 +61,6 @@ def flash_panda(panda_serial: str) -> Panda:
return panda
def panda_sort_cmp(a: Panda, b: Panda):
a_type = a.get_type()
b_type = b.get_type()
# make sure the internal one is always first
if a.is_internal() and not b.is_internal():
return -1
if not a.is_internal() and b.is_internal():
return 1
# sort by hardware type
if a_type != b_type:
return a_type < b_type
# last resort: sort by serial number
return a.get_usb_serial() < b.get_usb_serial()
def main() -> NoReturn:
count = 0
first_run = True
@ -136,7 +117,10 @@ def main() -> NoReturn:
no_internal_panda_count = 0
# sort pandas to have deterministic order
pandas.sort(key=cmp_to_key(panda_sort_cmp))
# * the internal one is always first
# * then sort by hardware type
# * as a last resort, sort by serial number
pandas.sort(key=lambda x: (not x.is_internal(), x.get_type(), x.get_usb_serial()))
panda_serials = [p.get_usb_serial() for p in pandas]
# log panda fw versions

Loading…
Cancel
Save