<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Cosi119a</title>
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <link rel="stylesheet" href="https://unpkg.com/reveal.js@4.0.2/dist/reset.css">
  <link rel="stylesheet" href="https://unpkg.com/reveal.js@4.0.2/dist/reveal.css">
  <link rel="stylesheet" href="https://unpkg.com/reveal.js@4.0.2/dist/theme/white.css" id="theme">
  <!--[if lt IE 9]>
  <script src="https://unpkg.com/reveal.js@4.0.2/lib/js/html5shiv.js"></script>
  <![endif]-->

  <!-- Custom CSS -->
  <link rel="stylesheet" href="/bootstrap/css/customslides.css">

  <style>
    .scrollable {
      bottom: 0;
      overflow-y: auto  !important;
      overflow-x: hidden !important;
    }

    
    .slides li ul {
      display: none;
    }
    
    .slides li p:not(:first-child) {
      display: none;
    }
    
  </style>
</head>

<body>
<div class="reveal">
  <div class="slides">
    <section data-markdown data-separator="^<slide_break></slide_break>" class="scrollable">
      <textarea data-template>
        **Reading: <a href="/content/topics/homeworks/hw_readings_119/70_hw_119_chapter3/">Read PRR Chapter 3</a>**

*Reminder: Readings are your responsibility. You will be expected to come to class prepared, having read the material, and ready to participate in the discussion*

<slide_break></slide_break>

## **Quiz today!**
* The quiz will take around 40 minutes

<slide_break></slide_break>

## Logistics

