Exam 3 Review

CMPSC 304 Robotic Agents

Topics: ROS2 & Autonomy Concepts

Format: Paper exam, closed everything
Duration: 50 minutes
Questions: 10 total — 10 points
  • 8 multiple-choice (1 point each)
  • 2 scenario-based (1 point each)

Press Space or to advance

What's Covered on Exam 3?

Topics: Weeks 10 through 14

  • ROS2 fundamentals: nodes, topics, messages, pub/sub, services
  • SLAM & mapping: lidar, occupancy grids, scan matching, loop closure
  • Navigation: AMCL localization, costmaps, inflation, path planning, Nav2
  • Writing ROS2 nodes: publishers, timers, subscribers, actions, NavigateToPose
  • Simulation to reality: sim-to-real gap, TurtleBot 4 hardware, odometry drift

Study Materials

Projects

  • Project 4: ROS Simulation & TurtleBots

Slides

Readings

  • IAR Chapters 12–14
  • Nav2 Documentation

Activities

  • Activity 5: ROS2 Setup & First Simulation
  • Activity 6: Understanding SLAM
  • Activity 7: Navigation Under the Hood
  • Activity 8: Writing Your First ROS2 Node
  • Activity 9: ROS2 Services & Subscriber Nodes

Review Game Rules

How to Answer:
  • Multiple Choice: Raise the correct colored paper 🔴 🟢 ⚫ 🟠
  • First person with correct color gets to answer!
  • Scenarios: Raise your hand ✋
  • First hand up gets to answer!
Scoring:
  • 🥇 1st place team: +1 point on exam
  • 🥈 2nd place team: +0.5 points on exam
  • 🥉 3rd place team: +0.25 points on exam

Work together with your team, but only one person answers at a time!

MULTIPLE CHOICE
QUESTIONS

🔴 🟢 ⚫ 🟠

Multiple Choice 1

In ROS2, what is a "node"?

🔴 Red: A physical sensor or actuator that is wired directly to the robot’s onboard computer

🟢 Green: A single process that performs one job and communicates through topics, services, or actions

Black: A YAML configuration file that defines all of the robot’s launch parameters

🟠 Orange: A single cell inside an occupancy grid map that stores a probability value

Answer: 🟢 Green — A single process that performs one job

A node is an independent process in the ROS2 computation graph.

  • Each node does one job (e.g., lidar driver, path planner)
  • Nodes communicate via topics, services, and actions
  • A robot system is a graph of connected nodes

Command: ros2 node list shows all running nodes.

Multiple Choice 2

In the ROS2 publish/subscribe pattern, which statement is TRUE?

🔴 Red: Publishers must know the identity of all subscribers

🟢 Green: Each topic can only have one publisher and one subscriber

Black: Publishers and subscribers are decoupled — neither knows who the other is

🟠 Orange: Subscribers send acknowledgments back to publishers after each message

Answer: ⚫ Black — Publishers and subscribers are decoupled

  • Modularity: Swap a real lidar for a simulated one — subscribers don’t change
  • Multiple publishers and subscribers can share the same topic
  • Contrast with Arduino: Sensor & motor code tightly coupled in one file. ROS2 separates them.

Activity 5–6: Lidar publishes to /scan; Cartographer subscribes to build the map.

Multiple Choice 3

In a ROS2 occupancy grid map, what does a cell value of 0 represent?

🔴 Red: An obstacle (wall)

🟢 Green: Unknown / unexplored space

Black: Free space — safe to traverse

🟠 Orange: The robot’s current position

Answer: ⚫ Black — Free space

  • 0 (white in .pgm): Free space
  • 100 (black in .pgm): Occupied — wall or obstacle
  • −1 (grey in .pgm): Unknown / unexplored

Activity 6: The .yaml file specifies free_thresh and occupied_thresh to convert pixel values. resolution gives meters-per-pixel.

Multiple Choice 4

What does AMCL (Adaptive Monte Carlo Localization) do?

🔴 Red: Builds a map of the environment from lidar scans

