def __init__(self, transformMatrix): self.root=Tkinter.Tk() self.root.title("IRondelle interface") self.path = [(0,0)] self.endingSignalReceived = False self.confirmationStartSignalReceived = False self.resistance = 0 self.resistanceColors = ['black','black','black'] self.command_letterValue='inconnu' self.command_rotationValue='Sens inconnu' self.clearWidgetsReceived = False self.chronoActive=False self.startTime = 0.0 self.Timeoffset=0.0 self.mapUpdater = MapUpdater(transformMatrix) self.obstaclePositions = [] self.colors = self._createColorsDictionary() self.run()
class GUI: SUBPLOT_ROWS = 1 SUBPLOT_COLUMNS = 1 SUBPLOT_PLOTPOSITION = 1 HEIGHT=11.7*0.6 WIDTH=5.6*0.6 MAP_SQUARE_SIZE_IN_CENTIMETERS = 2 RESISTANCE_WIDGET_HEIGHT = 100 RESISTANCE_WIDGET_WIDTH = 150 PADDING = 20 REFRESHRATE = 1500 FONT = ("Font", 12) def __init__(self, transformMatrix): self.root=Tkinter.Tk() self.root.title("IRondelle interface") self.path = [(0,0)] self.endingSignalReceived = False self.confirmationStartSignalReceived = False self.resistance = 0 self.resistanceColors = ['black','black','black'] self.command_letterValue='inconnu' self.command_rotationValue='Sens inconnu' self.clearWidgetsReceived = False self.chronoActive=False self.startTime = 0.0 self.Timeoffset=0.0 self.mapUpdater = MapUpdater(transformMatrix) self.obstaclePositions = [] self.colors = self._createColorsDictionary() self.run() def clearWidgets(self): plannedPath = Queue.Queue() obstacles = Queue.Queue() endingSignal = Queue.Queue() resistanceMesure = Queue.Queue() command_letter = Queue.Queue() command_rotation = Queue.Queue() startSignal = Queue.Queue() confirmationStartSignal = Queue.Queue() self.path = [(0,0)] self.endingSignalReceived = False self.confirmationStartSignalReceived = False self.clearWidgetsReceived = False self.resistance = 0 self.resistanceColors = ['black','black','black'] self.command_letterValue='inconnu' self.command_rotationValue='Sens inconnu' def _createColorsDictionary(self): colors = dict() colors['black'] = [0,0,0] colors['white'] = [1,1,1] colors['darkred'] = [1,0,0] colors['green'] = [0,1,0] colors['blue'] = [0,0,1] colors['gray'] = [0.5,0.5,0.5] colors['paleyellow'] = [1,1,0] colors['palepurple'] = [0.8,0.2,0.8] colors['orange'] = [1,0.5,0] colors['brown'] = [0.5,0.25,0] return colors def run(self): self.drawMap() self.placeStartSignal() self.placeChronometer() self.placeResistanceWidgets() self.placeCommandWidgets() self.placeExitButton() self.refreshWidgets() self.root.mainloop() def refreshMap(self): try: self.cartographySystem = self.mapUpdater.updateMap() except ValueError: print "Erreur position robot" while not obstacles.empty(): self.obstaclePositions = obstacles.get_nowait() rowNumber = 117 obstacleDiameterInCentimeters = 14 obstacleRadiusInCentimeters = obstacleDiameterInCentimeters/2 for x, y in self.obstaclePositions: try: i, j = convertCoordinatesToMapPoint((x, y), rowNumber, self.MAP_SQUARE_SIZE_IN_CENTIMETERS) obstacle = CircularObject(obstacleDiameterInCentimeters) self.cartographySystem.createObject(obstacle, i-obstacleRadiusInCentimeters, j-obstacleRadiusInCentimeters, walkable=False)#upperleft point for placement except e: print "Exception pour placer les obstacles" continue self.map_handle.set_data(self.cartographySystem.map) while len(self.subplot.lines) > 0 : self.subplot.lines.pop() while not plannedPath.empty(): self.path = plannedPath.get_nowait() for z in range(len(self.path)): x, y = self.path[z] i,j = convertCoordinatesToMapPoint((x, y), rowNumber, self.MAP_SQUARE_SIZE_IN_CENTIMETERS) i, j = int(i), int(j) self.path[z] = (i, j) self.drawPath() self.map_canvas.get_tk_widget().after(self.REFRESHRATE, self.refreshMap) def drawMap(self): self.cartographySystem = self.mapUpdater.updateMap() mapFigure = matplotlib.figure.Figure(figsize=(self.WIDTH, self.HEIGHT)) self.subplot = mapFigure.add_subplot(self.SUBPLOT_ROWS, self.SUBPLOT_COLUMNS, self.SUBPLOT_PLOTPOSITION) self.map_handle=self.subplot.imshow(self.cartographySystem.map) self.map_handle.set_cmap('spectral') self.map_handle.axes.get_xaxis().set_visible(False) self.map_handle.axes.get_yaxis().set_visible(False) mapFigure.subplots_adjust(left=0.02,right=0.98,bottom=0,top=1, wspace=0, hspace=0) self.map_canvas=FigureCanvasTkAgg(mapFigure, master=self.root) self.map_canvas.show() self.map_canvas.get_tk_widget().grid(padx=self.PADDING, pady=self.PADDING, row=0, rowspan=20) self.refreshMap() def drawPath(self): if not self.path: return previousPoint = self.path[0] for nextPoint in self.path: yPreviousPoint, xPreviousPoint = previousPoint yNextPoint, xNextPoint = nextPoint line=matplotlib.lines.Line2D([xPreviousPoint, xNextPoint],[yPreviousPoint, yNextPoint], linestyle='-', color='y', linewidth=3) self.subplot.add_line(line) previousPoint = nextPoint self.map_canvas.draw() def placeStartSignal(self): go=Tkinter.Button(self.root, font=self.FONT, text='Envoi du signal de départ', command=self.sendSignal) go.grid(padx=self.PADDING, pady=self.PADDING, row=0, column=1) self.confirmation=Tkinter.Label(self.root, font=self.FONT, text='Confirmation non reçue') self.confirmation.grid(padx=self.PADDING, pady=self.PADDING, row=1, column=1) def placeExitButton(self): exit=Tkinter.Button(self.root, font=self.FONT, text='Quitter', command=sys.exit) exit.grid(padx=self.PADDING, pady=self.PADDING, row=6, column=2, columnspan=2) def sendSignal(self): self.clearWidgets() startSignal.put(True) self.waitConfirmation() self.chronoActive=True self.startTime=time.time() self.handleChronometer() self.waitFinishedSignal() def waitConfirmation(self): while not confirmationStartSignal.empty(): self.confirmationStartSignalReceived = confirmationStartSignal.get_nowait() if not self.confirmationStartSignalReceived: self.confirmation.after(self.REFRESHRATE,self.waitConfirmation) else: self.confirmation['text']='Confirmation reçue' self.confirmation.grid(padx=self.PADDING, pady=self.PADDING, row=1, column=1) def waitFinishedSignal(self): while not endingSignal.empty(): self.endingSignalReceived=endingSignal.get_nowait() if not self.endingSignalReceived: self.confirmation.after(self.REFRESHRATE,self.waitFinishedSignal) else: self.Timeoffset=time.time()-self.startTime+self.Timeoffset; self.chronoActive=False startSignal.queue.clear() def placeChronometer(self): self.chronometer=Tkinter.Label(self.root, font=self.FONT, text="%02d:%02d" % (0, 0)) self.chronometer.grid(padx=self.PADDING, pady=self.PADDING, row=0, column=2) rst=Tkinter.Button(self.root, font=self.FONT, text='Reset Chronomètre', command=self.resetChrono) rst.grid(padx=self.PADDING, pady=self.PADDING, row=1, column=2, columnspan=2) def handleChronometer(self): if self.chronoActive: elapsedTime = time.time() - self.startTime + self.Timeoffset minutes = int(elapsedTime/60) seconds = int(elapsedTime - minutes*60.0) self.chronometer['text'] = "%02d:%02d" % (minutes, seconds) self.chronometer.after(1000,self.handleChronometer) def resetChrono(self): self.chronoActive=False minutes = 0 seconds = 0 self.Timeoffset=0.0 self.chronometer['text'] = "%02d:%02d" % (minutes, seconds) startSignal.queue.clear() def placeResistanceWidgets(self): self.resistanceLabel = Tkinter.Label(self.root, font=self.FONT, text='Résistance: Inconnue') self.resistanceLabel.grid(padx=self.PADDING, pady=self.PADDING, row=3, column=1, columnspan=2) resistanceFigure = matplotlib.figure.Figure(figsize=(3,2)) resistanceSubplot = resistanceFigure.add_subplot(self.SUBPLOT_ROWS, self.SUBPLOT_COLUMNS, self.SUBPLOT_PLOTPOSITION) self.resistance_handle = resistanceSubplot.imshow(numpy.zeros((self.RESISTANCE_WIDGET_HEIGHT,self.RESISTANCE_WIDGET_WIDTH,3))) self.resistance_handle.axes.get_xaxis().set_visible(False) self.resistance_handle.axes.get_yaxis().set_visible(False) resistanceFigure.subplots_adjust(left=0.02, right=0.98, bottom=0, top=1, wspace=0, hspace=0) self.resistance_canvas = FigureCanvasTkAgg(resistanceFigure, master=self.root) self.resistance_canvas.show() self.resistance_canvas.get_tk_widget().grid(padx=self.PADDING, pady=self.PADDING, row=4, column=1, columnspan=2) self.refreshResistance() def refreshResistance(self): while not resistanceMesure.empty(): self.resistance = resistanceMesure.get_nowait() self.resistanceColors= [str(color) for (_, color) in Resistance(self.resistance).toColorCode()] if self.resistance: self.resistanceLabel['text']='Résistance: '+str(self.resistance)+' Ohms' else: self.resistanceLabel['text']='Résistance: Inconnue' newResistanceColors = self._createResistanceColorBands() self.resistance_handle.set_data(newResistanceColors) self.resistance_canvas.draw() self.resistance_canvas.get_tk_widget().after(self.REFRESHRATE, self.refreshResistance) def _createResistanceColorBands(self): new_colors = numpy.zeros((self.RESISTANCE_WIDGET_HEIGHT, self.RESISTANCE_WIDGET_WIDTH, 3)) firstBandStartingPixel = 0 firstBandEndingPixel = self.RESISTANCE_WIDGET_WIDTH/3 secondBandStartingPixel = firstBandEndingPixel + 1 secondBandEndingPixel = 2*self.RESISTANCE_WIDGET_WIDTH/3 thirdBandStartingPixel = secondBandEndingPixel + 1 thirdBandEndingPixel = self.RESISTANCE_WIDGET_WIDTH band1 = new_colors[:, firstBandStartingPixel:firstBandEndingPixel, :] band2 = new_colors[:, secondBandStartingPixel:secondBandEndingPixel, :] band3 = new_colors[:, thirdBandStartingPixel:thirdBandEndingPixel, :] band1[:,:] = self.colors[self.resistanceColors[0]] band2[:,:] = self.colors[self.resistanceColors[1]] band3[:,:] = self.colors[self.resistanceColors[2]] return new_colors def placeCommandWidgets(self): self.command_letterLabel=Tkinter.Label(self.root, font=self.FONT, text='Commande: Coin Inconnu') self.command_letterLabel.grid(padx=self.PADDING, pady=self.PADDING, row=5, column=1) self.command_rotationLabel=Tkinter.Label(self.root, font=self.FONT, text='Commande: Sens Inconnu') self.command_rotationLabel.grid(padx=self.PADDING, pady=self.PADDING, row=6, column=1) self.refreshCommand() def refreshCommand(self): while not command_letter.empty(): self.command_letterValue = command_letter.get_nowait() while not command_rotation.empty(): self.command_rotationValue = command_rotation.get_nowait() self.command_letterLabel['text']='Commande: Coin ' + self.command_letterValue self.command_rotationLabel['text']='Commande: ' + self.command_rotationValue self.command_letterLabel.after(self.REFRESHRATE,self.refreshCommand) def refreshWidgets(self): while not clearWidgets.empty(): self.clearWidgetsReceived = clearWidgets.get_nowait() if not self.clearWidgetsReceived: self.confirmation.after(self.REFRESHRATE,self.waitConfirmation) else: self.confirmation['text']='Confirmation non reçue' self.confirmation.grid(padx=self.PADDING, pady=self.PADDING, row=1, column=1) self.clearWidgets()