1 of 19

Python, NumPy, ROS

Daniel Seita

CSCI 545: Introduction to Robotics (Fall 2025)

September 04, 2025

Based on slides from Varun Bhatt, Bryon Tjanaka, Chris Denniston, Stefanos Nikolaidis.

1

2 of 19

Python

Tutorial if you are new to python:�https://learnxinyminutes.com/docs/python/

Note: even though it is meant as a “quick” refresher, the best way to learn how to use a programing language is to use it (often).

2

3 of 19

Basic Python Usage

  • Hello world
  • Variables: int, float
  • Arithmetic operators: exponent; int vs float division
  • String: add, conversion, format, split
  • List: append, indexing, slicing
  • Dict: add, index
  • If else: elif, a < b < c conditions, and/or
  • For: lists, ranges, enumerate, dict keys
  • Functions: return, default values, keyword args
  • Classes: init, attribute, methods
  • Try except: raise

3

4 of 19

Python Pros

  • Great for prototyping - often cited as remarkably similar to pseudocode.
  • Huge user base, so easy to find solutions to simple issues.
  • Can talk to efficient libraries written in C/C++/Fortran (e.g., numpy, pytorch).
  • Many robotics researchers use it (labmates, anyone that uses learning, etc.).

4

Image from https://slides.btjanaka.net/545-python-intro/#/6 (actual source unknown)

5 of 19

Python Cons

  • Interpreted and not compiled, hence slow versus compiled C/C++ code.
    • This is why numpy/pytorch/etc call C/C++ code, though we “interface” using Python.
  • Poor concurrency support (Python GIL).
    • See this note (mid-July) about making GIL optional.
  • Type checking should be done manually.
    • Highly recommend to use linters (available as standalone, editor plugins, or in IDEs).
    • Also make use of assertions.
  • Weird at times…
    • See https://github.com/satwikkansal/wtfpython for some fun examples.
    • Though, arguably this is true for many programing languages…

5

6 of 19

Useful Tools: Python Management

  • Conda and virtual environments (e.g., uv).

    • Helps to isolate your Python development per project.
    • Avoid sudo pip install xyz !!
  • Formatting style.
  • Python debugger.
    • import pdb; pdb.set_trace()
  • Python profiling.

6

7 of 19

Useful Tools: Code Editors

My personal usage:

  • VS Code (always update to latest).
  • Extensions: Python, Python Debugger, Remote-SSH, GitHub Copilot, Pylint (for linting), Git Lens (for GitHub).
  • Google Python Style Guide.

Note: everyone’s needs and preferences will vary. Please do what works for you.

7

Good editors with plugins for autocompletion, linting, profiling, debugging, formatting, etc. (image is for VS Code)

8 of 19

NumPy

Official tutorial:�https://numpy.org/devdocs/user/quickstart.html

This is one of the most widely used Python libraries.

Also closely related to PyTorch and other Deep Learning libraries.

8

9 of 19

Demo

  • Array creation: array(…), zeros, ones.
  • Array properties: shape (+ reshape), dtype.
  • Indexing (this can get complex).
  • Copying: a = b doesn't copy; explicit copy does.
  • Arithmetic: matrix multiplication (no operator in python 2, use dot), broadcasting, sum, mean (+ per axis).
  • Random: np.random for quick testing.
  • Vectorization.

9

10 of 19

Softmax with Numpy Arrays (Vectorized)

  • You can use np.exp() for ex.
  • x is a vector = [x1, x2, …, xn]
  • Watch out for numerical stability.
    • More near the end of this page.

10

11 of 19

ROS

Official tutorial:�http://wiki.ros.org/ROS/Tutorials

Widely used in robotics research code today (and historically).

https://spectrum.ieee.org/the-origin-story-of-ros-the-linux-of-robotics

Note: installing can be tricky.

Use Docker, VMware, follow instructions carefully.

Answers.ros.org is deprecated; use robotics.stackexchange.com, etc.

11

12 of 19

Typical Robot

Need some way for these modules to talk to each other

12

Sensors (e.g., cameras)

Actuators (e.g., motors)

Algorithms (e.g., planning, localization)

13 of 19

ROS (Robot Operating System)

  • Not an operating system like the name suggests.
  • Provides tools to facilitate robot operation.
  • Designed for a heterogeneous computer cluster.
    • Abstracts low-level details about hardware through common messages.
  • Popular in robotics community and has well designed packages.
    • Need state estimation code for planning research? Just get a state estimation package.
  • Default for labs in this class:
    • ROS Noetic with Ubuntu 22.04/24.04

13

14 of 19

ROS2

14

15 of 19

Nodes, Topics, Publishers, and Subscribers

  • Nodes (green ovals): For running code, performs computation.
    • Formally: an executable that uses ROS to communicate with other nodes.
  • Topics (yellow boxes): Place to post/read messages (like message boards).
  • Publishers: Nodes that write to a topic.
  • Subscribers: Nodes that read messages from a topic.

15

Image from https://slides.btjanaka.net/545-python-intro/#/18 (actual source unknown) Code snippet from Daniel Seita here, just an example, not meant to be run.

16 of 19

Demo

  • roscore Do this to get started (or roslaunch)
    • Sets up necessary infrastructure for ROS to operate, and for nodes to communicate.
  • Reading and writing messages:
    • rosnode list
    • rostopic list
    • rostopic pub
    • rostopic echo
  • Turtlesim:
    • rosrun turtlesim turtlesim_node
      • (the main node that controls the turtle)
    • rosrun turtlesim turtle_teleop_key
      • (node to read keyboard commands and send velocities)
  • Visualizations:
    • rqt_graph
    • rqt_plot, rviz

16

17 of 19

Lab 01

  • Catkin: Creating and installing of ROS packages
  • More fun with turtlesim
  • Rosbag: For recording data from topics and playing them back (maybe at a faster speed)
  • Rospy: Python wrapper for ROS
    • Python code for publishers and subscribers

17

18 of 19

Miscellaneous

  • ROS Services: alternative to topics for immediate response.
    • One node advertises a service.
    • Other nodes can send a request to those service and get a reply immediately.
    • Usually meant for one-time calls that end quickly, not for continuous data streams.
  • URDF: file format (in “xml”) to describe the geometry of a robot.
  • Robot simulators: for simulating robots in an environment.
    • E.g., Gazebo, Isaac Sim, MuJoCo, etc.

18

19 of 19

Slide Credits

19