Best Python code snippet using localstack_python
modeThreads.py
Source:modeThreads.py
1# -*- coding: utf-8 -*-2"""3Created on Sat Dec 07 11:28:11 20194@author: Anaïs, Nicolas5"""6import time7import can8from threading import Thread, Lock9import sharedRessources as R10MOT=0x010 #identifiant commande moteur CAN11CMD_STOP = 0x0012CMD_V_BACK = 35 + 0x8013CMD_V_MIN = 5014CMD_V_SLOW = 65 + 0x8015CMD_V_TURBO = 75 + 0x8016CMD_O_LEFT = 0x8017CMD_O_RIGHT = 100 + 0x8018CMD_O_MIN = 5019#for auto only20REAL_SIGN_WIDTH = 0.20 #nb à déterminer en cm21FOCAL = 34222#thread emergency+modeThread23"""24emergencyOn = False25lockEmergencyOn = Lock()26"""27class ModeThread(Thread):28 def __init__(self, bus,intModeL):29 Thread.__init__(self)30 self.bus = bus31 self.intModeL = intModeL32 33 def sendMesgToMot(self,cmdV,cmdO):34 msg = can.Message(arbitration_id=MOT,data=[cmdV, cmdV, cmdO,0,0,0,0,0],extended_id=False)35 time.sleep(0.01)36 self.bus.send(msg)37 38 39class ModeAutoThread (ModeThread):40 def __init__(self, bus):41 ModeThread.__init__(self,bus,2)42 43 def run(self):44 self.sendMesgToMot(CMD_V_SLOW,CMD_STOP) 45 currentModeL=self.intModeL46 counterStopOn = False47 finalValueStopCounter = 048 counterStop=049 50 while (currentModeL==self.intModeL):51 # Emergency stop block52 R.lockEmergencyOn.acquire()53 stateEmergency = R.emergencyOn54 R.lockEmergencyOn.release()55 56 if(stateEmergency):57 R.modeLock.acquire()58 mode = "assist"59 R.modeLock.release()60 61 # Stop sign block62 63 if(counterStopOn):64 if(counterStop<finalValueStopCounter):65 counterStop+=0.166 else:67 self.sendMesgToMot(CMD_STOP,CMD_STOP) 68 time.sleep(4)69 self.sendMesgToMot(CMD_V_SLOW,CMD_STOP) 70 counterStop=071 finalValueStopCounter=072 counterStopOn = False73 74 else: 75 R.lockWidthStop.acquire() 76 widthStopSign = R.widthStop 77 R.lockWidthStop.release()78 if(widthStopSign):79 toSignDistance = (REAL_SIGN_WIDTH*FOCAL)/widthStopSign80 R.lockWheelSpeed.acquire()81 speedC= R.wheelSpeed82 R.lockWheelSpeed.release()83 if speedC >= 0.14 :84 finalValueStopCounter = (toSignDistance / speedC)-3 #calcul du temps à attendre, 1.2 => 100 pour la vitesse avant -1 pour la reconnaissance85 counterStopOn=True86 87 88 R.lockModeLaunched.acquire()89 currentModeL = R.modeLaunched90 R.lockModeLaunched.release()91 time.sleep(0.1)92 93 self.sendMesgToMot(CMD_STOP,CMD_STOP) 94 95class ModeAssistThread (ModeThread):96 def __init__(self, bus):97 ModeThread.__init__(self,bus,1)98 99 def run(self):100 self.sendMesgToMot(CMD_STOP,CMD_STOP) 101 currentModeL=self.intModeL102 cmdV = CMD_STOP103 cmdO = CMD_STOP104 R.turboLock.acquire()105 R.turbo ="off"106 R.turboLock.release()107 R.joystickLock.acquire()108 R.joystick = "none"109 R.joystickLock.release()110 R.stateLock.acquire()111 R.state = "off"112 R.stateLock.release()113 114 while (currentModeL==self.intModeL):115 R.stateLock.acquire() 116 currentState = R.state 117 R.stateLock.release()118 if(currentState=="on"):119 R.joystickLock.acquire() 120 currentJoystick = R.joystick 121 R.joystickLock.release()122 if(currentJoystick =="none"):123 cmdO = CMD_O_MIN124 cmdV = CMD_V_MIN 125 elif(currentJoystick=="right"):126 cmdO = CMD_O_RIGHT127 cmdV= CMD_V_MIN 128 elif(currentJoystick=="left"):129 cmdO = CMD_O_LEFT130 cmdV = CMD_V_MIN 131 elif(currentJoystick=="front"):132 R.turboLock.acquire() 133 currentTurbo = R.turbo 134 R.turboLock.release()135 if(currentTurbo=="on"):136 cmdV=CMD_V_TURBO137 else:138 cmdV= CMD_V_SLOW139 cmdO = CMD_O_MIN 140 141 elif(currentJoystick=="front&right"):142 cmdO = CMD_O_RIGHT143 cmdV= CMD_V_SLOW144 elif(currentJoystick=="front&left"):145 cmdO = CMD_O_LEFT146 cmdV= CMD_V_SLOW147 148 elif(currentJoystick=="back"):149 cmdO = CMD_O_MIN150 cmdV= CMD_V_BACK151 152 elif(currentJoystick=="back&right"):153 cmdO = CMD_O_RIGHT154 cmdV= CMD_V_BACK155 156 elif(currentJoystick=="back&left"):157 cmdO = CMD_O_LEFT158 cmdV= CMD_V_BACK159 self.sendMesgToMot(cmdV,cmdO)160 161 162 else:163 self.sendMesgToMot(CMD_STOP,CMD_STOP)164 165 166 R.lockModeLaunched.acquire()167 currentModeL = R.modeLaunched168 R.lockModeLaunched.release()169 time.sleep(0.1)170 ...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!