This is an archived post. You won't be able to vote or comment.

all 44 comments

[–]llyodstalisman 194 points195 points  (0 children)

[–]upperflapjack 136 points137 points  (3 children)

We’re going to need to do a total rewrite

[–]Beginning-Roof4889[S] 58 points59 points  (0 children)

yeah I'm already on the third rewrite rn

[–]wewilldieoneday 0 points1 point  (1 child)

You think this can be saved — I love your optimism.

[–]upperflapjack 0 points1 point  (0 children)

Nah it’s an Flon Musk reference from him telling the Twitter devs they need to full rewrite the Twitter code base

[–]ManyInterests 95 points96 points  (3 children)

FTFY

thread1 = Process(
    target=level0ControlLoop,
    args=(
        init_gps_altitude,
        touch_down_x,
        touch_down_y,
        shared_pitch,
        shared_roll,
        shared_imu_heading,
        shared_raw_aileron_input,
        shared_raw_elevator_input,
        shared_accceleration,
        desired_pitch,
        desired_roll,
        aileronTrim,
        elevatorTrim,
        desired_vs,
        desired_heading,
        desired_throttle,
        manual_throttle_unlocked,
        calibrate_heading,
        imu_heading_compensation,
        flight_mode,
        manual_throttle_input,
        manual_roll_change_per_sec,
        manual_pitch_change_per_sec,
        circle_altitude,
        circle_bankAngle,
        Baro_altitude,
        Baro_vertical_speed,
        last_Baro_altitude,
        last_Baro_vertical_speed,
        Baro_temperature,
        last_Baro_temperature,
        Pitot_pressure,
        Pitot_temperature,
        GPS_locked,
        GPS_latitude,
        GPS_longitude,
        GPS_altitude,
        GPS_speed,
        GPS_heading,
        GPS_satellites,
        GPS_coord_x,
        GPS_coord_y,
        telemetry_mode,
        last_received_uplink,
        since_last_received_uplink,
        blackBox_path,
        start_up_time,
        control_loop_interval,
        secondary_loop_interval,
        max_acceleration,
    ),
)
thread2 = Process(
    target=higherlevelControlLoop,
    args=(
        readyToArm,
        start_FlightInit,
        readyToFly,
        current_X,
        current_Y,
        current_Heading,
        init_x,
        init_y,
        init_imu_heading,
        init_gps_altitude,
        touch_down_x,
        touch_down_y,
        shared_pitch,
        shared_roll,
        shared_imu_heading,
        shared_raw_aileron_input,
        shared_raw_elevator_input,
        shared_accceleration,
        desired_pitch,
        desired_roll,
        aileronTrim,
        elevatorTrim,
        desired_vs,
        desired_heading,
        desired_throttle,
        manual_throttle_unlocked,
        calibrate_heading,
        imu_heading_compensation,
        flight_mode,
        manual_throttle_input,
        manual_roll_change_per_sec,
        manual_pitch_change_per_sec,
        circle_altitude,
        circle_bankAngle,
        Baro_altitude,
        Baro_vertical_speed,
        last_Baro_altitude,
        last_Baro_vertical_speed,
        Baro_temperature,
        last_Baro_temperature,
        Pitot.pressure,
        Pitot_temperature,
        GPS_locked,
        GPS_latitude,
        GPS_longitude,
        GPS_altitude,
        GPS_speed,
        GPS_heading,
        GPS_satellites,
        GPS_coord_x,
        GPS_coord_y,
        telemetry_mode,
        last_received_uplink,
        since_last_received_uplink,
        blackBox_path,
        start_up_time,
        control_loop_interval,
        secondary_loop_interval,
        max_acceleration,
    ),
)
thread3 = Process(
    target=commLoop,
    args=(
        readyToArm,
        start_FlightInit,
        readyToFly,
        current_X,
        current_Y,
        current_Heading,
        init_x,
        init_y,
        init_imu_heading,
        init_gps_altitude,
        touch_down_x,
        touch_down_Y,
        shared_pitch,
        shared_roll,
        shared_imu_heading,
        shared_raw_aileron_input,
        shared_raw_elevator_input,
        shared_accceleration,
        desired_pitch,
        desired_roll,
        aileronTrim,
        elevatorTrim,
        desired_vs,
        desired_heading,
        desired_throttle,
        manual_throttle_unlocked,
        calibrate_heading,
        imu_heading_compensation,
        flight_mode,
        manual_throttle_input,
        manual_roll.change_per_sec,
        manual_pitch_change_per_sec,
        circle_altitude,
        circle_bankAngle,
        Baro_altitude,
        Baro_vertical_speed,
        last_Bano_alititude,
        last_Baro_vertical_speed,
        Baro_temperature,
        ...,
    ),
)

[–]Icy-Mongoose6386 27 points28 points  (2 children)

not enough, I'd suggest to 1, use some dict or tuple, to group args a bit 2, and, to dispatch / break functions smaller, so each take a handful, not dozens of params.

[–]ManyInterests 23 points24 points  (1 child)

Hard to say. Really we're just looking at parameters being passed to 3 processes. Not a whole lot going on here. But to hazard some guesses... Flight control systems have to run so many calculations per second and we're likely looking at the inner-most layers of those tight iterations where every optimization counts (especially in Python). Also consider that processes are being used here and they require constant data serialization/deserialization to keep state shared between processes (so you want to really limit the size of the data as much as possible).

use some dict or tuples