🟢 Green: Plans the shortest path from start to goal

Black: Estimates the robot’s position on a known map using a particle filter

🟠 Orange: Controls the robot’s wheel motors directly

Answer: ⚫ Black — Estimates position on a known map via particles

AMCL answers: “Where am I on this map?”

  • Spreads particles (pose guesses) across possible positions
  • Compares each particle’s expected lidar scan to the actual scan
  • Good matches survive; poor matches removed — cloud converges

Key distinction: SLAM builds the map. AMCL uses an existing map to localize.

Multiple Choice 5

How do ROS2 services differ from topics?

🔴 Red: Services are faster than topics

🟢 Green: Services use a request/response pattern; topics use one-way publish/subscribe

Black: Topics can only carry sensor data; services carry commands

🟠 Orange: There is no difference — they are two names for the same thing

Answer: 🟢 Green — Services = request/response; Topics = one-way pub/sub

TopicsServices
Continuous data streamsOne-shot request → response
Fire and forgetClient waits for result
Many-to-manyOne-to-one per call
E.g., /cmd_vel, /scanE.g., /spawn, /set_pen

Activity 9: Service definitions use --- to separate request fields from response fields.

Multiple Choice 6

Which fields of a Twist message control a ground robot’s forward speed and turning?

🔴 Red: linear.z (forward) and angular.x (turn)

🟢 Green: linear.x (forward) and angular.z (turn)

Black: linear.y (forward) and angular.y (turn)

🟠 Orange: position.x (forward) and orientation.z (turn)

Answer: 🟢 Green — linear.x + angular.z

Twist has sub-fields: linear (x,y,z) and angular (x,y,z).

  • linear.x — forward/backward speed (m/s)
  • linear.y — lateral (sideways) speed — always 0 for differential-drive robots
  • angular.z — rotation rate (rad/s), positive = turn left

Activity 8: drive_pattern.py publishes Twist to /turtle1/cmd_vel. TurtleBot3 uses TwistStamped (adds a timestamp header).

Multiple Choice 7

A costmap has resolution: 0.05 m/cell and inflation_radius: 0.20 m. How many cells outward from a wall does inflation extend?

🔴 Red: 2 cells

🟢 Green: 20 cells

Black: 4 cells

🟠 Orange: 5 cells

Answer: ⚫ Black — 4 cells

Calculation: inflation_radius / resolution = 0.20 / 0.05 = 4 cells

  • The wall cell itself is lethal (cost 254)
  • Cells within robot_radiusinscribed (cost 253)
  • Remaining cells up to 4 away → decreasing cost
  • Beyond 4 cells → free (cost 0)

Activity 7: 14×14 costmap grid exercise used exactly these values.

Multiple Choice 8

What is the difference between the global costmap and the local costmap in Nav2?

🔴 Red: Global is for simulation; local is for real robots

🟢 Green: Global covers the entire map for path planning; local covers a small area around the robot for obstacle avoidance

Black: Global uses lidar; local uses the camera

🟠 Orange: No difference — same costmap at different zoom levels

Answer: 🟢 Green — Global = full map; Local = around robot

Global Costmap:
  • Covers the entire map
  • Based on the saved static map
  • Used for long-range path planning
Local Costmap:
  • Small area around the robot
  • Updated in real-time with live lidar
  • Used for immediate obstacle avoidance

Multiple Choice 9

Why would you use a ROS2 action (e.g., NavigateToPose) instead of publishing to a topic?

🔴 Red: Actions are faster than topics

🟢 Green: Actions provide feedback during execution, a result on completion, and can be cancelled

Black: Actions can carry larger messages than topics

🟠 Orange: Actions are required for any message that includes position data

Answer: 🟢 Green — Feedback, result, and cancellation

Actions are for long-running goals (e.g., navigate to a point).

  • Goal: Client sends a goal to the action server
  • Feedback: Server reports progress (e.g., current position)
  • Result: Final outcome when the task completes
  • Cancellable: Client can abort mid-execution

