Ask Your Question
0

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], '-')
            plt.axis("equal")
            plt.draw()
            plt.pause(0.00000000001)



# 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(
        [receiveData.rotMatrix.reshape(-1),
        receiveData.position,
        receiveData.linear_velocity,
        receiveData.angular_velocity])

        # 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))

        rate.sleep()

        counter += 1

        plt.ion()
        plt.show()

However I am getting the following error:

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 25 times

Last updated: Oct 17