使用flask-socket.io发出dronekit.io vehicle的属性更改

问题描述 投票:1回答: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.

这是我的代码:

sample.朋友

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请求,我应该搜索有关如何解决这个问题的信息,但我没有找到任何关于错误的信息。

希望你能帮助我。

非常感谢你,

Raniel

flask-socketio dronekit-python
1个回答
0
投票

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

你有几个选择:

  1. 指示事件的收件人和您正在使用的命名空间,以便不需要在上下文中查找它们。您可以通过添加roomnamespace参数来完成此操作。如果使用默认命名空间,请使用'/'作为命名空间。
  2. 通过添加broadcast=True作为参数,以及#1中指示的命名空间,向所有客户端发出。
© www.soinside.com 2019 - 2024. All rights reserved.