P4 Part 3: The waypoint node sends goals via NavigateToPose action, using send_goal_async() and a result_callback to chain waypoints.

Multiple Choice 10

When you save a SLAM map with map_saver_cli, what two files are created?

🔴 Red: map.png (color image) and map.json (waypoints)

🟢 Green: map.pgm (grayscale image) and map.yaml (metadata: resolution, origin, thresholds)

Black: map.bag (rosbag) and map.urdf (robot model)

🟠 Orange: map.csv (grid values) and map.xml (configuration)

Answer: 🟢 Green — .pgm image + .yaml metadata

  • .pgm — Grayscale image; white=free, black=occupied, grey=unknown
  • .yaml — Metadata including:
    • resolution: meters per pixel (e.g., 0.05)
    • origin: [x, y, yaw] of the bottom-left corner
    • free_thresh / occupied_thresh: probability cutoffs

Activity 6: Real-world map dimensions = pixel count × resolution.

Multiple Choice 11

In a ROS2 Python node, what does rclpy.spin(node) do?

🔴 Red: Rotates the robot in a circle

🟢 Green: Runs the event loop, processing timer callbacks and incoming messages

Black: Sends a single message and exits

🟠 Orange: Compiles the ROS2 package before running

Answer: 🟢 Green — Runs the event loop

rclpy.spin(node) is the event loop — like Arduino’s loop().

  • Blocks and repeatedly calls registered callbacks (timers, subscribers)
  • Without spin(), the node exits immediately
  • The node lifecycle: rclpy.init() → create node → rclpy.spin()rclpy.shutdown()

Activity 9: wall_avoider.py uses create_subscription() instead of a timer — both work with spin().

Multiple Choice 12

What is the key difference between open-loop and closed-loop control for a robot?

🔴 Red: Open-loop uses Wi-Fi; closed-loop uses Bluetooth

🟢 Green: Open-loop executes commands without reading sensors; closed-loop adjusts behavior based on sensor feedback

Black: Open-loop runs on a timer; closed-loop runs on a subscriber

🟠 Orange: Open-loop is for simulation only; closed-loop is for physical robots

Answer: 🟢 Green — Open-loop ignores sensors; closed-loop uses feedback

  • Open-loop: Send commands blindly (e.g., “drive forward 2 s, turn”). Drift accumulates.
  • Closed-loop: Read sensor data (pose, lidar) and adjust each callback.

Activity 8: drive_pattern.py = open-loop (timer-based, ignores pose). Activity 9: wall_avoider.py = closed-loop (subscriber reads pose, reacts).

Nav2 = closed-loop: AMCL updates position from lidar; local planner adjusts in real time.

Covered in: Writing ROS2 Nodes slides (publisher+timer vs subscriber patterns) + Activities 8 & 9.

SCENARIO-BASED
QUESTIONS

✋ First hand up gets to answer!

Scenario 1: Put in Order

A robot receives a navigation goal in Nav2. Put these steps in the correct order (1–6):
Local planner sends Twist to /cmd_vel AMCL localizes robot on the map Global planner finds a path on the global costmap Goal pose is received by Nav2 Local planner follows path using the local costmap Robot reaches the goal

Write the numbers 1–6 next to each step

Correct Order

  1. Goal pose received by Nav2 (RViz click or action client)
  2. AMCL localizes robot on the map (particle filter)
  3. Global planner finds path on global costmap
  4. Local planner follows path using local costmap
  5. Local planner sends Twist to /cmd_vel
  6. Robot reaches the goal (within xy_goal_tolerance)

Nav2 pipeline: Goal → AMCL → Global Planner → Local Planner → /cmd_vel → Done

Scenario 2: Interpret the Output

A student runs these commands. Answer the questions below.
$ ros2 topic info /cmd_vel
Type: geometry_msgs/msg/Twist
Publisher count: 2
Subscription count: 1

$ ros2 topic info /scan
Type: sensor_msgs/msg/LaserScan
Publisher count: 1
Subscription count: 3

