Table of Contents

The Python Repl

For better interaction while programming CRAM utilises a Repl interface. To get a similar experience in PyCRAM we use the IPython interactive console. In the following we will describe how to install IPython and how to use it as a PyCRAM Repl

Install IPython

To install IPython in Ubuntu just use the command below.

 apt-get install ipython3 

Setup IPython as a PyCRAM Repl

For convenience it is advised to create a setup file which configures the BulletWorld and defines some helper functions. This setup file could look something like this:

 
import rospy
from pycram.ik import ik
from pycram import bullet_world, bullet_world_reasoning
from pycram.robot_description import InitializedRobotDescription as robot_description

world = bullet_world.BulletWorld()

robot = bullet_world.Object("boxy", "robot", "resources/boxy.urdf", [0, 0, 0], [0, 0, 0, 1])
floor = bullet_world.Object("floor", "environment", "resources/plane.urdf")
milk = bullet_world.Object("milk", "milk", "resources/milk.obj")
world.robot = robot

tool = robot.get_link_position_and_orientation(robot_description.i.get_tool_frame("left"))
world.add_vis_axis(tool)

def move_milk_to_hand():
    hand_pose = robot.get_link_position(tool)
    milk.set_position(hand_pose)

Now start the IPython shell in the terminal by typing the following command:

ipython3

By default IPython starts in the current directory of the terminal. Next, navigate to the directory where the setup file is located. For this you can use the commands

cd

and

ls

Lastly, execute the setup file by typing

run <name of your setupfile>.py 

Now the BulletWorld window should show up and load all objects specified in the setup file. After everything is loaded you are presented with a prompt where you can now type python code as well as use the objects which are initialized in the executed code.

Enable autoreload

To use changes made in the Python file while the Repl is running you need to enable the iPython extension autoreload. This can be done using the iPython startup files, these are files which are always run if iPython is started. The startup files are located in ~/.ipython/profile_default/startup along with a README file which explains the usage of the startup files. In this directory create a file called 00-autoreload.ipy and enter the following code to the file.

%load_ext autoreload 
%autoreload 2

The first line loads the extension to iPython and the second line configures autoreload to reload all modules before the typed in code is executed.

Tip If you use iPython only as a Repl for PyCRAM you can create a symbolic link in the startup directory which links to your setup.py file.