Simplest controller simulator - 2D inverted pendulum

Post Reply
AjayT
Posts: 2
Joined: Sat Jul 11, 2015 7:25 pm

Simplest controller simulator - 2D inverted pendulum

Post by AjayT »

Hi, I wonder if anyone could offer some advice? I'm new to the Bullet physics code base, and app programming!

I'd like to use the examples that come with Bullet to try to make a very simple physics simulator. The simulator would be used to test online servo/controller designs under different motor strengths, parameter settings, etc

The physical system is a simple inverted compound pendulum, just a single rigid uniform rod. It's constrained to vertical planar motion, around the origin, by using a simple hinge constraint. Torque can be applied by a controller motor.
fig1.png
fig1.png (6.47 KiB) Viewed 5031 times
It seems this is nearly the same as the example

Code: Select all

bullet3/examples/Constraints/TestHingeTorque.cpp
except upside down? In the example the base, is at the top and the child/pendulum rod, is initially below it, (picture attached at bottom of post).

Here's a youtube video of similar physical system, and an online controller design - [Inverted Pendulum Swing-Up Design](https://www.youtube.com/watch?v=C39sm1L2g60)

For the physics simulation I'd like to just focus on one application of the servo/controllers action, which happens at the start of the simulation.

If say, theta_t is the angle of the rod with the vertical axis, at time step t. Then the inputs are, the initial state, ( theta_0 , angular velocity_0 ), and the torque impulse u applied by the the controller motor, at the start of the simulation, (timestep t=0).

I'd like to render an image of the pendulum at t=0.

Then, use Bullet to run the simulation for say T=3 or 4 or 5 steps/frames.

Then render an image of the pendulum at T, and output the final state, (theta_T , angular velocity_T )

I've tried doing this in Blender, but I found it would be very messy getting the controllers input into it, [Setup an inverted pendulum simulation with BGE](http://blender.stackexchange.com/questi ... 3693_33806)

At the moment I only want to render two frames, from one hard coded camera perspective, and the images can be greyscale, and small say 150x150 pixels. So, I think simulation using Bullet, and simple OpenGL rendering is the simplest way to do this?

##---------------------------------------

To get started, (I'm beginner), I think nearly all of the code I need is already in the examples? The problems pure dynamics, and (I think) there's no collision detection needed? An outline of the code I think is useful is,

i) adapt the HelloWorld example for the basic setup and simulation loop.

ii) adapt/understand the TestHingeTorque example

Code: Select all

bullet3/examples/Constraints/TestHingeTorque.cpp
which constructs the pendulum (rigid body + hinge) and impulse transfer (motor).

iii) adapt the rendering example,

Code: Select all

bullet3/examples/SimpleOpenGL3/main.cpp
to output the required frames.

iv) try to do this all with the Lua bindings - my controller code is written in Lua - I think this is possible?

[Bullet SWIG (Simplified Wrapper Interface Generator) 4 Lua](http://www.bulletphysics.org/Bullet/php ... lua#p12524)

##---------------------------------------

I'd just like to ask if people think this is the right way to do it, and if there's already code of something similar out there that I could build from?


Kind regards,

Aj
Attachments
TestHingeTorgeDemo.png
TestHingeTorgeDemo.png (296.3 KiB) Viewed 5020 times
AjayT
Posts: 2
Joined: Sat Jul 11, 2015 7:25 pm

Re: Simplest controller simulator - 2D inverted pendulum

Post by AjayT »

I just saw that [SimBody](https://github.com/simbody/simbody/blob ... ndulum.cpp) has a lot of examples of systems that are easy to understand.

What are the Pros and Cons of Bullet Physics vs SimBody?

In particular for dynamical systems, control theory & multi-joint simulations, rather than game physics?

Thanks :?:
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Simplest controller simulator - 2D inverted pendulum

Post by Erwin Coumans »

AjayT wrote:I just saw that [SimBody](https://github.com/simbody/simbody/blob ... ndulum.cpp) has a lot of examples of systems that are easy to understand.

What are the Pros and Cons of Bullet Physics vs SimBody?

In particular for dynamical systems, control theory & multi-joint simulations, rather than game physics?

Thanks :?:
SimBody is a great physics simulator, and I have regular meetings with its author, Michael Sherman. I'm not the right person to do a comparison, but here are some differences in a nutshell:

SimBody uses a variable time step to reach a very high accuracy. It uses compliant (soft) contacts by default, and event mechanisms to model interactions. It uses reduced coordinate multi body dynamics following the Robot and Multi Body dynamics book by Abi Jain. It is better documented than Bullet.

Bullet uses a fixed time step and has a focus on hard (impulsive) contact by default. I am currently using Bullet for motor control in robotics, and development go towards improving the Featherstone based articulated multibody for that purpose. Aside from the reduced coordinate btMultiBody, Bullet has also maximal coordinate rigid bodies with constraints. The Bullet code base is smaller and has less dependencies, compared to SimBody.

When using Bullet, you should be able to create a URDF text file of an inverted pendulum, load the URDF file in the example browser and apply control forces easily. I just added an inverted pendulum demo with some control example, download the latest revision of Bullet at http://github.com/bulletphysics/bullet3, the example also creates a screenshot automatically, see https://github.com/bulletphysics/bullet ... 6c09df99b6 The example is under the Multi Body section of the example browser.
inverted_pendulum_control.jpg
inverted_pendulum_control.jpg (118.27 KiB) Viewed 4886 times
I can recommend trying out both SimBody and Bullet and see which fits your needs.
Post Reply