Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Flying A Drone


Published on

Flying Crazyflie With Python
Sharing some code snippets, interesting things behind.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Flying A Drone

  1. 1. Flying A Drone Dineshkumar Developer @ ThoughtWorks
  2. 2. Agenda How Callbacks are Used by Crazyflie API (Middleware) Callbacks used in client, control copter with keyboard Voice controlled copter
  3. 3. On Action - Callbacks self.crazyflie.connected.add_callback(self._link_commander_to_copter) self.crazyflie.disconnected.add_callback(self._disconnected) self.crazyflie.connection_failed.add_callback(self._failed) self.crazyflie.connection_lost.add_callback(self._lost)
  4. 4. Callbacks class Caller(): def __init__(self): self.callbacks = [] def add_callback(self, cb): if ((cb in self.callbacks) is False): self.callbacks.append(cb) def remove_callback(self, cb): self.callbacks.remove(cb) def call(self, *args): for cb in self.callbacks: cb(*args)
  5. 5. Control Copter With Keys - Notify Commander if key_pressed[pygame.K_UP]: self.callback.notify(Commands.INCREASE_THRUST) elif key_pressed[pygame.K_DOWN]: self.callback.notify(Commands.DECREASE_THRUST) elif key_pressed[pygame.K_LEFT]: self.callback.notify(Commands.YAW_LEFT) elif key_pressed[pygame.K_RIGHT]: self.callback.notify(Commands.YAW_RIGHT)
  6. 6. Copter Commander - On An Event def notify(self, key): if key == "INCREASE_THRUST": self.increase_thrust() elif key == "DECREASE_THRUST": self.decrease_thrust() elif key == "ROLL_LEFT": self.roll_left() elif key == "ROLL_RIGHT": self.roll_right()
  7. 7. Copter Commander - Increase Thrust def increase_thrust(self): self.thrust += CopterConfigs.THRUST_OFFSET self.send_commands()
  8. 8. Control Copter With Keys def send_commands(self): self.check_values_range() if self.copter_commander: self.copter_commander.send_setpoint( self.roll, self.pitch, self.yaw, self.thrust)
  9. 9. Controlling Copter Connect to copter Using Pygame for key listeners On key press, send notifications to callback (callback.notify(ACTION)) Callback handle action accordingly (Here by sending commands to copter)
  10. 10. Voice Control class MessageHandler(socketserver.BaseRequestHandler): def handle(self): = self.request.recv(1024).decode('utf-8').strip() print("SERVER: received:" + global message_listener copter_response = message_listener.notify( self.request.sendall(copter_response.encode('utf-8'))
  11. 11. Voice Control def notify(self, command): response_from_action = self.voice_config.get_command_action(command).__call__() return response_from_action or self.voice_config.get_message(command)
  12. 12. Voice Control class VoiceControlConfig: def __init__(self, voice_controlled_actions): self.copter_commands = { "connect": (voice_controlled_actions.connect_to_copter, "I am trying to connect sir."), "disconnect": (voice_controlled_actions.disconnect_copter, "good bye."), "fly": (, "at your service."), ... } def get_message(self, command): return self.copter_commands.get(command)[1] def get_command_action(self, command): return self.copter_commands.get(command)[0]
  13. 13. Voice control Convert Voice commands to Text (use google, etc) The input sentences (easy to recognise) are converted to predefined commands. Send the text ‘Command’ to Message Listener Message Listener convert the commands to proper actions Based on actions invoke callbacks
  14. 14. Goals Autopilot Use Machine Learning to get commands from environment (eg: Direction to move from an image, altitude to fly) Seamless Interactive Flying Copter Network of Copter
  15. 15. Lets Fly...
  16. 16. Thank You Questions / Feedback