We're now masters of quarternions and at least what the definitions are. The next thing is, how do we go to and from the DCM, right? This is always the classic stuff. So if you have to convert Euler angles or add Euler angles, three to ones, and Euler angles three one threes. We always go to a DCM. So, we want to be able to go from our coordinates to a DCM and then from the DCM, extract our coordinates. I'm just giving you this formula here. Looks actually pretty compact. All you see is quaternions, there's no fractions of quaternions, there's no ratios. Ratios are always tricky because the denominator could go to zero potentially right. We don't even have a ratio here. It's just quaternions squares and some the differenced in different ways. There's clear patterns there. So it's really nice and easy to program. In the Book Two, I show you other compact vector notations instead of matrix component notations of this. And in your current homework actually, I'm asking you to derive this stuff. This takes a little bit more algebra, okay? From the earlier stuff. But you can do, you can use the basic definition, start to substitute in. You will have to use these identities that the sum squared of quaternion is equal to 1. because sometimes you get a form and like, well that looks close but not quite right. But if B1 squared and B2 and B3 and B naught squared all equal to 1. Wel, l then beta null squared is one minus the other three, right? And sometimes you can substitute that and manipulate it, now you're applying these constraints. So those are the tricks you have to kind of do to get there. So very easy mapping, no singularities, no issues with these numbers. Now, how do we get the inverse? This is going to look very similar in the mathematics here to how we went from the DCM to the principle rotation parameters. To get the angle, we end up taking the trace of the matrix and there was an inverse cosine somewhere to get all this stuff worked out, but it was a trace of the matrix that was the key. Remember beta naught actually only depends on the cosine of Phi over 2. So also it depends on a principle rotation angle. So we still use the trace of the matrix. If you take this term, this term, and this term and add them up, you add 1 to it, take the square root and then 1 over 2. You will end up with beta naught. And I'm going to let you validate the algebra. You guys know how to do algebra, but validate for this. You'll see this pattern as you work through these things with the homework. But that's it. So if this by itself gives me, for beta naught squared is trace + 1, taking the square root I have to get two possible answers, right? This is what we expect. If you know there's two possible sets for an attitude and you go from a DCM and get these coordinants and there's only one set of coordinants, something went wrong. Somewhere we have to have four possible, sorry, in this case two possible sets, and this is where it appears. So either you pick a beta naught that's positive or negative. So Kyle, right? No Cody, sorry. You were Kyle. [LAUGH] >> Casey. >> Casey, I'm completely off. >> It's okay. >> Cody, which one would you pick? Would you do plus or minus. When you attract this. Let's be honest, when you're programming what are you going to pick? Yeah, we're all lazy. Why add that extra negative sign. That's an extra keystroke, really. That's just really pushing it. Now, so when we're programming we do, hey this is the square root of this, human nature's going to go [SOUND] just pick the plus version. Turns out human nature for once is right. That's actually typically the most convenient answer because beta naught being positive. Sorry, what was your name again? Yes, right there. >> Lucas. >> Lucas. Is that a short or the long rotation if beta naught is positive? That's the short rotation, right? If I have zero rotation, beta naught is 1. If I have 180 degrees, the beta naught reduces to 0. And if I have more than 180 degrees it goes below 0 down to -1. Like the DCNs though beta naught, betas can never exceed one in value otherwise you're going to violate the unit norm constraint. So good, so people tend to just pick the plus one but if for some reason you needed the long rotation and there are actually cases where this could be happening. So it does depend on situations, one of them could be you're doing a maneuver and you have to avoid the sun, I want to rotate from here to here without my sensor ever looking straight at you guys. You guys are just too bright. You're going to blind the sensor and burn out pixels. So instead of doing the short way around, we might go, the short way gets me too close. If it's a single avoidance cone and the sun exclusion region you can just go, you know what, for the control just do it all the way around this way and that's when I'm going to drive back. It's a simple example where you might choose to do the long rotation. That's where you control it, right here, with the minus sign. Once you've picked a minus sign, the rest of the math, if you look here. And again, this is trivial. You can see one of them, if I subtract one of these off diagonal elements, these parts are always symmetric. And they're just going to cancel. Leaving you with these other parts, in this case beta threes. I know beta naught and then I can just divide through it and then I get it. Now don't flip signs anymore. You've just get to flip the sign once. That's for beta naught, everything else is completely determined, and this will give you all the four betas. Yes. >> Is it possible the beta not be zero? Is it possible for it not to be zero? >> It is possible for it to be zero. >> Yes, what orientation would that be? When is beta not zero? Right, I think it was 1 for 0 rotation, -1 for 360, so roughly in half is 180. That 180 degrees, cosine 180 over 2 is cosine 90 which is 0. So yes, this formula, as is, has numerical issues. That I was about to ask you guys, but you jumped ahead. Perfect. You're paying attention. So, this formula is very convenient, but do not program this on the flight system, because otherwise, you're going to introduce singularities just because of your method of how you pulled quaternions from this system, all right. And when it is zero could you use the ultimate set? Well mine is zero, it's still zero. So short and long doesn't really mean anything when you're at one eighty. This method just has issues with this, so there's an ultimate method that I'm about to discuss. And that's the one I really would recommend the singular of that, the Shepherd's method. Lets talk this through, this is a slightly modified version of that. The first one I showed you is easy to see, because you can just look at the patterns and it's almost like the PRV stuff. You can see trace, you can see delph diagonal differences and quickly you find the formulas but we there, the fundamentally what we did is we found beta naught first and then we find the other three by dividing by the one we found first. And the one we found could go to zero. That's a very real thing especially if you're in a tumbling situation. So is there a better way a more robust way to extract these coordinants? So this is Shepherds method, and instead of just finding beta naught squared first and then doing the square root and deciding + -, we actually found beta naught squares beta 1 squared, beta 2 squared and beta 3 squared. And so looking at these formulas I will let you look at this yourself. This is the trace, this is what we had earlier in the slide. But using the trace and these other elements, you can quickly just double check the algebra, got yup, these formulas do give you beta one squared, beta two, beta three squared. So we can compute all of these. So could we just stop here take the square root of each one of those, and I'm done. Matt, what do you think? The answer is obviously no, otherwise I wouldn't continue with this stuff, right? But the real question is why, why don't I just stop here? >> [INAUDIBLE] >> Exactly, that's it. because there's two sets we said. And one's just a minus of the other. However, you have to be consistent. You don't just change the sign on one of the betas. If you're changing the sign, it has to be on all four simultaneously. Otherwise, you're not looking at two points that are anti-points along this unit sphere. But you've just flipped one to the other quadrant, and that's a whole different orientation. So with this formula, we do know the magnitude squared of every beta, I just don't know the right sign. So this is why we can't stop here. It puts you on the wrong quadrant on this four dimensional space that we're dealing with. So we have to go further. So before we just had three formulas taking differences of the off diagonal terms and that's how we found beta one,two,three. Here,this is a slightly modified version,you can see three differences and three additions of diagonals,so you end up with six possible equations. And now here's basically the formula. Sheppard's method says first compute the square of every quaternion component. Now we figure out which of those quaternion components is the biggest. Whichever one i select like with the first formula that I showed, we're going to divide by that. So I don't want one that I'm dividing by zero, so I pick the most nonzero part. All right. That's what I'm going to choose. Now, lets go back. Sorry, what was your name again? Ben. Is it possible. That you're just having a bad day. You picked a very particular orientation where all four of these are zero. Is that possible? And the next question is why? Why is that not possible? >> [INAUDIBLE] >> Uh-huh. [INAUDIBLE] >> Quarternion are a constrained set of attitude coordinates. What is the constraint of the quarternion? What must it's, if I have B naught, B1, B2 and B3, what equation must they satisfy? [INAUDIBLE] >> Exactly, right? They have to reside on this unit surface. It's just a hyper surface because it's in four dimensional space. But they have to reside on the surface. There's no way you can go from, if you think of the Earth's globe, let's pretend it's a three dimensional surface, right? We have to move on the surface of the Earth, I wouldn't all of a sudden have a position that just tricks me to the center of the Earth. That would give me 0-0-0 coordinates. I cannot have 0-0-0-0, four zero's here because I have to reside by the units sphere. That would violate the unit [corturnium?] condition. So by definition, it's not possible that all four of them are zero. One of them or more have to be away from zero. So you just pick which one is the most away from zero. If two of them are identical, pick one. It's not a big deal. They're both, we're just looking for a well-conditioned numerical problem. So let's just say beta two ends up being, 0.6 and everything else is less than 0.6. Beta 2 squared is 0.6. That was big. You take the square root of that. You get to pick which sign. So it was Cody and Kyle right? Yep. >> [INAUDIBLE]. >> Casey, gosh, man. Still are getting it. Okay, Casey, which sign do you pick? So I pick this one as being the largest. I'm taking the square root. Do I do the plus or minus? >> [COUGH]. >> So, right now, we know we're going to have to divide by whichever one we pick. So, I'm picking, the first step is pick the largest square of a quaternian and then I'm going to have to here, if I pick beta 2, I'm going to divide by beta 2 here. So I have to find out what is beta 2? Not beta 2 squared. I take the square root of this term and square roots have two possible answers, plus and minus. Do you pick plus or do you pick minus? Andrew, what do you think? >> No. >> Why? >> [INAUDIBLE] >> Is that true with beta-2? >> [INAUDIBLE] >> [INAUDIBLE] >> Not necessarily, so I'm glad you said that because that's a common thing I hear. Where all the people go, positive quaternians mean short. No, it's only the scalar part of the quarternion. Because instead of, you could have an e2 that's positive or negative. You could do a short rotation about a negative access and that would give you a minus beta 2, but you're still doing a short rotation. It's the quaternion that responds to the scalar part, which is, here, beta not. That was always [INAUDIBLE] So here, if we want the short one, we would pick the positive one. Yes, Casey? >> [INAUDIBLE] >> No, not really. The reason I picked the positive one is because I just don't know and the lack of knowledge. So, in this case these I don't know if I pick a positive or negative, I'm not going to be guaranteed a short or long. Without looking at this one as a result of that choice I can't make that determination. So for this modified one don't spend a lot of time dreading over positive and negative. Just encoding people to pick the positive one, right it's a valid description. And we're not going to finish this method and at the end I can look at my beta and not the scale one and see if I picked the long or short, if it is long, I flip them. To give back the short rotation. At this stage you don't have enough information to really determine that. So I'm glad you guys made that distinction. Don't pick beta 1, 2 and 3 to do long or short. You can't pull that information from there. So let's say beta 2 was the largest. You do the math, do the square root. And pick the positive one. Just Because I don't know, so I'll make it easy, pick the positive one. Here, you have to pick the three equations that have beta two, which is this one, this one, and this one. So, you divide this right hand side by beta two to get beta naught, all right. You divide this equation by beta two and you end up with beta one. And you divide this equation by beta two and you get beta three. So in your code you end up with these if statements. If this is the largest then I use these three, if this is the largest I use this, this and this. If beta one is the largest I would use this one and these two, and so forth. Right. Yes. >> Do we pick the largest for numerical reasons? >> Yes. >> Okay. >> Yeah, because you have finite degree. And especially if you're dealing with old five computers that only had eight bits and other stuff, then you can start to really make a difference. With 16 bits if you picked 0.4 or 0.6, it doesn't make much difference. So just again people just go with the biggest. Which one of those is the biggest? I found the good big one, off I go. That's it. So, this is the step. So, instead of just having one set of the trace, and then taking three of these formulas, we end up kind of, the first step is find the biggest quaternion squared, take the square root, just keep it positive. Use the right three formulas to get the other three and now as a final step we can check that we have the short rotation, if you want that. If you don't care, the you either quarternion is correct. That makes sense? So this is Shepard's Method, this is typically what we use. And that's whats coded up typically because now you're completely singularity free, but it takes some logic thinking there to see get good conditioning, and pick the right numbers and the right formulas to extract it. These quaternions always exist. Any questions on that one? Good. Moving on.