示例#1
0
def send_mission():
    #read mission
    global waypoint
    global send_mission_once
    try:
        print("\nReading mission")
        waypoint = mi.save_mission(vehicle)
        send_mission_once = True
        print("\nMission read")
        #if waypoint is read, then set send_mission_once to True so that, mission can be sent instead of data.
    except Exception as e:
        err = {'context': 'Mission', 'msg': 'Mission FIle could not be read'}
        logger.error(err)
def send_data(threadName, delay):
    global waypoint  #needs to be global as it is accessed by on_mission_download, socket function
    global checker  #flag to start the functions to calculate total distance and eta once the mission is received from pixhawk
    total = 0
    check = True
    divisor = 1
    last_vel = 0
    data = {}
    # magcal(vehicle)
    while 1:
        # print("data sending ")
        try:
            loc = vehicle.location.global_frame
        except Exception as e:
            error = {'context': 'loc', 'msg': 'Location not found!!!'}
            socket.emit("errors", error)
        try:
            data["numSat"] = vehicle.gps_0.satellites_visible
        except Exception as e:
            error = {'context': 'numSat', 'msg': 'numSat not found!!'}
            socket.emit("errors", error)
        try:
            data["hdop"] = vehicle.gps_0.eph
        except Exception as e:
            error = {'context': 'loc', 'msg': 'Location not found!!!'}
            socket.emit("errors", error)
        try:
            data["fix"] = vehicle.gps_0.fix_type
        except Exception as e:
            error = {'context': 'fix', 'msg': 'fix type not found!!!'}
            socket.emit("errors", error)
        try:
            data["lat"] = format(loc.lat, '.15f')
            data["lng"] = format(loc.lon, '.15f')
        except Exception as e:
            error = {'context': 'format', 'msg': 'Long lat format error!!!'}
            socket.emit("errors", error)
        try:
            vel = vehicle.velocity
        except Exception as e:
            error = {'context': 'vel', 'msg': 'velocity not found!!!'}
            socket.emit("errors", error)
        try:
            status = str(vehicle.system_status)
        except Exception as e:
            error = {'context': 'status', 'msg': 'Status not found!!!'}
            socket.emit("errors", error)
        try:
            data["conn"] = 'True'
        except Exception as e:
            error = {'context': 'loc', 'msg': 'Location not found!!!'}
            socket.emit("errors", error)
        try:
            data["arm"] = vehicle.armed
        except Exception as e:
            error = {'context': 'arm', 'msg': 'Arm not found!!!'}
            socket.emit("errors", error)
        try:
            data["ekf"] = vehicle.ekf_ok
        except Exception as e:
            error = {'context': 'ekf', 'msg': 'ekf not found!!!'}
            socket.emit("errors", error)
        try:
            data["mode"] = vehicle.mode.name
        except Exception as e:
            error = {'context': ',mode', 'msg': 'Mode not found!!!'}
            socket.emit("errors", error)
        try:
            data["alt"] = format(loc.alt, '.2f')
        except Exception as e:
            error = {'context': 'alt', 'msg': 'altitude format error!!!'}
            socket.emit("errors", error)
        try:
            data["roll"] = vehicle.attitude.roll
        except Exception as e:
            error = {'context': 'roll', 'msg': 'roll not found!!!'}
            socket.emit("errors", error)
        try:
            data["pitch"] = vehicle.attitude.pitch
        except Exception as e:
            error = {'context': 'pitch', 'msg': 'pitch not found!!!'}
            socket.emit("errors", error)
        try:
            data["yaw"] = vehicle.attitude.yaw
        except Exception as e:
            error = {'context': 'yaw', 'msg': 'yaw not found!!!'}
            socket.emit("errors", error)
        try:
            data["altr"] = vehicle.location.global_relative_frame.alt
        except Exception as e:
            error = {'context': 'altr', 'msg': 'rel alt not found!!!'}
            socket.emit("errors", error)
        try:
            data["head"] = format(vehicle.heading, 'd')
        except Exception as e:
            error = {'context': 'head', 'msg': 'head not found!!!'}
            socket.emit("errors", error)
        try:
            data["as"] = format(vehicle.airspeed, '.3f')
        except Exception as e:
            error = {'context': 'as_format', 'msg': 'as format error!!!'}
            socket.emit("errors", error)
        try:
            data["lidar"] = vehicle.rangefinder.distance
        except Exception as e:
            error = {'context': 'lidar', 'msg': 'Lidar not found!!!'}
            socket.emit("errors", error)
        try:
            data["gs"] = format(vehicle.groundspeed, '.3f')
        except Exception as e:
            error = {'context': 'gs', 'msg': 'gs format error!!!'}
            socket.emit("errors", error)
        try:
            data["status"] = status[13:]
        except Exception as e:
            error = {'context': 'status', 'msg': 'status not found!!!'}
            socket.emit("errors", error)
        try:
            data["volt"] = format(vehicle.battery.voltage, '.2f')
        except Exception as e:
            error = {'context': 'volt', 'msg': 'voltage not found!!!'}
            socket.emit("errors", error)
        print(data["head"])
        if not vehicle.armed:
            time1 = 0
        # print(datetime.datetime.now())
        if checker:  #only if waypoints are successfully read
            if vehicle.armed and check:  # check is used to receive time of arming only once, at first arming
                time1 = datetime.datetime.now()  #receive time once armed
                check = False
            if not check:
                try:
                    time2 = datetime.datetime.now()  # calculate current time
                    flight_time = float(
                        (time2 -
                         time1).total_seconds())  # calculate total flight time
                    vel = float(
                        (last_vel + vehicle.groundspeed) / divisor
                    )  #calculate the average velocity upto current time
                    print(flight_time, total)
                    est = float(
                        (float(total) - flight_time * vel) /
                        3.5)  #estimate eta, with assummed velocity 3.5 m/s
                    #print(est)
                    if est <= 0.5:  #making sure eta is not less thatn 0.5
                        est = 0
                    data["est"] = est
                    last_vel += vehicle.groundspeed  #summing up velcity to average them
                    divisor += 1  # the number of values of velocity
                except Exception as e:
                    pass
        socket.emit('data', data)  #send data to server
        socket.on(
            'mission_download', on_mission_download
        )  #keep listening for download command for mission from server
        socket.on(
            'initiate_flight',
            on_initiate_flight)  #keep listening for fly command from user
        try:
            socket.on('positions', update_mission)
        except Exception:
            print("error positions")
        try:
            socket.on('RTL', set_mode_RTL)
        except Exception:
            pass
        try:
            socket.on('magcal', magcal_start)
        except Exception:
            pass
        try:
            socket.on('LAND', set_mode_LAND)
        except Exception:
            pass
        if vehicle.gps_0.fix_type > 1 and not checker:  ## check gps before downloading the mission as home points are not received without gps, and checker to download mission only once
            try:
                waypoint = mi.save_mission(
                    vehicle
                )  # call the save mission function which downloads the mission from pixhawk and arranges waypoints into a dictionary
                print("MISSION DOWNLOADED")
                total = dis.calculate_dist(
                    waypoint
                )  # calculate total distance between home and final waypoint
                print("total distance", waypoint, total)
                socket.emit(
                    'homePosition', waypoint[0]
                )  # send homePosition to server as soon as gps lock
                checker = True  # switch value of checker such that mission is downloaded only once from pixhawk, and condition to calculate eta is met
            except Exception as e:
                print(e)
                print("GPS error OR no mission file received!!!")
        socket1.wait(
            seconds=0.2
        )  #sends or waits for socket activities in every seconds specified
