Skip to content

Thread Experiments for ME495 Embedded Systems in robotics

License

Notifications You must be signed in to change notification settings

m-elwin/me495_threads

Repository files navigation

Overview

Provides some code to help explore threads and timing in python and ROS. You should examine the examples, run them, and write your own code to further your understand threads in python. At the end there are questions for you to turn in. You can edit this file and the code provided directly to give your answers.

Provided code

  1. scripts/gil_two_threads.py - Non-ROS python script that demonstrates the effect of the GIL on multi-threaded python code.
  2. nodes/node_square - Performs same computation in gil_two_threads but distributed across multiple nodes
  3. nodes/time_square - Times the computation of node_square
  4. launch/node_two_square.launch - launches the nodes used by the node_two_threads example
  5. nodes/sub_thread - Subscribes to a topic and sleeps for a specified period of time. Also prints diagnostic information. In conjunction with publishers that publish at different rates, you can begin to explore the nuances of subscriber threads.
  6. nodes/timers - Starts two timers and prints diagnostic information
  7. nodes/throughput - Publishes messages with different queue_lengths sizes, and at different frequencies.
  8. nodes/suborder - Subscribes, then initializes a variable. See what happens if something is already publishing...

Exercises

Use the provided code, code you write, and standard ROS tools to answer the following questions.

  1. Run scripts/gil_two_threads.py several times. How long did the single and multi-threaded computations take (use the best times among all trials)? Is the computation that uses two threads roughly twice as fast as the version that uses a single thread? Why or why not?

  2. Launch node_two_square.launch. This runs two nodes to perform the same computations as scripts/gil_two_threads.py and a third node to time the computations.

    • Is this faster than scripts/gil_two_threads.py? Why or why not?
    • Under what circumstances would this code outperform scripts/gil_two_threads.py and when would it underperform? (Hint, think about factors such as number of CPU cores available and setup overhead).
  3. Run scripts/race_cond.py a few times. Notice that it does not return consistent results. Your task is to use a a mutex (called a Lock in python) to fix the race condition in scripts/race_cond.py. The variable total should count precisely the total number of iterations of the loop in both threads, every time you run the code.

    • You can use with lock: to automatically acquire a lock at the beginning of the block and release it when the block ends, either normally or due to an exception being thrown.
  4. Run nodes/timers. After observing the behavior and seeing the output, what can you infer about the relationship between threads and rospy timers?

  5. What does rospy.spin do? You can infer a bit by commenting it out and by replacing it with an infinite while loop. Also feel free to look at the source code to answer this.

  6. Are main, callback1 and callback2 executed on the same thread or on different threads in nodes/timers?

  7. Run sub_thread and use rostopic to publish to the sleep topic. Is the sleeper subscriber running on the same thread as the main code?

  8. By manipulating the frequency at which you publish to sleep and the time you have the subscriber delay you can infer information about ROS's thread behavior. Write a node called pub_thread that uses a rospy.Timer to publish a message to sub_thread at 1 Hz. The node should count how many messages it publishes and print out an updated count after every time it publishes.

    • Describe the behavior that you see when the delay time sent by pub_thread is shorter than 1 second
    • What happens when you increase the delay time sent by pub_thread to 2 seconds?
      • Does one subscriber callback complete before the next subscriber callback is started?
      • After waiting for a few seconds, kill pub_thread but keep sub_thread running. What happens with sub_thread?
  9. Run two instances of the pub_thread node. What is the relationship between subscriber threads and publishers?

About

Thread Experiments for ME495 Embedded Systems in robotics

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published