EDIT: I’m realizing the way I explain the angles (as I was thinking of it) doesn’t make sense given the presented usage of fusor.angle1()
. It won’t matter for this explanation as it regards the code and result, but affects specifically my comment about strict “up,” so I’d love for someone who knows that function to chime in.
ProffieOS isn’t quite my thing, but C++ is, so I can dissect this line for you
Let’s start by breaking this out a little bit, because this uses what’s known as a “ternary” operator. It’s basically an “inline” if-statement. The ?
and :
separate it out, so that what’s before the ?
is basically the if
, what’s after the question mark is the true
case, and the :
is the else
case:
if (fusor.angle1() < -M_PI / 4) {
SetPreset(current_preset_.preset_num + (-5), true);
} else {
SetPreset(current_preset_.preset_num + 5, true);
}
Now what’s going on should be a bit more clear. We’re checking fusor.angle1()
to see if it’s less than negative PI divided by 4, which is negative 45 degrees. What exactly fusor.angle1()
returns relative to the hilt’s physical angle I’m unsure, but I assume it to be something of an absolute -PI to PI (-180 to 180, where 0 is horizontal) to describe the hilt rotation, so really here we’re checking if it’s within what constitutes down, not so much checking for specifically “up.”
Now from here, it should be pretty clear why we’re going back and forth 5 from the current preset, it’s because current_preset_.preset_num
is getting 5 (or negative 5) added to it! So it’s pretty trivial to instead set the value we want, say, for example, 0 or 29, like so:
if (fusor.angle1() < -M_PI / 4) {
// If pointing down
SetPreset(29, true);
} else {
SetPreset(0, true);
}
Now, what about making this preset-number-agnostic? Well, based on how NoSloppy’s code bit works (this is without doing any prop research, so take it with a little salt), it stands to reason that going over or under the preset range simply loops back around (think if we were at preset 0 and subtract 5, if that works without error, then we can do the following).
So, we could simply do:
if (fusor.angle1() < -M_PI / 4) {
// If pointing down
SetPreset(-1, true);
} else {
SetPreset(0, true);
}
When we’re pointing down, we SetPreset
to -1, which, following the above logic, should loop back around to the very last preset, whatever number that may be.
So now we have our solution, let’s make it a neat little one-liner again:
A ternary is like a function that returns whatever value you give it (That means both the true and false case must be the same type), in our case that means it’s kinda like a function:
int ternary(bool caseToEvaluate, int returnIfTrue, int returnIfFalse)
But instead it’s written like:
[Case To Evaluate] ? [Return if true] : [Return if false]
So, our case to evaluate is “If blade is pointing down,” which is the fusor.angle1() < -M_PI / 4
(Btw, it’s worth noting if you want strict angles for “up,” by this logic fusor.angle1() > (3 * M_PI) / 4
, there’s more “clean” ways to represent that, but it keeps it logically understandable)
We can put this whole ternary “function” inside the SetPreset
function, in fact, that’s what ternaries are largely for, compact little if/else’s.
So, if our case is true (and we are pointing down), we want to give -1
to SetPreset’s first argument, otherwise we will give it 0
:
SetPreset((fusor.angle1() < -M_PI / 4) ? -1 : 0, true);
And then tack the true
on for the second argument. Note I’ve put the boolean expression (the part that would logically be inside an if
) inside parentheses, this is just to make sure the compiler doesn’t get confused and interpret things wrong, and it might not even be necessary, but I like to keep it, since it either prevents problems or does absolutely nothing
So there you go, if you have any questions, feel free to ask. There’s always a chance I made a mistake, so if something’s confusing, that may be why, and I’m not familiar with the specifics of the inner-workings of ProffieOS, I’m just going based off what I infer, so if Fredrik or Brian want to chime in to correct or clarify usage here, please do!