Esempio n. 1
0
def accel_calibrate_reference():
    """run accelcal on reference board"""
    logger.info("STARTING REFERENCE ACCEL CALIBRATION")

    conn = connection.Connection(ref_only=True)

    logger.info("Turning safety off")
    rotate.set_rotation(conn, "level", wait=False)
    util.safety_off(conn.refmav)

    conn.ref.send("accelcal\n")
    for rotation in ["level", "left", "right", "up", "down", "back"]:
        try:
            conn.ref.expect("Place vehicle")
            conn.ref.expect("and press any key")
        except Exception as ex:
            util.failure("Failed to get place vehicle message for %s" % rotation)
        logger.debug("Rotating %s" % rotation)
        attitude = rotate.set_rotation(conn, rotation, wait=False)
        time.sleep(13)
        conn.ref.send("\n")
    i = conn.ref.expect(["Calibration successful", "Calibration FAILED"])
    if i != 0:
        util.failure("Accel calibration failed at %s" % time.ctime())
    logger.info("Calibration successful")
    rotate.set_rotation(conn, "level", wait=False)
    util.param_set(conn.ref, "AHRS_TRIM_X", 0)
    util.param_set(conn.ref, "AHRS_TRIM_Y", 0)
    util.discard_messages(conn.refmav)
    util.wait_heartbeat(conn.refmav)
Esempio n. 2
0
def accel_calibrate_reference():
    '''run accelcal on reference board'''
    logger.info("STARTING REFERENCE ACCEL CALIBRATION")

    conn = connection.Connection(ref_only=True)

    logger.info("Turning safety off")
    rotate.set_rotation(conn, 'level', wait=False)
    util.safety_off(conn.refmav)

    conn.ref.send("accelcal\n")
    for rotation in ['level', 'left', 'right', 'up', 'down', 'back']:
        try:
            conn.ref.expect("Place vehicle")
            conn.ref.expect("and press any key")
        except Exception as ex:
            util.failure("Failed to get place vehicle message for %s" %
                         rotation)
        logger.debug("Rotating %s" % rotation)
        attitude = rotate.set_rotation(conn, rotation, wait=False)
        time.sleep(13)
        conn.ref.send("\n")
    i = conn.ref.expect(["Calibration successful", "Calibration FAILED"])
    if i != 0:
        util.failure("Accel calibration failed at %s" % time.ctime())
    logger.info("Calibration successful")
    rotate.set_rotation(conn, 'level', wait=False)
    util.param_set(conn.ref, 'AHRS_TRIM_X', 0)
    util.param_set(conn.ref, 'AHRS_TRIM_Y', 0)
    util.discard_messages(conn.refmav)
    util.wait_heartbeat(conn.refmav)
Esempio n. 3
0
def accel_calibrate_run(conn):
    '''run accelcal'''
    logger.info("STARTING ACCEL CALIBRATION")
    wait_gyros(conn)
    time.sleep(2)
    if ETE == 0:
        logger.debug("re-running gyro cal")
        conn.discard_messages()
        conn.ref.send('gyrocal\n')
        conn.test.send('gyrocal\n')
        conn.ref.expect('Calibrated')
        conn.test.expect('Calibrated')
        wait_gyros(conn)
        rotate.set_rotation(conn, 'level', wait=False)

    logger.info("Turning safety off")
    util.safety_off(conn.refmav)
    logger.info("Turning on testjig mode")
    conn.test.send("factory_test start\n")

    # use zero trims on reference board
    util.param_set(conn.ref, 'AHRS_TRIM_X', 0)
    util.param_set(conn.ref, 'AHRS_TRIM_Y', 0)

    level_attitude = None
    conn.test.send("accelcal\n")
    for rotation in ['level', 'left', 'right', 'up', 'down', 'back']:
        try:
            conn.test.expect("Place vehicle")
            conn.test.expect("and press any key")
        except Exception as ex:
            util.show_tail(conn.testlog)
            util.failure("Failed to get place vehicle message for %s" %
                         rotation)
        attitude = rotate.set_rotation(conn, rotation)
        if rotation == 'level':
            level_attitude = attitude
        conn.test.send("\n")
    i = conn.test.expect(["Calibration successful", "Calibration FAILED"])
    if i != 0:
        logger.error(conn.test.before)
        logger.error("Calibration FAILED")
        util.show_tail(conn.testlog)
        util.failure("Accel calibration failed")
    #logger.info(conn.test.before)
    logger.info("Calibration successful")

    rotate.write_calibration()

    if ETE == 0:
        rotate.set_rotation(conn, 'level', wait=False)

    # get AHRS_TRIM_{X,Y} right
    adjust_ahrs_trim(conn, level_attitude)

    # rotate while integrating gyros to test all gyros are working
    rotate.gyro_integrate(conn)

    # finish in level position
    rotate.set_rotation(conn, 'level')
