| 51 | |
| 52 | |
| 53 | class Drone(object): |
| 54 | def __init__(self, server_enabled=True): |
| 55 | self.gps_lock = False |
| 56 | self.altitude = 30.0 |
| 57 | |
| 58 | # Connect to the Vehicle |
| 59 | self._log('Connected to vehicle.') |
| 60 | self.vehicle = vehicle |
| 61 | self.commands = self.vehicle.commands |
| 62 | self.current_coords = [] |
| 63 | self.webserver_enabled = server_enabled |
| 64 | self._log("DroneDelivery Start") |
| 65 | |
| 66 | # Register observers |
| 67 | self.vehicle.add_attribute_listener('location', self.location_callback) |
| 68 | |
| 69 | def launch(self): |
| 70 | self._log("Waiting for location...") |
| 71 | while self.vehicle.location.global_frame.lat == 0: |
| 72 | time.sleep(0.1) |
| 73 | self.home_coords = [self.vehicle.location.global_frame.lat, |
| 74 | self.vehicle.location.global_frame.lon] |
| 75 | |
| 76 | self._log("Waiting for ability to arm...") |
| 77 | while not self.vehicle.is_armable: |
| 78 | time.sleep(.1) |
| 79 | |
| 80 | self._log('Running initial boot sequence') |
| 81 | self.change_mode('GUIDED') |
| 82 | self.arm() |
| 83 | self.takeoff() |
| 84 | |
| 85 | if self.webserver_enabled is True: |
| 86 | self._run_server() |
| 87 | |
| 88 | def takeoff(self): |
| 89 | self._log("Taking off") |
| 90 | self.vehicle.simple_takeoff(30.0) |
| 91 | |
| 92 | def arm(self, value=True): |
| 93 | if value: |
| 94 | self._log('Waiting for arming...') |
| 95 | self.vehicle.armed = True |
| 96 | while not self.vehicle.armed: |
| 97 | time.sleep(.1) |
| 98 | else: |
| 99 | self._log("Disarming!") |
| 100 | self.vehicle.armed = False |
| 101 | |
| 102 | def _run_server(self): |
| 103 | # Start web server if enabled |
| 104 | cherrypy.tree.mount(DroneDelivery(self), '/', config=cherrypy_conf) |
| 105 | |
| 106 | cherrypy.config.update({'server.socket_port': 8080, |
| 107 | 'server.socket_host': '0.0.0.0', |
| 108 | 'log.screen': None}) |
| 109 | |
| 110 | print('''Server is bound on all addresses, port 8080 |