XTDrone/coordination/simplified_simulator/visualize.py

68 lines
1.6 KiB
Python
Raw Normal View History

2020-03-26 14:47:34 +08:00
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import numpy as np
import rospy
from geometry_msgs.msg import Twist,Pose,PoseStamped,TwistStamped
from gazebo_msgs.srv import GetModelState
import sys
2020-05-19 20:46:55 +08:00
uav_type = sys.argv[1]
uav_num = int(sys.argv[2])
2020-03-26 14:47:34 +08:00
2020-03-29 11:27:13 +08:00
step_time=0.005
2020-03-26 14:47:34 +08:00
pose_puber=[None]*uav_num
vel_puber=[None]*uav_num
plot_x=[0]*(uav_num)
plot_y=[0]*(uav_num)
plot_z=[0]*(uav_num)
fig = plt.figure()
plt.ion()
ax = Axes3D(fig)
label_lim = 20
def scroll_call_back(event):
global label_lim
if event.button == 'up':
label_lim+=2
#print('up')
elif event.button == 'down':
label_lim=label_lim-2 if label_lim>1 else 1
#print('down')
fig.canvas.mpl_connect('scroll_event', scroll_call_back)
def init():
ax.set_xlim3d(-label_lim, label_lim)
ax.set_ylim3d(-label_lim, label_lim)
ax.set_zlim3d(-label_lim, label_lim)
def pose_sub_callback(msg,id):
plot_x[id]=msg.pose.position.x
plot_y[id]=msg.pose.position.y
plot_z[id]=msg.pose.position.z
rospy.init_node('visualize')
rate = rospy.Rate(1/step_time)
for i in range(uav_num):
2020-05-19 20:46:55 +08:00
rospy.Subscriber(uav_type+'_'+str(i)+'/mavros/local_position/pose', PoseStamped, pose_sub_callback,i)
2020-03-26 14:47:34 +08:00
try:
while not rospy.is_shutdown():
init()
2020-04-03 11:04:41 +08:00
ax.scatter(plot_x, plot_y, plot_z, s=50, marker="o")
ax.tick_params(axis="x", labelsize=20)
ax.tick_params(axis="y", labelsize=20)
ax.tick_params(axis="z", labelsize=20)
2020-03-26 14:47:34 +08:00
plt.pause(step_time)
ax.cla()
rate.sleep()
except KeyboardInterrupt:
plt.ioff()