Beispiel #1
0
def face_tracking(droneVision:DroneVisionGUI,bebop:Bebop):
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    cv2.namedWindow("face_tracking")

    frame = droneVision.get_latest_valid_picture()

    while cv2.getWindowProperty('face_tracking', 0) >= 0:
        frame = droneVision.get_latest_valid_picture()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        (x,y,w,h) = None, None, None, None
        if len(faces) > 0:
            (x,y,w,h) = faces[0]
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 1)
            print("Face Size: "+ str(w*h))

        backup_threshold = 3600
        fwd_threshold = 2000

        if w is not None and w*h > backup_threshold:
            print("GOING BACK")
            bebop.fly_direct(roll=0,pitch=-20,yaw=0,vertical_movement=0,duration=0.1)
        elif w is not None and w*h < fwd_threshold:
            print("GOING FORWARD")
            bebop.fly_direct(roll=0, pitch=20, yaw=0, vertical_movement=0, duration=0.1)

        if x is not None and x+(w/2.0) > 650:
            print("GOING RIGHT")
            bebop.fly_direct(roll=0,pitch=0,yaw=70,vertical_movement=0,duration=0.1)
        elif x is not None and x+(w/2.0) < 200:
            print("GOING LEFT")
            bebop.fly_direct(roll=0, pitch=0, yaw=-70, vertical_movement=0, duration=0.1)

        cv2.imshow("face_tracking",frame)
        cv2.waitKey(10)

    bebop.safe_land(10)
    cv2.destroyAllWindows()
    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":
            print("move front")
            bebop.fly_direct(roll=0,
                             pitch=30,
                             yaw=0,
                             vertical_movement=0,
                             duration=0.25)
            c.send(("controller received " + ch).encode('utf-8'))
        elif ch == "s":
            print("move back")
            bebop.fly_direct(roll=0,
                             pitch=-30,
                             yaw=0,
                             vertical_movement=0,
                             duration=0.25)
            c.send(("controller received " + ch).encode('utf-8'))
        elif ch == "a":
            print("move left")
            bebop.fly_direct(roll=-30,
                             pitch=0,
Beispiel #3
0
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()
Beispiel #4
0
bebop = Bebop()

print("connecting")
success = bebop.connect(10)
print(success)

print("sleeping")
bebop.smart_sleep(5)

bebop.ask_for_state_update()

bebop.safe_takeoff(10)

print("Flying direct: going forward (positive pitch)")
bebop.fly_direct(roll=0, pitch=50, yaw=0, vertical_movement=0, duration=1)

print("Flying direct: yaw")
bebop.fly_direct(roll=0, pitch=0, yaw=50, vertical_movement=0, duration=1)

print("Flying direct: going backwards (negative pitch)")
bebop.fly_direct(roll=0, pitch=-50, yaw=0, vertical_movement=0, duration=0.5)

print("Flying direct: roll")
bebop.fly_direct(roll=50, pitch=0, yaw=0, vertical_movement=0, duration=1)

print("Flying direct: going up")
bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=50, duration=1)

# this works but requires a larger test space than I currently have. Uncomment with care and test only in large spaces!
#print("Flying direct: going around in a circle (yes you can mix roll, pitch, yaw in one command!)")
Beispiel #5
0
success = bebop.connect(10)
print(success)

# 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

if (success):
    # get the state information
    print("sleeping")
    bebop.smart_sleep(2)
    bebop.ask_for_state_update()
    bebop.smart_sleep(5)

    print("taking off!")
    bebop.safe_takeoff(5)
    bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=-20, duration=1)

    bebop.smart_sleep(3)

    print("Flying direct: going forward (positive pitch)")
    bebop.fly_direct(roll=0,
                     pitch=50,
                     yaw=0,
                     vertical_movement=0,
                     duration=2.2)

    bebop.smart_sleep(3)
    # moving up through the middle of the chair
    bebop.fly_direct(roll=0,
                     pitch=0,
                     yaw=0,
bebop = Bebop()

print("connecting")
success = bebop.connect(10)
print(success)

if (success):
    # get the state information
    print("sleeping")
    bebop.smart_sleep(2)
    bebop.ask_for_state_update()
    bebop.smart_sleep(2)

    print("taking off!")
    bebop.safe_takeoff(5)
    bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=-20, duration=1)
    
    bebop.smart_sleep(3)

    print("Flying direct: going forward (positive pitch)")
    bebop.fly_direct(roll=0, pitch=50, yaw=0, vertical_movement=0, duration=3.4)
    
    bebop.smart_sleep(5)
    
    bebop.fly_direct(roll=0, pitch=-50, yaw=0, vertical_movement=0, duration=3)
    
    #bebop.smart_sleep(3)
    #print("flipping")
    #bebop.flip("front")

    print("landing")
    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"):
			print("backward")
			bebop.fly_direct(roll=0, pitch=-percentage, yaw=0, vertical_movement=0, duration=duration_s)
			bebop.flat_trim()
		elif (char == "d"):
			print("right")
			bebop.fly_direct(roll=percentage, pitch=0, yaw=0, vertical_movement=0, duration=duration_s)
			bebop.flat_trim()
		elif (char == "j"):
			print("up")
