Example 2: Simple Pendulum without Controller - ihmcrobotics/ihmc-open-robotics-software-tutorials GitHub Wiki
Introduction
Welcome! In this tutorial, we will create a simulation of a simple pendulum.
If you haven't done so already, visit Importing Simulation Construction Set (SCS) Project to set up the project in your development environment.
Understanding the Contents of the Project
Now let's browse the simple-pendulum
project. There are three Java files here:
SimplePendulumSimulation.java
, which declares the simulation entrypoint. This sets up an SCS instance to simulate the model of the simple pendulum defined inSimplePendulumDefinition
with theSimplePendulumController
set up for feedback control.SimplePendulumDefinition.java
defines the physical model of the simple pendulum.SimplePendulumController.java
declares the feedback controller for the simple pendulum.
SimplePendulumSimulation.java
will look similar to MobileSimulation.java
, so if you would like a deeper understanding of how to simulate a project, I invite you to check out Example 1: Mobile Simulation. We will be going over SimplePendulumController.java
in the next example, so let's hop into SimplePendulumDefinition.java
.
SimplePendulumDefinition.java
We will heavily walk through the creation of one of the simplest robots possible: a pendulum. For more information on a simple pendulum, we invite you to check out this Wikipedia link.
In order to build this pendulum for our simulation, we will use only two SCS objects: one JointDefinition
and one RigidBodyDefinition
.
As described in Example 1: Mobile Simulation, the RigidBodyDefinition
object defines the physical properties (Mass
, Center of Mass offset
vector, Moment of Inertia
, ...) of a robot link and the JointDefintion
object is used to define the connections between robot links. To represent the pendulum's pivot, we will use a RevoluteJointDefinition
which is a rotational joint with a single degree of freedom. Revolute joints (also called hinge joints) allow rotation around a single axis specified upon creation (the Y-axis in this case).
For a more thorough explanation, I invite you to read through the code in the files. The code in each project of this tutorial has been notated heavily for user clarification, and to fully understand how the robot is built (both in the simulation and on the controller side), it would be good practice to read through the provided class files before or after running the simulations.
Run the Simulation
Before implementing the controller, let's see what happens when we simulate it. Open SimplePendulumSimulation.java
. Ensure that the line
pendulumRobot.addController(penulumController);
is commented out. In the newly opened Java editor, right click anywhere on the code then in the context menu select Run As > Java Application:
Soon enough, SCS should appear. Extend the variable and registry viewer by clicking on the three black bars on the top left of the window. This will show all the variables in the SCS system. Look for the q_FulcrumPin
variable in the search tab of the Variable Viewer, create two new graphs and drag the q_FulcrumPin
and time
into them. The q_FulcrumPin
variable represents the value of the pivot angle in radians and should oscillate between -π/2 and π/2.
Now, press the Simulate button. The pendulum should start swinging as expected and damp out to its equilibrium position.
Experiment with the Simulation
You can now experiment with different robot initial parameters (lengths, masses, center of mass locations, etc.) and damping. For example, first reset the sim to time 0, change a value such as damping_FulcrumPin
and press the Simulate button again to see how it affects the convergence time of the pendulum.
Adding a Controller
Now let's take a look at the simulation when we add a controller to the mix in Example 3: Simple Pendulum with Controller.