josephlewis42 / autopilot

Fork of the autopilot software that is now being used at the University of Denver.
GNU General Public License v3.0
1 stars 2 forks source link

Add Altimiter #37

Closed josephlewis42 closed 10 years ago

josephlewis42 commented 10 years ago

The altimiter needs a driver in the software, a python mockup is here, without the manual I can't figure out what else this device is reporting e.g. the second four bits of the first byte of each message.

    from __future__ import print_function
    import serial
    import sys

    import random

    baud = 38400
    resolution = 10 # resolution in cm
    averaged = 100

    def forward(src):
        print("Reading %s" % (src))

        source_ser = serial.Serial(src, int(baud))

        errors = 0
        avg = 0
        avgct = 0
        while True:
            header = ord(source_ser.read()) # read a byte
            value = ord(source_ser.read()) # read the value

            if avgct < averaged:
                if header == 0xbf:
                    errors += 1         
                else:
                    header = (header & 0b1111)
                    #print(header)
                    header *= 100
                    avg += value + header
                avgct += 1
            else:

                if averaged - errors == 0:
                    distance = 0.0          
                else:           
                    distance = ((float(avg) / (averaged - errors)) * resolution)
                errorstr = "" if errors == 0 else " (%s%% error readings)" % (errors * 100 / averaged)
                print("%sm (%s cm)%s" % (distance / 100, distance, errorstr))
                avg = 0
                avgct = 0
                errors = 0

        source_ser.close()

    if __name__ == "__main__":
        if len(sys.argv) < 2:
            print("Usage: %s from to baud [logfile]")
            exit()

        forward(sys.argv[1])