Beispiel #8
0
        print("sleeping")
        bebop.smart_sleep(2)

        bebop.ask_for_state_update()
        bebop.safe_takeoff(10)
        bebop.smart_sleep(2)

        print("Moving the camera using velocity")
        bebop.pan_tilt_camera_velocity(pan_velocity=0,
                                       tilt_velocity=-2,
                                       duration=4)

        bebop.fly_direct(roll=0,
                         pitch=10,
                         yaw=0,
                         vertical_movement=0,
                         duration=10)

        if (found == False):
            print("timeout")

        bebop.safe_land(10)
        print("Stopping vision")
        bebopVision.close_video()

    # disconnect nicely so we don't need a reboot
    bebop.disconnect()
else:
    print("Error connecting to bebop.  Retry")
Beispiel #9
0
class Drone:
    def __init__(self):
        self.drone = Bebop()

    def take_off(self):
        ### Connect to Bebop drone and take off ###
        success = self.drone.connect1(10)
        print(success)
        self.drone.safe_takeoff(2)

    def step(self, roll, pitch, yaw, vertical_movement, duration):
        ### Moves the Bebop drone given input parameters ###
        self.drone.fly_direct(self, roll, pitch, yaw, vertical_movement,
                              duration)

    def square(self):
        ### Move the drone in a square pattern###
        #move left and wait 1 second
        self.drone.fly_direct(self,
                              roll=-15,
                              pitch=0,
                              yaw=0,
                              vertical_movement=0,
                              duration=1)
        self.drone.smart_sleep(1)
        #move forward and wait 1 second
        self.drone.fly_direct(self,
                              roll=0,
                              pitch=15,
                              yaw=0,
                              vertical_movement=0,
                              duration=1)
        self.drone.smart_sleep(1)
        #move right and wait 1 second
        self.drone.fly_direct(self,
                              roll=15,
                              pitch=0,
                              yaw=0,
                              vertical_movement=0,
                              duration=1)
        self.drone.smart_sleep(1)
        #move back and wait 1 second
        self.drone.fly_direct(self,
                              roll=0,
                              pitch=-15,
                              yaw=0,
                              vertical_movement=0,
                              duration=1)
        self.drone.smart_sleep(1)

    def zigzag(self):
        ### Move the drone in a 3D zigzag pattern###
        #move left-forward-up and wait 1 second
        self.drone.fly_direct(self,
                              roll=-15,
                              pitch=15,
                              yaw=0,
                              vertical_movement=15,
                              duration=1)
        self.drone.smart_sleep(1)
        #move right-forward-down and wait 1 second
        self.drone.fly_direct(self,
                              roll=15,
                              pitch=15,
                              yaw=0,
                              vertical_movement=-15,
                              duration=1)
        self.drone.smart_sleep(1)
        #move left and wait 1 second
        self.drone.fly_direct(self,
                              roll=-15,
                              pitch=0,
                              yaw=0,
                              vertical_movement=0,
                              duration=1)
        self.drone.smart_sleep(1)
        #move right-back-up and wait 1 second
        self.drone.fly_direct(self,
                              roll=15,
                              pitch=-15,
                              yaw=0,
                              vertical_movement=15,
                              duration=1)
        self.drone.smart_sleep(1)
        #move left-back-down and wait 1 second
        self.drone.fly_direct(self,
                              roll=-15,
                              pitch=-15,
                              yaw=0,
                              vertical_movement=-15,
                              duration=1)
        self.drone.smart_sleep(1)

    def disconnect(self):
        ### Disconect Bebop drone ###
        self.drone.disconnect()

    def land(self):
        ### Disconnect Bebop drone and land ###
        success = self.drone.connect1(10)
        print(success)
        self.drone.ask_for_state_update()
        self.drone.safe_land(5)
        self.drone.disconnect()