* Any questions at the start?
* Our [Autonomous Robotics Lab Notebook](https://campusrover.github.io/labnotebook2/)

<slide_break></slide_break>

## Programming Assignment Discussion

* I corrected some things in the online description. Please ASK QUESTIONS if something is confusing
* Review first programming assignment: <a href="/content/topics/robotics/robotics_pas_2/10_pa_basic_control/">PA Basic Control</a>

<slide_break></slide_break>

## Chapter 3 Review

<slide_break></slide_break>

<h3 id="introduction-to-nodes-and-topics">Introduction to Nodes and Topics</h3>

<ul>
  <li>ROS is a distributed operating system</li>
  <li>Meaning it controls potentially (and typically) multiple computers</li>
  <li>One of them is in the robot itself</li>
  <li>Almost always there is at least one more which is more powerful</li>
  <li>It could be your laptop or your web workspace which runs on our <code>rover</code> cluster</li>
  <li>The computers have to be on the same network</li>
</ul>

<slide_break></slide_break>

<h3 id="communication-model">Communication model</h3>

<ul>
  <li>Message based</li>
  <li>Most important model is “publish/subscribe”</li>
  <li>publish a <strong>topic</strong>, subscribe to a <strong>topic</strong></li>
  <li>Many to many (Nodes can publish to many topics and subscribe to many topics)</li>
  <li>Topics have a name (e.g. /cmd_vel)</li>
  <li>Topics have an associated message</li>
  <li>Important to understand the difference between Topics and Messages</li>
  <li>Think of the topic as a function name, and the Message as argument list.</li>
</ul>

<slide_break></slide_break>

<h3 id="ros-nodes">ROS Nodes</h3>

<ul>
  <li>A ROS application is designed as a collection of separate ROS Nodes that communicate with each other</li>
  <li>ROS Nodes are separate processes and they can run on the different computers</li>
  <li>ROS Nodes are nothing more than programs written in C++ or Python</li>
  <li>What makes them a “node” is that they communicate with other programs in very specific ways</li>
</ul>

<slide_break></slide_break>

<h3 id="roscore">roscore</h3>

<ul>
  <li>roscore is a special program provided by ROS</li>
  <li>It is the “traffic cop” or “name service”</li>
  <li>It is the center of the ROS universe</li>
  <li>It is run on one of the computers</li>
  <li>Whenever a new ROS node starts, the first thing it does is to announce itself to roscore</li>
  <li>That way roscore always has a picture of the graph of ROS Nodes</li>
</ul>

<slide_break></slide_break>

<h3 id="ros-topics">ROS topics</h3>

<ul>
  <li>ROS Nodes are separate processes (which can even be running on separate computers)</li>
  <li>A function, method or class created in one Node is invisible and cannot be used or called by another</li>
  <li>They are really really separate</li>
  <li>ROS Nodes talk to each other with ROS topics</li>
</ul>

<slide_break></slide_break>

<h3 id="topics">Topics</h3>

<ul>
  <li>Topics have a name and a message type</li>
  <li>Two of the most common basic ones: <code>/cmd_vel</code> and <code>/odom</code></li>
  <li>Topics can be published and subscribed</li>
  <li>Which sends a receives messages of a certain message type</li>
  <li><code>cmd_vel</code>: sent to a robot to command it to move (locomotion)</li>
  <li><code>odom</code>:  received from a robot to report where (it thinks) it is</li>
</ul>

<slide_break></slide_break>

<h3 id="message-types">Message Types</h3>

<ul>
  <li><code>Twist</code> message is used for <code>cmd_vel</code></li>
  <li>Expresses a velocity in linear and angular aspects
    <ol>
      <li>Note our robot can only move forward and backward (x axis)</li>
      <li>And it can only rotate around (z axis)</li>
      <li>Twist doesn’t say anything about where the robot (thinks it) is</li>
      <li>“cmd_vel” is the topic to <em>directly</em> command the base (people often refer to the robot as a whole as the base) to move. The message type for cmd_vel is Twist</li>
    </ol>
  </li>
</ul>

<slide_break></slide_break>

<h3 id="publishing-and-subscribing">Publishing and Subscribing</h3>

<ul>
  <li>A node can “publish” a ROS topic</li>
  <li>Another node can “subscribe” to a ROS topic
    <ul>
      <li>The two ROS Nodes don’t know about each other</li>
      <li>A node can subscribe to a ROS topic that no one is publishing</li>
      <li>Two ROS Nodes can publish the same ROS topic with no one subscribing</li>
      <li>etc. etc. etc.</li>
    </ul>
  </li>
  <li>What is being published and subscribed?
    <ul>
      <li>messages</li>
      <li>A message is a data structure with named and typed fields</li>
      <li>A collection of messages already exists, e.g. Int32, which is message with one element, a 32 bit integer.</li>
    </ul>
  </li>
</ul>

<slide_break></slide_break>

<h3 id="ros-topic-cmdvel">ROS Topic /cmd_vel</h3>

<ul>
  <li>A common ROS topic that you will become very familiar with</li>
  <li>When a node (for example one you write) publishes <code>cmd_vel</code> it is asking the robot to move</li>
  <li>The message will include speed and direction</li>
  <li>There is a node (e.g. <code>motor_control</code>) which subscribes to cmd_vel</li>
  <li><code>motor_control</code> comes with the robot and is run automatically</li>
  <li><code>motor_control</code> is connected to the motors.</li>
  <li><code>motor_control</code> exists and runs even if no other node is publishing <code>cmd_vel</code></li>
  <li>Or if more than one node publishes <code>cmd_vel</code></li>
  <li>They are totally decoupled!</li>
</ul>

<slide_break></slide_break>

<h3 id="ros-topic-odom">ROS Topic /odom</h3>

<ul>
  <li>Another very common ROS topic that you will become very familiar with</li>
  <li>Odometry is the term for data coming from the wheels which allow the robot to estimate its location and speed.</li>
  <li>In this case, ROS is publishing the ROS topic <code>odom</code> constantly (say 30 times per second)</li>
  <li>Any node can subscribe to that ROS topic if it needs to know the current Odometry values</li>
  <li><a href="http://docs.ros.org/en/noetic/api/nav_msgs/html/msg/Odometry.html">Here is the message definition</a></li>
  <li><a href="https://claude.site/artifacts/e55a3b72-181e-4871-b274-e5db03f9c3d4">Here is a diagram</a></li>
</ul>

<pre><code class="language-python">
subs = rospy.Subscriber("/odom", Odometry, odom_callback)

def odom_callback(msg):
    x_position = msg.pose.pose.position.x
</code></pre>

<slide_break></slide_break>

<h3 id="ros-topic-scan">ROS Topic /scan</h3>

<ul>
  <li>Another very common ROS topic that you will learn and use a lot</li>
  <li>/scan contains information coming from the LIDAR sensor on a robot</li>
  <li>It also is published constantly, say 30 times per second</li>
  <li>It contains data about the surrounding area and obstacles seen by the robot</li>
  <li>Any node can subscribe to that ROS topic if it needs to know the current values</li>
  <li><a href="http://docs.ros.org/en/melodic/api/sensor_msgs/html/msg/LaserScan.html">Here is the message definition</a></li>
  <li><a href="https://claude.site/artifacts/d1c6bc70-cf0a-4795-a0e5-35f2dffaca1d">Here is a diagram</a></li>
</ul>

<pre><code class="language-python">
def scan_callback(self, msg):
  indices = list(range(350, 360)) + list(range(0, 11))
  relevant_ranges = [msg.ranges[i] for i in indices]
  valid_ranges = [r for r in relevant_ranges if msg.range_min &lt; r &lt; msg.range_max]
  if valid_ranges:
    self.average_distance = sum(valid_ranges) / len(valid_ranges)

def main():
    rospy.init_node('scan_subscriber', anonymous=True)
    rospy.Subscriber("/scan", LaserScan, scan_callback)
    rospy.spin()
</code></pre>

<slide_break></slide_break>

<h3 id="takeaways">Takeaways</h3>

<ul>
  <li>ROS: distributed operating system</li>
  <li>Node: One process which can run on any computer</li>
  <li>roscore: Single process which is the master controller</li>
  <li>Topic: Messages sent between nodes</li>
</ul>



<slide_break></slide_break>

<h2 class="shadow p-3 bg-white rounded">Thank you. Questions?<img class="img-fluid w-100" src="https://picsum.photos/800/100.jpg" /> <small>&nbsp;(random Image from picsum.photos)</small></h2>
      </textarea>
    </section>
  </div>
</div>

<script src="https://unpkg.com/reveal.js@4.0.2/dist/reveal.js"></script>
<!-- Plugins -->
<script src="https://unpkg.com/reveal.js@4.0.2/plugin/markdown/markdown.js"></script>
<script src="https://unpkg.com/reveal.js@4.0.2/plugin/highlight/highlight.js"></script>

<script>
    // Full list of configuration options available at:
    // https://github.com/hakimel/reveal.js#configuration
    Reveal.initialize({
        minScale: 0.2,
        maxScale: 1.4,

        markdown: {
            breaks: true
        },

        
        transition: "none",
        
        progress: true,
        

        // Optional reveal.js plugins
        plugins: [ RevealMarkdown, RevealHighlight ]
    })
</script>
</body>
