Python Setup
Command Line Tools
The command line interface is a text-based interface for interacting with programs on your computer. By contrast, we interact with many programs through a graphical user interface. For the course, you should get comfortable with command line interfaces. See this short course for using the command line to run things on your computer.
Programs like pycharm and VS Code give you a combination of a graphical user interface with a command line tool option, and are popular. However, they introduce an additional learning curve.
Windows
See this guide or this guide to using python on Windows.
Packages
We use the following major packages in python:
numpy: working with \(n\)-D arraysmujoco: Modeling and simulation of robotic systemsmatplotlib: plotting
The following command uses pip to install these python packages
pip install numpy mujoco matplotlib
If you want to use machine learning, consider the following packages:
torch: training of differentiable modelstorchvision: tools for vision modelspandas: working with datatqdm: progress bars in the command linescikit-learn: source some classic datasets
Virtual Environment
Virtual environments allow you to install a custom set of packages with custom package versions for a project.
Inside the robotics-course directory, run
python -m venv code-venv
to create a blank virtual environment.
Activate it, and then install packages. Upon activation, the command line prompt changes to start with the name of the virtual environment. Here, it would be (code-venv):
source code-venv/bin/activate
pip install <packages above>
When you are done, you can run deactivate to return to the default global environment.
NumPy
A nice overview of using numpy is located online here.
Two KEY issues to watch out for:
- Standard matrix multiplication in
numpyuses the@symbol, not the*symbol. Users coming fromMatlaborJuliaoften trip over this issue a few times until they internalize the new symbol. - 2D Arrays are stored in row-major form.
MuJoCo
Drag-and-Drop Mode
Open a terminal window (command window - cmd on Windows) and run
python3 -m mujoco.viewerThis command should open a window with nothing visible yet. You can drag xml files describing a MuJoCo environment and it will show up. The xml needs to be self-contained or use complete paths to subfiles.
Usage Example: Simulation
Once you have installed mujoco in python, you can run this code by running this command in the command line from the root of this repository:
python3 code/basic_mujoco.py
Create your own file
Copy the code below and past it in a file <filename>.py in your current folder and save it. Then run
python3 <filename>.py
Code inside code/basic_mujoco.py:
import mujoco
import mujoco.viewer
import time
# Load a model from a file (won't do this here to avoid external file calls)
# model = mujoco.MjModel.from_xml_path('path/to/your/model.xml')
# Create a simple pendulum model
model = mujoco.MjModel.from_xml_string("""
<mujoco>
<worldbody>
<light name="light1" pos="0.3 0.3 1"/>
<geom name="ground" type="plane" pos=" 0 0 -1.2" size="2 2 0.1" rgba="0.5 0.5 0.5 1"/>
<body name="pendulum" pos="0 0 0">
<joint name="pivot" type="hinge" pos="0 0 0" axis="0 1 0" damping="0.1"/>
<geom name="rod" type="capsule" fromto="0 0 0 0 0 0.6" size="0.05" rgba="0.5 0.5 0.5 1"/>
<geom name="mass" type="sphere" pos="0 0 0.6" size="0.07" rgba="0.8 0.2 0.2 1"/>
</body>
</worldbody>
</mujoco>
""")
data = mujoco.MjData(model)
data.qpos[0]=0.01
# Create a viewer
viewer = mujoco.viewer.launch(model, data,
show_left_ui=False,
show_right_ui=False,
)