Advertisement
Guest User

miniversion

a guest
Jul 26th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import gym
  2. import numpy as np
  3. import os
  4. import rospy
  5. import roslaunch
  6. import subprocess
  7. import time
  8. import math
  9.  
  10. from gym import utils, spaces
  11. import gazebo_env
  12. from gym.utils import seeding
  13.  
  14. from mavros_msgs.msg import OverrideRCIn, PositionTarget
  15. from sensor_msgs.msg import LaserScan, NavSatFix
  16. from std_msgs.msg import Float64;
  17. from gazebo_msgs.msg import ModelStates
  18.  
  19. from mavros_msgs.srv import CommandBool, CommandTOL, SetMode
  20. from geometry_msgs.msg import PoseStamped,Pose,Vector3,Twist,TwistStamped
  21. from std_srvs.srv import Empty
  22.  
  23. #For Stereo
  24. from sensor_msgs.msg import Image
  25. import cv2
  26. from cv_bridge import CvBridge, CvBridgeError
  27. import matplotlib.pyplot as plt
  28. from VelocityController import VelocityController
  29.  
  30. cur_pose = PoseStamped()
  31. def pos_cb(msg):
  32.     global cur_pose
  33.     cur_pose = msg
  34.  
  35. #Setup
  36. launchfile = "mpsl.launch"
  37. subprocess.Popen("roscore")
  38. print ("Roscore launched!")
  39.  
  40. # Launch the simulation with the given launchfile name
  41. rospy.init_node('gym', anonymous=True)
  42.  
  43. fullpath = os.path.join(os.path.dirname(__file__),"launch", launchfile)
  44.  
  45. subprocess.Popen(["roslaunch",fullpath])
  46. print ("Gazebo launched!")
  47.  
  48. gzclient_pid = 0
  49.  
  50.  
  51. local_pos = rospy.Publisher('mavros/setpoint_position/local',PoseStamped,queue_size=10)
  52.  
  53. raw_pos = rospy.Publisher('mavros/setpoint_raw/local',PositionTarget, queue_size=10)
  54.  
  55. mode_proxy = rospy.ServiceProxy('mavros/set_mode', SetMode)
  56.  
  57. arm_proxy = rospy.ServiceProxy('mavros/cmd/arming', CommandBool)
  58.  
  59. pos_sub = rospy.Subscriber('/mavros/local_position/pose', PoseStamped, callback=pos_cb)
  60.  
  61. start_pos = PoseStamped()
  62. start_pos.pose.position.x = 0
  63. start_pos.pose.position.y = 0
  64. start_pos.pose.position.z = 10
  65.  
  66. #Setting rpos
  67. target = PositionTarget()
  68. target.header.frame_id = "home"
  69. target.header.stamp = rospy.Time.now()
  70. target.coordinate_frame = 1
  71. target.type_mask = 4067
  72.  
  73. target.position.x = 0
  74. target.position.y = 0
  75. target.position.z = 5
  76. target.velocity.x = 2
  77. target.velocity.y = 0
  78. target.yaw = 0
  79.  
  80. print "Waiting for mavros..."
  81. data = None
  82. while data is None:
  83.     try:
  84.         data = rospy.wait_for_message('/mavros/global_position/rel_alt', Float64, timeout=5)
  85.     except:
  86.         pass
  87. print "wait for service"
  88. rospy.wait_for_service('mavros/set_mode')
  89. print "got service"
  90.  
  91. for i in range(0,100):
  92.     local_pos.publish(start_pos)
  93.  
  94. #setup offboard
  95. try:
  96.     success = mode_proxy(0,'OFFBOARD')
  97.     print success
  98. except rospy.ServiceException, e:
  99.     print ("mavros/set_mode service call failed: %s"%e)
  100.  
  101.  
  102. #Arm
  103. print "arming"
  104. rospy.wait_for_service('mavros/cmd/arming')
  105. try:
  106.    success = arm_proxy(True)
  107.    print success
  108. except rospy.ServiceException, e:
  109.    print ("mavros/set_mode service call failed: %s"%e)
  110.    print "armed"
  111.  
  112. #Main method
  113. rate = rospy.Rate(10)
  114. print "Main Running"
  115. while not rospy.is_shutdown():
  116.     raw_pos.publish(target)
  117.     rate.sleep()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement