I wired up the encoder for the pendulum and ran a few tests. It was a major pain to solder the wires into the connector. The connector is so small and light that it slides all over the place while I am trying to heat up the solder cup and insert the wire. I melted a big section of the plastic connector base while chasing the connector around. I ended up plugging the connector into the 9411 module to give it more bulk and managed to complete the job. It is pretty messy, though. I decided I need to buy a small vise to hold the parts while I am soldering. This will make it much easier. I found a few in the $20 range that look pretty good.
Once I wired it up, I tried out a copy of the position and speed measurement routines that I used for the cart. I quickly found a major bug in the algorithm – I was storing the space measurements as signed values, and when it was changing direction I was averaging large positive and negative numbers, which gives numbers close to zero. After division, I was getting very large speeds instead of close to zero like I should. I separated the signs from the magnitudes and everything was good. I tweaked a few other parts of the algorithm to get very accurate measurements. I found the pendulum encoder still has a pattern that repeats every four pulses, but it isn’t nearly as bad as the position encoder. At higher speeds averaging four spaces works very well to give a smooth speed measurement. At very low speeds, it is better to use single measurement to minimize the delay of the speed measurement.
Once I was satisfied with the measurements for the pendulum, I copied the changes back to the cart routines. They probably still need a little tweaking since the cart behaves differently than the pendulum, but it should still be an improvement since I eliminated some major bugs.