ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Matplotlib dynamic plot in ROS2 callback

asked 2020-03-12 23:32:15 -0600

kk2105 gravatar image

updated 2020-03-12 23:33:44 -0600

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()
edit retag flag offensive close merge delete

Comments

However this doesn't seem like working

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

jayess gravatar image jayess  ( 2020-03-13 01:55:14 -0600 )edit

@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?

kk2105 gravatar image kk2105  ( 2020-03-13 02:04:15 -0600 )edit
1

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)

marguedas gravatar image marguedas  ( 2020-03-13 04:03:17 -0600 )edit

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

kk2105 gravatar image kk2105  ( 2020-03-19 03:03:59 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-07-10 09:31:25 -0600

kk2105 gravatar image

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

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

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-03-12 23:32:15 -0600

Seen: 1,790 times

Last updated: Jul 10 '20