bebop = Bebop(drone_type="Bebop2") print("connecting to Bebop2") success = bebop.connect(10) print(success) try: if (success): print("sleeping") bebop.smart_sleep(2) bebop.ask_for_state_update() # set safe indoor parameters bebop.set_max_tilt(5) bebop.set_max_vertical_speed(1) # trying out the new hull protector parameters - set to 1 for a hull protection and 0 without protection bebop.set_hull_protection(1) while True: print("receiving") ch = c.recv(4096).decode() if ch == "t": print("take off") bebop.safe_takeoff(10) c.send(("controller received " + ch).encode('utf-8')) elif ch == "w":
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()
# Start the drone for take-off print("Sleeping...") bebop.smart_sleep(2) bebop.ask_for_state_update() print("Prepare for take-off...") # Set safe indoor/outdoor parameters print("Set indoor parameters...") max_tilt = 15 vertical_speed = 2 #1.5 max_altitude = 1.5 max_rotation_speed = 150 tilt_speed = 200 bebop.set_max_tilt(max_tilt) bebop.set_max_vertical_speed(vertical_speed) bebop.set_max_altitude(max_altitude) bebop.set_max_rotation_speed(max_rotation_speed) bebop.set_max_tilt_rotation_speed(tilt_speed) color_print("Indoor parameters set: OK", "SUCCESS") # Get initial sensor data_type print("--------- SENSOR DATA ----------") print("Battery: ", bebop.sensors.battery, "%") print("Flying state:", bebop.sensors.flying_state) print("Altitude: ", max_altitude, " m") print("Pitch/roll rotation speed (degrees): ", tilt_speed) print("Tilt (degrees): ", max_tilt) print("Vertical speed: ", vertical_speed, " m/s") print("Yaw rotation speed (degrees): ", max_rotation_speed)
def getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch if (success): bebop.ask_for_state_update() # set safe parameters bebop.set_max_tilt(15) # maximum allowable tilt in degrees: 5(very slow) ~ 30(very fast) bebop.set_max_vertical_speed(1) # maximum allowable vertical speed: 0.5 m/s ~ 2.5 m/s while True: char = getch() if (char == "w"): print("forward") bebop.fly_direct(roll=0, pitch=percentage, yaw=0, vertical_movement=0, duration=duration_s) bebop.flat_trim() elif (char == "a"): print("left") bebop.fly_direct(roll=-percentage, pitch=0, yaw=0, vertical_movement=0, duration=duration_s) bebop.flat_trim() elif (char == "s"):
# for indoors and does not use gps # from pyparrot.Bebop import Bebop bebop = Bebop() print("connecting") success = bebop.connect(10) print(success) if (success): # set safe indoor parameters bebop.set_max_tilt(5) # degrees bebop.set_max_vertical_speed(1) # meters/sec bebop.set_max_altitude(2) # 2 meters max altitude # trying out the new hull protector parameters - set to 1 for a hull protection and 0 without protection bebop.set_hull_protection(1) bebop.safe_takeoff(10) #move up vertical and then down - value for vertical movement represents % of vertical speed bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=20, duration=1) bebop.smart_sleep(2) bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=-20, duration=1) bebop.safe_land(10) print("DONE - disconnecting") bebop.smart_sleep(1)