C# source code for simulation of the cart and pole balancing task.
For details of this task, including derivations of the equations of motion, see the following technical research paper:
Equations of Motion for the Cart and Pole Control Task
The source code in this repository can be considered to be supplementary material for that paper.
This repository contains C# source code (src folder) for the cart-pole balancing task, and some R/ggplot2 scripts (r-ggplot2 folder) for creating plots for the above linked research paper.
The source is written in C# and targets .NET 10.0. However, the physics/maths code should be easy to port to any other language.
This project contains the maths/physics source code.
-
DoubleCartSinglePoleEquations.cs- Contains the equations of motion for the cart-pole model, giving the horizontal acceleration of the cart and the angular acceleration of the pole for a given model state and external force f (a horizontal force pushing the cart left or right).
CartSinglePolePhysics.cs- Contains an instance of
CartSinglePoleEquations, and applies Euler's method to update the model state from one timestep to the next. This is also the base class forCartSinglePolePhysicsRK2andCartSinglePolePhysicsRK4.
- Contains an instance of
CartSinglePolePhysicsRK2.cs- Contains an instance of
CartSinglePoleEquations, and applies the standard second-order Runge-Kutta method to update the model state from one timestep to the next.
- Contains an instance of
CartSinglePolePhysicsRK4.cs- Contains an instance of
CartSinglePoleEquations, and applies the classic fourth-order Runge-Kutta method to update the model state from one timestep to the next.
- Contains an instance of
-
Single- The classes in this folder/namespace mirror exactly those of the 'Double' folder, but provide physics calculation based on single precision floating point maths.
A small console app that can be used to run a simulation and save a CSV file containing the model state at each timestep. The CSV file can then be used to make plots, e.g. with R and ggplot2.
A small Windows Forms app. This app runs a simulation and shows a graph on screen of pole angle, cart position, and pole angular velocity over time.