Esempio n. 4
0
def accel_calibrate_run(conn):
    '''run accelcal'''
    logger.info("STARTING ACCEL CALIBRATION")
    wait_gyros(conn)
    time.sleep(2)
    if ETE == 0:
        logger.debug("re-running gyro cal")
        conn.discard_messages()
        conn.ref.send('gyrocal\n')
        conn.test.send('gyrocal\n')
        conn.ref.expect('Calibrated')
        conn.test.expect('Calibrated')
        wait_gyros(conn)
        rotate.set_rotation(conn, 'level', wait=False)

    logger.info("Turning safety off")
    util.safety_off(conn.refmav)
    logger.info("Turning on testjig mode")
    conn.test.send("factory_test start\n")

    # use zero trims on reference board
    util.param_set(conn.ref, 'AHRS_TRIM_X', 0)
    util.param_set(conn.ref, 'AHRS_TRIM_Y', 0)

    level_attitude = None
    conn.test.send("accelcal\n")
    for rotation in ['level', 'left', 'right', 'up', 'down', 'back']:
        try:
            conn.test.expect("Place vehicle")
            conn.test.expect("and press any key")
        except Exception as ex:
            util.show_tail(conn.testlog)
            util.failure("Failed to get place vehicle message for %s" % rotation)
        attitude = rotate.set_rotation(conn, rotation)
        if rotation == 'level':
            level_attitude = attitude
        conn.test.send("\n")
    i = conn.test.expect(["Calibration successful","Calibration FAILED"])
    if i != 0:
        logger.error(conn.test.before)
        logger.error("Calibration FAILED")
        util.show_tail(conn.testlog)
        util.failure("Accel calibration failed")
    #logger.info(conn.test.before)
    logger.info("Calibration successful")
    
    rotate.write_calibration()

    if ETE == 0:
        rotate.set_rotation(conn, 'level', wait=False)

    # get AHRS_TRIM_{X,Y} right
    adjust_ahrs_trim(conn, level_attitude)

    # rotate while integrating gyros to test all gyros are working
    rotate.gyro_integrate(conn)

    # finish in level position
    rotate.set_rotation(conn, 'level')
Esempio n. 5
0
def accel_calibrate_run(conn):
    '''run accelcal'''
    logger.info("STARTING ACCEL CALIBRATION")

    wait_gyros(conn)

    logger.debug("running ref gyro cal")
    conn.ref.send('gyrocal\n')
    conn.ref.expect('Calibrated')
    wait_gyros(conn)

    logger.info("Turning safety off")
    rotate.set_rotation(conn, 'level', wait=False)
    util.safety_off(conn.refmav)

    # use zero trims on reference board
    util.param_set(conn.ref, 'AHRS_TRIM_X', 0)
    util.param_set(conn.ref, 'AHRS_TRIM_Y', 0)

    level_attitude = None
    conn.test.send("accelcal\n")
    for rotation in ['level', 'left', 'right', 'up', 'down', 'back']:
        try:
            conn.test.expect("Place vehicle")
            conn.test.expect("and press any key")
        except Exception as ex:
            util.show_tail(conn.testlog)
            util.failure("Failed to get place vehicle message for %s" % rotation)
        attitude = rotate.set_rotation(conn, rotation)
        if rotation == 'level':
            level_attitude = attitude
        conn.test.send("\n")
    i = conn.test.expect(["Calibration successful","Calibration FAILED"])
    if i != 0:
        logger.error(conn.test.before)
        logger.error("Calibration FAILED")
        util.show_tail(conn.testlog)
        util.failure("Accel calibration failed at %s" % time.ctime())
    #logger.info(conn.test.before)
    logger.info("Calibration successful")
    rotate.write_calibration()
    rotate.set_rotation(conn, 'level', wait=False)
    adjust_ahrs_trim(conn, level_attitude)
Esempio n. 6
0
def accel_calibrate_run(conn):
    """run accelcal"""
    logger.info("STARTING ACCEL CALIBRATION")

    wait_gyros(conn)
    time.sleep(2)

    logger.debug("re-running gyro cal")
    conn.discard_messages()
    conn.ref.send("gyrocal\n")
    conn.test.send("gyrocal\n")
    conn.ref.expect("Calibrated")
    conn.test.expect("Calibrated")
    wait_gyros(conn)

    logger.info("Turning safety off")
    rotate.set_rotation(conn, "level", wait=False)
    util.safety_off(conn.refmav)

    # use zero trims on reference board
    util.param_set(conn.ref, "AHRS_TRIM_X", 0)
    util.param_set(conn.ref, "AHRS_TRIM_Y", 0)

    level_attitude = None
    conn.test.send("accelcal\n")
    for rotation in ["level", "left", "right", "up", "down", "back"]:
        try:
            conn.test.expect("Place vehicle")
            conn.test.expect("and press any key")
        except Exception as ex:
            util.show_tail(conn.testlog)
            util.failure("Failed to get place vehicle message for %s" % rotation)
        attitude = rotate.set_rotation(conn, rotation)
        if rotation == "level":
            level_attitude = attitude
        conn.test.send("\n")
    i = conn.test.expect(["Calibration successful", "Calibration FAILED"])
    if i != 0:
        logger.error(conn.test.before)
        logger.error("Calibration FAILED")
        util.show_tail(conn.testlog)
        util.failure("Accel calibration failed")
    # logger.info(conn.test.before)
    logger.info("Calibration successful")
    rotate.write_calibration()
    rotate.set_rotation(conn, "level", wait=False)

    # rotate while integrating gyros to test all gyros are working
    rotate.gyro_integrate(conn)

    # get AHRS_TRIM_{X,Y} right
    adjust_ahrs_trim(conn, level_attitude)

    # finish in level position
    rotate.set_rotation(conn, "level", quick=True)