Beispiel #10
0
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)
Beispiel #11
0
    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, bebop)

        #take off
        bebop.safe_takeoff(10)
        bebop.fly_direct(0, 0, 0, 50, 1)
        bebopVision.set_user_callback_function(
            userVision.save_pictures,
            user_callback_args=None)  #calls save picture continuously

        bebopVision.open_video()

    else:
        print("Error connecting to bebop.  Retry")
Beispiel #12
0
def qr_tracking(droneVision:DroneVisionGUI, bebop:Bebop):
    cv2.namedWindow('qr')

    while cv2.getWindowProperty('qr', 0) >= 0:
        img = droneVision.get_latest_valid_picture()
        x,y,w,h = None,None,None,None
        try:
            rect = zbar.decode(img, symbols=[zbar.ZBarSymbol.QRCODE])[0][2]
            poly = zbar.decode(img, symbols=[zbar.ZBarSymbol.QRCODE])[0][3]
            x,y,w,h = rect
            p1,p2,p3,p4 = poly
        except IndexError:
            pass

        if x is not None:
            # cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255))

            pts = np.array([[p1[0],p1[1]], [p2[0],p2[1]], [p3[0],p3[1]], [p4[0],p4[1]]], np.int32)
            pts.reshape(-1,1,2)
            cv2.polylines(img, [pts], True, (255, 0, 255), 5)

            area_t1 = abs((p1[0]*(p2[1]-p4[1])+p2[0]*(p4[1]-p1[1])+p4[0]*(p1[1]-p2[1]))/2.0)
            area_t2 = abs((p3[0] * (p2[1] - p4[1]) + p2[0] * (p4[1] - p3[1]) + p4[0] * (p3[1] - p2[1])) / 2.0)
            area = area_t2+area_t1
        backup_threshold = 20000
        fwd_threshold = 10000



        if w is not None and area > backup_threshold:
            print("GOING BACK")
            bebop.fly_direct(roll=0, pitch=-20, yaw=0, vertical_movement=0, duration=0.07)
        elif w is not None and area < fwd_threshold:
            print("GOING FORWARD")
            bebop.fly_direct(roll=0, pitch=20, yaw=0, vertical_movement=0, duration=0.07)

        if x is not None and x + (w / 2.0) > 550:
            print("GOING RIGHT")
            bebop.fly_direct(roll=0, pitch=0, yaw=100, vertical_movement=0, duration=0.1)
        elif x is not None and x + (w / 2.0) < 300:
            print("GOING LEFT")
            bebop.fly_direct(roll=0, pitch=0, yaw=-100, vertical_movement=0, duration=0.1)

        if x is not None and y + (h / 2.0) > 380:
            print("GOING DOWN")
            bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=-40, duration=0.1)
        elif x is not None and y + (h / 2.0) < 100:
            print("GOING UP")
            bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=40, duration=0.1)



        cv2.imshow('qr', img)
        cv2.waitKey(10)

    bebop.safe_land(10)
    cv2.destroyAllWindows()
