The following is just an idea, at this point; I haven't actually implemented it. I encourage you to try, and when you do, please let me know how it goes!
To make articulated robots -- things with arms and legs and waists on so on -- you need two things for each joint: a way to move it, and a way to know what position it's in.
The hobbyist robotics community has universally solved this problem with the use of servo motors, which contain internal encoders to measure their position. (Standard servos do not provide this position information back to the microcontroller, however. But they can be modified to do so, or you can use digital servos, which do provide that feedback.)
However, servos were designed for RC model needs, and are poorly suited to robot joints. Their biggest problem is that they have an output shaft on only one side; joints apply a strong lateral force to this shaft, which can quickly break your expensive servo. There are various ways to cope with this issue, mostly involving either permanently modifying the servo or adding wrap-around brackets, and even then the result is not as good as a proper gearbox with a through-shaft should be.
Such gearboxes are available, however -- for example, the Tamiya Worm Gear Box HE. This produces a strong torque on a through-shaft, which could easily and effectively form a robot joint. But that's only half of what we need -- we also need a position encoder so we can tell where the joint is.
Most commercial absolute encoders cost hundreds of dollars, though Bourns does make one (Mouser part number 652-EAW0J-B24-AEL) which is only about $11. But we can do even better. Relative (quadrature) encoders are available which are quite small and cheap (in the $1 range). But by itself, a relative encoder isn't good enough, since we need to know the absolute position of the joint, not just how far it moves.
So now we come to the big idea...
Add an additional optical encoder with a special encoder disc, that indicates whether the joint is ahead of or behind its "neutral" position. Start with a clear (transparent) plastic disc, which you can fit to the output shaft. Then decide what you want the neutral position of this joint to be (i.e., the position to which your robot will move when it is powered up), and how far the joint should be able to move ahead and behind that neutral position. Paint a section of the disc which corresponds to the ahead-range black, like this:
Add a simple optical emitter/sensor assembly around this disc (those cost less than $1), and now you can always tell whether your robot joint is ahead or behind the neutral position.
Finally, combine this with the quadrature encoder to give you a complete solution, with the use of this procedure: