1

我正在尝试使用flask-socket.io 从我的dronekit.io 车辆发送数据。不幸的是,我得到了这个日志:

Starting copter simulator (SITL)
SITL already Downloaded and Extracted.
Ready to boot.
Connecting to vehicle on: tcp:127.0.0.1:5760
>>> APM:Copter V3.3 (d6053245)
>>> Frame: QUAD
>>> Calibrating barometer
>>> Initialising APM...
>>> barometer calibration complete
>>> GROUND START
* Restarting with stat
latitude  -35.363261

>>> Exception in attribute handler for location.global_relative_frame
>>> Working outside of request context.    

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
longitude  149.1652299

>>> Exception in attribute handler for location.global_relative_frame
>>> Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

这是我的代码:

示例.py

from dronekit import connect, VehicleMode
from flask import Flask
from flask_socketio import SocketIO, emit
import dronekit_sitl
import time

sitl = dronekit_sitl.start_default()
connection_string = sitl.connection_string()
print("Connecting to vehicle on: %s" % (connection_string,))
vehicle = connect(connection_string, wait_ready=True)

def arm_and_takeoff(aTargetAltitude):

    print "Basic pre-arm checks"
    while not vehicle.is_armable:
        print " Waiting for vehicle to initialise..."
        time.sleep(1)

    print "Arming motors"
    vehicle.mode    = VehicleMode("GUIDED")
    vehicle.armed   = True

    while not vehicle.armed:
        print " Waiting for arming..."
        time.sleep(1)

    print "Taking off!"
    vehicle.simple_takeoff(aTargetAltitude)

    while True:
        if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95:
            print "Reached target altitude"
            break
        time.sleep(1)

last_latitude = 0.0
last_longitude = 0.0
last_altitude = 0.0

@vehicle.on_attribute('location.global_relative_frame')
def location_callback(self, attr_name, value):
    global last_latitude
    global last_longitude
    global last_altitude
    if round(value.lat, 6) != round(last_latitude, 6):
        last_latitude = value.lat
        print "latitude ", value.lat, "\n"
        emit("latitude", value.lat)
    if round(value.lon, 6) != round(last_longitude, 6):
        last_longitude = value.lon
        print "longitude ", value.lon, "\n"
        emit("longitude", value.lon)
    if round(value.alt) != round(last_altitude):
        last_altitude = value.alt
        print "altitude ", value.alt, "\n"
        emit("altitude", value.alt)

app = Flask(__name__)
socketio = SocketIO(app)

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000, debug=True)
    arm_and_takeoff(20)

由于日志的原因,我知道我不应该在“vehicle.on_attribute”装饰器方法中执行任何 HTTP 请求,我应该搜索有关如何解决此问题的信息,但我没有找到有关该错误的任何信息。

希望你能帮助我。

非常感谢,

拉尼尔

4

1 回答 1

0

默认情况下,该emit()函数将事件返回给活动客户端。如果您在请求上下文之外调用此函数,则没有活动客户端的概念,因此您会收到此错误。

你有几个选择:

  1. 指示事件的接收者和您正在使用的命名空间,以便无需在上下文中查找它们。您可以通过添加roomnamespace参数来做到这一点。'/'如果您使用默认命名空间,请用于命名空间。

  2. 通过添加broadcast=True作为参数以及 #1 中指示的命名空间向所有客户端发出。

于 2017-12-21T04:44:46.043 回答