Beispiel #13
0
def color_tracking(drone_vision:DroneVisionGUI, bebop:Bebop):

    def show_hist(hist):
        """Takes in the histogram, and displays it in the hist window."""
        bin_count = hist.shape[0]
        bin_w = 24
        img = np.zeros((256, bin_count * bin_w, 3), np.uint8)
        for i in range(bin_count):
            h = int(hist[i])
            cv2.rectangle(img, (i * bin_w + 2, 255), ((i + 1) * bin_w - 2, 255 - h),
                          (int(180.0 * i / bin_count), 255, 255),
                          -1)
        img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
        cv2.imshow('hist', img)

    showBackProj = False
    showHistMask = False

    frame = drone_vision.get_latest_valid_picture()

    if frame is not None:
        (hgt, wid, dep) = frame.shape
        cv2.namedWindow('camshift')
        cv2.namedWindow('hist')
        cv2.moveWindow('hist', 700, 100)  # Move to reduce overlap

        # Initialize the track window to be the whole frame
        track_window = (0, 0, wid, hgt)
        #
        # Initialize the histogram from the stored image
        # Here I am faking a stored image with just a couple of blue colors in an array
        # you would want to read the image in from the file instead
        histImage = np.array([[[110, 70, 50]],
                              [[111, 128, 128]],
                              [[115, 100, 100]],
                              [[117, 64, 50]],
                              [[117, 200, 200]],
                              [[118, 76, 100]],
                              [[120, 101, 210]],
                              [[121, 85, 70]],
                              [[125, 129, 199]],
                              [[128, 81, 78]],
                              [[130, 183, 111]]], np.uint8)
        histImage = cv2.imread('orange.jpg')
        histImage = cv2.cvtColor(histImage,cv2.COLOR_BGR2HSV)
        maskedHistIm = cv2.inRange(histImage, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
        cv2.imshow("masked",maskedHistIm)
        cv2.imshow("histim",histImage)
        hist = cv2.calcHist([histImage], [0], maskedHistIm, [16], [0, 180])
        cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
        hist = hist.reshape(-1)
        show_hist(hist)

        # start processing frames
        while cv2.getWindowProperty('camshift', 0) >= 0:
            frame = drone_vision.get_latest_valid_picture()
            vis = frame.copy()
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # convert to HSV
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))  # eliminate low and high saturation and value values


            # The next line shows which pixels are being used to make the histogram.
            # it sets to black all the ones that are masked away for being too over or under-saturated
            if showHistMask:
                vis[mask == 0] = 0

            prob = cv2.calcBackProject([hsv], [0], hist, [0, 180], 1)
            prob &= mask
            term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
            track_box, track_window = cv2.CamShift(prob, track_window, term_crit)
            print(track_box[1][0]*track_box[1][1])

            if showBackProj:
                vis[:] = prob[..., np.newaxis]
            try:
                cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                area = track_box[1][0]*track_box[1][1]
                if area > 7000:
                    print("GOING BACK")
                    bebop.fly_direct(roll=0,pitch=-20,yaw=0,vertical_movement=0,duration=0.5)
                    #bebop.smart_sleep(1)
                elif area < 4000:
                    print("GOING FORWARD")
                    bebop.fly_direct(roll=0,pitch=20,yaw=0,vertical_movement=0,duration=0.5)
                    #bebop.smart_sleep(1)
            except:
                pass
                # print("Track box:", track_box)

            cv2.imshow('camshift', vis)

            ch = chr(0xFF & cv2.waitKey(5))
            if ch == 'q':
                break
            elif ch == 'b':
                showBackProj = not showBackProj
            elif ch == 'v':
                showHistMask = not showHistMask

    bebop.safe_land(10)
    cv2.destroyAllWindows()
Beispiel #14
0
import math

bebop = Bebop()

print("connecting")
success = bebop.connect(20)
print(success)

print("sleeping")
bebop.smart_sleep(.5)

bebop.ask_for_state_update()

bebop.safe_takeoff(5)

bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=-20,
                 duration=1)  # lowwers drown
bebop.smart_sleep(1)
## first movment do not change
bebop.fly_direct(roll=0, pitch=50, yaw=0, vertical_movement=0,
                 duration=4.7)  # forward
bebop.fly_direct(roll=0, pitch=-100, yaw=0, vertical_movement=0, duration=1.2)
bebop.smart_sleep(2)

#second movement
bebop.fly_direct(roll=50, pitch=5, yaw=0, vertical_movement=0,
                 duration=1.7)  # right
bebop.fly_direct(roll=-50, pitch=0, yaw=0, vertical_movement=0, duration=.5)
bebop.smart_sleep(2)

# movment three
bebop.fly_direct(roll=0, pitch=50, yaw=0, vertical_movement=0,
Beispiel #15
0
reward_pool = []
best_reward=0
steps_grad = 0
dx = [0]
try:
	pitch = 80
	if (success):
	    bebop.smart_sleep(2)
	    bebop.ask_for_state_update()
	    print('safe_takeoff')
	    bebop.safe_takeoff(10)
	    bebop.set_hull_protection(1)
	    bebop.set_max_distance(10)
	    bebop.set_max_altitude(5)	    
	    print('going up')
	    bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=10, duration=3)
	    bebop.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=10, duration=3)
	    print('add pendulum')
	    bebop.smart_sleep(10)
		for e in range(num_episode):
			print('Episode : ', e)
			count =0
			angle = getAngle()
		    state = [[sum(dx), (pitch*0.01)*2.5, angle]]
			state = torch.FloatTensor(state)
			state = Variable(state)
		    while count<50:
				state_pool.append(state)
				m = Bernoulli(probs)
				# print('m', m)
				action = m.sample()