Wednesday, September 18, 2019

Opening the Space Box

Recently, I solved the high-value problem posed by Lockheed Martin's Space Box Challenge at my university. I was not prepared for what came next.

What is the Space Box?

Physically, it is a big, shiny, black box that looks like it came from a science fiction movie. The purpose of the box is to give university students a chance to demonstrate their problem-solving abilities. To get inside, you must solve one of four problems. There are three questions that are challenging, but they can be solved relatively easily with some thought and a little bit of math. Those three questions change daily for the three days the box was on campus. Upon solving a problem correctly, participants are rewarded by getting to go inside the box to view a cool space animation that surrounds you on all sides. Upon exiting the box, problem solvers receive a replica of the albums sent into space on the Voyager spacecraft. The fourth question, however, was much more difficult to solve than the other three. The reward was also much more substantial: A job offer from Lockheed Martin.

Opening the Box

As I mentioned, I solved the high-value problem, and I have since been offered a position working for Lockheed Martin. I am still in disbelief as I think about it right now. Many, many thanks to Lockheed Martin for bringing this challenge to my university and giving me this amazing opportunity. I am excited about the future.

The Problem and Solution

Many people have been asking me about the problem I solved and how I solved it. I usually just answer, "with some math." For the more curious, here is the problem I solved:

Right away, this problem looks very intimidating. It seems to combine aspects of orbital mechanics and electromagnetism into a single problem. However, after you start to look at the problem more closely, it is only asking us to find a relationship between two reference frames. So let's look at how to tackle this problem.

The problem basically defines a magnetic reference frame, and then asks us to find an expression for the angle at which the angular rate of rotation of the magnetic reference frame is double the orbital rate of the satellite (read: angular velocity).

