Learning Robotics Using Python - Sample Chapter
Learning Robotics Using Python - Sample Chapter
ee
Sa
pl
Chapter 7, Programming Vision Sensors Using Python and ROS, talks about
the introduction to Open-CV, OpenNI, and PCL libraries and interfacing these
to ROS and programming using Python.
Chapter 8, Working with Speech Recognition and Synthesis Using Python
and ROS, discusses speech recognition and synthesis using various libraries
and interfacing it to ROS programming using Python.
Chapter 9, Applying Artificial Intelligence to ChefBot Using Python, covers
tutorials to build a ChatterBot. This can be used to make the robot interactive.
Chapter 10, Integration of ChefBot Hardware and Interfacing it into ROS, Using
Python, explores tutorials to integrate the complete hardware and essential software
section. It mainly discusses autonomous navigation of the service robot and how
to program it using ROS and Python.
Chapter 11, Designing a GUI for a Robot Using Qt and Python, covers tutorials
on how to build a GUI for the user who operates the robot in a typical restaurant.
The GUI is built using Qt and the PyQt Python wrapper.
Chapter 12, The Calibration and Testing of ChefBot, explores tutorials
on how to calibrate and test the robot for the final run.
Mechanical Design of a
Service Robot
The main purpose of this book is to learn robotics by designing and building robots
and programming it using Python. To learn robotics, we will first look at how to
mechanically design a robot from scratch. The robot that we are going to build is
used as a service robot in hotels and restaurants to serve food and drinks.
In this chapter, we can see various mechanical components used in this robot. Also,
we can see how to assemble its components. We can design and assemble the parts
using CAD tool and also build a 3D model of robot for simulating the robot.
The actual robot deployed in hotels may be big, but here we are intending to build a
miniature version of it only for testing our technology. If you are interested to build a
robot from scratch, this chapter is for you. If you are not interested to build the robot,
you can choose some robotic platforms already available on the market to work with
this book.
To build the robot body, we first need to know the requirements of designing the
robot; after getting the requirements, we can design it and draw the model in 2D
CAD tools to manufacture the robot parts. We can also discuss the 3D model to
simulate the robot for the next chapter.
[ 17 ]
The robot should travel at a speed between 0.25 m/s and 1 m/s
The robot should be able to move and supply food to any table
avoiding obstacles
Now, we can identify the mechanical design requirements such as payload, moving
speed, ground clearance, robot height, and the cost of the robot. We will design the
body and select components accordingly. Let's discuss the robot mechanism we can
use to match these requirements.
[ 18 ]
Chapter 2
The next step is to select the mechanical components of this robot drive system, that
is, mainly motors, wheels, and robot chassis. Based on the requirements, we will first
discuss how to select the motor.
[ 19 ]
The calculated RPM with 9 cm diameter wheel and 0.35 m/s speed is 74 RPM. We
can consider 80 RPM as the standard value.
Motor RPM = 80
Motor Torque = 10.32 kg-cm
Wheel diameter = 9 cm
[ 20 ]
Chapter 2
TurtleBot Robot
We will design a robot similar to TurtleBot with our own moving platform and
components. Our design will also have a three layer architecture. Let's see what
all tools we want before we start designing.
Before we start designing the robot chassis, we need to know about Computer-aided
design (CAD) tools. The popular tools available for CAD are:
SolidWorks (http://www.solidworks.com/default.htm)
AutoCAD (http://www.autodesk.com/products/autocad/overview)
Maya (http://www.autodesk.com/products/maya/overview)
Inventor (http://www.autodesk.com/products/inventor/overview)
Blender (http://www.blender.org/download/)
LibreCAD (http://librecad.org/cms/home.html)
[ 21 ]
The chassis design can be designed using any software you are comfortable with.
Here, we will demonstrate the 2D model in LibreCAD and the 3D model in Blender.
One of the highlights of these applications is that they are free and available for all
OS platforms. We will use a 3D mesh viewing tool called MeshLab to view and
check the 3D model design and use Ubuntu as the main operating system. Also, we
can see the installation procedures of these applications in Ubuntu 14.04.2 to start
the designing process. We will provide tutorial links to install applications in other
platforms too.
Visit http://librecad.org/cms/home/from-source/linux.html to
build LibreCAD from source
Visit http://librecad.org/cms/home/installation/linux.html to
install LibreCAD in Debian/Ubuntu
Visit http://librecad.org/cms/home/installation/rpm-packages.
html to install LibreCAD in Fedora
Visit http://librecad.org/cms/home/installation/osx.html to
install LibreCAD in OS X
[ 22 ]
Chapter 2
Visit http://librecad.org/cms/home/installation/windows.html to
install LibreCAD in Windows
We can find the documentation on LibreCAD at the following link:
http://wiki.librecad.org/index.php/Main_Page.
Installing LibreCAD
The installation procedure for all operating systems is provided. If you are an
Ubuntu user, you can simply install it from the Ubuntu Software Centre as well.
Installing Blender
Visit the following download page to install Blender for your OS platform: http://
www.blender.org/download/. You can find the latest version of Blender here. Also,
you can find the latest documentation on Blender at http://wiki.blender.org/.
If you are using Ubuntu/Linux, you can simply install Blender via Ubuntu Software
Centre.
Installing MeshLab
MeshLab is available for all OS platforms. The following link will provide you the
download links of prebuilt binaries and source code of MeshLab:
http://meshlab.sourceforge.net/
If you are an Ubuntu user, you can install MeshLab from an apt package manager
using the following command:
$sudo apt-get install meshlab
[ 23 ]
[ 24 ]
Chapter 2
A CAD toolbar has the necessary components to draw a model. The following
screenshot shows the detailed overview of the CAD toolbar:
Layer List: This will have layers used in the current drawing. A basic
concept in computer-aided drafting is the use of layers to organize a
drawing. A detailed explanation of layers can be found at:
http://wiki.librecad.org/index.php/Layers.
[ 25 ]
Block: This is a group of entities and can be inserted in the same drawing
more than once with different attributes at different locations, different scale,
and rotation angle. A detailed explanation of Blocks can be found at the
following link:
http://wiki.librecad.org/index.php/Blocks.
Now, start sketching by setting the unit of drawing. Set the drawing unit to
centimeter. Open LibreCAD, navigate to Edit | Application Preference. Set Unit as
Centimeters, as shown in the following screenshot:
Let's start with the base plate design. The base plate has provisions to connect
motors, place battery, and control board.
[ 26 ]
Chapter 2
[ 27 ]
The dimensions are not marked on the diagram; instead, it's mentioned in the
following table:
Parts
M1 and M2
5x4
C1 and C2
Radius = 1.5
S(Screw)
0.15
2.5 x 10
Base plate
Radius = 15
We can discuss more about motor dimensions and clamp dimensions later.
[ 28 ]
Chapter 2
The motor design can vary according to the motor selection; if necessary, this motor
can be taken as per the design and can be changed after simulation. The X value in
the motor diagram can vary according to the speed and torque of motors. This is the
gear assembly of motor.
The following figure shows a typical wheel that we can use with a diameter of 90 cm.
The wheel with a diameter of 86.5 mm will become 90 mm after placing the grip.
[ 29 ]
The motor needs to be mounted on the base plate; to mount, we need a clamp which
can be screwed onto the plate and also connect the motor to the clamp. The following
figure shows a typical clamp we can use for this purpose. It's an L-shaped clamp,
with which we can mount the motor on one side and fit another side to the plate:
[ 30 ]
Chapter 2
The middle plate can be held above the hollow tubes from the base plate. This
arrangement is connected using another hollow tube that extends from the middle
plate. The tube from the middle plate will have a screw at the bottom to fix the tube
from the base plate to the middle plate, and a hollow end to connect the top plate.
The top and side view of the tube extending from the middle plate is shown in the
following figure:
This tube will connect the middle plate to the base plate and at the same time
provide a provision to connect the top plate.
[ 31 ]
After the top plate design, the robot chassis design is almost finished; let's see the
3D model building of this robot using Blender. The 3D model is built for simulation
purpose and the 2D design we build is mainly for manufacturing purpose.
[ 32 ]
Chapter 2
The following screenshot shows the blender workspace and tools that can be used to
work with 3D models:
The main reason why we are using Blender here is so that we can model the robot
using Python scripts. Blender has an inbuilt Python interpreter and a Python script
editor for coding purpose. We are not discussing about the user interface of Blender
here. We can find a good tutorial of Blender on its website. Refer to the following
link to learn about Blender's user interface:
http://www.blender.org/support/tutorials/
[ 33 ]
Blender uses Python 3.x. Blender. Python API is generally stable, but some areas
are still being added to and improved. Refer to http://www.blender.org/
documentation/blender_python_api_2_69_7/ for the documentation on
Blender Python API.
Let's discuss Blender Python APIs that we will use in our robot model script.
Edit any data inside Blender, such as scenes, meshes, particles, and so on
Blender provides the bpy module to the Python interpreter. This module can be
imported in a script and gives access to blender data, classes, and functions; scripts
that deal with Blender data will need to import this module. The main Python
modules we will use in bpy are:
Context Access: This provides access to Blender user interface functions from
the (bpy.context) script.
Data Access: This provides access to the Blender internal data (bpy.data).
[ 34 ]
Chapter 2
After selecting the Scripting tab, we can see a text editor and Python console
window in Blender. In the text editor, we can code using Blender APIs and also
try Python commands via the Python console. Click on the New button to create
a new Python script and name it robot.py. Now, we can design the 3D model of
robot using only Python scripts. The upcoming section has the complete script to
design our robot model. We can discuss the code before running it. We hope you
have read the Python APIs of Blender from their site. The code in the upcoming
section is split into six Python functions to draw three robot plates, draw motors
and wheels, draw four support tubes, and export into the STereoLithography (STL)
3D file format for simulation.
[ 35 ]
2. This following function will draw the base plate of the robot. This function
will draw a cylinder with a radius of 5 cm and cut a portion from the
opposite sides so that motors can be connected using the Boolean modifier
inside Blender:
#This function will draw base plate
def Draw_Base_Plate():
3. The following two commands will create two cubes with a radius of 0.05
meter on either side of the base plate. The purpose of these cubes is to
create a modifier that subtracts the cubes from the base plate. So in effect,
we will get a base plate with two cuts. After cutting the two sides, we will
delete the cubes:
bpy.ops.mesh.primitive_cube_add(radius=0.05,
location=(0.175,0,0.09))
bpy.ops.mesh.primitive_cube_add(radius=0.05,
location=(-0.175,0,0.09))
####################################################
####################################################
#Adding base plate
bpy.ops.mesh.primitive_cylinder_add(radius=0.15,
depth=0.005, location=(0,0,0.09))
#Adding boolean difference modifier from first cube
bpy.ops.object.modifier_add(type='BOOLEAN')
bpy.context.object.modifiers["Boolean"].operation =
'DIFFERENCE'
bpy.context.object.modifiers["Boolean"].object =
bpy.data.objects["Cube"]
bpy.ops.object.modifier_apply(modifier="Boolean")
[ 36 ]
Chapter 2
######################################################
######################################################
#Adding boolean difference modifier from second cube
bpy.ops.object.modifier_add(type='BOOLEAN')
bpy.context.object.modifiers["Boolean"].operation =
'DIFFERENCE'
bpy.context.object.modifiers["Boolean"].object =
bpy.data.objects["Cube.001"]
bpy.ops.object.modifier_apply(modifier="Boolean")
#######################################################
#######################################################
#Deselect cylinder and delete cubes
bpy.ops.object.select_pattern(pattern="Cube")
bpy.ops.object.select_pattern(pattern="Cube.001")
bpy.data.objects['Cylinder'].select = False
bpy.ops.object.delete(use_global=False)
4. The following function will draw the motors and wheels attached to the
base plate:
#This function will draw motors and wheels
def Draw_Motors_Wheels():
5. The following commands will draw a cylinder with a radius of 0.045 and 0.01
meter in depth for the wheels. After creating the wheels, it will be rotated
and translated into the cut portion of the base plate:
#Create first Wheel
bpy.ops.mesh.primitive_cylinder_add(radius=0.045,
depth=0.01, location=(0,0,0.07))
#Rotate
bpy.context.object.rotation_euler[1] = 1.5708
#Transalation
bpy.context.object.location[0] = 0.135
[ 37 ]
6. The following code will add two dummy motors to the base plate.
The dimensions of motors are mentioned in the 2D design. The motor is
basically a cylinder and it will be rotated and placed in the base plate:
#Adding motors
bpy.ops.mesh.primitive_cylinder_add(radius=0.018,
depth=0.06, location=(0.075,0,0.075))
bpy.context.object.rotation_euler[1] = 1.5708
bpy.ops.mesh.primitive_cylinder_add(radius=0.018,
depth=0.06, location=(-0.075,0,0.075))
bpy.context.object.rotation_euler[1] = 1.5708
7. The following code will add a shaft to the motors, similar to the motor
model; the shaft is also a cylinder and it will be rotated and inserted into
the motor model:
#Adding motor shaft
bpy.ops.mesh.primitive_cylinder_add(radius=0.006,
depth=0.04, location=(0.12,0,0.075))
bpy.context.object.rotation_euler[1] = 1.5708
bpy.ops.mesh.primitive_cylinder_add(radius=0.006,
depth=0.04, location=(-0.12,0,0.075))
bpy.context.object.rotation_euler[1] = 1.5708
#######################################################
#######################################################
8. The following code will add two caster wheels on the base plate.
Currently, we are adding a cylinder as wheel. In the simulation,
we can assign it as a wheel:
#Adding Caster Wheel
bpy.ops.mesh.primitive_cylinder_add(radius=0.015,
depth=0.05, location=(0,0.125,0.065))
bpy.ops.mesh.primitive_cylinder_add(radius=0.015,
depth=0.05, location=(0,-0.125,0.065))
[ 38 ]
Chapter 2
10. This function will draw the middle plate of the robot:
#Draw middle plate
def Draw_Middle_Plate():
bpy.ops.mesh.primitive_cylinder_add(radius=0.15,
depth=0.005, location=(0,0,0.22))
#Adding top plate
def Draw_Top_Plate():
bpy.ops.mesh.primitive_cylinder_add(radius=0.15,
depth=0.005, location=(0,0,0.37))
11. This function will draw all the four supporting hollow tubes for all the
three plates:
#Adding support tubes
def Draw_Support_Tubes():
##################################################################
###########################
#Cylinders
bpy.ops.mesh.primitive_cylinder_add(radius=0.007,
depth=0.30, location=(0.09,0.09,0.23))
bpy.ops.mesh.primitive_cylinder_add(radius=0.007,
depth=0.30, location=(-0.09,0.09,0.23))
bpy.ops.mesh.primitive_cylinder_add(radius=0.007,
depth=0.30, location=(-0.09,-0.09,0.23))
bpy.ops.mesh.primitive_cylinder_add(radius=0.007,
depth=0.30, location=(0.09,-0.09,0.23))
[ 39 ]
12. This function will export the designed robot to STL. We have to change the
STL file path before executing the script:
#Exporting into STL
def Save_to_STL():
bpy.ops.object.select_all(action='SELECT')
#
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.export_mesh.stl(check_existing=True,
filepath="/home/lentin/Desktop/exported.stl",
filter_glob="*.stl", ascii=False,
use_mesh_modifiers=True, axis_forward='Y',
axis_up='Z', global_scale=1.0)
#Main code
if __name__ == "__main__":
Draw_Base_Plate()
Draw_Motors_Wheels()
Draw_Middle_Plate()
Draw_Top_Plate()
Draw_Support_Tubes()
Save_to_STL()
13. After entering the code in the text editor, execute the script by pressing
the Run Script button, as shown in the following screenshot. The output
3D model will be shown on the 3D view of Blender. Also, if we check the
desktop, we can see the exported.stl file for the simulation purposes:
[ 40 ]
Chapter 2
The exported.stl file can be opened with MeshLab and the following is a
screenshot of MeshLab:
Questions
1. What is robot modeling and what are its uses?
2. What is the aim of 2D robot model?
3. What is the aim of 3D robot model?
4. What is the advantage of Python scripting over manual modeling?
[ 41 ]
Summary
This chapter was mainly aimed at robot mechanical designing. It also included the
robot parameter calculation and robot chassis design. In robot designing, we first need
to have the prerequisites ready. Once it's ready, we can calculate the requirements of
the components to be used in the robot. After the component requirements are met, we
design the robot chassis according to the given requirements. The robot chassis design
involves 2D design of all the parts required to build the robot. After 2D designing, we
saw how to build the 3D robot model using Blender and Python script. The 3D model
was built using the dimensions that we used in 2D drawing. We also covered the
Blender Python script to build the entire 3D model. In this chapter, we got the design
of the robot that can be used to manufacture it, and also developed a 3D model for
simulation. In the next chapter, we will discuss the simulation of this robot model and
some popular simulation tools.
[ 42 ]
www.PacktPub.com
Stay Connected: