How to have matplotlib animation of message?
Hi, I'm completely new to both ROS and not that good at programming generally so this will probably have an easy solution. But how do you use matplotlib to visualize something that happens inside the callback function? You can't put the lines inside callback function since it will error and say "main thread is not in main loop".
The code I have is below, I want to visualize the scan ranges from a lidar with matplotlib. How do I get a continually updating figure for the scan ranges?
#! /usr/bin/env python
import rospy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
from sensor_msgs.msg import LaserScan
x = np.linspace(0, 2* np.pi, 100)
y=np.random.random_integers(1, 100, 100)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(x, y, 'b-')
def callback(scan):
distance_list = scan.ranges
print(distance_list)
print("scan angle min", scan.angle_min)
print("scan angle incr", scan.angle_increment)
print("scan angle max", scan.angle_max)
line1.set_ydata(distance_list)
fig.canvas.draw()
rospy.init_node('laser_subscriber')
sub = rospy.Subscriber('\scan', LaserScan, callback)
rospy.spin()
Asked by lidar_help on 2018-11-27 10:35:23 UTC
Answers
One solution I've used in the past is to use the callback to update the value of a global (or class variable) that the main function can access. Then have a loop in your main thread running that grabs that data and plots it. This way the rate that you can handle callbacks is not limited by how slow matplotlib is.
Asked by ChuiV on 2019-06-11 09:36:09 UTC
Comments