def csv2rosbag(csvPath, outputBag): """ Creates a ROS bag file from a CSV file. In this script, only the time and altitude informations written into the ROS bag. """ log = pd.read_csv(csvPath) try: with rosbag.Bag(outputBag, 'w') as bag: for row in range(log.shape[0]): # Populate the data elements for time in seconds timestamp = rospy.Time.from_sec(log['Time(seconds)'][row]) altitude_msg = Altitude() altitude_msg.header.stamp = timestamp # Populate the data elements for altitude in meters # e.g. altitude_msg.relative = df['Relative'][row] altitude_msg.local = log['Altitude(meters)'][row] bag.write(TOPIC, altitude_msg, timestamp) print(bag) except: print("Error: Unable to open CSV file!") sys.exit() finally: bag.close()
def __init__(self, _threaded_mode = False): self.data_dir = "/tmp/BHG_DATA" self.csv_filename = "default_data.csv" self.datetimeData = "" self.is_recording = False self.rel_alt = Altitude() self.gps_fix = NavSatFix() self.odom = Odometry() self.imu_mag = MagneticField() self.imu_data = Imu() self.vel_gps = TwistStamped() self.temp_imu = Temperature() self.trigtime = "TrigTimeNotSet" self.arduino_dev = rospy.get_param('~arduino_dev', '/dev/ttyACM0') self.arduino_timeout = rospy.get_param('~arduino_timeout', 2) self.ardunio_ser = '' self.pub = rospy.Publisher('trig_timer', String, queue_size=10) rospy.Subscriber('/directory', String, self.directory_callback) rospy.Subscriber("/record", Bool, self.record_callback) rospy.Subscriber("/mavros/altitude", Altitude, self.alt_cb) # change to /global_position/rel_alt Float64 rospy.Subscriber("/mavros/global_position/global", NavSatFix, self.gps_cb) rospy.Subscriber("/mavros/global_position/local", Odometry, self.odom_cb) rospy.Subscriber("/mavros/imu/mag", MagneticField, self.mag_cb) rospy.Subscriber("/mavros/imu/data", Imu, self.imu_cb) rospy.Subscriber("/mavros/global_position/raw/gps_vel", TwistStamped, self.vel_cb) rospy.Subscriber("/mavros/imu/temperature_imu", Temperature, self.temp_cb)
def setUp(self): self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.home_position = HomePosition() self.local_position = PoseStamped() self.mission_wp = WaypointList() self.state = State() self.mav_type = None self.sub_topics_ready = { key: False for key in [ 'alt', 'ext_state', 'global_pos', 'home_pos', 'local_pos', 'mission_wp', 'state' ] } # ROS services service_timeout = 30 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: self.fail("failed to connect to services") self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('/mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) self.wp_clear_srv = rospy.ServiceProxy('mavros/mission/clear', WaypointClear) self.wp_push_srv = rospy.ServiceProxy('mavros/mission/push', WaypointPush) # ROS subscribers self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.home_pos_sub = rospy.Subscriber('mavros/home_position/home', HomePosition, self.home_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.mission_wp_sub = rospy.Subscriber('mavros/mission/waypoints', WaypointList, self.mission_wp_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback)
def csv2rosbag(csvFile, outputFile): log = pd.read_csv(csvFile) try: with rosbag.Bag(outputFile, 'w') as bag: for row in range(log.shape[0]): timestamp = rospy.Time.from_sec(log['Time(seconds)'][row]) altitude_msg = Altitude() altitude_msg.header.stamp = timestamp altitude_msg.local = (log['Altitude(meters)'][row]) # Populate the data elements for IMU # e.g. imu_msg.angular_velocity.x = df['a_v_x'][row] bag.write(TOPIC, altitude_msg, timestamp) print(bag) finally: bag.close()
def test_telemetry_serializer(self): """Tests telemetry serializer.""" # Set up test data. navsat = NavSatFix() navsat.latitude = 38.149 navsat.longitude = -76.432 altitude = Altitude() altitude.amsl = 30.48 pose_stamped = PoseStamped() pose_stamped.pose.orientation.w = 1.0 data = serializers.TelemetrySerializer.from_msg( navsat, altitude, pose_stamped) altitude_msl = serializers.meters_to_feet(altitude.amsl) # Compare. self.assertEqual(data["latitude"], navsat.latitude) self.assertEqual(data["longitude"], navsat.longitude) self.assertEqual(data["altitude_msl"], altitude_msl) self.assertEqual(data["uas_heading"], 90.0)
def setUp(self): self.rate = rospy.Rate(10) # 10hz self.has_global_pos = False self.global_position = NavSatFix() self.extended_state = ExtendedState() self.altitude = Altitude() self.state = State() self.mc_rad = 5 self.fw_rad = 60 self.fw_alt_rad = 10 self.last_alt_d = None self.last_pos_d = None self.mission_name = "" self.sub_topics_ready = { key: False for key in ['global_pos', 'home_pos', 'ext_state', 'alt', 'state'] } # setup ROS topics and services try: rospy.wait_for_service('mavros/mission/push', 30) rospy.wait_for_service('mavros/cmd/arming', 30) rospy.wait_for_service('mavros/set_mode', 30) except rospy.ROSException: self.fail("failed to connect to mavros services") self.wp_push_srv = rospy.ServiceProxy('mavros/mission/push', WaypointPush) self.set_arming_srv = rospy.ServiceProxy('/mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('/mavros/set_mode', SetMode) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.home_pos_sub = rospy.Subscriber('mavros/home_position/home', HomePosition, self.home_position_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.mavlink_pub = rospy.Publisher('mavlink/to', Mavlink, queue_size=1) # need to simulate heartbeat to prevent datalink loss detection self.hb_mav_msg = mavutil.mavlink.MAVLink_heartbeat_message( mavutil.mavlink.MAV_TYPE_GCS, 0, 0, 0, 0, 0) self.hb_mav_msg.pack(mavutil.mavlink.MAVLink('', 2, 1)) self.hb_ros_msg = mavlink.convert_to_rosmsg(self.hb_mav_msg) self.hb_thread = Thread(target=self.send_heartbeat, args=()) self.hb_thread.daemon = True self.hb_thread.start()
def test_telemetry_serializer(self): """Tests telemetry serializer.""" # Set up test data. navsat = NavSatFix() navsat.latitude = 38.149 navsat.longitude = -76.432 altitude = Altitude() altitude.amsl = 30.48 pose_stamped = PoseStamped() pose_stamped.pose.orientation.w = 1.0 data = serializers.TelemetrySerializer.from_msg(navsat, altitude, pose_stamped) altitude_msl = serializers.meters_to_feet(altitude.amsl) # Compare. self.assertEqual(data["latitude"], navsat.latitude) self.assertEqual(data["longitude"], navsat.longitude) self.assertEqual(data["altitude_msl"], altitude_msl) self.assertEqual(data["uas_heading"], 90.0)
def __init__(self, index): self.uav_index = index # #Subscribe to its global position topic # self.global_position = NavSatFix() # self.global_sub_name = "/uav{0}/mavros/global_position/global".format(index) # self.global_sub = rospy.Subscriber(self.global_sub_name, # NavSatFix, self.global_position_callback) # rospy.loginfo("UAV{0} global_position subscribed".format(index)) #Subscribe to its local position topic self.local_pose = PoseStamped() self.local_pose_sub_name = "/uav{0}/mavros/local_position/pose".format( index) self.local_pose_sub = rospy.Subscriber(self.local_pose_sub_name, PoseStamped, self.local_pose_callback) rospy.loginfo("UAV{0} position subscribed".format(index)) #Subscribe to its altitude topic self.altitude = Altitude() self.altitude_sub_name = "/uav{0}/mavros/altitude".format(index) self.altitude_sub = rospy.Subscriber(self.altitude_sub_name, Altitude, self.altitude_callback) rospy.loginfo("UAV{0} altitude subscribed".format(index)) #Publish the required velocity_control topic self.vel_cont = Twist() self.vel_pub_name = "/uav{0}/vel_control_topic".format(index) self.vel_pub = rospy.Publisher(self.vel_pub_name, Twist, queue_size=10) rospy.loginfo("UAV{0} Vel_Control publisher set up".format(index)) #Set up initial Twist message self.vel_cont.linear.x = 0.0 self.vel_cont.linear.y = 0.0 self.vel_cont.linear.z = 50.0 self.vel_cont.angular.x = 0.0 self.vel_cont.angular.y = 0.0 self.vel_cont.angular.z = 0.0 #Publish in a separate thread to better prevent failsafe self.pub_thread = Thread(target=self.publish_topic, args=()) self.pub_thread.daemon = True self.pub_thread.start() #Set current position self.curr_pose = self.local_pose.pose #Set current altitude self.curr_alt = self.altitude.local
def test_post_telemetry(self): """Tests posting telemetry data through client.""" # Set up test data. url = "http://interop" client_args = (url, "testuser", "testpass", 1.0) with InteroperabilityMockServer(url) as server: # Setup mock server. server.set_root_response() server.set_login_response() server.set_telemetry_response() # Connect client. pose_stamped = PoseStamped() pose_stamped.pose.orientation.w = 1.0 client = InteroperabilityClient(*client_args) client.wait_for_server() client.login() client.post_telemetry(NavSatFix(), Altitude(), pose_stamped)
def __init__(self): rospy.init_node('OffboardPosCtl', anonymous=True) self.pos = PoseStamped() self.altitude = Altitude() self.imu_data = Imu() self.state = State() self.radius = 1 self.pos_setpoint_pub = rospy.Publisher( 'mavros/setpoint_position/local', PoseStamped, queue_size=1) self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.imu_data_sub = rospy.Subscriber('mavros/imu/data', Imu, self.imu_data_callback) self.pos_sub = rospy.Subscriber('mavros/pos/pose', PoseStamped, self.position_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode)
def __init__(self): self.state = State() self.altitude = Altitude() self.global_position = HomePosition() self.local_position = PoseStamped() #fcu local position self.local_position_odom = Odometry() self.extended_state = ExtendedState() self.mode = '' self.rate = rospy.Rate(10) self.service_timeout = 30 self.offboard_request_threshold = rospy.Duration(5.0) self.arming_request_threshold = rospy.Duration(5.0) self.imu = Imu() self.current_yaw = None self.setup_pubsub() self.setup_services() self.current_target_pose = PositionTarget() self.current_pose = Point() self.home_pose = Point(0.0,0.0,0.0) self.waypoint_1 = Point(0.0,0.0,3.0) self.should_start_mission = False
def __init__(self): self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.imu_data = Imu() self.home_position = HomePosition() self.local_position = PoseStamped() self.mission_wp = WaypointList() self.state = State() self.mav_type = None self.pos = PoseStamped() self.radius = 1 self.sub_topics_ready = { key: False for key in [ 'alt', 'ext_state', 'global_pos', 'home_pos', 'local_pos', 'mission_wp', 'state', 'imu' ] } # ROS services service_timeout = 30 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: self.fail("failed to connect to services") self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) self.wp_clear_srv = rospy.ServiceProxy('mavros/mission/clear', WaypointClear) self.wp_push_srv = rospy.ServiceProxy('mavros/mission/push', WaypointPush) # ROS subscribers self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.imu_data_sub = rospy.Subscriber('mavros/imu/data', Imu, self.imu_data_callback) self.home_pos_sub = rospy.Subscriber('mavros/home_position/home', HomePosition, self.home_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.mission_wp_sub = rospy.Subscriber('mavros/mission/waypoints', WaypointList, self.mission_wp_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) # ROS publisher self.pos_setpoint_pub = rospy.Publisher( 'mavros/setpoint_position/local', PoseStamped, queue_size=1) self.pos_thread = Thread(target=self.send_pos, args=()) self.pos_thread.daemon = True self.pos_thread.start()
def setUp(self): print('@ctrl_server@ uav{0} setUp'.format(self.uav_number)) self.pos = PoseStamped() self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.home_position = HomePosition() self.local_position = PoseStamped() self.misson_wp = WaypointList() self.state = State() self.scan = LaserScan() self.mav_type = None self.ready = False # Target offset radius self.radius = 0.25 self.sub_topics_ready = { key: False for key in [ 'alt', 'ext_state', 'global_pos', 'home_pos', 'local_pos', 'mission_wp', 'state', 'scan' ] } # ROS services service_timeout = 60 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service(self.uav_prefix + 'mavros/param/get', service_timeout) rospy.wait_for_service(self.uav_prefix + 'mavros/cmd/arming', service_timeout) rospy.wait_for_service(self.uav_prefix + 'mavros/mission/push', service_timeout) rospy.wait_for_service(self.uav_prefix + 'mavros/mission/clear', service_timeout) rospy.wait_for_service(self.uav_prefix + 'mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: print("@ctrl_server@ failed to connect to services") self.get_param_srv = rospy.ServiceProxy(self.uav_prefix + 'mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy(self.uav_prefix + 'mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy(self.uav_prefix + 'mavros/set_mode', SetMode) self.wp_clear_srv = rospy.ServiceProxy(self.uav_prefix + 'mavros/mission/clear', WaypointClear) self.wp_push_srv = rospy.ServiceProxy(self.uav_prefix + 'mavros/mission/push', WaypointPush) # ROS subscribers self.alt_sub = rospy.Subscriber(self.uav_prefix + 'mavros/altitude', Altitude, self.altitude_callback) self.ext_state_sub = rospy.Subscriber(self.uav_prefix + 'mavros/extended_state', ExtendedState, self.extended_state_callback) self.global_pos_sub = rospy.Subscriber(self.uav_prefix + 'mavros/global_position/global', NavSatFix, self.global_position_callback) self.home_pos_sub = rospy.Subscriber(self.uav_prefix + 'mavros/home_position/home', HomePosition, self.home_position_callback) self.local_pos_sub = rospy.Subscriber(self.uav_prefix + 'mavros/local_position/pose', PoseStamped, self.local_position_callback) self.mission_wp_sub = rospy.Subscriber(self.uav_prefix + 'mavros/mission/waypoints', WaypointList, self.mission_wp_callback) self.state_sub = rospy.Subscriber(self.uav_prefix + 'mavros/state', State, self.state_callback) self.get_scan_srv = rospy.Subscriber('iris_rplidar_' + self.uav_number + '/laser/scan', LaserScan, self.scan_callback) # ROS publisers self.pos_setpoint_pub = rospy.Publisher( self.uav_prefix + 'mavros/setpoint_position/local', PoseStamped, queue_size=1) # send setpoints in seperate thread self.pos_thread = Thread(target=self.send_pos, args=()) self.pos_thread.daemon = True self.pos_thread.start() print('@ctrl_server@ setUp over') pass
def __init__(self, ros_rate=10, # slow as nothing happens in the main loop state_estimation_mode=State_estimation_method.GPS, enforce_height_mode_flag=False, height_mode_req=0, ): self.sem = state_estimation_mode self._node_alive = True self.ros_rate = ros_rate self.wd_initialised = False self.wd_fault_detected = False self.fault_this_loop = False # initialise data containers self.altitude = Altitude() self.altitude_bottom_clearance = Float32() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.optic_flow_raw = OpticalFlowRad() self.optic_flow_range = Range() self.home_position = HomePosition() self.local_position = PoseStamped() # self.gt_position = PoseStamped() self.mission_wp = WaypointList() self.state = State() self.mocap_pose = PoseStamped() self.camera_pose = PoseStamped() self.camera_yaw = None self.local_x = Float64().data self.local_y = Float64().data self.local_z = Float64().data self.gt_x = Float64().data self.gt_y = Float64().data self.gt_z = Float64().data # todo - we can probably live with just XX_vel_bod data?: self.x_vel = Float64().data self.y_vel = Float64().data self.gt_x_vel = Float64().data self.gt_y_vel = Float64().data self.vel_ts = Float64().data self.xy_vel = Float64().data self.x_vel_bod = Float64().data self.y_vel_bod = Float64().data self.xy_vel_bod = Float64().data self.body_yaw_rate = Float64().data self.global_compass_hdg_deg = Float64().data self.enforce_height_mode_flag = enforce_height_mode_flag self.height_mode_req = height_mode_req # threading locks self.setpoint_lock = Lock() # used for setting lock in our setpoint publisher so that commands aren't mixed # ROS services service_timeout = 10 rospy.loginfo("Searching for mavros services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/param/set', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.wait_for_service('/mavros/cmd/set_home') # rospy.wait_for_service('mavros/fcu_url', service_timeout) # todo - check how this is used in px4 self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_param_srv = rospy.ServiceProxy('mavros/param/set', ParamSet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) self.wp_clear_srv = rospy.ServiceProxy('mavros/mission/clear', WaypointClear) self.wp_push_srv = rospy.ServiceProxy('mavros/mission/push', WaypointPush) self.takeoff_srv = rospy.ServiceProxy('/mavros/cmd/takeoff', CommandTOL) self.cmd_home_srv = rospy.ServiceProxy('/mavros/cmd/set_home', CommandHome) rospy.loginfo("Required ROS services are up") except rospy.ROSException: self.shut_node_down(extended_msg="failed to connect to Mavros services - was the mavros node started?") # make sure we have information about our connection with FCU rospy.loginfo("Get our fcu string") try: self.fcu_url = rospy.get_param('mavros/fcu_url') except Exception as e: print (e) self.shut_node_down(extended_msg="cant find fcu url") # ensure that our height mode is as we expect it to be (if required) rospy.loginfo('check height_mode {}'.format(self.enforce_height_mode_flag)) if self.enforce_height_mode_flag: # todo - allow multiple attempts at this # res = self.mavros_interface.get_param_srv(param) param_read_attempts = 0 try: while param_read_attempts < 5: res = self.get_param_srv('EKF2_HGT_MODE') if res.success: self.height_mode = res.value.integer if self.height_mode == self.height_mode_req: rospy.loginfo('height mode {} as expected'.format(self.height_mode)) break else: raise Exception ("height mode is {} - (expected heightmode is {}) change parameter with QGround control and try again".format(self.height_mode, self.height_mode_req)) break else: rospy.logerr( "Couldn't read EKF2_HGT_MODE param on attempt {} - trying again".format(param_read_attempts)) param_read_attempts += 1 rospy.sleep(2) except Exception as e: rospy.logerr( "Couldn't read EKF2_HGT_MODE - shutting down".format(param_read_attempts)) self.shut_node_down(extended_msg= "height_mode error - traceback is {}".format(e)) # todo: ensure that our state estimation parameters are as available (this requires the state estimation # topic name so can't test this until we do something with mocap again) Actually, we can incorporate this into # the watchdog # if state_estimation_mode == State_estimation_method.MOCAP: # ROS subscribers self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state',ExtendedState,self.extended_state_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global',NavSatFix, self.global_position_callback) self.optic_flow_raw_sub = rospy.Subscriber('mavros/px4flow/raw/optical_flow_raw',OpticalFlowRad, self.optic_flow_raw_callback) self.optic_flow_range_sub = rospy.Subscriber('mavros/px4flow/ground_distance',Range,self.optic_flow_range_callback) self.home_pos_sub = rospy.Subscriber('mavros/home_position/home', HomePosition, self.home_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.mission_wp_sub = rospy.Subscriber('mavros/mission/waypoints', WaypointList, self.mission_wp_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.mocap_pos_sub = rospy.Subscriber('mavros/vision_pose/pose', PoseStamped, self.mocap_pos_callback) # self.camera_pose_sub = rospy.Subscriber(self.camera_pose_topic_name, PoseStamped, self.cam_pose_cb) # todo - add check for this signal to watchdog - or remap /mavros/local_position/velocity -> /mavros/local_position/velocity_local self.velocity_local_sub = rospy.Subscriber('/mavros/local_position/velocity_local', TwistStamped, self.vel_callback) self.velocity_body_sub = rospy.Subscriber('/mavros/local_position/velocity_body', TwistStamped, self.vel_bod_callback) self.compass_sub = rospy.Subscriber('/mavros/global_position/compass_hdg', Float64, self.compass_hdg_callback) self.ground_truth_sub = rospy.Subscriber('/body_ground_truth', Odometry, self.gt_position_callback) ## Ros publishers self.local_pos_pub_raw = rospy.Publisher('mavros/setpoint_raw/local', PositionTarget, queue_size=1) # ROS topics - this must come after our ROS subscribers topics_timeout = 30 rospy.loginfo("waiting for ROS topics") try: # check that essential messages are being subscribed to regularly for _ in np.arange(2): rospy.wait_for_message('mavros/local_position/pose', PoseStamped, topics_timeout) rospy.wait_for_message('mavros/extended_state', ExtendedState, topics_timeout) except rospy.ROSException: self.shut_node_down(extended_msg="Required ros topics not published") rospy.loginfo("ROS topics are up") # create a watchdog thread that checks topics are being received at the expected rates self.watchdog_thread = Thread(target=self.watchdog, args=()) self.watchdog_thread.daemon = True self.watchdog_thread.start()
def __init__(self): self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.local_position = PoseStamped() self.state = State() self.mav_type = None self.prev_position_x = 0 self.prev_position_y = 0 self.prev_position_z = 0 self.drift_x = 0 self.drift_y = 0 self.drift_z = 0 self.drift_threshold = 0.07 self.IsDrift = False self.sub_topics_ready = { key: False for key in ['alt', 'ext_state', 'global_pos', 'local_pos', 'state'] } # ROS services service_timeout = 30 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: self.fail("failed to connect to services") self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) # Publisher self.desired_pos = PoseStamped() self.desired_pos.pose.position.x = 0 self.desired_pos.pose.position.y = 0 self.desired_pos.pose.position.z = 0 self.radius = 0.3 self.yaw_th = 0.05 self.z_in = 0.1 self.z_ub = 2.7 # Height upper bound self.z_lb = 0.5 # Height lower bound self.pos_setpoint_pub = rospy.Publisher( 'mavros/setpoint_position/local', PoseStamped, queue_size=1) # send setpoints in seperate thread to better prevent failsafe self.pos_thread = Thread(target=self.send_pos, args=()) self.pos_thread.daemon = True self.pos_thread.start()
def __init__(self, *args): super(MavrosQuad, self).__init__(*args) self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.home_position = HomePosition() self.local_position = PoseStamped() self.mission_wp = WaypointList() self.state = State() self.mav_type = None self.local_velocity = TwistStamped() self.gazebo_load_name = 'rigid_body_load_1_vehicle::rb_link' self.gazebo_load_pose = Pose() self.gazebo_load_twist = Twist() self.gazebo_quad_name = 'rigid_body_load_1_vehicle::base_link_0' self.gazebo_quad_pose = Pose() self.gazebo_quad_twist = Twist() self.gazebo_imu_name = 'rigid_body_load_1_vehicle::iris_0/imu_link' self.gazebo_imu_pose = Pose() self.gazebo_imu_twist = Twist() self.sub_topics_ready = { key: False for key in [ 'alt', 'ext_state', 'gazebo', 'global_pos', 'home_pos', 'local_pos', 'local_vel', 'mission_wp', 'state', 'imu' ] } # ROS services service_timeout = 30 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: rospy.logerr("failed to connect to services") self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) self.wp_clear_srv = rospy.ServiceProxy('mavros/mission/clear', WaypointClear) self.wp_push_srv = rospy.ServiceProxy('mavros/mission/push', WaypointPush) # ROS subscribers self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.home_pos_sub = rospy.Subscriber('mavros/home_position/home', HomePosition, self.home_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.mission_wp_sub = rospy.Subscriber('mavros/mission/waypoints', WaypointList, self.mission_wp_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.local_vel_sub = rospy.Subscriber('mavros/local_position/velocity_local', TwistStamped, self.local_velocity_callback) self.imu_sub = rospy.Subscriber('/mavros/imu/data', Imu, self.imu_callback) self.gazebo_sub = rospy.Subscriber('/gazebo/link_states', LinkStates, self.gazebo_callback)
#local_position callback function def local_pose_callback(data): local_pose = data #altitude callback function def altitude_callback(data): altitude = data if __name__ == "__main__": #Set up prob_dist node rospy.init_node("Prob_Dist_node", anonymous = True) rospy.loginfo("Set up Probability Distribution Node") #UAV0 #Subscribe to current position local_pose = PoseStamped() local_pose_sub = rospy.Subscriber("/uav0/mavros/local_position/pose", PoseStamped, local_pose_callback) #Subscribe to current altitude altitude = Altitude() altitude_sub = rospy.Subscriber("/uav0/mavros/altitude", Altitude, altitude_callback) #Subscribe to detection accuracies #We consider that the detection accuracy will be provided by us directly #Gaussian heuristic function based on height and detection accuracy #Spin rospy.spin()
def __init__(self, *args): super(MavrosQuad, self).__init__(*args) self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.home_position = HomePosition() self.local_position = PoseStamped() self.mission_wp = WaypointList() self.state = State() self.mav_type = None self.local_velocity = TwistStamped() self.gazebo_load_name = 'rigid_body_load_1_vehicle::rb_link' self.gazebo_load_pose = Pose() self.gazebo_load_twist = Twist() self.gazebo_quad_name = 'rigid_body_load_1_vehicle::base_link_0' self.gazebo_quad_pose = Pose() self.gazebo_quad_twist = Twist() self.gazebo_imu_name = 'rigid_body_load_1_vehicle::iris_0/imu_link' self.gazebo_imu_pose = Pose() self.gazebo_imu_twist = Twist() self.q_input = np.array([0, 0, 0, 1]) self.T_input = 0 self.offboard_position_active = True self.offboard_attitude_active = False self.offboard_load_active = False self.sub_topics_ready = { key: False for key in [ 'alt', 'ext_state', 'gazebo', 'global_pos', 'home_pos', 'local_pos', 'local_vel', 'mission_wp', 'state', 'imu' ] } # ROS services service_timeout = 30 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: rospy.logerr("failed to connect to services") self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) self.wp_clear_srv = rospy.ServiceProxy('mavros/mission/clear', WaypointClear) self.wp_push_srv = rospy.ServiceProxy('mavros/mission/push', WaypointPush) # ROS subscribers self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.home_pos_sub = rospy.Subscriber('mavros/home_position/home', HomePosition, self.home_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.mission_wp_sub = rospy.Subscriber('mavros/mission/waypoints', WaypointList, self.mission_wp_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.local_vel_sub = rospy.Subscriber( 'mavros/local_position/velocity_local', TwistStamped, self.local_velocity_callback) self.imu_sub = rospy.Subscriber('/mavros/imu/data', Imu, self.imu_callback) self.gazebo_sub = rospy.Subscriber('/gazebo/link_states', LinkStates, self.gazebo_callback) # ROS Publishers # Attitude self.att = AttitudeTarget() self.att_setpoint_pub = rospy.Publisher('mavros/setpoint_raw/attitude', AttitudeTarget, queue_size=1) # send setpoints in seperate thread to better prevent failsafe self.att_thread = Thread(target=self.send_att, args=()) self.att_thread.daemon = True self.att_thread.start() # Pose self.pos = PoseStamped() self.pos_setpoint_pub = rospy.Publisher( 'mavros/setpoint_position/local', PoseStamped, queue_size=1) # send setpoints in seperate thread to better prevent failsafe self.pos_thread = Thread(target=self.send_pos, args=()) self.pos_thread.daemon = True self.pos_thread.start()
from geometry_msgs.msg import TwistStamped from sensor_msgs.msg import Temperature from std_msgs.msg import Float64 # Instantiate CvBridge bridge = CvBridge() dataDirectory = "/home/user1/Data/" # default value changes on subscribing # Camera directories, csv file, and bag file all go here #now = datetime.now() # current date and time flirSN = "FLIR18284612" flirDirectory = "" csvFilename = "" datetimeData = "" is_recording = False rel_alt = Altitude() gps_fix = NavSatFix() imu_mag = MagneticField() imu_data = Imu() vel_gps = TwistStamped() temp_imu = Temperature() imageCount = 0 def make_header(): header = "filename,rostime,rel_alt.monotonic,rel_alt.amsl,rel_alt.local,rel_alt.relative," header += "gps_fix.status.status,gps_fix.status.service,gps_fix.latitude,gps_fix.longitude,gps_fix.altitude," header += "imu_data.magnetic_field.x,imu_data.magnetic_field.y,imu_data.magnetic_field.z," header += "imu_mag.orientation.x,imu_mag.orientation.y,imu_mag.orientation.z,imu_mag.orientation.w, imu_mag.angular_velocity.x,imu_mag.angular_velocity.y,imu_mag.angular_velocity.z," header += "imu_mag.linear_acceleration:.x,imu_mag.linear_acceleration:.y,imu_mag.linear_acceleration:.z," header += "vel_gps.twist.linear.x,vel_gps.twist.linear.y,vel_gps.twist.linear.z,"
def __init__(self): self.altitude = Altitude() self.extended_state = ExtendedState() self.global_position = NavSatFix() self.local_position = PoseStamped() self.state = State() self.mav_type = None self.local_velocity = TwistStamped() self.sub_topics_ready = { key: False for key in ['alt', 'ext_state', 'global_pos', 'local_pos', 'state'] } # ROS services service_timeout = 30 rospy.loginfo("waiting for ROS services") try: rospy.wait_for_service('mavros/param/get', service_timeout) rospy.wait_for_service('mavros/cmd/arming', service_timeout) rospy.wait_for_service('mavros/mission/push', service_timeout) rospy.wait_for_service('mavros/mission/clear', service_timeout) rospy.wait_for_service('mavros/set_mode', service_timeout) rospy.loginfo("ROS services are up") except rospy.ROSException: self.fail("failed to connect to services") self.get_param_srv = rospy.ServiceProxy('mavros/param/get', ParamGet) self.set_arming_srv = rospy.ServiceProxy('mavros/cmd/arming', CommandBool) self.set_mode_srv = rospy.ServiceProxy('mavros/set_mode', SetMode) # Subscribers self.alt_sub = rospy.Subscriber('mavros/altitude', Altitude, self.altitude_callback) self.global_pos_sub = rospy.Subscriber('mavros/global_position/global', NavSatFix, self.global_position_callback) self.local_pos_sub = rospy.Subscriber('mavros/local_position/pose', PoseStamped, self.local_position_callback) self.state_sub = rospy.Subscriber('mavros/state', State, self.state_callback) self.ext_state_sub = rospy.Subscriber('mavros/extended_state', ExtendedState, self.extended_state_callback) self.local_vel_sub = rospy.Subscriber( 'mavros/local_position/velocity_body', TwistStamped, self.local_vel_callback) # Publishers self.desired_vel = TwistStamped() self.desired_vel.twist.angular.z = 0 self.desired_vel.twist.linear.x = 0 self.desired_vel.twist.linear.y = 0 self.desired_vel.twist.linear.z = 0 #self.radius = 0.3 #self.yaw_th = 0.05 self.yaw_current = 0 self.isLand = False self.yawrate = 0.5 self.xvel = 0.5 self.z_target = 0 self.z_in = 0.1 self.z_ub = 2.5 # Height upper bound self.z_lb = 0.5 # Height lower bound self.z_p_gain = 1.0 self.cmd_vel_pub = rospy.Publisher('mavros/setpoint_velocity/cmd_vel', TwistStamped, queue_size=1) # send setpoints in seperate thread to better prevent failsafe self.pos_thread = Thread(target=self.send_vel, args=()) self.pos_thread.daemon = True self.pos_thread.start() # Keyboard input thread self.key_thread = Thread(target=self.keyInput, args=()) self.key_thread.daemon = True self.key_thread.start()