<commit_before>"""
A very simple autopilot designed to simplify testing of planes by providing an
existing framework for getting a plane in the air.
"""

from . import packets
from .io import Protocol
from .packets import units


class TakeOffMixin:
    _take_off_state = None
    _take_off_heading = None

    def take_off(self):
        self.take_off_started()

    def take_off_got_data_packet(self, packet, address):
        if self._take_off_state is None:
            return False

        if self._take_off_state == 'started':
            _, _, true_heading, _ = packet.read_pitch_roll_headings()
            self._take_off_heading = true_heading
            print('Landing strip heading is:', true_heading)
            self._take_off_throttle()
            self._take_off_state = 'throttle'
        elif self._take_off_state == 'throttle':
            _, roll, true_heading, _ = packet.read_pitch_roll_headings()
            lift, _, _ = packet.read_aero_forces()

            rudder = (self._take_off_heading - true_heading) * 5
            elevator = 0
            aileron = 0

            # TODO calculate this based on weight of craft
            if lift >= 5000 * units.newton:
                elevator = 0.25
                aileron = -roll.to(units.radians).magnitude

            p = packets.DataPacket()
            p.write_joystick_elevator_aileron_rudder(rudder=rudder,
                                                     aileron=aileron,
                                                     elevator=elevator)
            self.send_packet(p)

        return True

    def _take_off_throttle(self):
        self.send_packet(packets.CommandPacket('sim/flight_controls/brakes_toggle_regular'))

        packet = packets.DataPacket()
        packet.write_throttle_command(1)<commit_msg>Turn off auto-pilot when reached altitude<commit_after>"""
A very simple autopilot designed to simplify testing of planes by providing an
existing framework for getting a plane in the air.
"""

from . import packets
from .io import Protocol
from .packets import units


class TakeOffMixin:
    _take_off_state = None
    _take_off_heading = None

    def take_off(self):
        self.take_off_started()

    def take_off_got_data_packet(self, packet, address):
        if self._take_off_state is None:
            return False

        if self._take_off_state == 'started':
            _, _, true_heading, _ = packet.read_pitch_roll_headings()
            self._take_off_heading = true_heading
            print('Landing strip heading is:', true_heading)
            self._take_off_throttle()
            self._take_off_state = 'throttle'
        elif self._take_off_state == 'throttle':
            _, roll, true_heading, _ = packet.read_pitch_roll_headings()
            lift, _, _ = packet.read_aero_forces()
            _,_, altitude, _ = packet.read_latitude_longitude_altitude()

            rudder = (self._take_off_heading - true_heading) * 5
            elevator = 0
            aileron = 0

            # TODO calculate this based on weight of craft
            if lift >= 5000 * units.newton:
                elevator = 0.3
                aileron = -roll.to(units.radians).magnitude

            # TODO get this as an argument
            if altitude >= 300 * units.meter:
                elevator = -0.5
                self._take_off_state = None

            p = packets.DataPacket()
            p.write_joystick_elevator_aileron_rudder(rudder=rudder,
                                                     aileron=aileron,
                                                     elevator=elevator)
            self.send_packet(p)

        return True

    def _take_off_throttle(self):
        self.send_packet(packets.CommandPacket('sim/flight_controls/brakes_toggle_regular'))

        packet = packets.DataPacket()
        packet.write_throttle_command(1)
{'fraction_non_alphanumeric': 0.07412190082644628, 'fraction_numerical': 0.006714876033057851, 'mean_word_length': 1.967816091954023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7278460', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1055, 'n_words': 248}