Robotics StackExchange | Archived questions

Matplotlib dynamic plot in ROS2 callback

Hi Guys,

I recently started working on ROS2 and encountered the below issue.

I am trying to plot the points in callback as given in the below code snippet. However this doesn't seem like working.
Could anybody please let me know the right way to plot the points using matplotlib.

Note: I am not getting any errors related to Multi-threading.

    import rclpy
    from rclpy.node import Node

    from std_msgs.msg import String
    from custom_msgs.msg import Custom

    import matplotlib.pyplot as plt 

    global_x_cordinates = []
    global_y_cordinates = []

    class MinimalSubscriber(Node):

        def __init__(self):
            super().__init__('minimal_subscriber')
            self.subscription = self.create_subscription(
                Custom,
                '/custom/custom_topic',
                self.listener_callback,
                10)
            self.subscription  # prevent unused variable warning

        def listener_callback(self, msg):
            #self.get_logger().info('I heard: "%s"' % msg.data.speed_data)

            point_size = len(msg.path)
            print("Number of points : {}".format(point_size))

            x_cordinates = []
            y_cordinates = []

            for each_point in msg.path:
                x_cordinates.append(each_point.x)
                y_cordinates.append(each_point.y)

            global global_x_cordinates 
            global_x_cordinates = x_cordinates

            global global_y_cordinates
            global_y_cordinates = y_cordinates

            plt.plot(global_x_cordinates, global_y_cordinates)

    def main(args=None):
        rclpy.init(args=args)

        minimal_subscriber = MinimalSubscriber()

        plt.show()
        rclpy.spin(minimal_subscriber)

        # Destroy the node explicitly
        # (optional - otherwise it will be done automatically
        # when the garbage collector destroys the node object)
        minimal_subscriber.destroy_node()
        rclpy.shutdown()

    if __name__ == '__main__':
        main()

Asked by kk2105 on 2020-03-12 23:32:15 UTC

Comments

However this doesn't seem like working

What does this mean? Is there an error (if there is one, please copy/paste it)?

Asked by jayess on 2020-03-13 01:55:14 UTC

@jayess Thanks for the reply.
I was not getting any error. After doing some debugging, I think the problem is I am running the ROS2 inside a docker. Since the docker has no GUI, it is not able to render the plotted graph.
Kindly correct me if I am wrong?
Also I would like to know, if I am running the ROS2 on host machine will the above code snippet works fine?

Asked by kk2105 on 2020-03-13 02:04:15 UTC

Does it work if you allow docker to run the gui apps ?

In this answer you can see a few ways to enable gui in docker (using docker directly, using rocker, using Singularity)

Asked by marguedas on 2020-03-13 04:03:17 UTC

I was able to fix the issue by running the docker with --env="DISPLAY". docker run --env="DISPLAY".

Asked by kk2105 on 2020-03-19 03:03:59 UTC

Answers

The solution for this problem is to run the docker with --env="DISPLAY".

docker run --env="DISPLAY" -it <docker_image_id>

Asked by kk2105 on 2020-07-10 09:31:25 UTC

Comments