pendsim: Developing, Simulating, and Visualizing Feedback Controlled Inverted Pendulum Dynamics

This package is a companion tool for exploring dynamics, control, and machine learning for the canonical cart-and-pendulum system. It includes a software simulation of the cart-and-pendulum system, a visualizer tool to create animations of the simulated system


Statement of need
Curricula in the study of dynamical systems and control can be quite abstract.When a student studies the abstract mathematical model of a system they have difficulty seeing the effects of control and modeling parameters.Because of this, direct experimentation is a natural way to better understand how these systems evolve over time given different controllers and parameters.
Physical laboratory setups are expensive, time-consuming, and limited to four or five students at a time.Virtual experiments are cheap, easy-to-setup, and accomodate hundreds of students at a time.Virtual experiments can augment course content, even for remote-only instruction.The virtual platform allows students to share their work, run experiments collaboratively or individually, and develop controllers or investigate system dynamics in a fast design-test loop.
Instructors can design experiments in pendsim, and subsequently measure any system parameter or variable, including the animation of the system.The package includes visualizations and pre-built controllers.The package is a great companion to any control or dynamical systems course material, in either a virtual, hybrid, or in-person context.

Example Usage
The software is a virtual laboratory.Users create an experiment by specifying a set of parameters: the pendulum (mass, length, friction, and so on), and the simulation parameters (such as external forces).The user can then design and apply a control policy in the simulation.Finally, the user can view the results of the simulation.The ability to rapidly create and run experiments allows for fast design-test loops.
This design-test-visualize sequence also allows instructors to introduce students to new topics and create interactive assignments that can augment theoretical class discussions and hardware experiments.Thus, pendsim may be naturally used in engineering courses related to dynamics and control that are taught in any format, including in-person, hybrid, and online settings.An example PID Controller Design Assignment notebook is available to show how students may use pendsim in a course assignment.
The following simple example (Example 1) shows the ease of creating, modeling, and visualizing a proportional-integral-derivative controller:  Rich plots of any system attribute are easy to generate.This example shows the plot in Figure 2:

Visualization (viz.py):
The simulation results are visualized in viz.py.The 'matplotlib' (Hunter, 2007) backend draws animations of the inverted pendulum and plots from the simulation.The visualization uses the results of the simulation to draw the inverted pendulum, including the external and control forces applied to it.The animation module allows for the system to plot real-time simulation data (e.g., data used by the controller) side by side with the animation.
The results of the simulation are easy to query and plot.This makes investigating the simulation easy and intuitive.

Example Notebooks:
The repository includes several notebooks which show the capabilities of the package.
Example notebooks are hosted on Google Colab: • Animated Plots

Figure 1 :
Figure 1: A still from the animation module.Here, a force pushes to the right (shown in red) while the controller pushes to the left to stabilize the pendulum (shown in blue).

Figure 2 :
Figure 2: This plot (generated by the code in Example 2) shows the angle of the pendulum over time as it evolves in simulation.The black dots show the measured angle, while the line shows an Unscented Kalman Filter estimation of the angle.Such plots are easy to generate from outputs of the simulation.