Esempio n. 7
0
def adjust_ahrs_trim(conn, level_attitude):
    """
    force the AHRS trim to zero, which removes the effect of the jig not being quite
    level. This is only incorrect if the accels are not aligned on the board, which
    we check in check_accel_cal later

    As a side effect this function set AHRS_ORIENTATION for the test board to 12
    """

    # start with board right way up
    rotate.set_rotation(conn, "level")

    # we need to work out what the error in attitude of the 3 IMUs on the test jig is
    # to do that we start with it level, and measure the roll/pitch as compared to the reference
    conn.discard_messages()
    ref_imu = conn.refmav.recv_match(type="RAW_IMU", blocking=True, timeout=3)
    test_imu1 = conn.testmav.recv_match(type="RAW_IMU", blocking=True, timeout=3)
    test_imu2 = conn.testmav.recv_match(type="SCALED_IMU2", blocking=True, timeout=3)
    test_imu3 = conn.testmav.recv_match(type="SCALED_IMU3", blocking=True, timeout=3)
    if ref_imu is None:
        util.failure("Lost comms to reference board in ahrs trim")
    if test_imu1 is None or test_imu2 is None or test_imu3 is None:
        util.failure("Lost comms to test board in ahrs trim")

    (ref_roll, ref_pitch) = util.attitude_estimate(ref_imu)
    (test_roll1, test_pitch1) = util.attitude_estimate(test_imu1)
    (test_roll2, test_pitch2) = util.attitude_estimate(test_imu2)
    (test_roll3, test_pitch3) = util.attitude_estimate(test_imu3)

    # get the roll and pitch errors
    roll_error1 = test_roll1 - ref_roll
    roll_error2 = test_roll2 - ref_roll
    roll_error3 = test_roll3 - ref_roll
    pitch_error1 = test_pitch1 - ref_pitch
    pitch_error2 = test_pitch2 - ref_pitch
    pitch_error3 = test_pitch3 - ref_pitch

    conn.discard_messages()
    ref_imu = conn.refmav.recv_match(type="RAW_IMU", blocking=True, timeout=3)
    test_imu1 = conn.testmav.recv_match(type="RAW_IMU", blocking=True, timeout=3)
    test_imu2 = conn.testmav.recv_match(type="SCALED_IMU2", blocking=True, timeout=3)
    test_imu3 = conn.testmav.recv_match(type="SCALED_IMU3", blocking=True, timeout=3)
    if ref_imu is None:
        util.failure("Lost comms to reference board in ahrs trim")
    if test_imu1 is None or test_imu2 is None or test_imu3 is None:
        util.failure("Lost comms to test board in ahrs trim")

    logger.debug(
        "Tilt Ref=(%.1f %.1f) Test1=(%.1f %.1f) Test2=(%.1f %.1f) Test3=(%.1f %.1f)"
        % (ref_roll, ref_pitch, test_roll1, test_pitch1, test_roll2, test_pitch2, test_roll3, test_pitch3)
    )

    if abs(ref_roll) > ROTATION_TOLERANCE or abs(ref_pitch) > ROTATION_TOLERANCE:
        util.failure("Reference board rotation error")

    logger.debug(
        "Tilt offsets: Roll(%.1f %.1f %.1f) Pitch(%.1f %.1f %.1f) "
        % (roll_error1, roll_error2, roll_error3, pitch_error1, pitch_error2, pitch_error3)
    )

    if abs(roll_error1) > TILT_TOLERANCE1 or abs(roll_error2) > TILT_TOLERANCE1 or abs(roll_error3) > TILT_TOLERANCE3:
        util.failure("Test board roll error")

    if (
        abs(pitch_error1) > TILT_TOLERANCE1
        or abs(pitch_error2) > TILT_TOLERANCE1
        or abs(pitch_error3) > TILT_TOLERANCE3
    ):
        util.failure("Test board pitch error")

    # flip upside down for the trim calculation
    rotate.set_rotation(conn, "back")

    # set orientation upside down for trim measurement
    util.param_set(conn.test, "AHRS_ORIENTATION", 12)

    # sleep an extra four seconds - we need to be very sure the board is still for trim
    time.sleep(4)
    conn.discard_messages()

    # average over 30 samples for trim
    num_samples = 30
    test_roll = [0] * 3
    test_pitch = [0] * 3
    msgs = ["RAW_IMU", "SCALED_IMU2", "SCALED_IMU3"]

    for i in range(num_samples):
        for j in range(3):
            test_imu = conn.testmav.recv_match(type=msgs[j], blocking=True, timeout=3)
            if test_imu is None:
                util.failure("Lost comms to test board in ahrs trim")
            (roll, pitch) = util.attitude_estimate(test_imu)
            test_roll[j] += roll
            test_pitch[j] += pitch

    for j in range(3):
        test_roll[j] /= num_samples
        test_pitch[j] /= num_samples

    logger.debug(
        "Average Trim tilt Test1=(%.1f %.1f) Test2=(%.1f %.1f) Test3=(%.1f %.1f)"
        % (test_roll[0], test_pitch[0], test_roll[1], test_pitch[1], test_roll[2], test_pitch[2])
    )

    # setting a positive trim value reduces the attitude that is
    # read. So setting a trim of 0.1 when level results in a attitude
    # reading of -5.8 degrees

    # this approach assumes the mpu6000 on the FMU (IMU3) is level
    # with respect to the board, and that any attitude error is due to
    # the isolation board mount. We use the average of the error from
    # IMU1 and IMU2
    trim_x = radians((test_roll[0] + test_roll[1]) * 0.5 - test_roll[2])
    trim_y = radians((test_pitch[0] + test_pitch[1]) * 0.5 - test_pitch[2])

    util.param_set(conn.test, "AHRS_TRIM_X", trim_x)
    time.sleep(0.2)
    util.param_set(conn.test, "AHRS_TRIM_Y", trim_y)
    time.sleep(0.2)
    logger.debug("Set trims AHRS_TRIM_X=%.4f AHRS_TRIM_Y=%.4f" % (trim_x, trim_y))