示例#3
0
def send_data(threadName, delay):
    global waypoint  #needs to be global as it is accessed by on_mission_download, socket function
    checker = False  #flag to start the functions to calculate total distance and eta once the mission is received from pixhawk
    total = 0
    check = True
    divisor = 1
    last_vel = 0
    data = {}
    while 1:
        #print("data sending ")
        try:
            loc = vehicle.location.global_frame
            data["numSat"] = vehicle.gps_0.satellites_visible
            data["hdop"] = vehicle.gps_0.eph
            data["fix"] = vehicle.gps_0.fix_type
            data["lat"] = format(loc.lat, '.15f')
            data["lng"] = format(loc.lon, '.15f')
        except Exception as e:
            pass
        vel = vehicle.velocity
        status = str(vehicle.system_status)
        data["conn"] = 'True'
        data["arm"] = vehicle.armed
        data["ekf"] = vehicle.ekf_ok
        data["mode"] = vehicle.mode.name
        data["alt"] = format(loc.alt, '.2f')
        data["altr"] = vehicle.location.global_relative_frame.alt
        data["head"] = format(vehicle.heading, 'd')
        data["as"] = format(vehicle.airspeed, '.3f')
        data["lidar"] = vehicle.rangefinder.distance
        data["gs"] = format(vehicle.groundspeed, '.3f')
        data["status"] = status[13:]
        data["volt"] = format(vehicle.battery.voltage, '.2f')
        print(datetime.datetime.now())
        if checker:  #only if waypoints are successfully read

            if vehicle.armed and check:  # check is used to receive time of arming only once, at first arming
                time1 = datetime.datetime.now()  #receive time once armed
                check = False
            if not check:
                try:
                    time2 = datetime.datetime.now()  # calculate current time
                    flight_time = float(
                        (time2 -
                         time1).total_seconds())  # calculate total flight time
                    vel = float(
                        (last_vel + vehicle.groundspeed) / divisor
                    )  #calculate the average velocity upto current time
                    est = float(
                        (float(total) - flight_time * vel) /
                        3.5)  #estimate eta, with assummed velocity 3.5 m/s
                    if est <= 0.5:  #making sure eta is not less thatn 0.5
                        est = 0
                    data["est"] = est
                    last_vel += vehicle.groundspeed  #summing up velcity to average them
                    divisor += 1  # the number of values of velocity
                except Exception as e:
                    pass

        socket.emit('data', data)  #send data to server
        socket.on(
            'mission_download', on_mission_download
        )  #keep listening for download command for mission from server
        socket.on(
            'initiate_flight',
            on_initiate_flight)  #keep listening for fly command from user
        print(data["head"])

        if vehicle.gps_0.fix_type > 1 and not checker:  ## check gps before downloading the mission as home points are not received without gps, and checker to download mission only once
            try:
                waypoint = mi.save_mission(
                    vehicle
                )  # call the save mission function which downloads the mission from pixhawk and arranges waypoints into a dictionary
                print("MISSION DOWNLOADED")
                total = dis.calculate_dist(
                    waypoint
                )  # calculate total distance between home and final waypoint
                socket.emit(
                    'homePosition', waypoint[0]
                )  # send homePosition to server as soon as gps lock
                checker = True  # switch value of checker such that mission is downloaded only once from pixhawk, and condition to calculate eta is met
            except Exception as e:
                print(e)
                print("GPS error OR no mission file received!!!")
        socket.wait(
            seconds=0.2
        )  #sends or waits for socket activities in every seconds specified