a) How many nodes are publishing velocity commands?

b) How many nodes are consuming the lidar data?

c) Is it a problem that two nodes publish to /cmd_vel? Why?

Answer

a) 2 nodes are publishing velocity commands to /cmd_vel.

b) 3 nodes are consuming (subscribing to) the lidar data on /scan.

c) Yes, it can be a problem! Two publishers on /cmd_vel means both are sending competing velocity commands to the robot. The robot will execute whichever message arrives last, causing jerky or unpredictable behavior.

  • Example: Teleop + a navigation node both publishing — robot fights itself
  • Fix: Stop one publisher (e.g., close the teleop terminal before running Nav2)
  • /scan having 3 subscribers is fine — topics support many subscribers with no conflict

Scenario 3: Mark the Inflation Zone

Given this 8×8 grid (# = wall, . = free), with inflation_radius = 4 cells and resolution = 0.05 m/cell, mark every free cell that falls within the inflation buffer as I. Leave cells outside the buffer as .
########
#......#
#......#
#..##..#
#..##..#
#......#
#......#
########

Same approach as Activity 7: check each . cell’s distance to the nearest #. If ≤ 4 cells, mark it I.

Answer: Inflation Zone

Every interior cell is ≤ 4 cells from a #, so all become I:

########
#IIIIII#
#IIIIII#
#II##II#
#II##II#
#IIIIII#
#IIIIII#
########
  • # = wall (lethal, cost 254) • I = inflated (non-zero cost; closer to # = higher cost)
  • Planner picks the minimum-cost path — prefers cells far from walls
  • In a small space the entire interior can be inflated — planner still navigates, but every cell has a cost

Scenario 4: Match the ROS2 Command to Its Output

Match each command (1–5) with the description of what it shows (A–E):
Commands:
  1. ros2 node list
  2. ros2 topic echo /scan
  3. ros2 topic hz /turtle1/pose
  4. ros2 service list
  5. ros2 topic info /cmd_vel
Outputs:

A. Shows publisher/subscriber counts and message type for a topic

B. Prints live laser distance measurements as they arrive

C. Lists all available request/response endpoints

D. Shows all running processes in the ROS2 graph

E. Reports the publish rate (~62 Hz) of a topic

Answer

CommandMatchWhat It Does
ros2 node listDLists all running nodes (processes)
ros2 topic echo /scanBPrints live messages from the lidar topic
ros2 topic hz /turtle1/poseEMeasures publish rate (~62 Hz for turtlesim pose)
ros2 service listCLists available services (e.g., /spawn, /set_pen)
ros2 topic info /cmd_velAShows type, publisher count, subscriber count

Key: echo = see data, hz = see rate, info = see metadata, list = see everything.

Key Concepts to Remember

ConceptWhat It DoesKey Detail
NodeIndependent process in ROS2 graphOne job per node
TopicNamed channel for pub/sub messagingDecoupled, many-to-many
ServiceRequest/response communicationOne-shot, --- separator in definition
ActionLong-running goal with feedbackGoal → feedback → result; cancellable
TwistVelocity commandlinear.x + angular.z
SLAMBuild map + track position simultaneouslyInputs: lidar + odometry
Occupancy gridGrid map: 0=free, 100=occupied, −1=unknown.pgm + .yaml files
AMCLLocalize on a known mapParticle filter converges over time
CostmapCost grid for path planningGlobal (full map) + Local (around robot)
InflationExpand obstacles for safetycells = inflation_radius / resolution
Nav2 pipelineFull navigation stackGoal → AMCL → Planner → Controller → /cmd_vel
Sim-to-real gapBehavior difference sim vs. realityNoise, drift, latency, docking, battery

Essential ROS2 Commands

ros2 node list            # See all running nodes
ros2 topic list           # See all active topics
ros2 topic echo /scan     # View live messages on a topic
ros2 topic hz /scan       # Check publish rate
ros2 topic info /cmd_vel  # See type + publisher/subscriber counts
ros2 service list         # See all available services