Esempio n. 8
0
    def __init__(self, ref_only=False):
        util.kill_processes(['mavproxy.py', GDB])
        
        self.reflog = StringIO()
        self.testlog = StringIO()
        self.ref = None
        self.test = None
        self.nsh = None
        self.refmav = None
        self.testmav = None
        
        try:
            if not ref_only:
                self.nsh = nsh_console.nsh_console()
        except Exception as ex:
            self.close()
            util.show_error('Connecting to nsh console', ex, self.testlog)

        try:
            self.ref = mav_reference.mav_reference(self.reflog)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to reference board1', ex, self.reflog)

        try:
            if not ref_only:
                self.test = mav_test.mav_test(self.testlog)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to test board1', ex, self.testlog)

        try:
            logger.info("CONNECTING MAVLINK TO REFERENCE BOARD")
            self.refmav = mavutil.mavlink_connection('127.0.0.1:14550')
            util.wait_heartbeat(self.refmav, timeout=30)
            util.wait_mode(self.refmav, IDLE_MODES)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to reference board2', ex, self.reflog)

        try:
            if not ref_only:
                logger.info("CONNECTING MAVLINK TO TEST BOARD at %s" % time.ctime())
                self.testmav = mavutil.mavlink_connection('127.0.0.1:14551')
                util.wait_heartbeat(self.testmav, timeout=30)
                logger.info("got heartbeat at %s" % time.ctime())
                util.wait_mode(self.testmav, IDLE_MODES)
                logger.info("Waiting for 'Ready to FLY'")
                self.test.expect('Ready to FLY', timeout=20)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to test board2 at %s' % time.ctime(), ex, self.testlog)

        if self.nsh is not None:
            # log any extra nsh data
            logger.debug("Draining nsh")
            try:
                self.nsh.read_nonblocking(4096, 1)
            except Exception as ex:
                pass

        try:
            if not ref_only and not ref_gyro_offset_ok(self.refmav):
                self.close()
                util.failure("Bad reference gyro - FAILED")
        except Exception as ex:
            self.close()
            util.show_error('testing reference gyros', ex)

        logger.info("Setting rotation level")
        try:
            rotate.set_rotation(self, 'level', wait=False)
        except Exception as ex:
            self.close()
            util.show_error("unable to set safety off", ex)
