def test_posctl(self):
        manIn = ManualInput()

        # arm and go into offboard
        manIn.arm()
        manIn.offboard()
        self.assertTrue(self.controlMode.flag_armed, "flag_armed is not set")
        self.assertTrue(self.controlMode.flag_control_offboard_enabled, "flag_control_offboard_enabled is not set")
        self.assertTrue(self.controlMode.flag_control_position_enabled, "flag_control_position_enabled is not set")

        # prepare flight path
        positions = (
            (0,0,0),
            (2,2,-2),
            (2,-2,-2),
            (-2,-2,-2),
            (2,2,-2))

        # flight path assertion
        self.fpa = FlightPathAssertion(positions, 1, 0)
        self.fpa.start()

        for i in range(0, len(positions)):
            self.reach_position(positions[i][0], positions[i][1], positions[i][2], 120)
            self.assertFalse(self.fpa.failed, "breached flight path tunnel (%d)" % i)
        
        # does it hold the position for Y seconds?
        positionHeld = True
        count = 0
        timeout = 50
        while(count < timeout):
            if(not self.is_at_position(2, 2, -2, 0.5)):
                positionHeld = False
                break
            count = count + 1
            self.rate.sleep()

        self.assertTrue(count == timeout, "position could not be held")
        self.fpa.stop()
class DirectOffboardPosctlTest(unittest.TestCase):

    def setUp(self):
        rospy.init_node('test_node', anonymous=True)
        rospy.Subscriber('iris/vehicle_control_mode', vehicle_control_mode, self.vehicle_control_mode_callback)
        rospy.Subscriber("iris/vehicle_local_position", vehicle_local_position, self.position_callback)
        self.pub_spt = rospy.Publisher('iris/position_setpoint_triplet', position_setpoint_triplet, queue_size=10)
        self.rate = rospy.Rate(10) # 10hz
        self.has_pos = False
        self.local_position = vehicle_local_position()
        self.control_mode = vehicle_control_mode()

    def tearDown(self):
        if self.fpa:
            self.fpa.stop()

    #
    # General callback functions used in tests
    #
    def position_callback(self, data):
        self.has_pos = True
        self.local_position = data

    def vehicle_control_mode_callback(self, data):
        self.control_mode = data


    #
    # Helper methods
    #
    def is_at_position(self, x, y, z, offset):
        rospy.logdebug("current position %f, %f, %f" % (self.local_position.x, self.local_position.y, self.local_position.z))
        desired = np.array((x, y, z))
        pos = np.array((self.local_position.x, self.local_position.y, self.local_position.z))
        return linalg.norm(desired - pos) < offset

    def reach_position(self, x, y, z, timeout):
        # set a position setpoint
        pos = position_setpoint()
        pos.valid = True
        pos.x = x
        pos.y = y
        pos.z = z
        pos.position_valid = True
        stp = position_setpoint_triplet()
        stp.current = pos
        self.pub_spt.publish(stp)

        # does it reach the position in X seconds?
        count = 0
        while count < timeout:
            if self.is_at_position(pos.x, pos.y, pos.z, 0.5):
                break
            count = count + 1
            self.rate.sleep()

        self.assertTrue(count < timeout, "took too long to get to position")

    #
    # Test offboard position control
    #
    def test_posctl(self):
        man_in = ManualInput()

        # arm and go into offboard
        man_in.arm()
        man_in.offboard()
        self.assertTrue(self.control_mode.flag_armed, "flag_armed is not set")
        self.assertTrue(self.control_mode.flag_control_offboard_enabled, "flag_control_offboard_enabled is not set")
        self.assertTrue(self.control_mode.flag_control_position_enabled, "flag_control_position_enabled is not set")

        # prepare flight path
        positions = (
            (0, 0, 0),
            (0, 0, -2),
            (2, 2, -2),
            (2, -2, -2),
            (-2, -2, -2),
            (2, 2, -2))

        # flight path assertion
        self.fpa = FlightPathAssertion(positions, 1, 0)
        self.fpa.start()

        for i in range(0, len(positions)):
            self.reach_position(positions[i][0], positions[i][1], positions[i][2], 120)
            self.assertFalse(self.fpa.failed, "breached flight path tunnel (%d)" % i)
        
        # does it hold the position for Y seconds?
        count = 0
        timeout = 50
        while count < timeout:
            if not self.is_at_position(2, 2, -2, 0.5):
                break
            count = count + 1
            self.rate.sleep()

        self.assertTrue(count == timeout, "position could not be held")
        self.fpa.stop()
