427 lines
10 KiB
Python
427 lines
10 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
|
|
actions_a_faire = []
|
|
|
|
def getProcess(a_dire):
|
|
return subprocess.Popen(["espeak",
|
|
"-v",
|
|
"mb/mb-fr1",
|
|
"-s",
|
|
"155",
|
|
a_dire
|
|
], stdout=subprocess.PIPE)
|
|
|
|
def dis_phrase_thread():
|
|
global continue_thread
|
|
global phrase
|
|
global actions_a_faire
|
|
prev_phrase = ""
|
|
while (continue_thread == 1) :
|
|
lock.acquire()
|
|
actions_a_faire2 = actions_a_faire.copy()
|
|
actions_a_faire.clear()
|
|
# Énoncer le déplacement
|
|
if phrase is not "" :
|
|
a_dire = phrase
|
|
if phrase is prev_phrase :
|
|
if not (phrase == "STOP !") :
|
|
a_dire = "Continue le déplacement !"
|
|
else :
|
|
a_dire = ""
|
|
if a_dire is not "" :
|
|
process = getProcess(a_dire)
|
|
lock.release()
|
|
prev_phrase = phrase
|
|
output, error = process.communicate()
|
|
else:
|
|
lock.release()
|
|
else :
|
|
lock.release()
|
|
# Énoncer les actions à faire, pas besoin de lock ici puisque l'on
|
|
# utilise une copie locale d'actions à faire
|
|
if len(actions_a_faire2)>0 :
|
|
getProcess("Actions !").communicate()
|
|
for action_a_faire in actions_a_faire2 :
|
|
process = getProcess(action_a_faire)
|
|
process.communicate()
|
|
|
|
def ajoute_action(action) :
|
|
global actions_a_faire
|
|
lock.acquire()
|
|
if action not in actions_a_faire :
|
|
actions_a_faire.append(action)
|
|
lock.release()
|
|
|
|
# propose la phrase à dire au thread qui parle
|
|
def dis_phrase(phrase1):
|
|
global phrase
|
|
lock.acquire()
|
|
phrase = 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
|
|
ajoute_action("Jusqu'à nouvel ordre, déplace toi accroupi !")
|
|
|
|
def releve_toi():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Jusqu'à nouvel ordre, déplace toi debout !")
|
|
|
|
def tends_bras_droit():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Jusqu'à nouvel ordre, tends le bras droit !")
|
|
|
|
def tends_bras_gauche():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Jusqu'à nouvel ordre, tends le bras gauche !")
|
|
|
|
def detends_bras_droit():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Jusqu'à nouvel ordre, détends le bras droit !")
|
|
|
|
def detends_bras_gauche():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Jusqu'à nouvel ordre, détends le bras gauche !")
|
|
|
|
def attrape():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Attrape !")
|
|
|
|
def lache():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Lâche !")
|
|
|
|
def saute():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Saute !")
|
|
|
|
def coup_pied_gauche():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("Coup de pied gauche !")
|
|
|
|
def coup_pied_droit():
|
|
global est_stop
|
|
est_stop = 0
|
|
ajoute_action("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_L3 = -1
|
|
L3 = -1
|
|
accroupi = 0
|
|
|
|
prev_X = -1
|
|
X = -1
|
|
attrape = 0
|
|
|
|
prev_TL = -1
|
|
TL = -1
|
|
pied_gauche= 0
|
|
|
|
prev_TR = -1
|
|
TR = -1
|
|
pied_droit = 0
|
|
|
|
prev_A = -1
|
|
A = -1
|
|
|
|
prev_brase_gauche = -1
|
|
bras_gauche = -1
|
|
bras_gauche_tendu = 0
|
|
|
|
prev_brase_droit = -1
|
|
bras_droit = -1
|
|
bras_droit_tendu = 0
|
|
|
|
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 event.code == "BTN_THUMBL" :
|
|
L3 = event.state
|
|
if event.code =="ABS_Z":
|
|
bras_gauche = event.state
|
|
if event.code =="ABS_RZ":
|
|
bras_droit = event.state
|
|
if event.code == "BTN_NORTH" :
|
|
X = event.state
|
|
if event.code == "BTN_SOUTH" :
|
|
A = event.state
|
|
if event.code == "BTN_TL" :
|
|
TL = event.state
|
|
if event.code == "BTN_TR" :
|
|
TR = event.state
|
|
|
|
if abs(absx) < DEAD_ZONE and abs(absy) < DEAD_ZONE and abs(absrx) < DEAD_ZONE :
|
|
neutral = 3
|
|
|
|
# Traitement des déplacements
|
|
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 :
|
|
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 :
|
|
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]()
|
|
|
|
# Traitements des actions à faire
|
|
|
|
if L3 > -1 and not L3 == prev_L3 :
|
|
if L3 == 1 :
|
|
if accroupi == 0 :
|
|
accroupi = 1
|
|
actions[17]()
|
|
else :
|
|
accroupi = 0
|
|
actions[18]()
|
|
prev_L3 = L3
|
|
|
|
if bras_gauche > -1 :
|
|
if bras_gauche > 200 :
|
|
if bras_gauche_tendu == 0 :
|
|
bras_gauche_tendu = 1
|
|
actions[20]()
|
|
elif bras_gauche < 30 :
|
|
if bras_gauche_tendu == 1 :
|
|
bras_gauche_tendu = 0
|
|
actions[22]()
|
|
|
|
if bras_droit > -1 :
|
|
if bras_droit > 200 :
|
|
if bras_droit_tendu == 0 :
|
|
bras_droit_tendu = 1
|
|
actions[19]()
|
|
elif bras_droit < 30 :
|
|
if bras_droit_tendu == 1 :
|
|
bras_droit_tendu = 0
|
|
actions[21]()
|
|
|
|
if X > -1 and not X == prev_X :
|
|
if X == 1 :
|
|
if attrape == 0 :
|
|
attrape = 1
|
|
actions[23]()
|
|
else :
|
|
attrape = 0
|
|
actions[24]()
|
|
prev_X = X
|
|
|
|
if A > -1 and not A == prev_A :
|
|
if A == 1 :
|
|
actions[25]()
|
|
prev_A = A
|
|
|
|
if TL > -1 and not TL == prev_A :
|
|
if TL == 1 :
|
|
actions[26]()
|
|
prev_A = TL
|
|
|
|
if TR > -1 and not TR == prev_A :
|
|
if TR == 1 :
|
|
actions[27]()
|
|
prev_A = TR
|