Esempio n. 9
0
    def __init__(self, ref_only=False):
        util.kill_processes(['mavproxy.py', GDB])

        self.reflog = StringIO()
        self.testlog = StringIO()
        self.ref = None
        self.test = None
        self.nsh = None
        self.refmav = None
        self.testmav = None

        try:
            if not ref_only:
                self.nsh = nsh_console.nsh_console()
        except Exception as ex:
            self.close()
            util.show_error('Connecting to nsh console', ex, self.testlog)

        try:
            self.ref = mav_reference.mav_reference(self.reflog)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to reference board1', ex, self.reflog)

        try:
            if not ref_only:
                self.test = mav_test.mav_test(self.testlog)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to test board1', ex, self.testlog)

        try:
            logger.info("CONNECTING MAVLINK TO REFERENCE BOARD")
            self.refmav = mavutil.mavlink_connection('127.0.0.1:14550')
            util.wait_heartbeat(self.refmav, timeout=30)
            util.wait_mode(self.refmav, IDLE_MODES)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to reference board2', ex, self.reflog)

        try:
            if not ref_only:
                logger.info("CONNECTING MAVLINK TO TEST BOARD")
                self.testmav = mavutil.mavlink_connection('127.0.0.1:14551')
                util.wait_heartbeat(self.testmav, timeout=30)
                logger.info("got heartbeat")
                util.wait_mode(self.testmav, IDLE_MODES)
                logger.info("Waiting for 'Ready to FLY'")
                self.fw_version = None
                self.px4_version = None
                self.nuttx_version = None
                self.stm32_serial = None
                ready = False
                self.test.send("param fetch\n")
                # log version information for later reference
                while (self.fw_version is None or self.px4_version is None
                       or self.nuttx_version is None
                       or self.stm32_serial is None or not ready):
                    i = self.test.expect([
                        'APM: ([^\r\n]*Copter[^\r\n]*)\r\n',
                        'APM: PX4: ([0-9a-f]+) NuttX: ([0-9a-f]+)\r\n',
                        'APM: PX4v2 ([0-9A-F]+ [0-9A-F]+ [0-9A-F]+)\r\n',
                        '(Ready to FLY)'
                    ],
                                         timeout=20)
                    if i == 3:
                        ready = True
                    elif i == 0:
                        self.fw_version = self.test.match.group(1)
                    elif i == 1:
                        self.px4_version = self.test.match.group(1)
                        self.nuttx_version = self.test.match.group(2)
                    elif i == 2:
                        self.stm32_serial = self.test.match.group(1)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to test board2', ex, self.testlog)

        if self.nsh is not None:
            # log any extra nsh data
            logger.debug("Draining nsh")
            try:
                self.nsh.read_nonblocking(4096, 1)
            except Exception as ex:
                pass

        try:
            if not ref_only and not ref_gyro_offset_ok(self.refmav):
                self.close()
                util.failure("Bad reference gyro - FAILED")
        except Exception as ex:
            self.close()
            util.show_error('testing reference gyros', ex)

        logger.info("Setting rotation level")
        try:
            rotate.set_rotation(self, 'level', wait=False)
        except Exception as ex:
            self.close()
            util.show_error("unable to set safety off", ex)
Esempio n. 10
0
    def __init__(self, ref_only=False):
        util.kill_processes(['mavproxy.py', GDB])
        
        self.reflog = StringIO()
        self.testlog = StringIO()
        self.ref = None
        self.test = None
        self.nsh = None
        self.refmav = None
        self.testmav = None
        
        try:
            if not ref_only:
                self.nsh = nsh_console.nsh_console()
        except Exception as ex:
            self.close()
            util.show_error('Connecting to nsh console', ex, self.testlog)

        try:
            self.ref = mav_reference.mav_reference(self.reflog)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to reference board1', ex, self.reflog)

        try:
            if not ref_only:
                self.test = mav_test.mav_test(self.testlog)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to test board1', ex, self.testlog)

        try:
            logger.info("CONNECTING MAVLINK TO REFERENCE BOARD")
            self.refmav = mavutil.mavlink_connection('127.0.0.1:14550')
            util.wait_heartbeat(self.refmav, timeout=30)
            util.wait_mode(self.refmav, IDLE_MODES)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to reference board2', ex, self.reflog)

        try:
            if not ref_only:
                logger.info("CONNECTING MAVLINK TO TEST BOARD")
                self.testmav = mavutil.mavlink_connection('127.0.0.1:14551')
                util.wait_heartbeat(self.testmav, timeout=30)
                logger.info("got heartbeat")
                util.wait_mode(self.testmav, IDLE_MODES)
                logger.info("Waiting for 'Ready to FLY'")
                self.fw_version = None
                self.px4_version = None
                self.nuttx_version = None
                self.stm32_serial = None
                ready = False
                self.test.send("param fetch\n")
                # log version information for later reference
                while (self.fw_version is None or
                       self.px4_version is None or
                       self.nuttx_version is None or
                       self.stm32_serial is None or
                       not ready):
                    i = self.test.expect(['APM: ([^\r\n]*Copter[^\r\n]*)\r\n',
                                          'APM: PX4: ([0-9a-f]+) NuttX: ([0-9a-f]+)\r\n',
                                          'APM: PX4v2 ([0-9A-F]+ [0-9A-F]+ [0-9A-F]+)\r\n',
                                          '(Ready to FLY)'],
                                         timeout=20)
                    if i == 3:
                        ready = True
                    elif i == 0:
                        self.fw_version = self.test.match.group(1)
                    elif i == 1:
                        self.px4_version = self.test.match.group(1)
                        self.nuttx_version = self.test.match.group(2)
                    elif i == 2:
                        self.stm32_serial = self.test.match.group(1)
        except Exception as ex:
            self.close()
            util.show_error('Connecting to test board2', ex, self.testlog)

        if self.nsh is not None:
            # log any extra nsh data
            logger.debug("Draining nsh")
            try:
                self.nsh.read_nonblocking(4096, 1)
            except Exception as ex:
                pass

        try:
            if not ref_only and not ref_gyro_offset_ok(self.refmav):
                self.close()
                util.failure("Bad reference gyro - FAILED")
        except Exception as ex:
            self.close()
            util.show_error('testing reference gyros', ex)

        logger.info("Setting rotation level")
        try:
            rotate.set_rotation(self, 'level', wait=False)
        except Exception as ex:
            self.close()
            util.show_error("unable to set safety off", ex)
