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

from .io import Protocol
from . import packets


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':
            _, _, true_heading, _ = packet.read_pitch_roll_headings()
            diff = self._take_off_heading - true_heading

            print(diff)

            p = packets.DataPacket()
            p.write_joystick_elevator_aileron_rudder(rudder=diff * 3, elevator=0.1)
            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)
        self.send_packet(packet)

    def take_off_started(self):
        self._take_off_state = 'started'

    def take_off_finished(self):
        self._take_off_state = None<commit_msg>Implement proper take off behaviour<commit_after>"""
A very simply 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()
            diff = self._take_off_heading - true_heading

            rudder = diff * 5
            elevator = 0
            aileron = 0

            lift, _, _ = packet.read_aero_forces()

            # 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)
        self.send_packet(packet)

    def take_off_started(self):
        self._take_off_state = 'started'

    def take_off_finished(self):
        self._take_off_state = None
{'fraction_non_alphanumeric': 0.07621182404034743, 'fraction_numerical': 0.004202857943401513, 'mean_word_length': 2.28426862925483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '767419', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1013, 'n_words': 224}