Skip to content

Instantly share code, notes, and snippets.

@Parlane
Created May 30, 2020 22:50
Show Gist options
  • Select an option

  • Save Parlane/de82d2c32bf55c1f48954f876fb2cf3f to your computer and use it in GitHub Desktop.

Select an option

Save Parlane/de82d2c32bf55c1f48954f876fb2cf3f to your computer and use it in GitHub Desktop.

Revisions

  1. Parlane created this gist May 30, 2020.
    38 changes: 38 additions & 0 deletions read_busy.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #! /bin/env python3

    import csv
    import struct

    def twos_complement(value, bits):
    if value & (1 << (bits-1)):
    value -= 1 << bits
    return value
    return value

    with open('CanBus Raw Output.csv', 'r', encoding="utf8") as f:
    reader = csv.DictReader(f, fieldnames=['CMD', 'LEN'], restkey='DATA')
    for row in reader:
    cmd = int(row['CMD'])
    length = int(row['LEN'])

    data = [int(x) for x in row['DATA'][:length]]

    if cmd == 0x201:
    rpm = ((data[0] << 8) + data[1]) / 3.85
    unknown = (data[2] << 8) + data[3]
    kph = (data[4] << 8) + data[5]
    kph = (kph - 10000) / 100
    throttle = data[6]
    unknown2 = data[7]

    #print(cmd, rpm, kph, throttle)
    elif cmd == 0x81:

    # Wheel turn is a signed 16 bit number
    number = (data[2] << 8) + data[3]
    wheel_turn = twos_complement(number, 16)

    # Highest bit indicates wheel is "centered"
    wheel_centered = (data[1] & 0x80) == 0x80

    print ("Wheel turned {turn:3d} degrees {dir}. Centered: {centered}".format(turn=abs(wheel_turn), centered=wheel_centered, dir=("LEFT" if wheel_turn < 0 else "RIGHT")))