Esempio n. 11
0
def adjust_ahrs_trim(conn, level_attitude):
    '''
    force the AHRS trim to zero, which removes the effect of the jig not being quite
    level. This is only incorrect if the accels are not aligned on the board, which
    we check in check_accel_cal later

    As a side effect this function set AHRS_ORIENTATION for the test board to 12
    '''

    # start with board right way up
    if ETE == 0:
        rotate.set_rotation(conn, 'level')

    # we need to work out what the error in attitude of the 3 IMUs on the test jig is
    # to do that we start with it level, and measure the roll/pitch as compared to the reference
    conn.discard_messages()
    ref_imu = conn.refmav.recv_match(type='RAW_IMU', blocking=True, timeout=3)
    test_imu1 = conn.testmav.recv_match(type='RAW_IMU',
                                        blocking=True,
                                        timeout=3)
    test_imu2 = conn.testmav.recv_match(type='SCALED_IMU2',
                                        blocking=True,
                                        timeout=3)
    test_imu3 = conn.testmav.recv_match(type='SCALED_IMU3',
                                        blocking=True,
                                        timeout=3)
    if ref_imu is None:
        util.failure("Lost comms to reference board in ahrs trim")
    if test_imu1 is None or test_imu2 is None or test_imu3 is None:
        util.failure("Lost comms to test board in ahrs trim")

    (ref_roll, ref_pitch) = util.attitude_estimate(ref_imu)
    (test_roll1, test_pitch1) = util.attitude_estimate(test_imu1)
    (test_roll2, test_pitch2) = util.attitude_estimate(test_imu2)
    (test_roll3, test_pitch3) = util.attitude_estimate(test_imu3)

    # get the roll and pitch errors
    roll_error1 = (test_roll1 - ref_roll)
    roll_error2 = (test_roll2 - ref_roll)
    roll_error3 = (test_roll3 - ref_roll)
    pitch_error1 = (test_pitch1 - ref_pitch)
    pitch_error2 = (test_pitch2 - ref_pitch)
    pitch_error3 = (test_pitch3 - ref_pitch)

    conn.discard_messages()
    ref_imu = conn.refmav.recv_match(type='RAW_IMU', blocking=True, timeout=3)
    test_imu1 = conn.testmav.recv_match(type='RAW_IMU',
                                        blocking=True,
                                        timeout=3)
    test_imu2 = conn.testmav.recv_match(type='SCALED_IMU2',
                                        blocking=True,
                                        timeout=3)
    test_imu3 = conn.testmav.recv_match(type='SCALED_IMU3',
                                        blocking=True,
                                        timeout=3)
    if ref_imu is None:
        util.failure("Lost comms to reference board in ahrs trim")
    if test_imu1 is None or test_imu2 is None or test_imu3 is None:
        util.failure("Lost comms to test board in ahrs trim")

    logger.debug(
        "Tilt Ref=(%.1f %.1f) Test1=(%.1f %.1f) Test2=(%.1f %.1f) Test3=(%.1f %.1f)"
        % (ref_roll, ref_pitch, test_roll1, test_pitch1, test_roll2,
           test_pitch2, test_roll3, test_pitch3))

    if (abs(ref_roll) > ROTATION_TOLERANCE
            or abs(ref_pitch) > ROTATION_TOLERANCE):
        util.failure("Reference board rotation error")

    logger.debug("Tilt offsets: Roll(%.1f %.1f %.1f) Pitch(%.1f %.1f %.1f) " %
                 (roll_error1, roll_error2, roll_error3, pitch_error1,
                  pitch_error2, pitch_error3))

    if (abs(roll_error1) > TILT_TOLERANCE1
            or abs(roll_error2) > TILT_TOLERANCE1
            or abs(roll_error3) > TILT_TOLERANCE3):
        util.failure("Test board roll error")

    if (abs(pitch_error1) > TILT_TOLERANCE1
            or abs(pitch_error2) > TILT_TOLERANCE1
            or abs(pitch_error3) > TILT_TOLERANCE3):
        util.failure("Test board pitch error")

    # flip upside down for the trim calculation
    if ETE == 0:
        rotate.set_rotation(conn, 'back')

    # set orientation upside down for trim measurement
    util.param_set(conn.test, 'AHRS_ORIENTATION', 12)
    util.param_set(conn.ref, 'AHRS_ORIENTATION', 12)

    # sleep an extra four seconds - we need to be very sure the board is still for trim
    time.sleep(4)
    conn.discard_messages()

    # average over 30 samples for trim
    num_samples = 30
    test_roll = [0] * 3
    test_pitch = [0] * 3
    msgs = ['RAW_IMU', 'SCALED_IMU2', 'SCALED_IMU3']

    for i in range(num_samples):
        for j in range(3):
            test_imu = conn.testmav.recv_match(type=msgs[j],
                                               blocking=True,
                                               timeout=3)
            if test_imu is None:
                util.failure("Lost comms to test board in ahrs trim")
            (roll, pitch) = util.attitude_estimate(test_imu)
            test_roll[j] += roll
            test_pitch[j] += pitch

    for j in range(3):
        test_roll[j] /= num_samples
        test_pitch[j] /= num_samples

    logger.debug(
        "Average Trim tilt Test1=(%.1f %.1f) Test2=(%.1f %.1f) Test3=(%.1f %.1f)"
        % (test_roll[0], test_pitch[0], test_roll[1], test_pitch[1],
           test_roll[2], test_pitch[2]))

    # setting a positive trim value reduces the attitude that is
    # read. So setting a trim of 0.1 when level results in a attitude
    # reading of -5.8 degrees

    # this approach assumes the mpu6000 on the FMU (IMU3) is level
    # with respect to the board, and that any attitude error is due to
    # the isolation board mount. We use the average of the error from
    # IMU1 and IMU2
    trim_x = radians((test_roll[0] + test_roll[1]) * 0.5 - test_roll[2])
    trim_y = radians((test_pitch[0] + test_pitch[1]) * 0.5 - test_pitch[2])

    util.param_set(conn.test, 'AHRS_TRIM_X', trim_x)
    time.sleep(0.2)
    util.param_set(conn.test, 'AHRS_TRIM_Y', trim_y)
    time.sleep(0.2)
    logger.debug("Set trims AHRS_TRIM_X=%.4f AHRS_TRIM_Y=%.4f" %
                 (trim_x, trim_y))
    util.param_set(conn.ref, 'AHRS_ORIENTATION', 0)
