class ARDrone(Drone): def __init__(self): super().__init__() self.bebop = Bebop() print("connecting to bebop drone") self.connection.emit("progress") self.success = self.bebop.connect(5) if self.success: self.connection.emit("on") self.bebop.set_max_altitude(20) self.bebop.set_max_distance(20) self.bebop.set_max_rotation_speed(180) self.bebop.set_max_vertical_speed(2) self.bebop.enable_geofence(1) self.bebop.set_hull_protection(1) # todo: battery signal to emit (look in sensors) #TODO test this piece of code self.bebop.set_user_sensor_callback(print, self.bebop.sensors.battery) else: print("refresh....") self.connection.emit("off") def take_off(self): self.bebop.safe_takeoff(5) def land(self): self.bebop.safe_land(5) def stop(self): self.bebop.disconnect() def fly_direct(self, roll, pitch, yaw, vertical_movement): my_roll = self.bebop._ensure_fly_command_in_range(roll) my_pitch = self.bebop_ensure_fly_command_in_range(pitch) my_yaw = self.bebop_ensure_fly_command_in_range(yaw) my_vertical = self.bebop_ensure_fly_command_in_range(vertical_movement) command_tuple = self.bebop.command_parser.get_command_tuple( "ardrone3", "Piloting", "PCMD") self.bebop.drone_connection.send_single_pcmd_command( command_tuple, my_roll, my_pitch, my_yaw, my_vertical) def process_motion(self, _up, _rotate, _front, _right): velocity_up = _up * self.max_vert_speed velocity_yaw = _rotate * self.max_rotation_speed velocity_pitch = _front * self.max_horiz_speed velocity_roll = _right * self.max_horiz_speed #print("PRE", velocity_roll, velocity_pitch, velocity_up, velocity_yaw) self.fly_direct(velocity_roll, velocity_pitch, velocity_yaw, velocity_up)
# import drone from pyparrot.Bebop import Bebop # function to uncover payload def strip_packet(m): ba = bitstring.BitArray(bytes=m) # encode string and turn to bit array pt = ba[9:16] # type of packet contents # cc = ba[4:8] # number of extra header fields print(pt.uint) # for testing purposes # return ba[(12 + cc) * 8:] # bit contents # create drone and start video drone = Bebop() drone.connect(10) drone.set_video_resolutions("rec1080_stream480") drone.set_video_framerate("24_FPS") drone.start_video_stream() # create socket of required type, and bind to port sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # opens with ipv4 address and reads UDP packet sock.bind(('192.168.42.63', 55004)) # binds machine to port # receive packet and get payload for _ in range(10): msg = sock.recv(4096) strip_packet(msg) # payload = stripPacket(msg) '''The received contents are H.264 encoded video. While we can put packets together, there doesn't seem to be
class myoBebop(object): def __init__(self , indoorFly = True): '''类初始化函数''' self.bebop = None self.indoorFlyFlag = indoorFly self.actionType = "rest" #当前的动作类型 self.startSuccessFlag = False #初始化是否成功标志位 self.takeOfFlag = False #是否起飞标志位 self.landFlag = True #是否降落标志位 self.startFlag = False #程序开始标志位 self.executeCommondFlag = True #命令执行成功标志位 #动作映射字典 self.actionMapDict = { "front" : "fist" , "back" : "open_hand" , "left" : "one" , "right" : "two" , "up" : "good", "down" : "low" , "rotateLeft" : "three" , "rotateRight" : "four" , "takeOf": "ok" , "land" : "love"} self.tempCount = 0 #计数 self.excuteFlag1 = True #读取动作类别标志位 self.excuteFlag2 = True #读取按键值的标志位 self.flyType = 0 #飞机飞行类别(0:正常飞行 , 1:特技飞行) self.moveStepTime = 1 #飞行器水平运行步长时间 self.rotateStepTime = 1 #飞行器旋转运行步长时间 def start(self): '''开始函数''' #初始化飞机对象 self.bebop = Bebop() #连接飞机 print("connecting") self.success = self.bebop.connect(10) #与飞机进行连接(最大尝试次数为10次) if self.success == True: print("sleeping") self.bebop.smart_sleep(5) self.bebop.ask_for_state_update() #获取飞机的状态 if self.indoorFlyFlag == True: self.setIndoorFly() print("set indoor fly sucess!!!") print("The aircraft was successfully initialized!") self.startSuccessFlag = True #开始成功 else: print("The connection failed. Please check again!") self.startSuccessFlag = False #开始失败 self.removeExitFile() def bebopFly(self , actionType): '''根据动作类别进行相应的动作''' if actionType == self.actionMapDict["front"]: #向前飞 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll=0, pitch=50, yaw=0, vertical_movement=0, duration=self.moveStepTime) print("fly front") elif self.flyType == 1: self.success = self.bebop.flip(direction="front") print("flip front") print("mambo flip result %s" % self.success) self.bebop.smart_sleep(5) self.executeCommondFlag = True elif actionType == self.actionMapDict["back"]: #向后飞 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll=0, pitch=-50, yaw=0, vertical_movement=0, duration=self.moveStepTime) print("fly back") elif self.flyType == 1: self.success = self.bebop.flip(direction="back") print("flip back") print("mambo flip result %s" % self.success) self.bebop.smart_sleep(5) self.executeCommondFlag = True elif actionType == self.actionMapDict["left"]: #左飞 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll=50, pitch=0, yaw=0, vertical_movement=0, duration=self.moveStepTime) print("fly left") elif self.flyType == 1: self.success = self.bebop.flip(direction="left") print("flip left") print("mambo flip result %s" % self.success) self.bebop.smart_sleep(5) self.executeCommondFlag = True elif actionType == self.actionMapDict["right"]: #右飞 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll=-50, pitch=0, yaw=0, vertical_movement=0, duration=self.moveStepTime) print("fly right") elif self.flyType == 1: self.success = self.bebop.flip(direction="right") print("flip right") print("mambo flip result %s" % self.success) self.bebop.smart_sleep(5) self.executeCommondFlag = True elif actionType == self.actionMapDict["up"]: #上飞 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll = 0, pitch = 0, yaw = 0, vertical_movement=50, duration = self.moveStepTime) print("fly up") elif self.flyType == 1: pass self.executeCommondFlag = True elif actionType == self.actionMapDict["down"]: #下飞 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll = 0, pitch = 0, yaw = 0, vertical_movement= - 50, duration = self.moveStepTime) print("fly down") elif self.flyType == 1: pass self.executeCommondFlag = True elif actionType == self.actionMapDict["rotateLeft"]: #向左旋转 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll=0, pitch=0, yaw=50, vertical_movement=0, duration=self.moveStepTime) print("fly rotateLeft") elif self.flyType == 1: pass self.executeCommondFlag = True elif actionType == self.actionMapDict["rotateRight"]: #向右旋转 self.executeCommondFlag = False print("flying state is %s" % self.bebop.sensors.flying_state) if self.flyType == 0: self.bebop.fly_direct(roll=0, pitch=0, yaw = -50, vertical_movement=0, duration=self.moveStepTime) print("fly rotateRight") elif self.flyType == 1: pass self.executeCommondFlag = True elif actionType == self.actionMapDict["takeOf"]: #起飞 self.startFlag = True if self.landFlag == True: self.landFlag = False self.executeCommondFlag = False print("take of ") self.bebop.safe_takeoff(10) self.bebop.smart_sleep(5) self.executeCommondFlag = True self.takeOfFlag = True elif actionType == self.actionMapDict["land"]: #降落 if self.takeOfFlag == True: self.takeOfFlag = False self.executeCommondFlag = False print("land ") self.bebop.safe_land(10) self.bebop.smart_sleep(5) self.landFlag = True self.executeCommondFlag = True #myo失去连接后,飞机自动着落 def safeAction(self): if self.startFlag == True: if self.flyType == 0: if self.tempCount > 100: self.bebop.safe_land(10) self.bebop.smart_sleep(5) print("DONE - disconnecting") self.bebop.disconnect() self.mThread.forcedStopThread() elif self.flyType == 1: if self.tempCount > 300: self.bebop.safe_land(10) self.bebop.smart_sleep(5) print("DONE - disconnecting") self.bebop.disconnect() self.mThread.forcedStopThread() def setTheActionMain(self): '''设置动作类别线程函数''' if self.excuteFlag1 == True: self.excuteFlag1 = False os.system("python2 myoMain.py") time.sleep(0.01) def removeExitFile(self , fileName = "actionTempData.dat" ): '''删除已经存在的文件''' if os.path.exists(fileName) == True: os.remove(fileName) def setIndoorFly(self): '''设置室内飞行的参数''' self.bebop.set_max_tilt(5) self.bebop.set_max_vertical_speed(1) def getKeyValueMain(self): '''获取按键值的线程函数''' if self.excuteFlag2 == True: self.excuteFlag2 = False os.system("python2 quit.py" ) time.sleep(0.01) #按键退出函数 def quitMain(self): print("quit") self.bebop.safe_land(10) self.bebop.smart_sleep(5) self.bebop.disconnect() self.mThread.forcedStopThread() def getTheActionMain(self): '''得到动作类别线程函数''' global quitFlag while True: self.actionType = getTheCurrentAction() getTheKeyValue() if quitFlag == True: self.quitMain() else: if self.actionType == None: self.tempCount += 1 else: self.tempCount = 0 self.safeAction() if self.executeCommondFlag == True: self.bebopFly(self.actionType) time.sleep(0.01) def run(self): '''运行主程序''' try: self.mThread = myThread() self.mThread.addThread('setTheActionMain' , self.setTheActionMain , 0) self.mThread.addThread('getTheActionMain' , self.getTheActionMain , 0) self.mThread.addThread('getKeyValueMain' , self.getKeyValueMain , 0) self.mThread.runThread() except KeyboardInterrupt: print("DONE") self.bebop.disconnect() self.mThread.forcedStopThread()
bebop.pan_tilt_camera_velocity(pan_velocity=0, tilt_velocity=-2, duration=4) # land bebop.safe_land(5) print("Finishing demo and stopping vision") bebopVision.close_video() # disconnect nicely so we don't need a reboot print("disconnecting") bebop.disconnect() if __name__ == "__main__": # make my bebop object bebop = Bebop() # connect to the bebop success = bebop.connect(5) if (success): # start up the video bebopVision = DroneVisionGUI(bebop, is_bebop=True, user_code_to_run=demo_user_code_after_vision_opened, user_args=(bebop,)) userVision = UserVision(bebopVision) bebopVision.set_user_callback_function(userVision.save_pictures, user_callback_args=None) bebopVision.open_video() else: print("Error connecting to bebop. Retry")
od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) # Loading label map # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) drone = Bebop() success = drone.connect(5) #drone.set_picture_format('jpeg') is_bebop = True if (success): # get the state information print("sleeping") drone.smart_sleep(1) drone.ask_for_state_update() drone.smart_sleep(1) status = input("Input 't' if you want to TAKE OFF or not : ") bebopVision = DroneVisionGUI(drone, is_bebop=True, buffer_size=200,user_code_to_run=tracking_target, user_args=(drone, status, q)) Kim = Kim(bebopVision) bebopVision.set_user_callback_function(Kim.detect_target, user_callback_args=(category_index,detection_graph,sess))
mamboVision.close_video() mambo.smart_sleep(5) print("disconnecting") mambo.disconnect() if __name__ == "__main__": # make my mambo object # remember to set True/False for the wifi depending on if you are using the wifi or the BLE to connect # the address can be empty if you are using wifi mambo = Bebop() print("trying to connect to mambo now") success = mambo.connect(num_retries=3) print("connected: %s" % success) if (success): # get the state information print("sleeping") mambo.smart_sleep(1) mambo.ask_for_state_update() mambo.smart_sleep(1) # setup the extra window to draw the markers in cv2.namedWindow("ExampleWindow") cv2.namedWindow("dst") print("Preparing to open vision") mambo.pan_tilt_camera_velocity(-90, 0, 1)
while bebop_vision.vision_running: img = bebop_vision.get_latest_valid_picture() if img is not None: instruction = qr_manager.read_qr_code(img) if instruction is not None: if instruction == "take_off": bebop.safe_takeoff(5) elif instruction == "land": bebop.safe_land(5) bebop_vision.close_video() elif instruction in instructions: values = instructions[instruction] bebop.move_relative(values[0], values[1], values[2], values[3]) bebop.disconnect() if __name__ == "__main__": BEBOP = Bebop(drone_type="Bebop") SUCCESS = BEBOP.connect(20) INSTRUCTIONS = file_manager.get_navigation_instructions() if SUCCESS: BEBOP_VISION = DroneVisionGUI( BEBOP, is_bebop=True, user_code_to_run=demo_user_code_after_vision_opened, user_args=(BEBOP, INSTRUCTIONS)) BEBOP_VISION.open_video() else: print("Error connecting to bebop. Retry")
from pyparrot.Bebop import Bebop import threading import keyboard drone1 = Bebop() drone1.connect(10) def interface(drone1): print('interface is ON') while not keyboard.is_pressed('esc') and not keyboard.is_pressed('esc'): if keyboard.is_pressed('l'): print('Landing') drone1.land() elif keyboard.is_pressed(' '): print('Taking off') drone1.safe_takeoff(10) elif keyboard.is_pressed('w'): print('Moving forward') drone1.fly_direct(roll=0, pitch=50, yaw=0, vertical_movement=0, duration=0.1) elif keyboard.is_pressed('s'): print('Moving backward') drone1.fly_direct(roll=0, pitch=-50, yaw=0, vertical_movement=0, duration=0.1)
droneVision.close_video() # disconnect nicely so we don't need a reboot print("disconnecting") drone.disconnect() if __name__ == "__main__": drone_type = input("Input drone type bebop 'b' or mambo 'm' : ") num_classes = 3 # number of classes if drone_type == 'b': drone = Bebop() success = drone.connect(5) drone.set_picture_format('jpeg') # 영상 포맷 변경 is_bebop = True height = 480 width = 856 elif drone_type == 'm': mamboAddr = "58:FB:84:3B:12:62" drone = Mambo(mamboAddr, use_wifi=True) success = drone.connect(num_retries=3) is_bebop = False height = 360 width = 640 # drone.set_max_tilt() if (success):
class PyParrot(gui.MyFrame1): def __init__(self, parent): gui.MyFrame1.__init__(self, parent) self.statusBar.SetStatusWidths([100, -1]) self.statusBar.SetStatusText('Not Connected') self.lc_commands.InsertColumn(0, 'command', width=300) self.bebop = Bebop() # load saved commands from file self._loadJsonFile() def OnClose( self, event ): if wx.MessageBox("Commands are not yet saved. Do you want to save now?", "Save Changes", wx.ICON_QUESTION | wx.YES_NO) == wx.YES: # Retrive commands from lc_commands cmdList = [] for i in range(self.lc_commands.GetItemCount()): cmdList.append(self.lc_commands.GetItemText(i)) self._saveJsonFile(cmdList) self.OnDisconnect(None) event.Skip() # calls the parent close method def OnConnect( self, event ): isConnected = self.bebop.connect(10) statusText = 'Connected' if not isConnected: statusText = 'Not ' + statusText self.statusBar.SetStatusText(statusText) def OnDisconnect( self, event ): self.bebop.disconnect() self.statusBar.SetStatusText('Disconnected') self.statusBar.SetStatusText(f'Battery: {self.bebop.sensors.battery}%', 1) def OnTakeOff( self, event ): self.bebop.safe_takeoff(10) # By default, setting the status text only sets the first # one. So we must specify the second status text (index 1 # , instead of index 0) self.statusBar.SetStatusText('Taking off', 1) def OnLand( self, event ): self.bebop.safe_land(10) self.statusBar.SetStatusText('Landing', 1) def OnAddTakeOff( self, event ): self._addCommand(f'{CmdType.TakeOff.value},10') def OnAddLand( self, event ): self._addCommand(f'{CmdType.Land.value},10') def OnAddRelative( self, event ): x = int(self.fld_x.GetValue()) y = int(self.fld_y.GetValue()) z = int(self.fld_z.GetValue()) rads = int(self.fld_radians.GetValue()) self._addCommand(f'{CmdType.Relative.value},{x},{y},{z},{rads}') def OnAddDirect( self, event ): roll = int(self.fld_roll.GetValue()) pitch = int(self.fld_pitch.GetValue()) yaw = int(self.fld_yaw.GetValue()) vertical = int(self.fld_vertical.GetValue()) duration = int(self.fld_duration.GetValue()) self._addCommand(f'{CmdType.Direct.value},{roll},{pitch},{yaw},{vertical},{duration}') def OnAddSleep( self, event ): sleep = int(self.fld_sleep.GetValue()) self._addCommand(f'{CmdType.Sleep.value},{sleep}') def OnAddFlip( self, event ): dir = str(self.fld_direction.GetValue()) self._addCommand(f'{CmdType.Flip.value},{dir}') def OnAddFlyGrid(self, event): print("Make sure you take off first!") heightGain = int(self.fld_gridHeight.GetValue()) lengthForward = int(self.fld_gridLength.GetValue()) width = int(self.fld_gridWidth.GetValue()) biLines = int(self.fld_gridLines.GetValue()) widthSection = width/(biLines+1) # Rise self._addCommand(f'{CmdType.Relative.value},0,0,{heightGain},0') # Fly forward and turn right self._addCommand(f'{CmdType.Relative.value},{lengthForward},0,0,90') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly right and turn right self._addCommand(f'{CmdType.Relative.value},{widthSection},0,0,90') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly back and turn left self._addCommand(f'{CmdType.Relative.value},{lengthForward},0,0,-90') # Flip self._addCommand(f'{"flip"},{"up"}') while (biLines >= 2): biLines -= 2 # Fly right and turn left self._addCommand(f'{CmdType.Relative.value},{widthSection},0,0,-90') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly forward and turn right self._addCommand(f'{CmdType.Relative.value},{lengthForward},0,0,90') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly right and turn right self._addCommand(f'{CmdType.Relative.value},{widthSection},0,0,90') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly back and turn left self._addCommand(f'{CmdType.Relative.value},{lengthForward},0,0,-90') # Flip self._addCommand(f'{"flip"},{"up"}') if (biLines == 1): biLines -= 1 # Fly right and turn left self._addCommand(f'{CmdType.Relative.value},{widthSection},0,0,-90') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly forward self._addCommand(f'{CmdType.Relative.value},{lengthForward},0,0,0') # Flip self._addCommand(f'{"flip"},{"up"}') # Fly back left to home self._addCommand(f'{CmdType.Relative.value},{-lengthForward},{-width},0,0') # Flip self._addCommand(f'{"flip"},{"up"}') # Descend self._addCommand(f'{CmdType.Relative.value},0,0,{-heightGain},0') else: # Fly left and turn left self._addCommand(f'{CmdType.Relative.value},{-width},0,0,-90') # Flip self._addCommand(f'{"flip"},{"up"}') # Descend self._addCommand(f'{CmdType.Relative.value},0,0,{-heightGain},0') print("Make sure you land!") def OnRemove( self, event ): self.lc_commands.DeleteItem(self.lc_commands.GetFocusedItem()) def OnUp( self, event ): """Swap the selected item with the one above it""" index = self.lc_commands.GetFocusedItem() if index >= 1: selItemStr = self.lc_commands.GetItemText(index) aboveItemStr = self.lc_commands.GetItemText(index-1) self.lc_commands.SetItemText(index, aboveItemStr) self.lc_commands.SetItemText(index-1, selItemStr) self.lc_commands.Focus(index-1) def OnDown( self, event ): """Swap the selected item with the one below it""" index = self.lc_commands.GetFocusedItem() if index < self.lc_commands.GetItemCount() - 1: selItemStr = self.lc_commands.GetItemText(index) belowItemStr = self.lc_commands.GetItemText(index+1) self.lc_commands.SetItemText(index, belowItemStr) self.lc_commands.SetItemText(index+1, selItemStr) self.lc_commands.Focus(index+1) def OnClear( self, event ): self.lc_commands.DeleteAllItems() def OnRunFromSelection( self, event ): index = self.lc_commands.GetFocusedItem() while index > 0: self.lc_commands.DeleteItem(index - 1) index = self.lc_commands.GetFocusedItem() self.OnRunCommands(event) def OnRunCommands( self, event ): """Go through each item in lc_commands and convert the string to a list. Then use the first item in the list to determine the command type, and the rest of the items are the params""" # Retrive commands from lc_commands cmdList = [] for i in range(self.lc_commands.GetItemCount()): cmdList.append(self.lc_commands.GetItemText(i)) self._saveJsonFile(cmdList) # Abort running attempt if not connected if not self.bebop.drone_connection.is_connected: print("No Connection. Aborting process.") return # === Run Commands === for i in range(self.lc_commands.GetItemCount()): args = self.lc_commands.GetItemText(i).split(',') self.statusBar.SetStatusText(f'Executing command: {args}', 1) try: if (args[0] == CmdType.Sleep.value): self.bebop.smart_sleep(int(args[1])) elif args[0] == CmdType.TakeOff.value: self.bebop.safe_takeoff(int(args[1])) elif args[0] == CmdType.Land.value: self.bebop.safe_land(int(args[1])) elif args[0] == CmdType.Direct.value: self.bebop.fly_direct(int(args[1]), int(args[2]), int(args[3]), int(args[4]), int(args[5])) elif args[0] == CmdType.Relative.value: self.bebop.move_relative(int(args[1]), int(args[2]), int(args[3]), math.radians(int(args[4]))) elif args[0] == CmdType.Flip.value: self.bebop.flip(str(args[1])) except Exception as e: print("Error occurred: ") print(e) continue def _loadJsonFile(self): # Open up JSON file with last run commands filePath = os.getcwd() + os.sep + "cmd_data.json" if os.path.isfile(filePath): f = open(filePath,"r") s = f.read() commands = json.loads(s) # Input commands into GUI interface for c in commands: self._addCommand(c) def _saveJsonFile(self, cmdList): # Place all commands into JSON file and write them to the disk jsonStr = json.dumps(cmdList) filePath = os.getcwd() + os.sep + "cmd_data.json" with open(filePath,"w") as f: f.write(jsonStr) def _addCommand(self, cmd: str): self.lc_commands.InsertItem(self.lc_commands.GetItemCount(), cmd) self.statusBar.SetStatusText(f'Added command: {cmd}', 1)
from pyparrot.Bebop import Bebop import time bebop = Bebop() bebop.connect(10) bebop.ask_for_state_update() time.sleep(2) bebop.safe_takeoff(10)