Exemple #1
0
def watchAngles_(angleCallback=None, maxTime=None):
    t0 = time.time()
    #device_list = ts_api.getComPorts(filter=ts_api.TSS_FIND_USB)
    device_list = ts_api.getComPorts()

    print "device_list:", device_list
    com_port = device_list[0]
    device = ts_api.TSBTSensor(com_port=com_port)

    ## If a connection to the COM port fails, None is returned.
    if not device:
        print "Cannot get device"
        return

    ## The class instances have all of the functionality that corresponds to the
    ## 3-Space Sensor device type it is representing.
    print("==================================================")
    print("Getting the filtered tared quaternion orientation.")
    quat = device.getTaredOrientationAsQuaternion()
    if quat is not None:
        print(quat)
    print("==================================================")
    print("Getting the raw sensor data.")
    data = device.getAllRawComponentSensorData()
    gyro = data[:3]
    print "gyro:", gyro
    if data is not None:
        print data
        print(
            "[%f, %f, %f] --Gyro\n"
            "[%f, %f, %f] --Accel\n"
            "[%f, %f, %f] --Comp" % data)

    i = 0
    while 1:
        i += 1
        t = time.time()
        dt = t - t0
        if maxTime and dt > maxTime:
            break
        angles = device.getTaredOrientationAsEulerAngles()
        #print i, angles
        angles = map(math.degrees, angles)
        pitch, roll, yaw = angles
        print "%4d %7.1f %7.1f %7.1f" % (i, yaw, pitch, roll)
        if angleCallback != None:
            angleCallback(angles)

    ## Now close the port.
    device.close()
## (Friendly name, 3-Space Type, 3-Space ID, 3-Space Firmware Version String,
##  3-Space Hardware Version String, is in bootloader mode)
for device_port in device_list:
    com_port, friendly_name, device_type = device_port
    if device_type == "USB":
        device = ts_api.TSUSBSensor(com_port=com_port)
    elif device_type == "DNG":
        device = ts_api.TSDongle(com_port=com_port)
    elif device_type == "WL":
        device = ts_api.TSWLSensor(com_port=com_port)
    elif device_type == "EM":
        device = ts_api.TSEMSensor(com_port=com_port)
    elif device_type == "DL":
        device = ts_api.TSDLSensor(com_port=com_port)
    elif device_type == "BT":
        device = ts_api.TSBTSensor(com_port=com_port)
    elif device_type == "BTL":
        device = "Device on " + com_port + " is in bootloader mode."
    elif device_type == "???":
        port_info = ts_api.getDeviceInfoFromComPort(com_port, poll_device=True)
        device_type = port_info[1]
        is_bootloader = port_info[5]
        if is_bootloader:
            device = "Device on %s is in bootloader mode." % com_port
        elif device_type == "USB":
            device = ts_api.TSUSBSensor(com_port=com_port)
        elif device_type == "EM":
            device = ts_api.TSEMSensor(com_port=com_port)
        ## None of the COM ports in the list were 3-Space Sensor devices
        else:
            device = "Device on %s is not a 3-Space Sensor device." % com_port