From db5715330f4eb95f5f2df296acbf7e1f9b7d3214 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 12 Sep 2014 13:06:08 +0900 Subject: [PATCH 001/796] 0.2.4: Fix wpt download for fast booting boards Thanks @mrpollo for the report. --- droneapi/module/api.py | 2 +- setup.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/droneapi/module/api.py b/droneapi/module/api.py index bb069a002..131c266ea 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -39,13 +39,13 @@ def __init__(self, module): def download(self): '''Download all waypoints from the vehicle''' + self.wait_valid() self.__wp.fetch() # BIG FIXME - wait for full wpt download before allowing any of the accessors to work def wait_valid(self): '''Block the calling thread until waypoints have been downloaded''' # FIXME this is a super crufty spin-wait, also we should give the user the option of specifying a timeout - print 'waiting for download' while (self.__wp.wp_op is not None) and not self.__module.api.exit: time.sleep(0.200) diff --git a/setup.py b/setup.py index 05f0cda28..f8d508ed5 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,7 @@ from setuptools import setup, Extension -import numpy as np import platform -version = '0.2.3' +version = '0.2.4' ext_modules = [] From 2b123b6d93af27133bb032320aa9eef297bff6c7 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 12 Sep 2014 13:06:18 +0900 Subject: [PATCH 002/796] require latest mavproxy --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index f8d508ed5..72c673cd4 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, Extension import platform -version = '0.2.4' +version = '0.2.5' ext_modules = [] @@ -13,7 +13,7 @@ url = 'https://github.com/diydrones/droneapi-python', author = '3D Robotics', install_requires = [ 'pymavlink', - 'MAVProxy >= 1.3.7', + 'MAVProxy >= 1.3.12', 'protobuf >= 2.5.0' ], author_email = 'kevinh@geeksville.com', classifiers=['Development Status :: 4 - Beta', From 077ab40f3a0e9c2380792677a00dcf2e10f17a20 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 12 Sep 2014 13:20:04 +0900 Subject: [PATCH 003/796] don't publish stale docs --- update-docs.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/update-docs.sh b/update-docs.sh index 64b1a4d35..26415149d 100755 --- a/update-docs.sh +++ b/update-docs.sh @@ -2,9 +2,10 @@ rm -r /tmp/autodocs set -e +python ./setup.py build echo Switching to doc tree cd docs -make html +make singlehtml cp -a _build/html /tmp/autodocs cd .. git checkout gh-pages From fc2df8ebe2179f52f3ead5e81a8b145e5bfc649a Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 12 Sep 2014 13:32:31 +0900 Subject: [PATCH 004/796] don't publish stale docs --- docs/index.rst | 2 +- update-docs.sh | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index c9aa1187d..3f78bf248 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -11,7 +11,7 @@ Contents: .. toctree:: :maxdepth: 2 -.. automodule:: DroneApi +.. automodule:: droneapi.lib :members: :inherited-members: diff --git a/update-docs.sh b/update-docs.sh index 64b1a4d35..179eca3ea 100755 --- a/update-docs.sh +++ b/update-docs.sh @@ -2,6 +2,7 @@ rm -r /tmp/autodocs set -e +python ./setup.py build echo Switching to doc tree cd docs make html From d50fecc325e39d89b373f70c73903fd1c6723872 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 12 Sep 2014 13:33:42 +0900 Subject: [PATCH 005/796] even more docs fixes --- update-docs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-docs.sh b/update-docs.sh index 26415149d..179eca3ea 100755 --- a/update-docs.sh +++ b/update-docs.sh @@ -5,7 +5,7 @@ set -e python ./setup.py build echo Switching to doc tree cd docs -make singlehtml +make html cp -a _build/html /tmp/autodocs cd .. git checkout gh-pages From 4ffb67c2000a68b2fa7148f88141656514675235 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 6 Oct 2014 06:08:11 +0900 Subject: [PATCH 006/796] release 1.0.0 - depend on mavproxy 1.3.13 for my windows fixes --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 72c673cd4..5669a6f5a 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, Extension import platform -version = '0.2.5' +version = '1.0.0' ext_modules = [] @@ -13,7 +13,7 @@ url = 'https://github.com/diydrones/droneapi-python', author = '3D Robotics', install_requires = [ 'pymavlink', - 'MAVProxy >= 1.3.12', + 'MAVProxy >= 1.3.13', 'protobuf >= 2.5.0' ], author_email = 'kevinh@geeksville.com', classifiers=['Development Status :: 4 - Beta', From 241bacd3ec4d1ac720e0c45c5e562a0c08f3afa4 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 5 Dec 2014 14:02:44 -1000 Subject: [PATCH 007/796] Wait on reading params until after they have been fetched from vehicle. Fixes a bug reported by D Hugues. --- droneapi/module/api.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/droneapi/module/api.py b/droneapi/module/api.py index 131c266ea..b6af7947e 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -24,11 +24,24 @@ def __init__(self, module): self.__module = module def __getitem__(self, name): + self.wait_valid() return self.__module.mav_param[name] def __setitem__(self, name, value): + self.wait_valid() self.__module.mpstate.functions.param_set(name, value) + def wait_valid(self): + '''Block the calling thread until parameters have been downloaded''' + # FIXME this is a super crufty spin-wait, also we should give the user the option of specifying a timeout + pstate = self.__param.pstate + while (pstate.mav_param_count == 0 or len(pstate.mav_param_set) != pstate.mav_param_count) and not self.__module.api.exit: + time.sleep(0.200) + + @property + def __param(self): + return self.__module.module('param') + class MPCommandSequence(CommandSequence): """ See CommandSequence baseclass for documentation. From e510f519fc47324f5a6321237906f88f91af8639 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 5 Dec 2014 14:03:26 -1000 Subject: [PATCH 008/796] bump version to 1.0.1 and release --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 5669a6f5a..6065e71b8 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, Extension import platform -version = '1.0.0' +version = '1.0.1' ext_modules = [] From 206a35070985bf3210439973dc84179e60ccd038 Mon Sep 17 00:00:00 2001 From: Eric Liao Date: Mon, 8 Dec 2014 10:19:19 -0800 Subject: [PATCH 009/796] Added querying of mount_status. --- droneapi/module/api.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/droneapi/module/api.py b/droneapi/module/api.py index b6af7947e..9d67fed49 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -176,6 +176,10 @@ def groundspeed(self): def airspeed(self): return self.__module.airspeed + @property + def mount_status(self): + return [ self.__module.mount_pitch, self.__module.mount_yaw, self.__module.mount_roll ] + @property def channel_override(self): overrides = self.__rc.override @@ -319,6 +323,10 @@ def __init__(self, mpstate): self.yawspeed = None self.rollspeed = None + self.mount_pitch = None + self.mount_yaw = None + self.mount_roll = None + self.rc_readback = {} self.last_waypoint = 0 @@ -398,6 +406,12 @@ def set(chnum, v): set(6, m.chan6_raw) set(7, m.chan7_raw) set(8, m.chan8_raw) + elif typ == "MOUNT_STATUS": + self.mount_pitch = m.pointing_a / 100 + self.mount_roll = m.pointing_b / 100 + self.mount_yaw = m.pointing_c / 100 + self.__on_change('mount') + if (self.vehicle is not None) and hasattr(self.vehicle, 'mavrx_callback'): self.vehicle.mavrx_callback(m) From 401d8f7e83e75f8d5398e80980e389a1cabd4ae6 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Thu, 11 Dec 2014 09:56:06 -0800 Subject: [PATCH 010/796] Basic test harness to measure latency/computation costs --- example/perf_test.py | 151 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 example/perf_test.py diff --git a/example/perf_test.py b/example/perf_test.py new file mode 100644 index 000000000..0cd44ec59 --- /dev/null +++ b/example/perf_test.py @@ -0,0 +1,151 @@ +# +# This is a small example of the python drone API +# Usage: +# * mavproxy.py +# * module load api +# * api start small-demo.py +# +from droneapi.lib import VehicleMode +from pymavlink import mavutil +import time +from datetime import datetime +import traceback + +from matplotlib import pyplot +from numpy import arange +import bisect + +findings = """ +Reception latency: +* due to a top level select() on the udp port, the latency for calling process_master seems to be sub 1ms (limit of python timer resolution) + +Reception periodicity: +TBD + +Background processing perodicity: +TBD + +Sending perodicity: +TBD + +Computation efficency: +FIXME + +Max closed loop rate: +* 20ms+/-300us when talking to SITL (every time we recieve a cmd_ack we immediately send a pair of ROI related msgs) +* The less than 300us variablity makes me think SITL has some 20ms poll rate + +""" + +global v + +def scatterplot(x,y): + pyplot.plot(x,y,'b.') + pyplot.xlim(min(x)-1,max(x)+1) + pyplot.ylim(min(y)-1,max(y)+1) + pyplot.show() + + +def mavrx_debug_handler(message): + """Measure heartbeat periodically""" + mtype = message.get_type() + # t = time.time() + dt = datetime.now() + t = dt.minute * 60 + dt.second + dt.microsecond / (1e6) + + #if mtype == 'HEARTBEAT': + if mtype == 'COMMAND_ACK': + #traceback.print_stack() + print "Received", t + send_testpackets() + + +def send_testpackets(): + print "send ROI cmds" + + # create the SET_POSITION_TARGET_GLOBAL_INT command + msg = v.message_factory.set_position_target_global_int_encode( + 0, # time_boot_ms (not used) + 0, 0, # target system, target component + mavutil.mavlink.MAV_FRAME_GLOBAL, # frame + 0b1000000011000111, # type_mask - enable velocity only + 0, 0, 0, # x, y, z positions (not used) + 0, 0, 0.0, # x, y, z velocity in m/s + 0, 0, 0, # x, y, z acceleration (not used) + 0, 0) # yaw, yaw_rate (not used) + + # send command to vehicle + v.send_mavlink(msg) + + # set ROI + msg = v.message_factory.command_long_encode( + 0, 0, # target system, target component + mavutil.mavlink.MAV_CMD_DO_SET_ROI, #command + 0, #confirmation + 0, 0, 0, 0, #params 1-4 + 0, + 0, + 0 + ) + + v.send_mavlink(msg) + + # Always call flush to guarantee that previous writes to the vehicle have taken place + v.flush() + +# First get an instance of the API endpoint +api = local_connect() +# get our vehicle - when running with mavproxy it only knows about one vehicle (for now) +v = api.get_vehicles()[0] +# Print out some interesting stats about the vehicle +print "Mode: %s" % v.mode +print "Location: %s" % v.location +print "Attitude: %s" % v.attitude +print "Velocity: %s" % v.velocity +print "GPS: %s" % v.gps_0 +print "Armed: %s" % v.armed +print "groundspeed: %s" % v.groundspeed +print "airspeed: %s" % v.airspeed + +# Use of the following method is not recommended (it is better to add observer callbacks to attributes) but if you need it +# it is available... +v.set_mavlink_callback(mavrx_debug_handler) + +# You can read and write parameters +#print "Param: %s" % v.parameters['THR_MAX'] + +# Now download the vehicle waypoints +cmds = v.commands +cmds.download() +cmds.wait_valid() +print "Home WP: %s" % cmds[0] +print "Current dest: %s" % cmds.next + +# Test custom commands +# Note: For mavlink messages that include a target_system & target_component, those values +# can just be filled with zero. The API will take care of using the correct values +# For instance, from the xml for command_long: +# Send a command with up to seven parameters to the MAV +# +# target_system : System which should execute the command (uint8_t) +# target_component : Component which should execute the command, 0 for all components (uint8_t) +# command : Command ID, as defined by MAV_CMD enum. (uint16_t) +# confirmation : 0: First transmission of this command. 1-255: Confirmation transmissions (e.g. for kill command) (uint8_t) +# param1 : Parameter 1, as defined by MAV_CMD enum. (float) +# param2 : Parameter 2, as defined by MAV_CMD enum. (float) +# param3 : Parameter 3, as defined by MAV_CMD enum. (float) +# param4 : Parameter 4, as defined by MAV_CMD enum. (float) +# param5 : Parameter 5, as defined by MAV_CMD enum. (float) +# param6 : Parameter 6, as defined by MAV_CMD enum. (float) +# param7 : Parameter 7, as defined by MAV_CMD enum. (float) +#msg = v.message_factory.command_long_encode(0, 0, +# mavutil.mavlink.MAV_CMD_CONDITION_YAW, 0, +# 0, 0, 0, 0, 1, 0, 0) +#print "Created msg: %s" % msg +#v.send_mavlink(msg) + +print "Disarming..." +v.armed = False +v.flush() + + From 0cd7a2c72eab44c59ccbaa28423477e453610109 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Thu, 11 Dec 2014 11:34:56 -0800 Subject: [PATCH 011/796] measure intervals a little more nicely. --- example/perf_test.py | 52 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/example/perf_test.py b/example/perf_test.py index 0cd44ec59..a0f4f2c30 100644 --- a/example/perf_test.py +++ b/example/perf_test.py @@ -33,12 +33,18 @@ Max closed loop rate: * 20ms+/-300us when talking to SITL (every time we recieve a cmd_ack we immediately send a pair of ROI related msgs) -* The less than 300us variablity makes me think SITL has some 20ms poll rate +* The less than 300us variablity makes me think SITL has some 20ms poll rate - need to try with real vehicle + +Interval (sec) 0.019865 +MaxInterval (sec) 0.021927 +MinInterval (sec) 0.018421 + """ global v + def scatterplot(x,y): pyplot.plot(x,y,'b.') pyplot.xlim(min(x)-1,max(x)+1) @@ -46,22 +52,56 @@ def scatterplot(x,y): pyplot.show() -def mavrx_debug_handler(message): - """Measure heartbeat periodically""" - mtype = message.get_type() +def cur_usec(): + """Return current time in usecs""" # t = time.time() dt = datetime.now() t = dt.minute * 60 + dt.second + dt.microsecond / (1e6) + return t + +class MeasureTime(object): + def __init__(self): + self.prevtime = cur_usec() + self.previnterval = 0 + self.numcount = 0 + self.reset() + + def reset(self): + self.maxinterval = 0 + self.mininterval = 10000 + + def update(self): + now = cur_usec() + self.numcount = self.numcount + 1 + self.previnterval = now - self.prevtime + self.prevtime = now + self.maxinterval = max(self.previnterval, self.maxinterval) + self.mininterval = min(self.mininterval, self.previnterval) + print "Interval", self.previnterval + if (self.numcount % 100) == 0: + if self.numcount == 100: + # Ignore delays during startup + self.reset() + print "MaxInterval", self.maxinterval + print "MinInterval", self.mininterval + + +acktime = MeasureTime() + +def mavrx_debug_handler(message): + """Measure heartbeat periodically""" + mtype = message.get_type() + global sendtime #if mtype == 'HEARTBEAT': if mtype == 'COMMAND_ACK': #traceback.print_stack() - print "Received", t + acktime.update() send_testpackets() def send_testpackets(): - print "send ROI cmds" + #print "send ROI cmds" # create the SET_POSITION_TARGET_GLOBAL_INT command msg = v.message_factory.set_position_target_global_int_encode( From ee6b04b788a7021375ea16491de12cfcbaa45c68 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 15 Dec 2014 11:25:27 -0800 Subject: [PATCH 012/796] remove debugging output --- droneapi/module/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/droneapi/module/api.py b/droneapi/module/api.py index 9d67fed49..0a5bb1b6d 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -245,7 +245,7 @@ def wait_init(self): for i in range(0, int(timeout / pollinterval)): # Don't let the user try to fly while the board is still booting mode = self.__get_mode().name - print "mode is", mode + # print "mode is", mode if mode != "INITIALISING" and mode != "MAV": return From 3f1e0277da2455b37ff5c5df2ac6849095f6c335 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 15 Dec 2014 12:22:24 -0800 Subject: [PATCH 013/796] Print perf measurements less often --- example/perf_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example/perf_test.py b/example/perf_test.py index a0f4f2c30..9c3cb343a 100644 --- a/example/perf_test.py +++ b/example/perf_test.py @@ -77,11 +77,12 @@ def update(self): self.prevtime = now self.maxinterval = max(self.previnterval, self.maxinterval) self.mininterval = min(self.mininterval, self.previnterval) - print "Interval", self.previnterval + if (self.numcount % 100) == 0: if self.numcount == 100: # Ignore delays during startup self.reset() + print "Interval", self.previnterval print "MaxInterval", self.maxinterval print "MinInterval", self.mininterval From 99c1c8cacf58ef56f7ed2c106c2d856f2792f5b9 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Tue, 16 Dec 2014 13:42:09 -0800 Subject: [PATCH 014/796] update perftest results with various platforms --- example/perf_test.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/example/perf_test.py b/example/perf_test.py index 9c3cb343a..3e8e377c3 100644 --- a/example/perf_test.py +++ b/example/perf_test.py @@ -35,11 +35,33 @@ * 20ms+/-300us when talking to SITL (every time we recieve a cmd_ack we immediately send a pair of ROI related msgs) * The less than 300us variablity makes me think SITL has some 20ms poll rate - need to try with real vehicle +SITL Interval (sec) 0.019865 MaxInterval (sec) 0.021927 MinInterval (sec) 0.018421 - +AVR plane load: 20ms+/-7ms +Interval 0.02061 +MaxInterval 0.025496 +MinInterval 0.011533 + +PX4 quad load on Edsion: 20ms +60ms -5ms (VERY HIGH VARIABILITY - TBD is it Edison or PX4) +Interval 0.0281970000001 +MaxInterval 0.0786720000001 +MinInterval 0.0161290000001 + +PX4 quad load on a pixhawk talking to my desktop - similar variability as with an Edison: +Interval 0.01989 +MaxInterval 0.0688479999999 +MinInterval 0.00722900000005 +Interval 0.019929 +MaxInterval 0.0688479999999 +MinInterval 0.00722900000005 +Interval 0.0189700000001 +MaxInterval 0.0688479999999 +MinInterval 0.00722900000005 + +mavproxy.py --master=/dev/ttyMFD1,115200 --cmd="api start perf_test.py" """ global v @@ -79,7 +101,7 @@ def update(self): self.mininterval = min(self.mininterval, self.previnterval) if (self.numcount % 100) == 0: - if self.numcount == 100: + if self.numcount == 200: # Ignore delays during startup self.reset() print "Interval", self.previnterval @@ -97,6 +119,7 @@ def mavrx_debug_handler(message): #if mtype == 'HEARTBEAT': if mtype == 'COMMAND_ACK': #traceback.print_stack() + #print "GOT ACK" acktime.update() send_testpackets() @@ -107,7 +130,7 @@ def send_testpackets(): # create the SET_POSITION_TARGET_GLOBAL_INT command msg = v.message_factory.set_position_target_global_int_encode( 0, # time_boot_ms (not used) - 0, 0, # target system, target component + 1, 1, # target system, target component mavutil.mavlink.MAV_FRAME_GLOBAL, # frame 0b1000000011000111, # type_mask - enable velocity only 0, 0, 0, # x, y, z positions (not used) @@ -120,7 +143,8 @@ def send_testpackets(): # set ROI msg = v.message_factory.command_long_encode( - 0, 0, # target system, target component + 1, 1, # target system, target component + #mavutil.mavlink.MAV_CMD_DO_SET_RELAY, #command mavutil.mavlink.MAV_CMD_DO_SET_ROI, #command 0, #confirmation 0, 0, 0, 0, #params 1-4 @@ -148,6 +172,9 @@ def send_testpackets(): print "groundspeed: %s" % v.groundspeed print "airspeed: %s" % v.airspeed +import time +time.sleep(30) + # Use of the following method is not recommended (it is better to add observer callbacks to attributes) but if you need it # it is available... v.set_mavlink_callback(mavrx_debug_handler) @@ -189,4 +216,5 @@ def send_testpackets(): v.armed = False v.flush() +# send_testpackets() From 4a4ab2ea171b356e9e0720aebfdccb694e11ec70 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Wed, 17 Dec 2014 11:23:15 -0800 Subject: [PATCH 015/796] Finished performance tests --- example/perf_test.py | 48 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/example/perf_test.py b/example/perf_test.py index 3e8e377c3..f7c1e4720 100644 --- a/example/perf_test.py +++ b/example/perf_test.py @@ -35,7 +35,7 @@ * 20ms+/-300us when talking to SITL (every time we recieve a cmd_ack we immediately send a pair of ROI related msgs) * The less than 300us variablity makes me think SITL has some 20ms poll rate - need to try with real vehicle -SITL +SITL copter load Interval (sec) 0.019865 MaxInterval (sec) 0.021927 MinInterval (sec) 0.018421 @@ -45,12 +45,12 @@ MaxInterval 0.025496 MinInterval 0.011533 -PX4 quad load on Edsion: 20ms +60ms -5ms (VERY HIGH VARIABILITY - TBD is it Edison or PX4) +PX4 quad load on Edsion: 20ms +60ms -5ms (VERY HIGH VARIABILITY - mostly due to px4 side - see below) Interval 0.0281970000001 MaxInterval 0.0786720000001 MinInterval 0.0161290000001 -PX4 quad load on a pixhawk talking to my desktop - similar variability as with an Edison: +PX4 quad load on a pixhawk (a9defa35) talking to my desktop - similar variability as with an Edison: Interval 0.01989 MaxInterval 0.0688479999999 MinInterval 0.00722900000005 @@ -61,6 +61,45 @@ MaxInterval 0.0688479999999 MinInterval 0.00722900000005 +or here's 20ish of the interval values seen on the px4 (a9defa35) Test +Interval 0.020012 +Interval 0.0199689999999 +Interval 0.0229640000002 +Interval 0.0171049999999 +Interval 0.0198150000001 +Interval 0.0211049999998 +Interval 0.0199740000003 +Interval 0.0199459999999 +Interval 0.0199590000002 +Interval 0.0200379999997 +Interval 0.0200850000001 +Interval 0.0198839999998 +Interval 0.0200420000001 +Interval 0.0199539999999 +Interval 0.0200760000002 +Interval 0.0199029999999 +Interval 0.0200950000003 +Interval 0.0517199999999 + +now testing with a plane load with a px4 (a9defa35) at 56kbps - highly variable 25 to 82ms +Interval 0.0589850000001 +MaxInterval 0.0829760000001 +MinInterval 0.0258819999999 + +but change to 115kbps and things are much better +Interval 0.0201160000001 +MaxInterval 0.044656 +MinInterval 0.0150279999998 + +and changing things to 500kbps everything is just peachie - 18ms +Interval 0.018119 +MaxInterval 0.02527 +MinInterval 0.015737 + +Recommendations: +Run link as fast as you can 1500kbps? +Turn on hw flow control (and use --rtscts on mavproxy) + mavproxy.py --master=/dev/ttyMFD1,115200 --cmd="api start perf_test.py" """ @@ -100,6 +139,7 @@ def update(self): self.maxinterval = max(self.previnterval, self.maxinterval) self.mininterval = min(self.mininterval, self.previnterval) + #print "Interval", self.previnterval if (self.numcount % 100) == 0: if self.numcount == 200: # Ignore delays during startup @@ -119,7 +159,7 @@ def mavrx_debug_handler(message): #if mtype == 'HEARTBEAT': if mtype == 'COMMAND_ACK': #traceback.print_stack() - #print "GOT ACK" + #print "GOT ACK", message acktime.update() send_testpackets() From 259e790cbc1982ed01f768d33cff44457b416680 Mon Sep 17 00:00:00 2001 From: The Gitter Badger Date: Thu, 18 Dec 2014 15:18:49 +0000 Subject: [PATCH 016/796] Added Gitter badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 22666a9f9..93dc1b3e4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # DroneAPI-python +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/diydrones/droneapi-python?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + Build Status: [![Dependency Status](https://www.codeship.io/projects/869efe80-ae1a-0131-b6a4-527e64ff213a/status)](https://www.codeship.io/projects/19685) From dfaf885f4986c15a69c114196093d28ec283d9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Wed, 17 Dec 2014 19:01:40 -0800 Subject: [PATCH 017/796] add a global load_path for app context --- droneapi/lib/__init__.py | 2 ++ droneapi/module/api.py | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/droneapi/lib/__init__.py b/droneapi/lib/__init__.py index 1aa3ecfb5..61c1b17fb 100644 --- a/droneapi/lib/__init__.py +++ b/droneapi/lib/__init__.py @@ -2,6 +2,8 @@ import threading from pymavlink import mavutil +local_path = '' + def web_connect(authinfo): """ Connect to the central dronehub server diff --git a/droneapi/module/api.py b/droneapi/module/api.py index 0a5bb1b6d..2522a4be1 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -1,3 +1,4 @@ +import os import time import threading import traceback @@ -342,6 +343,8 @@ def __init__(self, mpstate): self.web = None self.web_interface = 0 self.web_serverid = None + + self.local_path = os.path.dirname(os.getcwd()) print("DroneAPI loaded") def __on_change(self, *args): @@ -529,10 +532,22 @@ def cmd_api(self, args): # Just kill the youngest self.cmd_kill(max(self.threads.keys)) elif args[0] == "start": - if len(args) != 2: - print("usage: api load ") + if len(args) < 2: + print("usage: api start ") return - g = { "local_connect" : self.get_connection } + + g = { + "local_connect" : self.get_connection, + "local_path": self.local_path + } + + # define local_path in target file + # this helps to add context to apps + # that need to load external resources + # w/o relative paths + if len(args) == 3: + g["local_path"] = args[2] + APIThread(self, lambda: execfile(args[1], g), args[1]) else: print("Invalid api subcommand") From e92aa8987892178e5bd8ff69ac9d7e7b5c9b50a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Thu, 18 Dec 2014 10:30:02 -0800 Subject: [PATCH 018/796] check if thread is alive before trying to kill it --- droneapi/module/api.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/droneapi/module/api.py b/droneapi/module/api.py index 2522a4be1..4db647108 100644 --- a/droneapi/module/api.py +++ b/droneapi/module/api.py @@ -437,7 +437,8 @@ def send_to_server(self, m): pass def thread_remove(self, t): - del self.threads[t.thread_num] + if t.thread_num in self.threads.keys(): + del self.threads[t.thread_num] def thread_add(self, t): self.threads[t.thread_num] = t @@ -448,7 +449,8 @@ def cmd_list(self): print str(t) def cmd_kill(self, n): - self.threads[n].kill() + if self.threads[n].isAlive(): + self.threads[n].kill() def get_connection(self): return self.api From b45c9da7d22e6e672e985d14c9d77917b822c908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Tue, 16 Dec 2014 12:48:52 -0800 Subject: [PATCH 019/796] organize demos --- example/{ => follow_me}/fake-gps-data.log | 0 example/{ => follow_me}/follow_me.py | 1 - example/{ => follow_me}/run-fake-gps.sh | 0 example/{ => sketch}/client_sketch.py | 1 - example/{ => small_demo}/small_demo.py | 0 5 files changed, 2 deletions(-) rename example/{ => follow_me}/fake-gps-data.log (100%) rename example/{ => follow_me}/follow_me.py (99%) rename example/{ => follow_me}/run-fake-gps.sh (100%) rename example/{ => sketch}/client_sketch.py (99%) rename example/{ => small_demo}/small_demo.py (100%) diff --git a/example/fake-gps-data.log b/example/follow_me/fake-gps-data.log similarity index 100% rename from example/fake-gps-data.log rename to example/follow_me/fake-gps-data.log diff --git a/example/follow_me.py b/example/follow_me/follow_me.py similarity index 99% rename from example/follow_me.py rename to example/follow_me/follow_me.py index d1e811afe..706fed958 100644 --- a/example/follow_me.py +++ b/example/follow_me/follow_me.py @@ -63,4 +63,3 @@ def followme(): print "Error: gpsd service does not seem to be running, plug in USB GPS or run run-fake-gps.sh" followme() - diff --git a/example/run-fake-gps.sh b/example/follow_me/run-fake-gps.sh similarity index 100% rename from example/run-fake-gps.sh rename to example/follow_me/run-fake-gps.sh diff --git a/example/client_sketch.py b/example/sketch/client_sketch.py similarity index 99% rename from example/client_sketch.py rename to example/sketch/client_sketch.py index 460b2dd1d..c77381c41 100644 --- a/example/client_sketch.py +++ b/example/sketch/client_sketch.py @@ -84,4 +84,3 @@ v.ap_pin7_mode = "dout" v.ap_pin7_value = 1 v.flush() - diff --git a/example/small_demo.py b/example/small_demo/small_demo.py similarity index 100% rename from example/small_demo.py rename to example/small_demo/small_demo.py From 74171c5a13a480fc288d2f012e51841b7366e56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Tue, 16 Dec 2014 13:14:09 -0800 Subject: [PATCH 020/796] drone delivery example app --- example/drone_delivery/drone_delivery.py | 143 +++++++++++++++++ example/drone_delivery/drone_tracker.py | 144 ++++++++++++++++++ example/drone_delivery/html/assets/styles.css | 78 ++++++++++ .../drone_delivery/html/bottom-scripts.html | 3 + example/drone_delivery/html/command.html | 65 ++++++++ example/drone_delivery/html/head.html | 8 + example/drone_delivery/html/header.html | 13 ++ example/drone_delivery/html/index.html | 31 ++++ example/drone_delivery/html/mapbox.html | 11 ++ example/drone_delivery/html/track.html | 77 ++++++++++ example/drone_delivery/requirements.pip | 6 + 11 files changed, 579 insertions(+) create mode 100644 example/drone_delivery/drone_delivery.py create mode 100644 example/drone_delivery/drone_tracker.py create mode 100644 example/drone_delivery/html/assets/styles.css create mode 100644 example/drone_delivery/html/bottom-scripts.html create mode 100644 example/drone_delivery/html/command.html create mode 100644 example/drone_delivery/html/head.html create mode 100644 example/drone_delivery/html/header.html create mode 100644 example/drone_delivery/html/index.html create mode 100644 example/drone_delivery/html/mapbox.html create mode 100644 example/drone_delivery/html/track.html create mode 100644 example/drone_delivery/requirements.pip diff --git a/example/drone_delivery/drone_delivery.py b/example/drone_delivery/drone_delivery.py new file mode 100644 index 000000000..def078a28 --- /dev/null +++ b/example/drone_delivery/drone_delivery.py @@ -0,0 +1,143 @@ +import os, os.path +import random +import simplejson +import time +import math +import numpy +import pid +import pdb + +from pymavlink import mavutil +import droneapi.lib +from droneapi.lib import VehicleMode, Location + +import cherrypy +from cherrypy.process import wspbus, plugins +from jinja2 import Environment, FileSystemLoader + +def get_fake_coords(): + return [ + [-117.03426361083983, 32.604675440554985], + [-117.01692581176758, 32.592527461735294], + [-117.00834274291992, 32.59325060181434], + [-116.99838638305664, 32.59122579488824], + [-116.99718475341797, 32.58601893844575], + [-116.99769973754883, 32.579799241291816], + [-116.9992446899414, 32.57531500590736], + [-117.00662612915038, 32.57502569269813] + ] + +class DroneTrackerPlugin(plugins.SimplePlugin): + def __init__(self, bus, drone_klass): + plugins.SimplePlugin.__init__(self, bus) + self.drone = drone_klass() + + def start(self): + self.bus.log('DroneAPI Start') + + def stop(self): + self.bus.log('DroneAPI Stop') + +class Drone(object): + def __init__(self): + self.init_time = time.time() + self.api = droneapi.lib.local_connect() + #pdb.set_trace() + #self.vehicle = self.api.get_vehicles()[0] + +class Templates: + def __init__(self): + self.options = self.get_options() + self.environment = Environment(loader=FileSystemLoader('html')) + + def get_options(self): + return { + 'width': 670, + 'height': 470, + 'zoom': 13, + 'format': 'png', + 'access_token': 'pk.eyJ1IjoibXJwb2xsbyIsImEiOiJtUG0tRk9BIn0.AqAiefUV9fFYRo-w0jFR1Q', + 'mapid': 'mrpollo.kfbnjbl0', + 'home_coords': [-117.0068, 32.5738], + 'menu': [ + {'name': 'Home', 'location': '/'}, + {'name': 'Track', 'location': '/track'}, + {'name': 'Command', 'location': '/command'} + ], + 'current_url': '/', + 'json': '' + } + + def index(self): + self.options = self.get_options() + self.options['current_url'] = '/' + return self.get_template('index') + + def track(self, current_coords): + self.options = self.get_options() + self.options['current_url'] = '/track' + self.options['current_coords'] = current_coords + json = simplejson.dumps(self.options) + self.options['json'] = json + return self.get_template('track') + + def command(self): + self.options = self.get_options() + self.options['current_url'] = '/command' + return self.get_template('command') + + def get_template(self, file_name): + template = self.environment.get_template( file_name + '.html') + return template.render(options=self.options) + + index.exposed = True + + +class Webserver(object): + def __init__(self): + self.templates = Templates() + DroneTrackerPlugin(cherrypy.engine, Drone).subscribe() + + @cherrypy.expose + def index(self): + return self.templates.index() + + @cherrypy.expose + def track(self, lat=None, lon=None): + # FIX RANDOM COORDS + random_current_coords = get_fake_coords() + random.shuffle(random_current_coords) + current_coords = random_current_coords[0] + + return self.templates.track(current_coords) + + @cherrypy.expose + def command(self): + return self.templates.command() + + @cherrypy.expose + @cherrypy.tools.json_out() + def vehicle(self): + # FIX RANDOM COORDS + random_current_coords = get_fake_coords() + random.shuffle(random_current_coords) + current_coords = random_current_coords[0] + + # TOTALLY FAKE DATA + # NEED TO REMOVE THIS + # AND FIX THE PARAM NAMES + # FOR VEHICLE UPDATES + return dict(name='cool copter', position=current_coords) + +#if __name__ == '__main__': +conf = { + '/': { + 'tools.sessions.on': True, + 'tools.staticdir.root': os.path.abspath(os.getcwd()) + }, + '/static': { + 'tools.staticdir.on': True, + 'tools.staticdir.dir': './html/assets' + } +} +cherrypy.quickstart(Webserver(), '/', conf) diff --git a/example/drone_delivery/drone_tracker.py b/example/drone_delivery/drone_tracker.py new file mode 100644 index 000000000..6bf37d303 --- /dev/null +++ b/example/drone_delivery/drone_tracker.py @@ -0,0 +1,144 @@ +import os, os.path +import random +import simplejson +import time +import math +import numpy +import pid +#import ansicolors +import pdb + +from pymavlink import mavutil +import droneapi.lib +from droneapi.lib import VehicleMode, Location + +import cherrypy +from cherrypy.process import wspbus, plugins +from jinja2 import Environment, FileSystemLoader + +def get_fake_coords(): + return [ + [-117.03426361083983, 32.604675440554985], + [-117.01692581176758, 32.592527461735294], + [-117.00834274291992, 32.59325060181434], + [-116.99838638305664, 32.59122579488824], + [-116.99718475341797, 32.58601893844575], + [-116.99769973754883, 32.579799241291816], + [-116.9992446899414, 32.57531500590736], + [-117.00662612915038, 32.57502569269813] + ] + +class DroneTrackerPlugin(plugins.SimplePlugin): + def __init__(self, bus, drone_klass): + plugins.SimplePlugin.__init__(self, bus) + self.drone = drone_klass() + + def start(self): + self.bus.log('DroneAPI Start') + + def stop(self): + self.bus.log('DroneAPI Stop') + +class Drone(object): + def __init__(self): + #pdb.set_trace() + self.init_time = time.time() + self.api = droneapi.lib.local_connect() + self.vehicle = self.api.get_vehicles()[0] + +class Templates: + def __init__(self): + self.options = self.get_options() + self.environment = Environment(loader=FileSystemLoader('html')) + + def get_options(self): + return { + 'width': 670, + 'height': 470, + 'zoom': 13, + 'format': 'png', + 'access_token': 'pk.eyJ1IjoibXJwb2xsbyIsImEiOiJtUG0tRk9BIn0.AqAiefUV9fFYRo-w0jFR1Q', + 'mapid': 'mrpollo.kfbnjbl0', + 'home_coords': [-117.0068, 32.5738], + 'menu': [ + {'name': 'Home', 'location': '/'}, + {'name': 'Track', 'location': '/track'}, + {'name': 'Command', 'location': '/command'} + ], + 'current_url': '/', + 'json': '' + } + + def index(self): + self.options = self.get_options() + self.options['current_url'] = '/' + return self.get_template('index') + + def track(self, current_coords): + self.options = self.get_options() + self.options['current_url'] = '/track' + self.options['current_coords'] = current_coords + json = simplejson.dumps(self.options) + self.options['json'] = json + return self.get_template('track') + + def command(self): + self.options = self.get_options() + self.options['current_url'] = '/command' + return self.get_template('command') + + def get_template(self, file_name): + template = self.environment.get_template( file_name + '.html') + return template.render(options=self.options) + + index.exposed = True + + +class Webserver(object): + def __init__(self): + self.templates = Templates() + DroneTrackerPlugin(cherrypy.engine, Drone).subscribe() + + @cherrypy.expose + def index(self): + return self.templates.index() + + @cherrypy.expose + def track(self, lat=None, lon=None): + # FIX RANDOM COORDS + random_current_coords = get_fake_coords() + random.shuffle(random_current_coords) + current_coords = random_current_coords[0] + + return self.templates.track(current_coords) + + @cherrypy.expose + def command(self): + return self.templates.command() + + @cherrypy.expose + @cherrypy.tools.json_out() + def vehicle(self): + # FIX RANDOM COORDS + random_current_coords = get_fake_coords() + random.shuffle(random_current_coords) + current_coords = random_current_coords[0] + + # TOTALLY FAKE DATA + # NEED TO REMOVE THIS + # AND FIX THE PARAM NAMES + # FOR VEHICLE UPDATES + return dict(name='cool copter', position=current_coords) + +#if __name__ == '__main__': +conf = { + '/': { + 'tools.sessions.on': True, + 'tools.staticdir.root': os.path.abspath(os.getcwd()) + }, + '/static': { + 'tools.staticdir.on': True, + 'tools.staticdir.dir': './html/assets' + } +} +cherrypy.quickstart(Webserver(), '/', conf) diff --git a/example/drone_delivery/html/assets/styles.css b/example/drone_delivery/html/assets/styles.css new file mode 100644 index 000000000..ada80fee7 --- /dev/null +++ b/example/drone_delivery/html/assets/styles.css @@ -0,0 +1,78 @@ +body { + padding-top: 20px; + padding-bottom: 20px; +} + +.form-inline { + margin-top: 20px; +} + +#map, #staticMap { + width: 670px; + height: 470px; +} + +.header, +.marketing, +.footer { + padding-right: 15px; + padding-left: 15px; +} + +.header { + border-bottom: 1px solid #e5e5e5; +} + +.header h3 { + padding-bottom: 19px; + margin-top: 0; + margin-bottom: 0; + line-height: 40px; +} + +.footer { + padding-top: 19px; + color: #777; + border-top: 1px solid #e5e5e5; +} + +@media (min-width: 768px) { + .container { + max-width: 730px; + } +} +.container-narrow > hr { + margin: 30px 0; +} + +.jumbotron { + text-align: center; + border-bottom: 1px solid #e5e5e5; +} + +.jumbotron .btn { + padding: 14px 24px; + font-size: 21px; +} + +.marketing { + margin: 40px 0; +} +.marketing p + h4 { + margin-top: 28px; +} + +@media screen and (min-width: 768px) { + .header, + .marketing, + .footer { + padding-right: 0; + padding-left: 0; + } + .header { + margin-bottom: 30px; + } + .jumbotron { + border-bottom: 0; + } +} diff --git a/example/drone_delivery/html/bottom-scripts.html b/example/drone_delivery/html/bottom-scripts.html new file mode 100644 index 000000000..0b7a716c8 --- /dev/null +++ b/example/drone_delivery/html/bottom-scripts.html @@ -0,0 +1,3 @@ + + + diff --git a/example/drone_delivery/html/command.html b/example/drone_delivery/html/command.html new file mode 100644 index 000000000..83cbb5071 --- /dev/null +++ b/example/drone_delivery/html/command.html @@ -0,0 +1,65 @@ + + + + {% include 'head.html' %} + + +
+ {% import 'header.html' as header %} +
+ +