The only outside information needed to solve this problem is a dipole model of the Earth's magnetic field, which can be found in multiple forms across the internet, including Wikipedia. Because the expressions get quite long - and because I am lazy - I use the following shorthand:
Since the problem uses a reference angle measured from the equator, this is the form we want.
\[B_r=-\frac{2 B_0}{R^3}s_\alpha\]
\[B_\theta = \frac{B_0}{R^3}c_\alpha\]
\[|B| = \frac{B_0}{R^3} \sqrt{1+3s_\alpha^2}\]
where \(R\) is the distance from the center of the orbit divided by the radius of Earth. Note that these equations define a polar coordinate system which depends on the angular position within the orbit and both \(B_0\) and \(R\), neither of which are given. As it turns out, we don't need those values to solve the problem. I will define a set of unit vectors fixed on the satellite body, with \(\hat{e}_r\) pointing radially outward from the orbit and \(\hat{e}_\theta\) pointing along the trajectory of the satellite. The magnetic field is then given by
\[\vec{B} = \frac{B_0}{R^3}\left(-2s_\alpha \hat{e}_r + c_\alpha \hat{e}_\theta\right)\]
Using the given definition of the magnetic reference frame, we can compute the unit vector
\[\hat{x}_{mz} = \frac{\vec{B}}{|\vec{B}|} = \frac{-2s_\alpha}{\sqrt{1+3s_\alpha^2}}\hat{e}_r + \frac{c_\alpha}{\sqrt{1+3s_\alpha^2}}\hat{e}_\theta\]
We could calculate the others unit vectors also, but there is no need. We only need one unit vector to compute the angle of the reference frame with respect to the inertial reference frame. We need to express everything in terms of an inertial reference frame, because that is the reference frame in which the angular velocity of the satellite is defined. If we define the inertial frame with \(\hat{x}_1\) pointing to the right and \(\hat{x}_2\) pointing up, then we can write 
\[\hat{e}_r = c_\alpha \hat{x}_1 + s_\alpha \hat{x}_2 \]
\[\hat{e}_\theta = -s_\alpha \hat{x}_1 + c_\alpha \hat{x}_2 \]
Substitution into the equation above (and a little bit of algebra) gives
\[\hat{x}_{mz} = \left(\frac{-3s_\alpha c_\alpha}{\sqrt{1+3s_\alpha^2}}\right)\hat{x}_1 + \left(\frac{-2s_\alpha^2+c_\alpha^2}{\sqrt{1+3s_\alpha^2}}\right)\hat{x}_2\]
Ultimately, we need an expression of the angle between the magnetic reference frame and the inertial reference frame so we can take a derivative. To get this angle, we simply take a dot product. Defining \(\beta\) as the positive angle between \(\hat{x}_1\) and \(\hat{x}_{mz}\),
\[ \cos(\beta) = \hat{x}_{mz} \cdot \hat{x}_1 = \frac{-3s_\alpha c_\alpha}{\sqrt{1+3s_\alpha^2}} \]
Take the inverse cosine, and we get
\[ \beta = \cos^{-1}\left(\frac{-3s_\alpha c_\alpha}{\sqrt{1+3s_\alpha^2}}\right) \]
Now we have our expression of the angle of the magnetic reference frame. All that's left to do is take a derivative. I used Wolfram Alpha for help with this, although it can be done by hand with a fair amount of patience.
\[\dot{\beta} = \frac{-3\dot{\alpha}\left(3 s_\alpha^4 + s_\alpha^2 - c_\alpha^2 \right)}{\left(3s_\alpha^2+1\right)\sqrt{3s_\alpha^2-9s_\alpha^2 c_\alpha^2 + 1}}\]
Note that \(\dot{\beta}=\omega_{mz}\) and \(\dot{\alpha} = \omega_0\), so what we have is an expression for \(\omega_{mz}\) in terms of \(\omega_0\). With some trigonometric identities and simplification, we get a slightly nicer expression.
\[\omega_{mz} = \frac{-3\left(\cos(2\alpha)-3\right)}{2\left(\sin^2(\alpha)+1\right)}\omega_0\]
The question asks for an expression for when the angular rate of rotation of the magnetic reference frame is exactly twice the orbit rate. In other words, when does that coefficient equal 2? There are a few ways to do this, but Wolfram Alpha again makes short work of the problem.
\[\frac{-3\left(\cos(2\alpha)-3\right)}{2\left(\sin^2(\alpha)+1\right)} = 2 \quad\textrm{when}\quad \alpha=\pi n \pm \frac{1}{2}\cos^{-1}\left(\frac{1}{3}\right) \quad\textrm{for}\quad\ n\in\mathbb{Z}\].
If you just need the first instance of the angle (which in this case also happens to be the magnitude angle from the horizon in all four quadrants) just take \(n=0\) and you get
\[\alpha = \frac{1}{2}\cos^{-1}\left(\frac{1}{3}\right)\]
That's it. It takes no more than a page or so of work, and it can be done in 30 minutes if you know what you are doing.

Wednesday, January 9, 2019

Teensy Trouble

In my Ph.D. studies, I have been doing a fair amount of embedded programming. My favorite microcontroller by far has been the aptly-named Teensy. These microcontrollers have some serious hardware at a very reasonable price. They are also incredibly easy to program due to the fact that they can be programmed with arduino code. I could go on and on about why I like them, but today I found a relatively minor issue that caused me a lot of trouble.

The Teensy 3.6 has many pins capable of reading analog voltages. The two pins I decided to use for my project are pins 32 (A13) and 35 (A16). I implemented the functionality using analogRead(13)  and analogRead(16), respectively. Interestingly, A13 worked as expected, but A16 did not.  Instead, it behaved like a floating pin, and even shorting that pin to ground had no impact on the reading (this should have been a clue, actually). It wasn't just A16, either. All analog pins numbered A14 or higher were not working, while all numbered A13 or lower were working as expected.

After much frustration and much investigation, I found the answer. Since analogRead() can accept either analog pin numbers or actual pin numbers as its argument, numbers over 14 are ambiguous to the code, so it assumes the argument is the actual pin number. To avoid this, you can either use the actual pin number or use the analog pin number preceded by "A" (i.e. A14, A15, etc.). This works because those are just macros to the actual pin numbers. In my case, using analogRead(A16) solved the problem.