Profile image

[FT HELP PLEASE] Is there a "cleaner" way to code this?

4,287 Formula350  3.1 years ago

OK So as I was typing a post to ask for help on how to code this, I had my EUREKA! moment and managed to figure it out just as I was about to hit Submit.. :D But that left me wonder, given the troubles I have wrapping my head around math and coding, if perhaps there was a more elegant or simpler (cleaner) way to have done this?

What I'm tackling is Rolling induced by Yaw (Rudder), to counter-act the roll by applying a tiny bit of Aileron input only during Yaw events; otherwise, it passes along the Roll input.
(This counter-roll gets applied to the opposite wing to the side I'm "turning" towards, which is why it's a negative-value being multiplied against.)

LEFT WING: Roll>0 | Roll<0 ? Roll : (Yaw>0 ? Yaw * -0.055 : 0)
RIGHT WING: Roll>0 | Roll<0 ? Roll : (Yaw<0 ? Yaw * -0.055 : 0)

(I had tried to use clamp, but ... we won't even get into the mmmmassive failure that resulted in! lol)

TL;DR - There any way to simplify that code? It works as needed, but even to me it just looks ham-fisted... :}

  • Log in to leave a comment
  • Profile image
    16.5k edensk

    L: Roll - (Roll=0 ? clamp01(Yaw)*0.055 : 0)
    R: Roll + (Roll=0 ? clamp01(-Yaw)*0.055 : 0)

    Pinned 3.1 years ago
  • Profile image
    4,287 Formula350

    Yep, that did it! Does exactly the same thing as mine now but shorter!
    Thanks :D
    (Although, as to why adding the outer parenthesis solved that issue, is definitely beyond me! lmao)
    .
    I've pinned the functional code and am leaving this line of text to hopefully avoid confusion, should anyone read the comments :P

    3.1 years ago
  • Profile image
    16.5k edensk

    forgot the parentheses lol

    3.1 years ago
  • Profile image
    4,287 Formula350

    @edensk

    L: Roll - Roll=0 ? clamp01(Yaw)*0.055 : 0
    R: Roll + Roll=0 ? clamp01(-Yaw)*0.055 : 0

    Now it ends up canceling out Roll from working, period lol
    ... And also makes the Aileron move in the opposite direction :}
    .
    Though, I can't even figure out how that manages to even react with Yaw with that code, given from what I can tell the "IF" part is looking solely for Roll related Input, and the "THEN" is the performing of Yaw-based stuff. EDIT: Nevermind, makes sense now. "IF there is no Roll input" "Then, do this if Yaw is present"
    EDIT 2: The Aileron being just a lack of Negative in front of the decimal value.
    .
    The "ELSE" being zero explains the lack of any Roll input getting applied, and I presume that it should be : Roll instead of : 0?
    EDIT 3: Nope, I'm a derp! That wouldn't work since the "IF" part means it never will make to the "ELSE" part lol

    3.1 years ago
  • Profile image
    16.5k edensk

    @Formula350 Oh I see, edited it so it works like yours

    +1 3.1 years ago
  • Profile image
    4,287 Formula350

    @rexzion lol Yea me too :sob: I know enough to kludge together code and get it working (usually...), but I don't actually know the more-advanced stuff that would let me properly get it done.


    @edensk Yep I never would've came up with that result.
    However, while using yours DOES yield functional results, it actually doesn't produce identical results as mine. (as determined through using DebugExpressions)
    .

    Situation A:

    YOUR CODE:
    YAW Left = 0.055
    ROLL Left = -1
    YAW Left + ROLL Left = -0.945
    .
    MY CODE:
    YAW Left = 0.055
    ROLL Left = -1
    YAW Left + ROLL Left = -1

    Situation B:

    YOUR CODE:
    YAW Left = 0.055
    ROLL Right = 1
    YAW Left + ROLL Left = 1.055
    ,
    MY CODE:
    YAW Left = 0.055
    ROLL Right = 1
    YAW Left + ROLL Right = 1

    I only mention that because I was intending to also apply this to the Tail Elevators and they would require significantly more compensation applied than just 0.055, to adjust for the amount of Nose-Down that occurs. Which would actually lead to a situation where if I were applying Yaw+Pitch, my Elevator's input would either be far less-than its max, or far over its max. (depending on whether I'm pitching up or down)
    .
    It's the latter situation that would be of concern, since it could result in those Elevator flaps stalling-out.
    BUT! All examples of functional code like you provided is actually really helpful for me, since I learn better through examples than I do being giving explanations for how to use stuff and trying to get it working. :D

    3.1 years ago
  • Profile image
    44.3k rexzion

    honestly when i make ft code i just add stuff til it works somewhat as intended and i don't touch it again in fear of breaking everything

    +1 3.1 years ago