Looking for articles on this is a bit tricky. It's a form of "inverted pendulum".
In a lot of articles most of the equations are just assumed.
But here's one that shows some of them:
Stabilizing an inverted pendulum
You'll notice that there are a lot of "dt"s in there. Timing is everything.
Conceptually, if you start with a "perfectly" balanced inverted pendulum - one that's basically rotating reaction masses on the top of a stick - as that article describes. Then if you can see how spinning those masses can topple the pendulum, then you should be able to see how the opposite procedure can re-balance it.
So if you spin the reaction mass at the top, then to conserve angular momentum, the post that supports the reaction mass must rotate in the opposite direction. Once it starts tipping, the mass of the post will make it tip faster. So, to correct the tipping, you will need more reverse angular momentum than you put in originally. By the time you have the device righted, the reaction mass will be spinning opposite the to the direction of that first maneuver.
To balance, basically, all you need to do is over-correct by cranking in enough angular momentum to force the center of gravity not only to the point where it is directly over the balance point, but a little further so you can zero out the angular momentum perfectly as you restore the vertical position.
Hope that makes sense to you.