Premier commit
il est capable de parler De donner les indication aller tout droit/arrière ou aller à gauche/droite Reste à faire les diagonnales pour compléter le premier joystick
This commit is contained in:
290
control.py
Normal file
290
control.py
Normal file
@@ -0,0 +1,290 @@
|
||||
import subprocess
|
||||
import inputs
|
||||
import threading
|
||||
|
||||
MAX_VAL=32767
|
||||
DEAD_ZONE = 20
|
||||
SENSIBILITY = 2
|
||||
lock = threading.Lock()
|
||||
phrase = "";
|
||||
continue_thread = 1
|
||||
est_stop = 0
|
||||
|
||||
def dis_phrase_thread():
|
||||
global continue_thread
|
||||
global phrase
|
||||
prev_phrase = ""
|
||||
while (continue_thread == 1) :
|
||||
lock.acquire()
|
||||
if phrase is not "" :
|
||||
a_dire = phrase
|
||||
if phrase is prev_phrase :
|
||||
if not (phrase == "STOP !") :
|
||||
a_dire = "Continue !"
|
||||
else :
|
||||
a_dire = ""
|
||||
if a_dire is not "" :
|
||||
process = subprocess.Popen(["espeak",
|
||||
"-v",
|
||||
"mb/mb-fr1",
|
||||
"-s",
|
||||
"120",
|
||||
a_dire
|
||||
], stdout=subprocess.PIPE)
|
||||
lock.release()
|
||||
prev_phrase = phrase
|
||||
output, error = process.communicate()
|
||||
else:
|
||||
lock.release()
|
||||
else :
|
||||
lock.release()
|
||||
|
||||
# propose la phrase à dire au thread qui parle
|
||||
def dis_phrase(phrase1):
|
||||
global phrase
|
||||
lock.acquire()
|
||||
phrase = phrase1
|
||||
print("propose {}".format(phrase1))
|
||||
lock.release()
|
||||
|
||||
# raffinerie des entrées
|
||||
|
||||
def raffine_entree(entree):
|
||||
val = int((entree *100)/32767)
|
||||
return val
|
||||
|
||||
# Contrôle génériques
|
||||
|
||||
def stop():
|
||||
global est_stop
|
||||
if est_stop == 0 :
|
||||
dis_phrase("STOP !")
|
||||
est_stop = 1
|
||||
|
||||
def encore():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Continue !")
|
||||
|
||||
# Déplacements
|
||||
|
||||
def avance_droit():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Avance tout droit !")
|
||||
|
||||
def avance_en_tournant_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Avance en tournant à gauche !")
|
||||
|
||||
def avance_en_tournant_droite():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Avance en tournant à droite !")
|
||||
|
||||
def avance_diag_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Avance en diagonale gauche !")
|
||||
|
||||
def avance_diag_droite():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Avance en diagonale droite !")
|
||||
|
||||
def recule_droit():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Recule tout droit !")
|
||||
|
||||
def recule_en_tournant_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Recule en tournant à gauche !")
|
||||
|
||||
def recule_en_tournant_droite():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Recule en tournant à droite !")
|
||||
|
||||
def recule_diag_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Recule en diagonale gauche !")
|
||||
|
||||
def recule_diag_droite():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Recule en diagonale droite !")
|
||||
|
||||
def pas_cote_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Pas de côté vers la gauche !")
|
||||
|
||||
def pas_cote_droit():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Pas de côté vers la droite !")
|
||||
|
||||
# Orientation
|
||||
|
||||
def tourne_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Oriente toi vers la gauche !")
|
||||
|
||||
def tourne_droite():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Oriente toi vers la droite !")
|
||||
|
||||
# Actions
|
||||
|
||||
def accroupis_toi():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Jusqu'à nouvel ordre, déplace toi accroupi !")
|
||||
|
||||
def releve_toi():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Jusqu'à nouvel ordre, déplace toi debout !")
|
||||
|
||||
def tends_bras_droit():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Jusqu'à nouvel ordre, tends le bras droit !")
|
||||
|
||||
def tends_bras_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Jusqu'à nouvel ordre, tends le bras gauche !")
|
||||
|
||||
def detends_bras_droit():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Jusqu'à nouvel ordre, détends le bras droit !")
|
||||
|
||||
def detends_bras_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Jusqu'à nouvel ordre, détends le bras gauche !")
|
||||
|
||||
def attrape():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Attrape !")
|
||||
|
||||
def lache():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Lâche !")
|
||||
|
||||
def saute():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Saute !")
|
||||
|
||||
def coup_pied_gauche():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Coup de pied gauche !")
|
||||
|
||||
def coup_pied_droit():
|
||||
global est_stop
|
||||
est_stop = 0
|
||||
dis_phrase("Coup de pied droit !")
|
||||
|
||||
print(inputs.devices.gamepads)
|
||||
|
||||
actions = {
|
||||
0 : encore,
|
||||
1 : stop,
|
||||
2 : avance_droit,
|
||||
3 : avance_en_tournant_gauche,
|
||||
4 : avance_en_tournant_droite,
|
||||
5 : avance_diag_gauche,
|
||||
6 : avance_diag_droite,
|
||||
7 : recule_droit,
|
||||
8 : recule_en_tournant_gauche,
|
||||
9 : recule_en_tournant_droite,
|
||||
10 : recule_diag_gauche,
|
||||
11 : recule_diag_droite,
|
||||
13 : pas_cote_gauche,
|
||||
14 : pas_cote_droit,
|
||||
15 : tourne_gauche,
|
||||
16 : tourne_droite,
|
||||
17 : accroupis_toi,
|
||||
18 : releve_toi,
|
||||
19 : tends_bras_droit,
|
||||
20 : tends_bras_gauche,
|
||||
21 : detends_bras_droit,
|
||||
22 : detends_bras_gauche,
|
||||
23 : attrape,
|
||||
24 : lache,
|
||||
25 : saute,
|
||||
26 : coup_pied_gauche,
|
||||
27 : coup_pied_droit,
|
||||
}
|
||||
|
||||
|
||||
combo_actuel = -1
|
||||
combo_precedent = -1
|
||||
|
||||
t1 = threading.Thread(target=dis_phrase_thread)
|
||||
t1.start()
|
||||
|
||||
absx = -1
|
||||
prev_absx = -1
|
||||
state_absx=False
|
||||
absy = -1
|
||||
prev_absy = -1
|
||||
state_absy=False
|
||||
|
||||
prev_action = -1
|
||||
|
||||
while (True) :
|
||||
neutral = 0
|
||||
events = inputs.get_gamepad()
|
||||
for event in events:
|
||||
if event.code =="ABS_X":
|
||||
absx = raffine_entree(event.state)
|
||||
if (absx > 0 and absx>DEAD_ZONE) or (absx <0 and absx<-DEAD_ZONE):
|
||||
state_absx = True
|
||||
if event.code =="ABS_Y":
|
||||
absy = raffine_entree(event.state)
|
||||
if (absy > 0 and absy>DEAD_ZONE) or (absy <0 and absy<-DEAD_ZONE):
|
||||
state_absy = True
|
||||
|
||||
if abs(absx) < DEAD_ZONE and abs(absy) < DEAD_ZONE :
|
||||
neutral = 2
|
||||
|
||||
action_voulue = -1
|
||||
if state_absx == True :
|
||||
if absx < -80 :
|
||||
action_voulue = 13
|
||||
elif absx > 80 :
|
||||
action_voulue = 14
|
||||
#print("absx : {}".format(absx))
|
||||
state_absx = False
|
||||
|
||||
if state_absy == True :
|
||||
if absy < -80 :
|
||||
action_voulue = 2
|
||||
elif absy > 80 :
|
||||
action_voulue = 7
|
||||
#print("absy : {}".format(absx))
|
||||
state_absy = False
|
||||
|
||||
|
||||
if neutral == 2 :
|
||||
action_voulue = 1
|
||||
|
||||
if action_voulue > -1 :
|
||||
actions[action_voulue]()
|
||||
|
||||
|
||||
prev_absx = absx
|
||||
|
||||
Reference in New Issue
Block a user