313 lines
7.4 KiB
Python
313 lines
7.4 KiB
Python
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
|
|
absy = -1
|
|
|
|
absrx = -1
|
|
absry = -1
|
|
|
|
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 event.code =="ABS_Y":
|
|
absy = raffine_entree(event.state)
|
|
if event.code =="ABS_RX":
|
|
absrx = raffine_entree(event.state)
|
|
if event.code =="ABS_RY":
|
|
absry = raffine_entree(event.state)
|
|
|
|
|
|
if abs(absx) < DEAD_ZONE and abs(absy) < DEAD_ZONE and abs(absrx) < DEAD_ZONE :
|
|
neutral = 3
|
|
|
|
action_voulue = -1
|
|
if abs(absx) > DEAD_ZONE :
|
|
if absx < -80 :
|
|
action_voulue = 13
|
|
elif absx > 80 :
|
|
action_voulue = 14
|
|
|
|
if abs(absy) > DEAD_ZONE :
|
|
if absy < -80 :
|
|
action_voulue = 2
|
|
elif absy > 80 :
|
|
action_voulue = 7
|
|
|
|
if abs(absx) > DEAD_ZONE and abs(absy) > DEAD_ZONE :
|
|
print("absy : {}".format(absx))
|
|
print("absx : {}".format(absy))
|
|
if (absx <0 and absx < -40) and (absy <0 and absy < -40) :
|
|
action_voulue = 5
|
|
elif (absx >0 and absx > 40) and (absy <0 and absy < -40) :
|
|
action_voulue = 6
|
|
elif (absx >0 and absx > 40) and (absy >0 and absy > 40) :
|
|
action_voulue = 11
|
|
elif (absx <0 and absx < -40) and (absy >0 and absy > 40) :
|
|
action_voulue = 10
|
|
|
|
if abs(absrx) > DEAD_ZONE :
|
|
if absrx < -DEAD_ZONE :
|
|
action_voulue = 15
|
|
elif absrx > DEAD_ZONE :
|
|
action_voulue = 16
|
|
|
|
if abs(absrx) > DEAD_ZONE and abs(absy) > DEAD_ZONE :
|
|
print("absy : {}".format(absx))
|
|
print("absx : {}".format(absy))
|
|
if (absrx <0 and absrx < -DEAD_ZONE) and (absy <0 and absy < -40) :
|
|
action_voulue = 3
|
|
elif (absrx >0 and absrx > DEAD_ZONE) and (absy <0 and absy < -40) :
|
|
action_voulue = 4
|
|
elif (absrx >0 and absrx > DEAD_ZONE) and (absy >0 and absy > 40) :
|
|
action_voulue = 9
|
|
elif (absrx <0 and absrx < -DEAD_ZONE) and (absy >0 and absy > 40) :
|
|
action_voulue = 8
|
|
|
|
if neutral == 3 :
|
|
action_voulue = 1
|
|
|
|
if action_voulue > -1 :
|
|
actions[action_voulue]()
|
|
|