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

Plot Computed Variables from ROS Subscriber

asked 2019-10-17 00:36:11 -0600

haloted gravatar image

updated 2019-10-17 13:59:26 -0600

jayess gravatar image

Hi all, I am trying to plot data that I obtained from ROS Subscriber. I have tried using this code from question

So I modified my code to be as follows:

def PlotVariable(self, msg):
        global counter

        if counter % 10 == 0:
            stamp = msg.header.stamp
            time = stamp.secs + stamp.nsecs * 1e-9
            plt.plot(time, self.linear_velocity[0], '-')

# If execution is needed will run after this line
if __name__ == "__main__":
    counter = 0   
    # This call initialises the class
    receiveData = OptitrackReceive()

    rate = rospy.Rate(10)

    while not rospy.is_shutdown():

        obsArray = np.hstack(

        # rospy.loginfo("Velocity: \r\n" + str(receiveData.velocity))
        rospy.loginfo("Position: \r\n" + str(receiveData.pos_converted.pose.position))
        rospy.loginfo("Orientation: \r\n" + str(receiveData.eulerAngle))
        rospy.loginfo("Rotational Matrix \r\n" + str(receiveData.rotMatrix))
        rospy.loginfo("Rotational Matrix from Quat: \r\n" + str(receiveData.rotMatrixComp))
        rospy.loginfo("Linear Velocity: \r\n" + str(receiveData.linear_velocity))
        rospy.loginfo("Angular Velocity: \r\n" + str(receiveData.angular_velocity))
        rospy.loginfo("Observation Array: \r\n" + str(obsArray))


        counter += 1


However I am getting the following error:

Traceback (most recent call last):
  File "/home/ted/catkin_ws/src/transmit_thrust/scripts/", line 201, in <module>
  File "/home/ted/.local/lib/python3.5/site-packages/matplotlib/", line 254, in show
    return _show(*args, **kw)
  File "/home/ted/.local/lib/python3.5/site-packages/matplotlib/", line 3248, in show
  File "/home/ted/.local/lib/python3.5/site-packages/matplotlib/", line 437, in show
  File "/home/ted/.local/lib/python3.5/site-packages/matplotlib/backends/", line 570, in show
    self._shown = True
  File "/usr/lib/python3.5/", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ted/.local/lib/python3.5/site-packages/matplotlib/backends/", line 28, in _restore_foreground_window_at_end
  File "/home/ted/.local/lib/python3.5/site-packages/matplotlib/backends/", line 568, in show
    self.canvas.manager.window.attributes('-topmost', 1)
  File "/usr/lib/python3.5/tkinter/", line 1636, in wm_attributes
RuntimeError: main thread is not in main loop
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/", line 621, in after_cancel
    data ='after', 'info', id)
RuntimeError: main thread is not in main loop

A solution to this problem or other solutions to plotting in real time would be appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-10-17 06:18:38 -0600

haloted gravatar image

I solved my own problem using rqt_plot. By publishing velocity information in a geometry_twist message to a specific set topic and call the rqt_plot to visualise it.

edit flag offensive delete link more

Question Tools



Asked: 2019-10-17 00:36:11 -0600

Seen: 295 times

Last updated: Oct 17 '19