First simulation

In this story we will be simulating our first robot with Bullet physics engine with PyBullet library and using the programming language Python, yeah Python, as the popularity and versatility of this language sooner we will be programming extra-terrestrial habitats with this… just a random thought 😈

Anyhow, Bullet physics engine is a piece of awesome software written in c, c++ and is used by many game development software’s, simulation software’s, researchers and lot more. In laymen terms a physics engine gives you power to create your own universe (in software form only don’t get excited 😃) you will be having control of each and every aspect of the environment like you want gravity to be +9.8 instead of -9.8 you got it, you like winds you can have them any time you want and in any direction you want basically you can set all parameters as your needs.

Installation and running for first time

For now you need only PyBullet library to install open your terminal and type

pip install PyBullet

for Ubuntu installation is as simple as this, for windows you may have to install VS C++ development package if you haven’t installed it earlier. Now lets run the first simulation, code explanation will be in code snippet.

# Importing required libraries PyBullet and timeimport pybullet as sim
import time
# Connecting to GUI of PyBullet there is a terminal based simulation # support also but we dont want thatphysicsclient = sim.connect(p.GUI)#Setting gravity to -9.8 in z direction i.e. downwardssim.setGravity(0, 0, -9.8)# Running simulation indefinitely if physicsclient != 0 otherwise it # wont runwhile physicsclient == 0:
# This will make our simulation run indefinetly or until you are connected.
Output of above code snippet

You can rotate the environment by holding ctrl + rightMouseButton and dragging your mouse and holding ctrl and scrolling through mouse wheel let you zoom in and zoom out in the scene, lets load our robot and see, code explanation is with comets in code. PyBullet has two modes for running and stepping simulation, first one is like similar to pause and play and second one is to run in real time. In first mode commands will update only when you wish to by running sim.stepSimulation(), whereas in second mode the commands will update in real time like in the sequential manner in which your code is written. Here in this series we will use the second mode in which the real time simulation takes place below code snippet explains it all.

# Importing PyBullet and time modules
import pybullet as sim
import time
# Connecting to the GUI (Graphical User Interface) of the pybullet.

physicsclient = sim.connect(p.GUI)
# Setting gravity to 9.8 m/s in -z direction
# Coordinate system used is (x, y, z)
# setRealTimeSimulation will enable the second mode of running
# simulation.
sim.setGravity(0, 0, -9.8)
sim.setRealTimeSimulation(True, physicsclient)
# Enter path to your urdf filepath = "path to your URDF file"# Loading the URDF file of robot
armID = sim.loadURDF(path, useFixedBase=True, basePosition=[0, 0,0])
# Running simulation for 5 seconds
Collage of screenshots of the simulation

In above image we can see that the body of robot is swinging around and bumped into its own body 😆 this is because we haven't set any joint positions and motor parameters to act on the body, don't worry we will do that in next step.

import pybullet as sim
import time
physicsclient = sim.connect(p.GUI)
sim.setGravity(0, 0, -9.8)
sim.setRealTimeSimulation(True, physicsclient)
path = "path to your URDF file"
armID = sim.loadURDF(path, useFixedBase=True, basePosition=[0, 0,0])
# Now we will get number of joints in our robot by function
joints = sim.getNumJoints(armID)
# setting all joints to position 0
# parameters in function are
# 1 bodyID=armID
# 2 jointNumber=range(6) i.e. [0, 1, 2, 3, 4, 5]
# 3 controlType=sim.POSITION_CONTROL i.e. position control for joint
# 4 positions=[0]*6 i.e. [0, 0, 0, 0, 0, 0]
sim.POSITION_CONTROL, [0] * 6)
# Making simulation wait for 0.5 seconds before next command
# setting a single joint to a specific position parameters will be #
# same
sim.setJointMotorControl2(armID, 1, sim.POSITION_CONTROL,
# running simulation for 3 sec
Snapshot of the positions

By this method we can control positions of the joints PyBullet offers more refine control over the joints while controlling torque and velocity that we will see in upcoming stories. function documentation you can see with a single line of command or if you use PyCharm then simple ctrl+q inside function braces super simple stuff 😆

print(sim.loadURDF.__doc__)# use this line of code with any function you can see its
# documentation printed

Thats it for this story in next story we will try to make our tool of our robot to follow a specific path. I have created a script to write URDF file for your robot just design the parts, run script and follow what stated there its in my GitHub code for this story will also be there check them out.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store