I finally figured out the cart model. I had to take a different approach that took a while to figure out.
I used the fact that the back emf of a motor is proportional to the speed of rotation, and the torque plus back emf are equal to the PWM voltage (with a few constants mixed in). Since the force is proportional to the torque (related by the diameter of the gear), I was able to write the following equation:
ddx = C PWM – D dx – E (dT*dT sinT – ddT cosT)
C and D are constants related to the force (torque). D includes the back emf and the friction term. E is the constant that defines the interaction between pendulum motion and cart motion.
After smoothing data and using a least squares fit, I came up with the values C=0.017, D=40.49, and E=0.02915. When looking at the best fit curve, it matches pretty well except at the points where the speed crosses zero. At those points, the speed lags briefly before catching up and there is a mismatch. Looking back at the data that correlates speed to PWM value, I decided to linearize the PWM requested by the user so that the steady state speed is always a fixed multiple of the requested PWM value. To do this, I fit separate lines to the positive and negative portions of the speed vs PWM data. The final result was to increase the PWM magnitude of negative values by 41. For positive values, the magnitude is multiplied by 1.025 then increased by 34. Previously, the magnitude was just increased by 27 with no scaling. These new values are expected to eliminate the gap when the PWM is near zero. I still need to do further testing to verify this. After I verify the PWM scaling works well, I will collect several sets of data and verify the values for my models of the pendulum and the cart. I know they are very close now, but I would like them to be even better. I want the most accurate model possible for designing control algorithms without using the hardware.