示例#3
0
class DirectOffboardPosctlTest(unittest.TestCase):
    def setUp(self):
        rospy.init_node('test_node', anonymous=True)
        self.helper = PX4TestHelper("direct_offboard_posctl_test")
        self.helper.setUp()

        rospy.Subscriber('iris/vehicle_control_mode', vehicle_control_mode,
                         self.vehicle_control_mode_callback)
        self.sub_vlp = rospy.Subscriber("iris/vehicle_local_position",
                                        vehicle_local_position,
                                        self.position_callback)
        self.pub_spt = rospy.Publisher('iris/position_setpoint_triplet',
                                       position_setpoint_triplet,
                                       queue_size=10)
        self.rate = rospy.Rate(10)  # 10hz
        self.has_pos = False
        self.local_position = vehicle_local_position()
        self.control_mode = vehicle_control_mode()

    def tearDown(self):
        if self.fpa:
            self.fpa.stop()

        self.helper.tearDown()

    #
    # General callback functions used in tests
    #

    def position_callback(self, data):
        self.has_pos = True
        self.local_position = data

    def vehicle_control_mode_callback(self, data):
        self.control_mode = data

    #
    # Helper methods
    #
    def is_at_position(self, x, y, z, offset):
        rospy.logdebug("current position %f, %f, %f" %
                       (self.local_position.x, self.local_position.y,
                        self.local_position.z))
        desired = np.array((x, y, z))
        pos = np.array((self.local_position.x, self.local_position.y,
                        self.local_position.z))
        return linalg.norm(desired - pos) < offset

    def reach_position(self, x, y, z, timeout):
        # set a position setpoint
        pos = position_setpoint()
        pos.valid = True
        pos.x = x
        pos.y = y
        pos.z = z
        pos.position_valid = True
        stp = position_setpoint_triplet()
        stp.current = pos
        self.pub_spt.publish(stp)
        self.helper.bag_write('px4/position_setpoint_triplet', stp)

        # does it reach the position in X seconds?
        count = 0
        while count < timeout:
            if self.is_at_position(pos.x, pos.y, pos.z, 0.5):
                break
            count = count + 1
            self.rate.sleep()

        self.assertTrue(count < timeout, "took too long to get to position")

    #
    # Test offboard position control
    #
    def test_posctl(self):
        man_in = ManualInput()

        # arm and go into offboard
        man_in.arm()
        man_in.offboard()
        self.assertTrue(self.control_mode.flag_armed, "flag_armed is not set")
        self.assertTrue(self.control_mode.flag_control_offboard_enabled,
                        "flag_control_offboard_enabled is not set")
        self.assertTrue(self.control_mode.flag_control_position_enabled,
                        "flag_control_position_enabled is not set")

        # prepare flight path
        positions = ((0, 0, 0), (0, 0, -2), (2, 2, -2), (2, -2, -2),
                     (-2, -2, -2), (2, 2, -2))

        # flight path assertion
        self.fpa = FlightPathAssertion(positions, 1, 0)
        self.fpa.start()

        for i in range(0, len(positions)):
            self.reach_position(positions[i][0], positions[i][1],
                                positions[i][2], 120)
            self.assertFalse(self.fpa.failed,
                             "breached flight path tunnel (%d)" % i)

        # does it hold the position for Y seconds?
        count = 0
        timeout = 50
        while count < timeout:
            if not self.is_at_position(2, 2, -2, 0.5):
                break
            count = count + 1
            self.rate.sleep()

        self.assertTrue(count == timeout, "position could not be held")
        self.fpa.stop()