#importation des modules import RPi.GPIO as GPIO import time import sys,tty,termios import tkinter as tk import threading #librairie pour les threads, taches en parallele from threading import Thread #librairie pour les threads, taches en parallele from queue import Queue #initialisation de la numerotation GPIO.setmode(GPIO.BOARD) #Temps d'atente global SPSTime global PSStime SPSTime=0.02 #Max frequency : 25Mhz = 0.000 000 04s 0.0000002 #Serial Parallel PSStime=0.01 #Max frequency : 50Mhz = 0.000 000 02s 0.0000001 #Parallel Serial #Speed sensor variables #Leg 0, Motor 0 global SpSensL0M0 SpSensL0M0 = 0 #Variables d'entrée global MotorSelect #Selection of the motor to act on MotorSelect=0 global GI0 global GI1 global GI2 global GI3 global GI4 global GI5 global GI6 global GI7 global GI8 global GI9 global GI10 global GI11 global GI12 global GI13 global GI14 global GI15 GI0=2 #L0M1Speed GI1=2 #L0M2Speed GI2=2 #L0RightSensor GI3=2 #L0LeftSensor GI4=2 #L1M1Speed GI5=2 #L1M2Speed GI6=2 #L1RightSensor GI7=2 #L1LeftSensor GI8=2 #L2M1Speed GI9=2 #L2M2Speed GI10=2 #L2RightSensor GI11=2 #L2LeftSensor GI12=2 #L3M1Speed GI13=2 #L3M2Speed GI14=2 #L3RightSensor GI15=2 #L3LeftSensor #Variables de sortie global GO0 global GO1 global GO2 global GO3 global GO4 global GO5 global GO6 global GO7 global GO8 global GO9 global GO10 global GO11 global GO12 global GO13 global GO14 global GO15 GO0=0 #L0In1 GO1=0 #L0In2 GO2=0 #L0In3 GO3=0 #L0In4 GO4=0 #L1In1 GO5=0 #L1In2 GO6=0 #L1In3 GO7=0 #L1In4 GO8=0 #L2In1 GO9=0 #L2In2 GO10=0 #L2In3 GO11=0 #L2In4 GO12=0 #L3In1 GO13=0 #L3In2 GO14=0 #L3In3 GO15=0 #L3In4 #Tableau de données d'entrée et sortie DataLoadIn = [GI0, GI1, GI2, GI3, GI4, GI5, GI6, GI7, GI8, GI9, GI10, GI11, GI12, GI13, GI14, GI15]; #Tableau des données d'entrée DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Ser -> // SPSER=40 #Serial output pin SPSCLK=36 #Shift registers clock pin SPRCLK=38 #Output register clock #// -> Ser PSSIN=33 #Serial data input PSPL=35 #Parallel loading of the inputs of the shift register PSCLK=37 #Configuration de la commande du registre a décalage Paralle -> Serie GPIO GPIO.setup(PSPL, GPIO.OUT, initial=True) # broche SER est une sortie numerique, a létat bas au début GPIO.setup(PSSIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # broche SIN est une entrée avec resistance de pull down GPIO.setup(PSCLK, GPIO.OUT, initial=False) #Configuration de la commande du registre a décalage Serie -> Paralle GPIO GPIO.setup(SPSER, GPIO.OUT, initial=False) # broche SER est une sortie numerique, a létat bas au début et avec resistance de pull down GPIO.setup(SPSCLK, GPIO.OUT, initial=False) # broche SCLK est une sortie numerique GPIO.setup(SPRCLK, GPIO.OUT, initial=False) #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Value to display on the screen through threading ThingToPrint = 0 #Threader for displaying some data class PrintScreen(Thread): def __init__(self): Thread.__init__(self) def WriteScreen(): global ThingToPrint print("Lis \n") while True: #global Arg print(ThingToPrint) time.sleep(1) #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Initialisation GPIO.cleanup print("clean up") #Mise a 0 de toutes les sorties du registre a décalage for SPiR in range(16): print("reset", SPiR) GPIO.output(SPSER, False) time.sleep(0.0001) GPIO.output(SPSCLK, True) time.sleep(0.0001) GPIO.output(SPSCLK, False) print("Reset=", SPiR) #activation sortie registres GPIO.output(SPRCLK, True) time.sleep(0.0001) GPIO.output(SPRCLK, False) #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Function for writing data in the outputs S --> P class Writing(threading.Thread): #S -> // thread def __init__(self): threading.Thread.__init__(self) #Continuously Write the data to the register def Write(): global GO0 global GO1 global GO2 global GO3 global GO4 global GO5 global GO6 global GO7 global GO8 global GO9 global GO10 global GO11 global GO12 global GO13 global GO14 global GO15 global SPSTime print("Start S-P Thread") while True: #print("debut while") #Serial output DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie #Sending data 1 by 1 starting from the last one to the register for SPiDL in reversed(DataLoadOut): if GPIO.input(SPSER) != SPiDL:#If the GPIO outpu pin is not in the wanted state, invert it GPIO.output(SPSER, not GPIO.input(SPSER)) #One clock cycle GPIO.output(SPSCLK, True) GPIO.output(SPSCLK, False) #Activate shift register once to load the next data (no clock needed) GPIO.output(SPRCLK, True) GPIO.output(SPRCLK, False) #print("DataLoadOut", DataLoadOut) #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Reading the data from the shift register #// -> S thread class Reading(threading.Thread): def __init__(self): threading.Thread.__init__(self) def Read(): #Serial input global GI0 global GI1 global GI2 global GI3 global GI4 global GI5 global GI6 global GI7 global GI8 global GI9 global GI10 global GI11 global GI12 global GI13 global GI14 global GI15 global PSStime print ("start P-S Thread") while True: #Loading data in register #Clock cycle on PS for loading data (no need of clock signal) GPIO.output(PSPL, False) GPIO.output(PSPL, True) #As long as the 16 bits didn't pass for i, PSiDL in enumerate(reversed(DataLoadIn)): DataIn = GPIO.input(PSSIN) DataLoadIn[15-i] = DataIn #Lecture de l'entrée série et insertion de la valeur dans la case n°iDL GPIO.output(PSCLK, True)#un coups d'horloge pour passer au bit suivant GPIO.output(PSCLK, False) #print("DataLoadIn", DataLoadIn) #Loading of the data of the table GI0 = DataLoadIn[0] GI1 = DataLoadIn[1] GI2 = DataLoadIn[2] GI3 = DataLoadIn[3] GI4 = DataLoadIn[4] GI5 = DataLoadIn[5] GI6 = DataLoadIn[6] GI7 = DataLoadIn[7] GI8 = DataLoadIn[8] GI9 = DataLoadIn[9] GI10 = DataLoadIn[10] GI11 = DataLoadIn[11] GI12 = DataLoadIn[12] GI13 = DataLoadIn[13] GI14 = DataLoadIn[14] GI15 = DataLoadIn[15] #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX class TouchSensors(threading.Thread): def __init__(self): threading.Thread.__init__(self) def Sensors(): #Serial input global GI2 global GI3 global GI6 global GI7 global GI10 global GI11 global GI14 global GI15 #Serial Outputs global GO0 #M1In1 global GO1 #M1In2 global GO2 #M1In3 global GO3 #M1In4 global GO4 #M2In1 global GO5 #M2In2 global GO6 #M2In3 global GO7 #M2In4 global GO8 #M3In1 global GO9 #M3In2 global G10 #M3In3 global G11 #M3In4 global G12 #M4In1 global G13 #M4In2 global G14 #M4In3 global G15 #M4In4 print ("start Sensors Thread") while True: #Leg 0, Right Sensor if GI2 == 1: GO0=0 GO1=0 #Leg 0, Left Sensor elif GI3 == 1: GO2=0 GO3=0 #Leg 1, Right Sensor elif GI6 == 1: GO4=0 GO5=0 #Leg 1, Left Sensor elif GI7 == 1: GO6=0 GO7=0 #Leg 2, Right Sensor elif GI10 == 1: GO8=0 GO9=0 #Leg 2, Left Sensor elif GI11 == 1: GO10=0 GO11=0 #Leg 3, Right Sensor elif GI14 == 1: GO12=0 GO13=0 #Leg 3, Left Sensor elif GI15 == 1: GO14=0 GO15=0 #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ##class Moving(threading.Thread): ## def __init__(self): ## threading.Thread.__init__(self) ## ## def Movement(): ## GO0=1 #L0In1 ## GO1=0 #L0In2 ## GO2=1 #L0In3 ## GO3=0 #L0In4 ## GO4=1 #L1In1 ## GO5=0 #L1In2 ## GO6=1 #L1In3 ## GO7=0 #L1In4 ## GO8=1 #L2In1 ## GO9=0 #L2In2 ## GO10=1 #L2In3 ## GO11=0 #L2In4 ## GO12=1 #L3In1 ## GO13=0 #L3In2 ## GO14=1 #L3In3 ## GO15=0 #L3In4 ## while True: ## if GI2 == 0: #Serial input ## #Leg 1, Right Sensor ## elif GI3 == 1 ## GO0=0 ## GO1=0 ## #Leg 0, Left Sensor ## elif GI3 == 1 ## GO0=0 ## GO1=0 ## #Leg 1, Right Sensor ## elif GI3 == 1 ## GO0=0 ## GO1=0 ## #Leg 0, Left Sensor ## elif GI3 == 1 ## GO0=0 ## GO1=0 #Motor 1, Right Sensor #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #changing of the data for moving the legs #Changes the data of the register to move the leg to the Right def MRight(): global GO0 #M1In1 global GO1 #M1In2 global GO2 #M1In3 global GO3 #M1In4 global GO4 #M2In1 global GO5 #M2In2 global GO6 #M2In3 global GO7 #M2In4 global GO8 #M3In1 global GO9 #M3In2 global GO10 #M3In3 global GO11 #M3In4 global GO12 #M4In1 global GO13 #M4In2 global GO14 #M4In3 global GO15 #M4In4 if GO0==0 and GO1==0 and GO4==0 or (GO0==1 and GO1==1 and GO4==1): #If stopped, start turning print("Problem, reset") GO0=0 GO1=1 GO4=0 if GO0==0 and GO1==1 and GO4==0: #If stopped, start turning GO0=0 GO1=1 GO4=1 elif GO0==1 and GO1==0 and GO4==1: #If turning the other way, stop it GO0=0 GO1=1 GO4=0 elif GO0==1 and GO1==0 and GO4==0: #If turning the other way, stop it GO0=0 GO1=1 GO4=1 #DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie #Changes the data of the register to move the leg to the Left def MLeft(): global GO0 #In1 global GO1 #In2 global GO2 #In3 global GO3 #In4 global GO4 #ENA global GO5 #ENB if GO0==0 and GO1==0 and GO4==0 or (GO0==1 and GO1==1 and GO4==1): #If stopped, start turning print("Problem, reset") GO0=0 GO1=1 GO4=0 if GO0==1 and GO1==0 and GO4==0: #If stopped, start turning GO0=1 GO1=0 GO4=1 elif GO0==0 and GO1==1 and GO4==1: #If turning the other way, stop it GO0=1 GO1=0 GO4=0 elif GO0==0 and GO1==1 and GO4==0: #If turning the other way, stop it GO0=1 GO1=0 GO4=1 #DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie #Changes the data of the register to move the leg to the Left def MFwd(): global GO0 #L1 In1 global GO1 #L1 In2 global GO2 #L1 In3 global GO3 #L1 In4 global GO4 #L2 In1 global GO5 #L2 In2 global GO6 #L2 In3 global GO7 #L2 In4 global GO8 #L3 In1 global GO9 #L3 In2 global GO10 #L3 In3 global GO11 #L3 In4 global GO12 #L4 In1 global GO13 #L4 In2 global GO14 #L4 In3 global GO15 #L4 In4 global MotorSelect #Leg 0 Motor 1 if MotorSelect == 1: if GO0==1 and GO1==1: #If Bug, Stop turning print("Problem, reset") GO0=0 GO1=0 if GO0==0 and GO1==0: #If stopped, start turning GO0=1 GO1=0 elif GO0==0 and GO1==1: #If turning the other way, stop it GO0=0 GO1=0 #Leg 0 Motor 2 elif MotorSelect==2: if GO2==1 and GO3==1: #If Bug, Stop turning print("Problem, reset") GO2=0 GO3=0 if GO2==0 and GO3==0: #If stopped, start turning GO2=1 GO3=0 elif GO2==0 and GO3==1: #If turning the other way, stop it GO2=0 GO3=0 #Leg 1 Motor 1 elif MotorSelect==3: if GO4==1 and GO5==1: #If Bug, Stop turning print("Problem, reset") GO4=0 GO5=0 if GO4==0 and GO5==0: #If stopped, start turning GO4=1 GO5=0 elif GO4==0 and GO5==1: #If turning the other way, stop it GO4=0 GO5=0 #Leg 1 Motor 2 elif MotorSelect==4: if GO6==1 and GO7==1: #If Bug, Stop turning print("Problem, reset") GO6=0 GO7=0 if GO6==0 and GO7==0: #If stopped, start turning GO6=1 GO7=0 elif GO6==0 and GO7==1: #If turning the other way, stop it GO6=0 GO7=0 #Leg 2 Motor 1 if MotorSelect == 5: if GO8==1 and GO9==1: #If Bug, Stop turning print("Problem, reset") GO8=0 GO9=0 if GO8==0 and GO9==0: #If stopped, start turning GO8=1 GO9=0 elif GO8==0 and GO9==1: #If turning the other way, stop it GO8=0 GO9=0 #Leg 2 Motor 2 elif MotorSelect==6: if GO10==1 and GO11==1: #If Bug, Stop turning print("Problem, reset") GO10=0 GO11=0 if GO10==0 and GO11==0: #If stopped, start turning GO10=1 GO11=0 elif GO10==0 and GO11==1: #If turning the other way, stop it GO10=0 GO11=0 #Leg 3 Motor 1 elif MotorSelect==7: if GO12==1 and GO13==1: #If Bug, Stop turning print("Problem, reset") GO12=0 GO13=0 if GO12==0 and GO13==0: #If stopped, start turning GO12=1 GO13=0 elif GO12==0 and GO13==1: #If turning the other way, stop it GO12=0 GO13=0 #Leg 3 Motor 2 elif MotorSelect==8: if GO14==1 and GO15==1: #If Bug, Stop turning print("Problem, reset") GO14=0 GO15=0 if GO14==0 and GO15==0: #If stopped, start turning GO14=1 GO15=0 elif GO14==0 and GO15==1: #If turning the other way, stop it GO14=0 GO15=0 #DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie #print(DataLoadOut) #Changes the data of the register to walk backward def MBwd(): global GO0 #L1 In1 global GO1 #L1 In2 global GO2 #L1 In3 global GO3 #L1 In4 global GO4 #L2 In1 global GO5 #L2 In2 global GO6 #L2 In3 global GO7 #L2 In4 global GO8 #L3 In1 global GO9 #L3 In2 global GO10 #L3 In3 global GO11 #L3 In4 global GO12 #L4 In1 global GO13 #L4 In2 global GO14 #L4 In3 global GO15 #L4 In4 global MotorSelect if MotorSelect == 1: if GO0==1 and GO1==1: #If Bug, Stop turning print("Problem, reset") GO0=0 GO1=0 if GO0==0 and GO1==0: #If stopped, start turning GO0=0 GO1=1 elif GO0==1 and GO1==0: #If turning the other way, stop it GO0=0 GO1=0 elif MotorSelect==2: if GO2==1 and GO3==1: #If Bug, Stop turning print("Problem, reset") GO2=0 GO3=0 if GO2==0 and GO3==0: #If stopped, start turning GO2=0 GO3=1 elif GO2==1 and GO3==0: #If turning the other way, stop it GO2=0 GO3=0 elif MotorSelect==3: if GO4==1 and GO5==1: #If Bug, Stop turning print("Problem, reset") GO4=0 GO5=0 if GO4==0 and GO5==0: #If stopped, start turning GO4=0 GO5=1 elif GO4==1 and GO5==0: #If turning the other way, stop it GO4=0 GO5=0 elif MotorSelect==4: if GO6==1 and GO7==1: #If Bug, Stop turning print("Problem, reset") GO6=0 GO7=0 if GO6==0 and GO7==0: #If stopped, start turning GO6=0 GO7=1 elif GO6==1 and GO7==0: #If turning the other way, stop it GO6=0 GO7=0 if MotorSelect == 5: if GO8==1 and GO9==1: #If Bug, Stop turning print("Problem, reset") GO8=0 GO9=0 if GO8==0 and GO9==0: #If stopped, start turning GO8=0 GO9=1 elif GO8==1 and GO9==0: #If turning the other way, stop it GO8=0 GO9=0 elif MotorSelect==6: if GO10==1 and GO11==1: #If Bug, Stop turning print("Problem, reset") GO10=0 GO11=0 if GO10==0 and GO11==0: #If stopped, start turning GO10=0 GO11=1 elif GO10==1 and GO11==0: #If turning the other way, stop it GO10=0 GO11=0 elif MotorSelect==7: if GO12==1 and GO13==1: #If Bug, Stop turning print("Problem, reset") GO12=0 GO13=0 if GO12==0 and GO13==0: #If stopped, start turning GO12=0 GO13=1 elif GO12==1 and GO13==0: #If turning the other way, stop it GO12=0 GO13=0 elif MotorSelect==8: if GO14==1 and GO15==1: #If Bug, Stop turning print("Problem, reset") GO14=0 GO15=0 if GO14==0 and GO15==0: #If stopped, start turning GO14=0 GO15=1 elif GO14==1 and GO15==0: #If turning the other way, stop it GO14=0 GO15=0 #DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie #print(DataLoadOut) #Detection of the key press events def key(event): global MotorSelect print("press " + event.keysym) if event.keysym == "Right": MRight() elif event.keysym == "Left": MLeft() elif event.keysym == "Up": MFwd() elif event.keysym == "Down": MBwd() elif event.keysym == "KP_1": MotorSelect=1 elif event.keysym == "KP_2": MotorSelect=2 elif event.keysym == "KP_3": MotorSelect=3 elif event.keysym == "KP_4": MotorSelect=4 elif event.keysym == "KP_5": MotorSelect=5 elif event.keysym == "KP_6": MotorSelect=6 elif event.keysym == "KP_7": MotorSelect=7 elif event.keysym == "KP_8": MotorSelect=8 elif event.keysym == "Escape": StopProgram() #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Class for calculating the frequency of the Speed sensors class SpSens(threading.Thread): #Speed recording Thread def __init__(self): threading.Thread.__init__(self) def SpSensL0M0(): global ThingToPrint global SpSensL0M0 global GI0 OneOn=10 LastData=0 EndTime=0 StartTime=0 LastRise=0 print("Start SpSens Thread") while True: #print("DataLoad In = ", DataLoadIn[0]) #print("GI0 = ", GI0) #StartTime=time.time() #Record the start reading time if LastData==0 and GI0==1: #if the last data read was a 1 Rise=time.time() #Record the time when the bit went to 1 LastData=1 #Store the State that was just read Impulsion = Rise-LastRise #calculate the time passed between the last record and the current one if OneOn==0: #Changes the data to print only once on 10 ThingToPrint = Impulsion OneOn=1 #Put the counter back to it's original value LastRise=Rise #Store the actualy read time for the next pass OneOn-=1 #Decrement the reading variable elif LastData==1 and GI0==0: #If the last data read was a 0 LastData=0 #print("Fall") #else: #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX #Create the threads CallPSThread = threading.Thread(target=Reading.Read) print("Create Call PS Thread") CallSPThread = threading.Thread(target=Writing.Write) print("Create Call SP Thread") CallSpSensL0M0 = threading.Thread(target=SpSens.SpSensL0M0) print("Create Call Sensors Thread") #Start the threads CallPSThread.start() print("Start PS Thread") CallSPThread.start() print("Start the Serial -> Parallel Thread") #CallSpSensL0M0.start() print("Start Sensors Thread") # Thread for displaying speed at screen CallPrintScreen = threading.Thread(target=PrintScreen.WriteScreen) print("call Print\n") #CallPrintScreen.start() print("Call Print Start\n") #Create a TK Window root = tk.Tk() print("Reading Keyboard inputs") root.bind_all('', key) #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX def StopProgram(): global GO0 #In1 global GO1 #In2 global GO2 #In3 global GO3 #In4 global GO4 #ENA global GO5 #ENB GO0=0 #In1 GO1=0 #In2 GO2=0 #In3 GO3=0 #In4 GO4=0 #ENA GO5=0 #ENB DataLoadOut = [GO0, GO1, GO2, GO3, GO4, GO5, GO6, GO7, GO8, GO9, GO10, GO11, GO12, GO13, GO14, GO15]; #Tableau des données de sortie CallSpThread.join() print("Stop the Serial -> Parallel Thread") CallPSThread.stop() print("Stop PS Thread") CallSPThread.stop() print("Stop Sensors Thread") CallSpSensL0M0.stop() print("Stop Print Start\n") CallPrintScreen.stop() print("Stop Speed sensor L0M0") CallPSThread.join() CallSpSensL0M0.join() CallPrintScreen.join() CallSpSensL0M0.join() sys.exit()