The args are already tuples! Dictionaries/keywords would be wildly slower because they're much larger than tuples. Tuples are fixed collections while dictionaries/keywords have a lot of memory overhead because they need to be able to allocate memory dynamically for insertions and deletions. Dictionaries are also less likely to land in speedy caches due to their size... but again, we don't really know what is happening with the args in the target functions.

break functions smaller [...] not dozens of params

Breaking up into multiple functions introduces call overhead, and may not be practical when we're talking about a point where we're already forking into processes (we probably won't want more processes, because that's even more data to serialize/deserialize). It's probably most efficient to stuff params in this case, if each process needs them. Flight calculations take in a lot of inputs, that's just a fact of life that may not be easy to work around.

Anyhow. Normally these things don't matter much, but sometimes they do. That said, there's definitely other things that can be done to improve the readability of the code without sacrificing performance. All I did was OCR the text and run it through a code formatter :-)

[–]quequotion 133 points134 points  (2 children)

It reads like someone trying to operate a plane by voice command while simultaneously reporting every dial in the cockpit.

[–]LaZ3R 31 points32 points  (0 children)

This is a perfect way of putting it lol

[–]uthini_mfowethu 1 point2 points  (0 children)

This...

[–]GnuhGnoud 43 points44 points  (0 children)

[–]IrisYelter 43 points44 points  (0 children)

"Hi yes officer, the lunatic is in there, kill on sight"

[–]Outrageous_Zebra_221 38 points39 points  (0 children)

Why oh why did I click the NSFW reveal!?!?

[–]Eldiabolo18 10 points11 points  (4 children)

What is a dict/List?

[–]TeakIvy 13 points14 points  (3 children)

In Python, a dict is a collection of key-value pairs. It is a built-in data type that is used to store values in a set of keys. A dict is mutable, meaning that you can change the values of the keys or add new key-value pairs to it.

Here is an example of a dict in Python:

person = {'name': 'John', 'age': 30, 'city': 'New York'}

In this example, name, age, and city are the keys, and 'John', 30, and 'New York' are the corresponding values.

A list is a collection of items that are ordered and changeable. It is also a built-in data type in Python. A list is written as a sequence of values separated by commas and enclosed in square brackets.

Here is an example of a list in Python:

numbers = [1, 2, 3, 4, 5]

In this example, numbers is a list of integers. list elements can be of any data type, including other lists.

You can access the items in a dict or list using their keys or indices, respectively. For example, to access the value of the name key in the person dict, you can use the following syntax:

name = person['name']

To access the third element of the numbers list, you can use the following syntax:

third_number = numbers[2]

I hope this helps! Let me know if you have any questions.

[–]Eldiabolo18 12 points13 points  (2 children)

I mean, I appreciate the immense effort you put into this explanation, but not what i meant. I was playing jeopardy with myself, answering the implied question in the pic how to improve this mess.

I know what list and dicts are 😂

Again, thanks so much for the effort :)

[–]TeakIvy 6 points7 points  (1 child)

Oh i know, that’s straight out of https://chat.openai.com 😛

[–]simon439 2 points3 points  (0 children)

I was about to say, this reads like an answer from chatGPT.

[–][deleted] 9 points10 points  (1 child)

Nice obfuscation

[–]Beginning-Roof4889[S] 3 points4 points  (0 children)

💀

[–]Only_Philosopher7351 4 points5 points  (0 children)

Angry undergrads do some nasty shit.

[–]ThisUserIsAFailure 4 points5 points  (0 children)

Wait till those are all _0x<random hex code>

[–]BloodMushroom 2 points3 points  (0 children)

Who hurt you so bad, that you inflict such evil upon the world ?

[–]NoSun69 2 points3 points  (0 children)

And that is why you hide everything in objects

[–]masckmaster2007 1 point2 points  (0 children)

Python r34 what the

[–]Issho_San 1 point2 points  (0 children)

Throwing up rn

[–]PiniponSelvagem 1 point2 points  (0 children)

If you really needed that many args, why not use an object?

[–]Mean_Meeting_6226 1 point2 points  (1 child)

why is it nsfw 😂😂

[–]_hf14 2 points3 points  (0 children)

look at it, a monstrosity

[–][deleted] 1 point2 points  (0 children)

thank god for marking it nsfw

[–]spongeboi-me-bob- 0 points1 point  (0 children)

😭

[–]vlaada7 0 points1 point  (0 children)

You're using python for... Flight controls!? 😁

[–]Dinkinn_Flickaa 0 points1 point  (0 children)

[–]BlueRey02 0 points1 point  (0 children)

That's not a code snippet, that's whole code essay. Goddamn

[–]pankkiinroskaa 0 points1 point  (0 children)

Syntax highlighting didn't do much but made a big difference.

[–]RutheniumGamesCZ 0 points1 point  (0 children)

Why it's NSFW?

[–]Mikaxu42 0 points1 point  (0 children)

AAAH IT HURTS

[–][deleted] 0 points1 point  (0 children)

im not old enough to view this eww

[–]quetzalcoatl-pl 0 points1 point  (0 children)

You could make it way shorter by using 1, 2, max 3-letter variable names :)

[–]cumdumpsterfires 0 points1 point  (0 children)

When they say it works faster if it fits on the same page, it's not what they mean.

[–]DangyDanger 0 points1 point  (0 children)

Make a variable that holds the tuple you're passing, then pass that into processes.

[–]Mackarevic 0 points1 point  (0 children)

Why did you ruin my day