Esempio n. 12
0
def adjust_ahrs_trim(conn, level_attitude):
    '''
    force the AHRS trim to zero, which removes the effect of the jig not being quite
    level. This is only incorrect if the accels are not aligned on the board, which
    we check in check_accel_cal later
    '''
    util.param_set(conn.test, 'AHRS_TRIM_X', 0)
    util.param_set(conn.test, 'AHRS_TRIM_Y', 0)

    # get the board level
    rotate.set_rotation(conn, 'level')

    # be really sure it has stopped moving. This next measurement is critical
    time.sleep(2)

    # we need to work out what the error in attitude of the 3 IMUs on the test jig is
    # to do that we start with it level, and measure the roll/pitch as compared to the reference
    # then we rotate it to pitch 90 and measure the yaw error

    # check all accels are in range
    conn.discard_messages()
    ref_imu = conn.refmav.recv_match(type='RAW_IMU', blocking=True, timeout=3)
    test_imu1 = conn.testmav.recv_match(type='RAW_IMU', blocking=True, timeout=3)
    test_imu2 = conn.testmav.recv_match(type='SCALED_IMU2', blocking=True, timeout=3)
    test_imu3 = conn.testmav.recv_match(type='SCALED_IMU3', blocking=True, timeout=3)
    if ref_imu is None:
        util.failure("Lost comms to reference board in ahrs trim")
    if test_imu1 is None or test_imu2 is None or test_imu3 is None:
        util.failure("Lost comms to test board in ahrs trim")
    ref_accel = Vector3(ref_imu.xacc, ref_imu.yacc, ref_imu.zacc)*9.81*0.001

    (ref_roll, ref_pitch) = util.attitude_estimate(ref_imu)
    (test_roll1, test_pitch1) = util.attitude_estimate(test_imu1)
    (test_roll2, test_pitch2) = util.attitude_estimate(test_imu2)
    (test_roll3, test_pitch3) = util.attitude_estimate(test_imu3)

    # get the roll and pitch errors
    roll_error1 = (test_roll1 - ref_roll)
    roll_error2 = (test_roll2 - ref_roll)
    roll_error3 = (test_roll3 - ref_roll)
    pitch_error1 = (test_pitch1 - ref_pitch)
    pitch_error2 = (test_pitch2 - ref_pitch)
    pitch_error3 = (test_pitch3 - ref_pitch)

    # rotate to up position while integrating gyros to test
    # all gyros are working
    rotate.gyro_integrate(conn)

    # finish rotation to pitch 90 to measure the yaw error
    rotate.set_rotation(conn, 'up')

    conn.discard_messages()
    ref_imu = conn.refmav.recv_match(type='RAW_IMU', blocking=True, timeout=3)
    test_imu1 = conn.testmav.recv_match(type='RAW_IMU', blocking=True, timeout=3)
    test_imu2 = conn.testmav.recv_match(type='SCALED_IMU2', blocking=True, timeout=3)
    test_imu3 = conn.testmav.recv_match(type='SCALED_IMU3', blocking=True, timeout=3)
    if ref_imu is None:
        util.failure("Lost comms to reference board in ahrs trim")
    if test_imu1 is None or test_imu2 is None or test_imu3 is None:
        util.failure("Lost comms to test board in ahrs trim")
    ref_accel = Vector3(ref_imu.xacc, ref_imu.yacc, ref_imu.zacc)*9.81*0.001

    # start rotating back to level ready for the end of the test
    rotate.set_rotation(conn, 'level', wait=False)

    # when pointing straight up the roll_esimtate() actually estimates yaw error in body frame
    ref_yaw = util.roll_estimate(ref_imu)
    test_yaw1 = util.roll_estimate(test_imu1)
    test_yaw2 = util.roll_estimate(test_imu2)
    test_yaw3 = util.roll_estimate(test_imu3)
    yaw_error1 = test_yaw1 - ref_yaw
    yaw_error2 = test_yaw2 - ref_yaw
    yaw_error3 = test_yaw3 - ref_yaw

    logger.debug("Tilt Ref=(%.1f %.1f %.1f) Test1=(%.1f %.1f %.1f) Test2=(%.1f %.1f %.1f) Test3=(%.1f %.1f %.1f)" % (
        ref_roll, ref_pitch, ref_yaw,
        test_roll1, test_pitch1, test_yaw1,
        test_roll2, test_pitch2, test_yaw2,
        test_roll3, test_pitch3, test_yaw3))

    if (abs(ref_roll) > ROTATION_TOLERANCE or
        abs(ref_pitch) > ROTATION_TOLERANCE or
        abs(ref_yaw) > ROTATION_TOLERANCE):
        util.failure("Reference board rotation error")

    logger.debug("Tilt offsets: Roll(%.1f %.1f %.1f) Pitch(%.1f %.1f %.1f) Yaw(%.1f %.1f %.1f) " % (
        roll_error1, roll_error2, roll_error3,
        pitch_error1, pitch_error2, pitch_error3,
        yaw_error1, yaw_error2, yaw_error3))

    if (abs(roll_error1) > TILT_TOLERANCE1 or
        abs(roll_error2) > TILT_TOLERANCE1 or
        abs(roll_error3) > TILT_TOLERANCE3):
        util.failure("Test board roll error")

    if (abs(pitch_error1) > TILT_TOLERANCE1 or
        abs(pitch_error2) > TILT_TOLERANCE1 or
        abs(pitch_error3) > TILT_TOLERANCE3):
        util.failure("Test board pitch error")

    if (abs(yaw_error1) > TILT_TOLERANCE1 or
        abs(yaw_error2) > TILT_TOLERANCE1 or
        abs(yaw_error3) > TILT_TOLERANCE3):
        util.failure("Test board yaw error")

    # setting a positive trim value reduces the attitude that is
    # read. So setting a trim of 0.1 when level results in a attitude
    # reading of -5.8 degrees

    # this assumes the reference board always reads correct attitude
    # and that there is no attitude discrepance between test and
    # reference boards    
    trim_x = radians((roll_error1+roll_error2)/2)
    trim_y = radians((pitch_error1+pitch_error2)/2)
    logger.debug("OLD Set trims AHRS_TRIM_X=%.4f AHRS_TRIM_Y=%.4f" % (trim_x, trim_y))

    # this new approach assumes the mpu6000 on the FMU (IMU3) is level
    # with respect to the board, and that any attitude error is due to
    # the isolation board mount. We use the average of the error from
    # IMU1 and IMU2
    trim_x = radians((test_roll1+test_roll2)*0.5 - test_roll3)
    trim_y = radians((test_pitch1+test_pitch2)*0.5 - test_pitch3)

    util.param_set(conn.test, 'AHRS_TRIM_X', trim_x)
    time.sleep(0.2)
    util.param_set(conn.test, 'AHRS_TRIM_Y', trim_y)
    time.sleep(0.2)
    logger.debug("Set trims AHRS_TRIM_X=%.4f AHRS_TRIM_Y=%.4f" % (trim_x, trim_y))