DroneAPI Demos {{options.current_url}}

+
+
+ {% import 'mapbox.html' as mapbox%} +
+
+
+
+
+
+ +
+
+ +
+ +
+
+
+
+

© 3D Robotics Inc.

+
+ + {% include 'bottom-scripts.html' %} + + + diff --git a/example/drone_delivery/html/head.html b/example/drone_delivery/html/head.html new file mode 100644 index 000000000..64eb745ca --- /dev/null +++ b/example/drone_delivery/html/head.html @@ -0,0 +1,8 @@ + + + +Drone Delivery App + + + + diff --git a/example/drone_delivery/html/header.html b/example/drone_delivery/html/header.html new file mode 100644 index 000000000..eeb7201dd --- /dev/null +++ b/example/drone_delivery/html/header.html @@ -0,0 +1,13 @@ +{% macro list_item(name, location='#', active=False) -%} +
  • + {{name}} +
  • +{%- endmacro %} + +{% macro navigation(list, current_url, class='') -%} +
      + {%- for item in list %} + {{ list_item(item.name, item.location, (item.location == current_url)) }} + {%- endfor %} +
    +{%- endmacro %} diff --git a/example/drone_delivery/html/index.html b/example/drone_delivery/html/index.html new file mode 100644 index 000000000..70bac6911 --- /dev/null +++ b/example/drone_delivery/html/index.html @@ -0,0 +1,31 @@ + + + + {% include 'head.html' %} + + +
    + {% import 'header.html' as header %} +
    + +

    DroneAPI Demos {{options.current_url}}

    +
    +
    +

    Drone Delivery App

    +

    + Track and Command a drone to delivery packages from a web app +

    +

    + Track + Command +

    +
    +
    +

    © 3D Robotics Inc.

    +
    + + {% include 'bottom-scripts.html' %} + + diff --git a/example/drone_delivery/html/mapbox.html b/example/drone_delivery/html/mapbox.html new file mode 100644 index 000000000..e99c1f07b --- /dev/null +++ b/example/drone_delivery/html/mapbox.html @@ -0,0 +1,11 @@ +{% macro static_map(options, coords) -%} + +{%- endmacro %} + +{% macro static_map_with_marker(options, coords, marker_coords) -%} + +{%- endmacro %} + +{% macro marker_string(coords, label='heliport', color='f86767') -%} + pin-m-{{ label }}+{{ color }}({{ coords[0] }},{{ coords[1] }}) +{%- endmacro %} diff --git a/example/drone_delivery/html/track.html b/example/drone_delivery/html/track.html new file mode 100644 index 000000000..e280e8e37 --- /dev/null +++ b/example/drone_delivery/html/track.html @@ -0,0 +1,77 @@ + + + + {% include 'head.html' %} + + +
    + {% import 'header.html' as header %} +
    + +

    DroneAPI Demos {{options.current_url}}

    +
    +
    + {% import 'mapbox.html' as mapbox%} +
    + {{ mapbox.static_map_with_marker(options, options.home_coords, options.current_coords) }} +
    +
    +
    +
    +
    +
    + +
    +
    + +
    + +
    +
    +
    +
    +

    © 3D Robotics Inc.

    +
    +
    + {% include 'bottom-scripts.html' %} + + + diff --git a/example/drone_delivery/requirements.pip b/example/drone_delivery/requirements.pip new file mode 100644 index 000000000..1b0db761d --- /dev/null +++ b/example/drone_delivery/requirements.pip @@ -0,0 +1,6 @@ +numpy +simplejson +pid +cherrypy +jinja2 +droneapi From eecbe11cfa8c32f4bdfdccbc9d9d1fbef8bb7ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Tue, 16 Dec 2014 13:21:40 -0800 Subject: [PATCH 021/796] remove duplicate file --- example/drone_delivery/drone_tracker.py | 144 ------------------------ 1 file changed, 144 deletions(-) delete mode 100644 example/drone_delivery/drone_tracker.py diff --git a/example/drone_delivery/drone_tracker.py b/example/drone_delivery/drone_tracker.py deleted file mode 100644 index 6bf37d303..000000000 --- a/example/drone_delivery/drone_tracker.py +++ /dev/null @@ -1,144 +0,0 @@ -import os, os.path -import random -import simplejson -import time -import math -import numpy -import pid -#import ansicolors -import pdb - -from pymavlink import mavutil -import droneapi.lib -from droneapi.lib import VehicleMode, Location - -import cherrypy -from cherrypy.process import wspbus, plugins -from jinja2 import Environment, FileSystemLoader - -def get_fake_coords(): - return [ - [-117.03426361083983, 32.604675440554985], - [-117.01692581176758, 32.592527461735294], - [-117.00834274291992, 32.59325060181434], - [-116.99838638305664, 32.59122579488824], - [-116.99718475341797, 32.58601893844575], - [-116.99769973754883, 32.579799241291816], - [-116.9992446899414, 32.57531500590736], - [-117.00662612915038, 32.57502569269813] - ] - -class DroneTrackerPlugin(plugins.SimplePlugin): - def __init__(self, bus, drone_klass): - plugins.SimplePlugin.__init__(self, bus) - self.drone = drone_klass() - - def start(self): - self.bus.log('DroneAPI Start') - - def stop(self): - self.bus.log('DroneAPI Stop') - -class Drone(object): - def __init__(self): - #pdb.set_trace() - self.init_time = time.time() - self.api = droneapi.lib.local_connect() - self.vehicle = self.api.get_vehicles()[0] - -class Templates: - def __init__(self): - self.options = self.get_options() - self.environment = Environment(loader=FileSystemLoader('html')) - - def get_options(self): - return { - 'width': 670, - 'height': 470, - 'zoom': 13, - 'format': 'png', - 'access_token': 'pk.eyJ1IjoibXJwb2xsbyIsImEiOiJtUG0tRk9BIn0.AqAiefUV9fFYRo-w0jFR1Q', - 'mapid': 'mrpollo.kfbnjbl0', - 'home_coords': [-117.0068, 32.5738], - 'menu': [ - {'name': 'Home', 'location': '/'}, - {'name': 'Track', 'location': '/track'}, - {'name': 'Command', 'location': '/command'} - ], - 'current_url': '/', - 'json': '' - } - - def index(self): - self.options = self.get_options() - self.options['current_url'] = '/' - return self.get_template('index') - - def track(self, current_coords): - self.options = self.get_options() - self.options['current_url'] = '/track' - self.options['current_coords'] = current_coords - json = simplejson.dumps(self.options) - self.options['json'] = json - return self.get_template('track') - - def command(self): - self.options = self.get_options() - self.options['current_url'] = '/command' - return self.get_template('command') - - def get_template(self, file_name): - template = self.environment.get_template( file_name + '.html') - return template.render(options=self.options) - - index.exposed = True - - -class Webserver(object): - def __init__(self): - self.templates = Templates() - DroneTrackerPlugin(cherrypy.engine, Drone).subscribe() - - @cherrypy.expose - def index(self): - return self.templates.index() - - @cherrypy.expose - def track(self, lat=None, lon=None): - # FIX RANDOM COORDS - random_current_coords = get_fake_coords() - random.shuffle(random_current_coords) - current_coords = random_current_coords[0] - - return self.templates.track(current_coords) - - @cherrypy.expose - def command(self): - return self.templates.command() - - @cherrypy.expose - @cherrypy.tools.json_out() - def vehicle(self): - # FIX RANDOM COORDS - random_current_coords = get_fake_coords() - random.shuffle(random_current_coords) - current_coords = random_current_coords[0] - - # TOTALLY FAKE DATA - # NEED TO REMOVE THIS - # AND FIX THE PARAM NAMES - # FOR VEHICLE UPDATES - return dict(name='cool copter', position=current_coords) - -#if __name__ == '__main__': -conf = { - '/': { - 'tools.sessions.on': True, - 'tools.staticdir.root': os.path.abspath(os.getcwd()) - }, - '/static': { - 'tools.staticdir.on': True, - 'tools.staticdir.dir': './html/assets' - } -} -cherrypy.quickstart(Webserver(), '/', conf) From 7547a17db18e7dfacd9fef7376b49d969d14a2aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Wed, 17 Dec 2014 15:49:06 -0800 Subject: [PATCH 022/796] streamline drone control --- example/drone_delivery/drone_delivery.py | 116 ++++++++++------------- 1 file changed, 49 insertions(+), 67 deletions(-) diff --git a/example/drone_delivery/drone_delivery.py b/example/drone_delivery/drone_delivery.py index def078a28..a401878e5 100644 --- a/example/drone_delivery/drone_delivery.py +++ b/example/drone_delivery/drone_delivery.py @@ -1,11 +1,5 @@ import os, os.path -import random import simplejson -import time -import math -import numpy -import pid -import pdb from pymavlink import mavutil import droneapi.lib @@ -15,40 +9,37 @@ from cherrypy.process import wspbus, plugins from jinja2 import Environment, FileSystemLoader -def get_fake_coords(): - return [ - [-117.03426361083983, 32.604675440554985], - [-117.01692581176758, 32.592527461735294], - [-117.00834274291992, 32.59325060181434], - [-116.99838638305664, 32.59122579488824], - [-116.99718475341797, 32.58601893844575], - [-116.99769973754883, 32.579799241291816], - [-116.9992446899414, 32.57531500590736], - [-117.00662612915038, 32.57502569269813] - ] - -class DroneTrackerPlugin(plugins.SimplePlugin): - def __init__(self, bus, drone_klass): - plugins.SimplePlugin.__init__(self, bus) - self.drone = drone_klass() - - def start(self): - self.bus.log('DroneAPI Start') - - def stop(self): - self.bus.log('DroneAPI Stop') +ROOT_FOLDER = '/home/user/Droneapi' class Drone(object): - def __init__(self): - self.init_time = time.time() - self.api = droneapi.lib.local_connect() - #pdb.set_trace() - #self.vehicle = self.api.get_vehicles()[0] + def __init__(self, home_coords): + self.api = local_connect() + self.vehicle = self.api.get_vehicles()[0] + self.commands = self.vehicle.commands + self.altitude = 30 + + # Fly drone to home location + self.vehicle.mode = VehicleMode('GUIDED') + self.goto(home_coords) + + def goto(self, location, relative=None): + location_object = Location( + location[0], location[1], + self.altitude, + is_relative=relative + ) + self.commands.goto(location_object) + self.vehicle.flush() + self.current_location = location_object + + def get_location(self): + return [self.current_location.lat, self.current_location.lon] class Templates: - def __init__(self): + def __init__(self, home_coords): + self.home_coords = home_coords self.options = self.get_options() - self.environment = Environment(loader=FileSystemLoader('html')) + self.environment = Environment(loader=FileSystemLoader( ROOT_FOLDER + 'html')) def get_options(self): return { @@ -58,7 +49,7 @@ def get_options(self): 'format': 'png', 'access_token': 'pk.eyJ1IjoibXJwb2xsbyIsImEiOiJtUG0tRk9BIn0.AqAiefUV9fFYRo-w0jFR1Q', 'mapid': 'mrpollo.kfbnjbl0', - 'home_coords': [-117.0068, 32.5738], + 'home_coords': self.home_coords, 'menu': [ {'name': 'Home', 'location': '/'}, {'name': 'Track', 'location': '/track'}, @@ -77,67 +68,58 @@ def track(self, current_coords): self.options = self.get_options() self.options['current_url'] = '/track' self.options['current_coords'] = current_coords - json = simplejson.dumps(self.options) - self.options['json'] = json + self.options['json'] = simplejson.dumps(self.options) return self.get_template('track') - def command(self): + def command(self, current_coords): self.options = self.get_options() self.options['current_url'] = '/command' + self.options['current_coords'] = current_coords return self.get_template('command') def get_template(self, file_name): template = self.environment.get_template( file_name + '.html') return template.render(options=self.options) - index.exposed = True - - class Webserver(object): def __init__(self): - self.templates = Templates() - DroneTrackerPlugin(cherrypy.engine, Drone).subscribe() + home_coords = [32.5738, -117.0068] + self.templates = Templates(home_coords) + self.drone = Drone(home_coords) @cherrypy.expose def index(self): return self.templates.index() - @cherrypy.expose - def track(self, lat=None, lon=None): - # FIX RANDOM COORDS - random_current_coords = get_fake_coords() - random.shuffle(random_current_coords) - current_coords = random_current_coords[0] - - return self.templates.track(current_coords) - @cherrypy.expose def command(self): - return self.templates.command() + return self.templates.command(self.drone.get_location()) @cherrypy.expose @cherrypy.tools.json_out() def vehicle(self): - # FIX RANDOM COORDS - random_current_coords = get_fake_coords() - random.shuffle(random_current_coords) - current_coords = random_current_coords[0] - - # TOTALLY FAKE DATA - # NEED TO REMOVE THIS - # AND FIX THE PARAM NAMES - # FOR VEHICLE UPDATES - return dict(name='cool copter', position=current_coords) - -#if __name__ == '__main__': + return dict(position=self.drone.get_location()) + + @cherrypy.expose + def track(self, lat=None, lon=None): + # Get new coordinates from Vehicle + current_coords = self.drone.get_location() + + # PROCESS POST REQUEST FROM COMMAND + if(lat != None and lon != None): + self.drone.goto([float(lat), float(lon)], True) + + return self.templates.track(current_coords) + conf = { '/': { 'tools.sessions.on': True, - 'tools.staticdir.root': os.path.abspath(os.getcwd()) + 'tools.staticdir.root': ROOT_FOLDER }, '/static': { 'tools.staticdir.on': True, 'tools.staticdir.dir': './html/assets' } } + cherrypy.quickstart(Webserver(), '/', conf) From 5e0fe2d2d01dda4dc02f8e71fecabdac7fb7ee1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Roche?= Date: Wed, 17 Dec 2014 15:49:26 -0800 Subject: [PATCH 023/796] cleanup mapbox coordinate mess --- example/drone_delivery/html/command.html | 14 +++++++++++--- example/drone_delivery/html/mapbox.html | 6 +++--- example/drone_delivery/html/track.html | 12 +++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/example/drone_delivery/html/command.html b/example/drone_delivery/html/command.html index 83cbb5071..447e95114 100644 --- a/example/drone_delivery/html/command.html +++ b/example/drone_delivery/html/command.html @@ -13,7 +13,6 @@

    DroneAPI Demos {{options.current_url}}

    - {% import 'mapbox.html' as mapbox%}
    @@ -36,14 +35,22 @@

    DroneAPI Demos {{options.current_url}}

    {% include 'bottom-scripts.html' %}