def on_cbAuto_clicked( self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() else: # Return to a hover when leaving auto mode. self.controller.hover()
def on_cbAuto_clicked(self, checked): if checked: self.state = 1 self.elapsedTimer = QElapsedTimer() self.rotate = 0 elif not checked: self.controller.SendLand() self.controller.SendLand1()
def make_move(self, state): """Makes a decision of the player's next move (abstract) Args: state: current board state Returns: Player's hole number which defines a player's next move """ self._running_timer = QElapsedTimer() self._running_timer.start() return -1
def put(self, item, block=True, timeout=None): """Put an item into the queue. Parameters ---------- block : bool If True(default), the caller thread will block until the queue has a free space available for putting an new item. If False, the `Full` exception will be raised if there is no free space in the queue timeout : int The max time to wait for a new space to be avaible, in milliseconds. """ self.mutex.lock() try: # Check if the queue has a limit (0 means not) if self.maxsize > 0: # Raise Full if block is False and the queue is at max cap. if not block: if self._qsize() == self.maxsize: raise Full # If a timeout is not provided, wait indefinitely elif timeout is None: while self._qsize() == self.maxsize: self.item_removed.wait(self.mutex) elif timeout < 0: raise ValueError("'timeout' must be a non-negative number") else: timer = QElapsedTimer() timer.start() while self._qsize() == self.maxsize: remaining = timeout - timer.elapsed() if remaining <= 0.0: raise Full self.item_removed.wait(self.mutex, remaining) self._put(item) self.item_added.wakeOne() finally: self.mutex.unlock()
def on_cbAuto_clicked(self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() else: # Return to a hover when leaving auto mode. self.controller.hover()
def move(self, a, b, a_fin, b_fin, t): # For test only: return a random move global timer global timeElapsed global totalMoves totalMoves += 1 #To keep track of all the moves # defining the alpha, beta, and depth parameters to be used alpha = MIN beta = MAX depth = 7 #Also tried with 3 and 7 machine = ai() # timing variables timer = QElapsedTimer() timer.start() t_start = time.time() val_found = False #To set priorities for i in range(6): if a[i] >= 13: #Highest priority is when a pit has more than 13 pebbles -- Heuristic 1 bestMove = i val_found = True if not val_found: children = self.setChildrenIndex(a, b, a_fin, b_fin) # generating all children bestMove = -1 bestValue = -float("inf") # evaluate every child to find the ultimate best move index for child in children: # obtaining the weighted value of child from the alpha-beta minimax algorithm value = machine.minimax(child[0][0].a, child[0][0].b, child[0][0].a_fin, child[0][0].b_fin, depth, False, child[0][1], child[0][1], alpha, beta, t_start, t) # check if the child value returned by minimax is valid (i.e.: larger than -infinity) if value > bestValue: bestMove = child[ 1] # if valid, set the new best move to the index that produces current child bestValue = max( value, bestValue ) # update best value to the maximum between the old and the current child value for j in range(6): #Heuristic 5 if b[j] == 0: (ch1, sec1) = machine.updateLocalState_childrenGeneration( bestMove, b, a, b_fin, a_fin) if (ch1.a_fin - a_fin < 3 and a[5 - j] != 0): #Condition for checking bestMove = 5 - j #Makes avoiding being eaten the best move if bestMove == -1: bestMove = 0 #Arbitrary timeElapsed = timeElapsed + timer.elapsed() print "Time This Move: %f ms" % timer.elapsed() print "Total Time Elapsed: %f ms" % timeElapsed print "Average Time per Move: %f ms" % (timeElapsed / totalMoves) print "Total Moves: %d" % totalMoves print "------------------------\n" return bestMove
def get(self, block=True, timeout=None): """Remove and return an item from the queue. Parameters ---------- block : bool If True(default), the caller thread will block until the queue has an item available for putting an new item. If False, the `Empty` exception will be raised if there is no item in the queue timeout : int The max time to wait for a new item to be avaible, in milliseconds. """ self.mutex.lock() try: if not block: if not self._qsize(): raise Empty elif timeout is None: while not self._qsize(): self.item_added.wait(self.mutex) elif timeout < 0: raise ValueError("'timeout' must be a non-negative number") else: timer = QElapsedTimer() timer.start() while not self._qsize(): remaining = timeout - timer.elapsed() if remaining <= 0.0: raise Empty self.item_added.wait(self.mutex, remaining) item = self._get() self.item_removed.wakeOne() return item finally: self.mutex.unlock()
def on_cbAuto_clicked(self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 self.time = 0 self.scan = 1 self.scan_count = 0 self.scan_right = 1 self.scan_forward = 0 self.scan_left = 0 slef.scan_back = 0 self.rotate = False self.area = 0 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() elif not checked: self.controller.SendLand() else: # Return to a hover when leaving auto mode. self.controller.hover()
def __enter__(self): # Load in the GUI. uic.loadUi(self.uiFile, self) # Open a webcam. self.cap = cv2.VideoCapture(self.webcamIndex) assert self.cap.isOpened() # Scan for possible resolutions from a list of common resolutions. self.supported_resolution = [] for resolution in ( # 16:9 aspect ratio: (1920, 1080), (1280, 720), # 4:3 aspect ratio: (1024, 768), (640, 480), ): self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, resolution[0]) self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, resolution[1]) if ( self.cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) == resolution[0] and self.cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) == resolution[1] ): self.supported_resolution.append(resolution) self.cbWebcamResolution.addItem("{}x{}".format(resolution[0], resolution[1]), resolution) if self.webcamInitialResolution < 0: self.webcamInitialResolution = len(self.supported_resolution) - 1 self.cbWebcamResolution.setCurrentIndex(self.webcamInitialResolution) # Set up a timer to read from the webcam. self.webcamTimer = QTimer(self) self.webcamTimer.setInterval(0) self.webcamTimer.timeout.connect(self._on_webcamTimer_timeout) self.webcamTimer.start() # Monitor the fps. self.fpsLabel = QLabel() self.statusBar().addPermanentWidget(self.fpsLabel) self.fpsTimer = QElapsedTimer() self.fpsTimer.start() return self
class MavControl(ButtonGui): # DRONE 1 def on_pbTakeoff_pressed(self): print("TAKE OFF!") self.controller.SendTakeoff() def on_pbLand_pressed(self): print("LAND") self.controller.SendLand() def on_pbUp_pressed(self): print("Up") self.controller.SetCommand(roll=0, pitch=0, yaw_velocity=0, z_velocity=0.5) def on_pbUp_released(self): print("Up done") self.controller.hover() def on_pbDown_pressed(self): print("Down") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0,z_velocity=-0.5) def on_pbDown_released(self): print("Down done") self.controller.hover() def on_pbForward_pressed(self): print("Forward") self.controller.SetCommand(roll=0,pitch=0.5,yaw_velocity=0,z_velocity=0) def on_pbForward_released(self): print("Forward done") self.controller.hover() def on_pbBackward_pressed(self): print("Backward") self.controller.SetCommand(roll=0,pitch=-0.5,yaw_velocity=0,z_velocity=0) def on_pbBackward_released(self): print("Backward done") self.controller.hover() def on_pbLeft_pressed(self): print("Left") self.controller.SetCommand(roll=0.5,pitch=0,yaw_velocity=0,z_velocity=0) def on_pbLeft_released(self): print("Left done") self.controller.hover() def on_pbRight_pressed(self): print("Right") self.controller.SetCommand(roll=-0.5,pitch=0,yaw_velocity=0,z_velocity=0) def on_pbRight_released(self): print("Right done") self.controller.hover() def on_pbRotateLeft_pressed(self): print("Rotate Left") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0.5,z_velocity=0) def on_pbRotateLeft_released(self): print("Rotate Left done") self.controller.hover() def on_pbRotateRight_pressed(self): print("Rotate Right") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=-0.5,z_velocity=0) def on_pbRotateRight_released(self): print("Rotate Right done") self.controller.hover() def on_pbFlatTrim_pressed(self): print("Flat trimmed") self.controller.SetFlatTrim() def on_pbEmergency_pressed(self): print("Emergency signal sent (or reset)") self.controller.SendEmergency() def on_pbToggleCamera_pressed(self): print("Camera Toggled") self.controller.ToggleCamera() def on_pbBlinkRed_pressed(self): print("LEDs Blinking Red") self.controller.SetLedAnimation(2,3,3) def on_pbBlinkOrange_pressed(self): print("LEDs Blinking Orange") self.controller.SetLedAnimation(3,3,3) def on_pbBlinkGreen_pressed(self): print("LEDs Blinking Green") self.controller.SetLedAnimation(1,3,3) def on_pbRecord(self): self.controller.RecordUsb() ################################################ # drone 2 def on_pbTakeoff_2_pressed(self): print("TAKE OFF!") self.controller.SendTakeoff1() def on_pbLand_2_pressed(self): print("LAND") self.controller.SendLand1() def on_pbUp_2_pressed(self): print("Up") self.controller.SetCommand1(roll=0, pitch=0, yaw_velocity=0, z_velocity=0.5) def on_pbUp_2_released(self): print("Up done") self.controller.hover1() def on_pbDown_2_pressed(self): print("Down") self.controller.SetCommand1(roll=0,pitch=0,yaw_velocity=0,z_velocity=-0.5) def on_pbDown_2_released(self): print("Down done") self.controller.hover1() def on_pbForward_2_pressed(self): print("Forward") self.controller.SetCommand1(roll=0,pitch=0.5,yaw_velocity=0,z_velocity=0) def on_pbForward_2_released(self): print("Forward done") self.controller.hover1() def on_pbBackward_2_pressed(self): print("Backward") self.controller.SetCommand1(roll=0,pitch=-0.5,yaw_velocity=0,z_velocity=0) def on_pbBackward_2_released(self): print("Backward done") self.controller.hover1() def on_pbLeft_2_pressed(self): print("Left") self.controller.SetCommand1(roll=0.5,pitch=0,yaw_velocity=0,z_velocity=0) def on_pbLeft_2_released(self): print("Left done") self.controller.hover1() def on_pbRight_2_pressed(self): print("Right") self.controller.SetCommand1(roll=-0.5,pitch=0,yaw_velocity=0,z_velocity=0) def on_pbRight_2_released(self): print("Right done") self.controller.hover1() def on_pbRotateLeft_2_pressed(self): print("Rotate Left") self.controller.SetCommand1(roll=0,pitch=0,yaw_velocity=0.5,z_velocity=0) def on_pbRotateLeft_2_released(self): print("Rotate Left done") self.controller.hover1() def on_pbRotateRight_2_pressed(self): print("Rotate Right") self.controller.SetCommand1(roll=0,pitch=0,yaw_velocity=-0.5,z_velocity=0) def on_pbRotateRight_2_released(self): print("Rotate Right done") self.controller.hover1() def on_pbFlatTrim_2_pressed(self): print("Flat trimmed") self.controller.SetFlatTrim1() def on_pbEmergency_2_pressed(self): print("Emergency signal sent (or reset)") self.controller.SendEmergency1() def on_pbToggleCamera_2_pressed(self): print("Camera Toggled") self.controller.ToggleCamera1() def on_pbBlinkRed_2_pressed(self): print("LEDs Blinking Red") self.controller.SetLedAnimation(2,3,3) def on_pbBlinkOrange_2_pressed(self): print("LEDs Blinking Orange") self.controller.SetLedAnimation(3,3,3) def on_pbBlinkGreen_2_pressed(self): print("LEDs Blinking Green") self.controller.SetLedAnimation(1,3,3) def on_pbRecord_2(self): self.controller.RecordUsb1() ################################################ @pyqtSlot(bool) def on_cbAuto_clicked(self, checked): if checked: self.state = 1 self.elapsedTimer = QElapsedTimer() self.rotate = 0 elif not checked: self.controller.SendLand() self.controller.SendLand1() def fly(self, x_center, y_center, cont_area): self.lbAuto.setText('') if self.state == 1: self.lbVideo1.hide() self.controller.SetCommand(roll=0,pitch=0.1,yaw_velocity=0,z_velocity=0) self.updateAutoLabel('Takeoff!') self.controller.SendTakeoff() self.elapsedTimer.start() self.state = 2 elif self.state == 2: self.updateAutoLabel('Wait until take off completed') if self.elapsedTimer.elapsed() >= 5000: self.controller.hover() self.state = 3 self.time = self.elapsedTimer.elapsed() elif self.state == 3: if(self.rotate == 1): self.controller.SetCommand(roll=0.0,pitch=0,yaw_velocity=0.2,z_velocity=0) else: self.controller.hover() if(self.elapsedTimer.elapsed() %100): self.rotate = 1 else: self.rotate = 0 if(self.time + 15000 < self.elapsedTimer.elapsed()): self.controller.hover() self.state = 4 elif self.state == 4: self.controller.SendLand() self.lbVideo.hide() self.lbVideo1.show() self.controller.SendTakeoff1() self.state = 5 self.time = self.elapsedTimer.elapsed() elif self.state == 5: self.updateAutoLabel('Wait until take off completed') if self.elapsedTimer.elapsed() >= 5000 + self.time: self.state = 6 self.time = self.elapsedTimer.elapsed() elif self.state == 6: if(self.rotate == 1): self.controller.SetCommand1(roll=0.0,pitch=0,yaw_velocity=-0.2,z_velocity=0) else: self.controller1.hover1() if(self.elapsedTimer.elapsed() %100): self.rotate = 1 else: self.rotate = 0 if(self.time + 15000 < self.elapsedTimer.elapsed()): self.controller.hover1() self.state = 7 elif self.state == 7: self.controller.SendLand1() self.lbVideo.show() self.cbAuto.toggle() def updateAutoLabel(self, # A string to add to the explanation. s): self.lbAuto.setText(self.lbAuto.text() + s)
class MavControl(ButtonGui): # This is called when the pbPressed button is pressed. # Naming is similar for other functions. def on_pbTakeoff_pressed(self): print("TAKE OFF!") self.controller.SendTakeoff() def on_pbLand_pressed(self): print("LAND") self.controller.SendLand() def on_pbUp_pressed(self): print("Up") self.controller.SetCommand(roll=0, pitch=0, yaw_velocity=0, z_velocity=0.5) def on_pbUp_released(self): print("Up done.") self.controller.hover() def on_pbDown_pressed(self): print("Down") self.controller.SetCommand(roll=0, pitch=0, yaw_velocity=0, z_velocity=-0.5) def on_pbDown_released(self): print("Down done.") self.controller.hover() def on_pbForward_pressed(self): print("Forward") self.controller.SetCommand(roll=0, pitch=0.5, yaw_velocity=0, z_velocity=0) def on_pbForward_released(self): print("Forward done.") self.controller.hover() def on_pbBackward_pressed(self): print("Back") self.controller.SetCommand(roll=0, pitch=-0.5, yaw_velocity=0, z_velocity=0) def on_pbBackward_released(self): print("Backed it up.") self.controller.hover() def on_pbLeft_pressed(self): print("Left") self.controller.SetCommand(roll=0.3, pitch=0, yaw_velocity=0, z_velocity=0) def on_pbLeft_released(self): print("Left done.") self.controller.hover() def on_pbRight_pressed(self): print("Right") self.controller.SetCommand(roll=-0.3, pitch=0, yaw_velocity=0, z_velocity=0) def on_pbRight_released(self): print("Right done.") self.controller.hover() def on_pbRoLeft_pressed(self): print("Turn down") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0.6,z_velocity=0) def on_pbRoLeft_released(self): print("For what!") self.controller.hover() def on_pbRoRight_pressed(self): print("Turn up!") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=-0.6,z_velocity=0) def on_pbRoRight_released(self): print("Turnt!") self.controller.hover() def on_pbEmergency_pressed(self): print("It's goin' down!") self.controller.SendEmergency() def on_pbFlat_pressed(self): print("flat") self.controller.SetFlatTrim() def on_pbLED_pressed(self): print("RAVE!") self.controller.SetLedAnimation(4,2,5) def on_pbCamChange_pressed(self): print("Look!") self.controller.ToggleCamera() @pyqtSlot(bool) def on_cbAuto_clicked(self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() else: # Return to a hover when leaving auto mode. self.controller.hover() # This is only called when the Auto checkbox is checked. def fly(self, # The x coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.width() - 1``. x_center, # The y coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.height() - 1``. y_center, # The area, in pixels, of the tracked region. cont_area): # Clear the description of what this does. self.lbAuto.setText('') ori_area=cont_area # Decide what to do based on the state. if self.state == 1: # Take off # ^^^^^^^^ # The Auto checkbox was just checked. Take off # to start out mission. self.updateAutoLabel('Takeoff!') self.controller.SendTakeoff() print('Takeoff') #print(cont_area,'area') # Measure time from takeoff. self.elapsedTimer.start() #print(x_center,'xy',y_center) # Move to the next state, waiting for takeoff to # finish. self.state = 2 print('x_center',x_center) print('y_center',y_center) print(' area',cont_area) elif self.state == 2: # Wait until take off completed # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ self.updateAutoLabel('Wait until take off completed') print('state2') # Don't send any commands until we're flying. # So, wait 5 seconds then go to the next state. if self.elapsedTimer.elapsed() >= 5000: self.state = 8 # Starting timer elif self.state ==3: self.controller.hover() self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0.3,z_velocity=0) self.updateAutoLabel('search for color') if (x_center>0 and y_center>0): self.elapsedTimer.restart() self.state=7 elif self.state==7: self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0.2,z_velocity=0) print(x_center,'xy',y_center) if (x_center<160 and x_center>0 ): print(x_center,'xy',y_center) self.controller.hover() if self.elapsedTimer.elapsed() >= 3000: print('state3') print(cont_area) self.elapsedTimer.restart() self.state =8 elif self.state == 4: # ...your ideas... print('state 4') #if (x_center < ???): # FIX TABBING FOR XCENTER self.updateAutoLabel('Flying Foward State 4') #if(x_center>=160 or x_center==-1 ): # self.state=7 # Changed the speed for safety #self.controller.SetCommand(0, 0.0625, 0, 0) #print(cont_area) #if (cont_area>4000 ) : # self.elapsedTimer.restart() # self.controller.hover() self.state ==5 elif self.state == 5: self.controller.hover() if(x_center==-1 and y_center==-1): self.state=3 if(x_center>=160): xr_speed=((x_center-160)*0.0031)**1.5 if xr_speed<0.03: xr_speed=0.03 self.controller.SetCommand(roll=-xr_speed, pitch=0.01, yaw_velocity=0, z_velocity=0) print('move right') print('x_center',x_center) print('y_center',y_center) elif(x_center<=140): xl_speed=((140-x_center)*0.0031)**1.5 if xl_speed<0.03: xl_speed=0.03 self.controller.SetCommand(roll=xl_speed, pitch=0.01, yaw_velocity=0, z_velocity=0) print('move left') print('x_center',x_center) print('y_center',y_center) else: self.controller.hover() self.state=6 elif self.state==6: self.controller.hover() if(x_center==-1 and y_center==-1): self.state=3 if(y_center>=110): print('move downward') yu_speed=(y_center-110)*0.005 if yu_speed<0.03: yu_speed=0.03 self.controller.SetCommand(roll=0, pitch=0.01, yaw_velocity=0, z_velocity=-yu_speed) print('x_center',x_center) print('y_center',y_center) elif(y_center<=90): print('move upward') yd_speed=(90-y_center)*0.005 if yd_speed<0.03: yd_speed=0.03 self.controller.SetCommand(roll=0, pitch=0.01, yaw_velocity=0, z_velocity=yd_speed) print('x_center',x_center) print('y_center',y_center) else: self.elapsedTimer.restart() self.state=8 elif self.state==8: self.controller.hover() if(cont_area>9000): print('move backward') if(x_center==-1 and y_center==-1): self.state=3 b_speed=((cont_area-9000)*0.0001)**4 if b_speed>=0.3: b_speed=0.25 if(x_center>=160): xr_speed=((x_center-160)*0.0031)**1.5 if xr_speed<0.02: xr_speed=0.02 self.controller.SetCommand(roll=-xr_speed, pitch=-b_speed, yaw_velocity=0, z_velocity=0) print('move right backward') print('x_center',x_center) print('y_center',y_center) elif(x_center<=140): xl_speed=((140-x_center)*0.0031)**1.5 if xl_speed<0.02: xl_speed=0.02 self.controller.SetCommand(roll=xl_speed, pitch=-b_speed, yaw_velocity=0, z_velocity=0) print('move left backward') print('x_center',x_center) print('y_center',y_center) else: self.controller.SetCommand(roll=0, pitch=-b_speed, yaw_velocity=0, z_velocity=0) #self.controller.hover() print(' area',cont_area) elif(cont_area<7000): f_speed=((7000-cont_area)*0.0001)**3.5 if f_speed>=0.3: f_speed=0.25 if(x_center==-1 and y_center==-1): self.state=3 if(x_center>=160): xr_speed=((x_center-160)*0.0031)**2.5 self.controller.SetCommand(roll=-xr_speed, pitch=f_speed, yaw_velocity=0, z_velocity=0) print('move right forward') print('x_center',x_center) print('y_center',y_center) elif(x_center<=140): xl_speed=((140-x_center)*0.0031)**2.5 self.controller.SetCommand(roll=xl_speed, pitch=f_speed, yaw_velocity=0, z_velocity=0) print('move left forward') print('x_center',x_center) print('y_center',y_center) else: self.controller.SetCommand(roll=0, pitch=f_speed, yaw_velocity=0, z_velocity=0) print('move foreward') #self.controller.hover() print(' area',cont_area) print('speed:',f_speed) #if(cont_area<=2500 and (x_center>=170 or x_center<=140)): # self.controller.hover() #if(cont_area<=2000): # self.state=3 #if(x_center<=140 or x_center>= 190): # self.state=5 else: pef_area=cont_area self.controller.hover() if self.elapsedTimer.elapsed() >= 3000: self.elapsedTimer.restart() if(x_center<=140 or x_center>= 190): self.state=5 if(y_center>=110 or y_center<=90): self.state=6 if(x_center==-1 and y_center==-1): self.state=3 else: self.controller.hover() print(' area',cont_area) print('got you!') #if self.elapsedTimer.elapsed() >= 5000: # self.state=9 elif self.state == 9: print('state 5') self.updateAutoLabel('Land State 5') self.controller.hover() if self.elapsedTimer.elapsed() >= 2000: self.controller.SendLand() self.cbAuto.toggle() else: self.updateAutoLabel('Unknown state! Help!') # 1. Determine what to do by examining ``x_center``, # ``y_center``, etc. # # 2. Explain what your code will do: # ``self.updateAutoLabel('Flying up!')``. # # 3. Then do it, using something like: # ``self.controller.SetCommand(roll, pitch, # yaw_velocity, z_velocity)``, where you fill in # numbers in place of ``roll``, ``pitch``, etc. # # A template for your code:: # # if (x_center < ???): # self.updateAutoLabel('Flying left!') # self.controller.SetCommand(0.3, 0, 0, 0) # Explain what the drone is doing in auto mode by # displaying strings telling its intentions. def updateAutoLabel(self, # A string to add to the explanation. s): self.lbAuto.setText(self.lbAuto.text() + s)
class MainWindow(QMainWindow): def __init__( self, # Webcam index (0 = first, etc.). webcamIndex=0, # Resolution, as an index into ``self.supported_resolution``. webcamInitialResolution=-1, # Qt Designer file to load. uiFile=None, # Other args to pass on to Qt. *args, **kwargs ): super(MainWindow, self).__init__(*args, **kwargs) self.uiFile = uiFile or join(dirname(__file__), "cv_video.ui") self.webcamIndex = webcamIndex self.webcamInitialResolution = webcamInitialResolution def __enter__(self): # Load in the GUI. uic.loadUi(self.uiFile, self) # Open a webcam. self.cap = cv2.VideoCapture(self.webcamIndex) assert self.cap.isOpened() # Scan for possible resolutions from a list of common resolutions. self.supported_resolution = [] for resolution in ( # 16:9 aspect ratio: (1920, 1080), (1280, 720), # 4:3 aspect ratio: (1024, 768), (640, 480), ): self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, resolution[0]) self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, resolution[1]) if ( self.cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) == resolution[0] and self.cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) == resolution[1] ): self.supported_resolution.append(resolution) self.cbWebcamResolution.addItem("{}x{}".format(resolution[0], resolution[1]), resolution) if self.webcamInitialResolution < 0: self.webcamInitialResolution = len(self.supported_resolution) - 1 self.cbWebcamResolution.setCurrentIndex(self.webcamInitialResolution) # Set up a timer to read from the webcam. self.webcamTimer = QTimer(self) self.webcamTimer.setInterval(0) self.webcamTimer.timeout.connect(self._on_webcamTimer_timeout) self.webcamTimer.start() # Monitor the fps. self.fpsLabel = QLabel() self.statusBar().addPermanentWidget(self.fpsLabel) self.fpsTimer = QElapsedTimer() self.fpsTimer.start() return self def __exit__(self, exc_type, exc_value, traceback): self.cap.release() self.webcamTimer.timeout.disconnect(self._on_webcamTimer_timeout) self.webcamTimer.stop() @pyqtSlot(int) def on_cbWebcamResolution_currentIndexChanged( self, # The index of the current item in the combobox index, ): resolution = self.cbWebcamResolution.itemData(index) self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, resolution[0]) self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, resolution[1]) @pyqtSlot() def _on_webcamTimer_timeout(self): # Get a frame from the webcam. success_flag, cvImage = self.cap.read() assert success_flag self._processImage(cvImage) self.fpsLabel.setText("{} fps".format(round(1000.0 / self.fpsTimer.elapsed()))) self.fpsTimer.restart() # This routine is called whenever a webcam image is available to process. It # should also display the resulting image. def _processImage( self, # The OpenCV image to process. cvImage, ): # Don't process; to demonstrate, simply display the image. cvImageToQt(cvImage, self.lOrig)
class MavControl(ButtonGui): # This is called when the pbPressed button is pressed. # Naming is similar for other functions. def on_pbTakeoff_pressed(self): print("TAKE OFF!") self.controller.SendTakeoff() def on_pbLand_pressed(self): print("LAND") self.controller.SendLand() def on_pbUp_pressed(self): print("Up") self.controller.SetCommand(roll=0, pitch=0, yaw_velocity=0, z_velocity=0.5) def on_pbUp_released(self): print("Up done.") self.controller.hover() @pyqtSlot(bool) def on_cbAuto_clicked( self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() else: # Return to a hover when leaving auto mode. self.controller.hover() # This is only called when the Auto checkbox is checked. def fly( self, # The x coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.width() - 1``. x_center, # The y coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.height() - 1``. y_center, # The area, in pixels, of the tracked region. cont_area): # Clear the description of what this does. self.lbAuto.setText('') # Decide what to do based on the state. if self.state == 1: # Take off # ^^^^^^^^ # The Auto checkbox was just checked. Take off # to start out mission. self.updateAutoLabel('Takeoff!') #self.controller.SendTakeoff() # Measure time from takeoff. self.elapsedTimer.start() # Move to the next state, waiting for takeoff to # finish. self.state = 2 elif self.state == 2: # Wait until take off completed # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ self.updateAutoLabel('Wait until take off completed') # Don't send any commands until we're flying. # So, wait 5 seconds then go to the next state. if self.elapsedTimer.elapsed() >= 5000: self.state = 3 elif self.state == 3: # ...your ideas... self.updateAutoLabel('State 3') else: self.updateAutoLabel('Unknown state! Help!') # 1. Determine what to do by examining ``x_center``, # ``y_center``, etc. # # 2. Explain what your code will do: # ``self.updateAutoLabel('Flying up!')``. # # 3. Then do it, using something like: # ``self.controller.SetCommand(roll, pitch, # yaw_velocity, z_velocity)``, where you fill in # numbers in place of ``roll``, ``pitch``, etc. # # A template for your code:: # # if (x_center < ???): # self.updateAutoLabel('Flying left!') # self.controller.SetCommand(0.3, 0, 0, 0) # Explain what the drone is doing in auto mode by # displaying strings telling its intentions. def updateAutoLabel( self, # A string to add to the explanation. s): self.lbAuto.setText(self.lbAuto.text() + s)
class MavControl(ButtonGui): # This is called when the pbPressed button is pressed. # Naming is similar for other functions. def on_pbTakeoff_pressed(self): print("TAKE OFF!") self.controller.SendTakeoff() def on_pbLand_pressed(self): print("LAND") self.controller.SendLand() def on_pbUp_pressed(self): print("Up") self.controller.SetCommand(roll=0, pitch=0, yaw_velocity=0, z_velocity=0.5) def on_pbUp_released(self): print("Up done.") self.controller.hover() @pyqtSlot(bool) def on_cbAuto_clicked(self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() else: # Return to a hover when leaving auto mode. self.controller.hover() # This is only called when the Auto checkbox is checked. def fly(self, # The x coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.width() - 1``. x_center, # The y coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.height() - 1``. y_center, # The area, in pixels, of the tracked region. cont_area): # Clear the description of what this does. self.lbAuto.setText('') # Decide what to do based on the state. if self.state == 1: # Take off # ^^^^^^^^ # The Auto checkbox was just checked. Take off # to start out mission. self.updateAutoLabel('Takeoff!') #self.controller.SendTakeoff() # Measure time from takeoff. self.elapsedTimer.start() # Move to the next state, waiting for takeoff to # finish. self.state = 2 elif self.state == 2: # Wait until take off completed # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ self.updateAutoLabel('Wait until take off completed') # Don't send any commands until we're flying. # So, wait 5 seconds then go to the next state. if self.elapsedTimer.elapsed() >= 5000: self.state = 3 elif self.state == 3: # ...your ideas... self.updateAutoLabel('State 3') else: self.updateAutoLabel('Unknown state! Help!') # 1. Determine what to do by examining ``x_center``, # ``y_center``, etc. # # 2. Explain what your code will do: # ``self.updateAutoLabel('Flying up!')``. # # 3. Then do it, using something like: # ``self.controller.SetCommand(roll, pitch, # yaw_velocity, z_velocity)``, where you fill in # numbers in place of ``roll``, ``pitch``, etc. # # A template for your code:: # # if (x_center < ???): # self.updateAutoLabel('Flying left!') # self.controller.SetCommand(0.3, 0, 0, 0) # Explain what the drone is doing in auto mode by # displaying strings telling its intentions. def updateAutoLabel(self, # A string to add to the explanation. s): self.lbAuto.setText(self.lbAuto.text() + s)
class Method(object): """Base class for gameplay methods Attributes: _name: title of the method _short_name: shorter title _run_time_limit: amount of seconds that method can run; if the method will run out of time then its player will loose the game _player: number of player which uses this method (0 or 1) _disabled: if True then method will not be active in GUI _ai_level: level of the AI experience (from 1 to 5) _running_timer: QElapsedTimer object that is used to check if the method has run out of the time limit """ _name = "Unknown" _short_name = "Unknown" _run_time_limit = 60 _player = 1 _disabled = False _ai_level = 1 _running_timer = None def __init__(self, player_num, ai_level=1, run_time_limit=60): """Inits a method with player number and AI level Args: player_num: player's number (0 or 1) ai_level: player's AI experience level (from 1 to 5) run_time_limit: running time limit """ self._player = player_num self._ai_level = ai_level self._run_time_limit = run_time_limit def name(self): """Returns method's name""" return self._name def short_name(self): """Returns method's short name""" return self._short_name def is_disabled(self): """If the method is disabled""" return self._disabled def set_run_time_limit(self, run_time_limit): """Sets running time limit""" self._run_time_limit = run_time_limit def set_player(self, player_num): """Sets a player number (0 or 1)""" self._player = player_num def is_time_expired(self, time_limit=-1): """Checks if the method is running out of time limit It uses QElapsedTimer object (self._running_timer) to check the elapsed time. Timer starts in self.make_move function Args: time_limit: in seconds Returns: True/False whether the method is running out of time limit """ if time_limit<0: time_limit = self._run_time_limit return self._running_timer.hasExpired(time_limit*1000) def make_move(self, state): """Makes a decision of the player's next move (abstract) Args: state: current board state Returns: Player's hole number which defines a player's next move """ self._running_timer = QElapsedTimer() self._running_timer.start() return -1
class MavControl(ButtonGui): # This is called when the pbPressed button is pressed. # Naming is similar for other functions. def on_pbTakeoff_pressed(self): print("TAKE OFF!") self.controller.SendTakeoff() def on_pbLand_pressed(self): print("LAND") self.controller.SendLand() def on_pbUp_pressed(self): print("Up") self.controller.SetCommand(roll=0, pitch=0, yaw_velocity=0, z_velocity=0.5) def on_pbUp_released(self): print("Up done") self.controller.hover() ################ Josh added ################ def on_pbDown_pressed(self): print("Down") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0,z_velocity=-0.5) def on_pbDown_released(self): print("Down done") self.controller.hover() def on_pbForward_pressed(self): print("Forward") self.controller.SetCommand(roll=0,pitch=0.5,yaw_velocity=0,z_velocity=0) def on_pbForward_released(self): print("Forward done") self.controller.hover() def on_pbBackward_pressed(self): print("Backward") self.controller.SetCommand(roll=0,pitch=-0.5,yaw_velocity=0,z_velocity=0) def on_pbBackward_released(self): print("Backward done") self.controller.hover() def on_pbLeft_pressed(self): print("Left") self.controller.SetCommand(roll=0.5,pitch=0,yaw_velocity=0,z_velocity=0) def on_pbLeft_released(self): print("Left done") self.controller.hover() def on_pbRight_pressed(self): print("Right") self.controller.SetCommand(roll=-0.5,pitch=0,yaw_velocity=0,z_velocity=0) def on_pbRight_released(self): print("Right done") self.controller.hover() def on_pbRotateLeft_pressed(self): print("Rotate Left") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=0.5,z_velocity=0) def on_pbRotateLeft_released(self): print("Rotate Left done") self.controller.hover() def on_pbRotateRight_pressed(self): print("Rotate Right") self.controller.SetCommand(roll=0,pitch=0,yaw_velocity=-0.5,z_velocity=0) def on_pbRotateRight_released(self): print("Rotate Right done") self.controller.hover() def on_pbFlatTrim_pressed(self): print("Flat trimmed") self.controller.SetFlatTrim() def on_pbEmergency_pressed(self): print("Emergency signal sent (or reset)") self.controller.SendEmergency() def on_pbToggleCamera_pressed(self): print("Camera Toggled") self.controller.ToggleCamera() def on_pbBlinkRed_pressed(self): print("LEDs Blinking Red") self.controller.SetLedAnimation(2,3,3) def on_pbBlinkOrange_pressed(self): print("LEDs Blinking Orange") self.controller.SetLedAnimation(3,3,3) def on_pbBlinkGreen_pressed(self): print("LEDs Blinking Green") self.controller.SetLedAnimation(1,3,3) def on_pbDance_pressed(self): print("I'm Dancing") def on_pbDance_released(self): print("Done dancing") def on_pbTakePicture_released(self): print("Taking Picture") global_flags.image_flag = 1 # def on_pbGoToRectangle_pressed(self, x_center,y_center): # print("Going to selected rectangle") # #x_center = self.x_center # #y_center = self.y_center # if (x_center == -1) and (y_center == -1): # #self.controller.SendLand() # print('Out of Range') # elif(x_center > 85): # print('Go Right') # #self.controller.SetCommand(-0.1, 0, 0, 0) # elif (x_center < 60): # print('Go Left') # #self.controller.SetCommand(0.1, 0, 0, 0) # elif(y_center > 65): # print('Go Down') # #self.controller.SetCommand(0, 0, 0, -0.1) # elif (y_center < 40): # print('Go Up') # #self.controller.SetCommand(0, 0, 0, 0.1) # else: # self.controller.hover() # #print('hover') def on_pbRecord(self): self.controller.RecordUsb() ################################################ @pyqtSlot(bool) def on_cbAuto_clicked(self, # True is the checkbox is checked; False if not. checked): if checked: # Initialize our state if we're just entering # auto mode. self.state = 1 self.time = 0 self.scan = 1 self.scan_count = 0 self.scan_right = 1 self.scan_forward = 0 self.scan_left = 0 slef.scan_back = 0 self.rotate = False self.area = 0 # Create a timer for use in ``fly()``. self.elapsedTimer = QElapsedTimer() elif not checked: self.controller.SendLand() else: # Return to a hover when leaving auto mode. self.controller.hover() # This is only called when the Auto checkbox is checked. def fly(self, # The x coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.width() - 1``. x_center, # The y coordinate of the center of the tracked area. # It ranges between 0 and ``self.lbVideo.height() - 1``. y_center, # The area, in pixels, of the tracked region. cont_area, found_red): # Clear the description of what this does. self.lbAuto.setText('') # Decide what to do based on the state. if self.state == 1: # Take off # ^^^^^^^^ # The Auto checkbox was just checked. Take off # to start out mission. self.updateAutoLabel('Takeoff!') self.controller.SendTakeoff() #print x_center,y_center,cont_area # Measure time from takeoff. self.elapsedTimer.start() # Move to the next state, waiting for takeoff to # finish. self.state = 2 elif self.state == 2: # Wait until take off completed # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ self.updateAutoLabel('Wait until take off completed') # Don't send any commands until we're flying. # So, wait 5 seconds then go to the next state. if self.elapsedTimer.elapsed() >= 5000: self.state = 3 self.time = self.elapsedTimer.elapsed() self.updateAutoLabel('Looking for Landing') elif self.state == 3: if self.elapsedTimer.elapsed() >= 3000: self.controller.hover() global_flags.image_flag = 1 # if found land if global_flags.found_landing == 1 self.state = 4 elif self.time >= (self.elapsedTimer.elapsed() + 10000): self.time = self.elapsedTimer.elapsed() # first start going right if self.scan_right == 1: self.controller.SetCommand(-0.1, 0.0, 0, 0) self.scan_count += 1 if(self.scan_count == self.scan): self.scan_right = 0 self.scan_forward = 1 self.scan_count = 0 elif self.scan_forward == 1: self.controller.SetCommand(0.0, 0.1, 0, 0) self.scan_count += 1 if(self.scan_count == self.scan): self.scan_forward = 0 self.scan_left = 1 # increase the count so the circle becomes bigger self.scan += 1 self.scan_count = 0 elif self.scan_left == 1: self.controller.SetCommand(0.1, 0.0, 0, 0) self.scan_count += 1 if(self.scan_count == self.scan): self.scan_left = 0 self.scan_back = 1 self.scan_count = 0 elif self.scan_back == 1: self.controller.SetCommand(0.0, -0.1, 0, 0) self.scan_count += 1 if(self.scan_count == self.scan): self.scan_forward = 0 self.scan_right = 1 self.scan += 1 self.scan_count = 0 elif self.state == 4: global_flags.image_flag = 1 self.updateAutoLabel('Landing') self.controller.SendLand() self.cbAuto.toggle() else: self.updateAutoLabel('Unknown state! Help!') self.controller.SendLand() # 1. Determine what to do by examining ``x_center``, # ``y_center``, etc. # # 2. Explain what your code will do: # ``self.updateAutoLabel('Flying up!')``. # # 3. Then do it, using something like: # ``self.controller.SetCommand(roll, pitch, # yaw_velocity, z_velocity)``, where you fill in # numbers in place of ``roll``, ``pitch``, etc. # # A template for your code:: # # if (x_center < ???): # self.updateAutoLabel('Flying left!') # self.controller.SetCommand(0.3, 0, 0, 0) # Explain what the drone is doing in auto mode by # displaying strings telling its intentions. def updateAutoLabel(self, # A string to add to the explanation. s): self.lbAuto.setText(self.lbAuto.text() + s)