Pour des paramètres nommés, il semble qu'il faille utiliser des dictionnaires... EDIT : prettyprint !
Je regarde ce que ca donne passé en json...
Je suis arrivé à faire ce que je voulais. Tout est paramétrable.
Voici le code source.
Code : Tout sélectionner
import socket
import os
import json
#Chargement du fichier de propriete
with open('nunchukServer.properties', 'r') as infile:
properties=json.load(infile)
print ''
print '****** Properties ******'
print json.dumps(properties, indent=4, sort_keys=True)
print '******'
#init des gpio : tous en sortie, sens moteur en avant, vitesse a 0
os.system('gpio -g mode %d out' % (properties['motor1PinPwm'],))
os.system('gpio -g mode %d out' % (properties['motor1Pin1'],))
os.system('gpio -g mode %d out' % (properties['motor1Pin2'],))
os.system('gpio -g mode %d out' % (properties['motor2PinPwm'],))
os.system('gpio -g mode %d out' % (properties['motor2Pin1'],))
os.system('gpio -g mode %d out' % (properties['motor2Pin1'],))
os.system('gpio -g write %d 0' % (properties['motor1PinPwm'],))
os.system('gpio -g write %d 0' % (properties['motor1Pin1'],))
os.system('gpio -g write %d 1' % (properties['motor1Pin2'],))
os.system('gpio -g write %d 0' % (properties['motor2PinPwm'],))
os.system('gpio -g write %d 0' % (properties['motor2Pin1'],))
os.system('gpio -g write %d 1' % (properties['motor2Pin2'],))
#chargement des fichiers de configuration vitesse/sens des moteurs
with open('vitesseGauche.json', 'r') as infile:
vitesseGauche=json.load(infile)
with open('vitesseDroite.json', 'r') as infile:
vitesseDroite=json.load(infile)
with open('sensGauche.json', 'r') as infile:
sensGauche=json.load(infile)
with open('sensDroite.json', 'r') as infile:
sensDroite=json.load(infile)
print ''
print '****** Directions and speeds ******'
print vitesseGauche
print "\n".join(["\t".join(map(str, r)) for r in vitesseGauche])
print vitesseDroite
print "\n".join(["\t".join(map(str, r)) for r in vitesseDroite])
print sensGauche
print "\n".join(["\t".join(map(str, r)) for r in sensGauche])
print sensDroite
print "\n".join(["\t".join(map(str, r)) for r in sensDroite])
print '******'
#chargement des fichiers de seuil pour conversion vers index dans fichiers de vitesse/sens des moteurs
with open('seuilsY.json', 'r') as infile:
seuilsY=json.load(infile)
with open('seuilsX.json', 'r') as infile:
seuilsX=json.load(infile)
print ''
print '****** Thresholds ******'
print seuilsX
print seuilsY
print '******'
#init servoblaster
print ''
print '****** Launching Servod... ******'
os.system('sudo killall servod')
os.system('sudo %sservod --p1pins="%s" --min=0us --max=20000us' % (properties['ServodPath'], properties['servodp1pins']))
print '******'
#init socket
connexion_principale = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_principale.bind(('', properties['Port']))
connexion_principale.listen(5)
connexion_avec_client, infos_connexion = connexion_principale.accept()
print ''
print '****** Connected client : ******'
print(infos_connexion)
print '******'
print ''
#Boucle en attente du client.
#Pas de temporisation : recv est bloquant
#Temporisation de 0.1s sur le client
msg_recu = b""
oldConvertedValues = ""
while msg_recu.decode() != "fin":
msg_recu = connexion_avec_client.recv(1024)
#print(msg_recu.decode())
msg_recu = msg_recu.decode()
vals = msg_recu.split(';')
x=int(vals[0])
y=int(vals[1])
#supression de points aberants
if y>230:
y=0
#conversion ordonnee nunchuk en index dans fichiers de vitesses/sens, selon fichier de seuil
found=False
index=0
while index < len(seuilsY) and not(found):
if y<seuilsY[index]:
y=index;
found=True
else:
index=index+1
if not(found):
y=len(seuilsY)
#conversion abscisse
found=False
index=0
while index < len(seuilsX) and not(found):
if x<seuilsX[index]:
x=index;
found=True
else:
index=index+1
if not(found):
x=len(seuilsX)
#impression de controle uniquement si x ou y a change
convertedValues = 'x:%d y:%d' % (x, y)
if convertedValues!=oldConvertedValues:
print 'x:%d y:%d' % (x, y)
oldConvertedValues=convertedValues
#Application du sens de rotation
if sensGauche[y][x]==0:
os.system('gpio -g write %d 0' % (properties['motor1Pin1'],))
os.system('gpio -g write %d 1' % (properties['motor1Pin2'],))
else:
os.system('gpio -g write %d 1' % (properties['motor1Pin1'],))
os.system('gpio -g write %d 0' % (properties['motor1Pin2'],))
if sensDroite[y][x]==0:
os.system('gpio -g write %d 0' % (properties['motor2Pin1'],))
os.system('gpio -g write %d 1' % (properties['motor2Pin2'],))
else:
os.system('gpio -g write %d 1' % (properties['motor2Pin1'],))
os.system('gpio -g write %d 0' % (properties['motor2Pin2'],))
#Application de la vitesse de rotation
os.system('echo 0=%d%% > /dev/servoblaster' % (vitesseGauche[y][x], ))
os.system('echo 1=%d%% > /dev/servoblaster' % (vitesseDroite[y][x], ))
connexion_avec_client.send(b"done.")
print("Fermeture de la connexion")
connexion_avec_client.close()
connexion_principale.close()
Voici l'impression écran au lancement (ça évite de mettre tous les fichiers de conf) :
Code : Tout sélectionner
****** Properties ******
{
"Port": 12800,
"ServodPath": "/home/pi/PiBits/ServoBlaster/user/",
"motor1Pin1": 17,
"motor1Pin2": 18,
"motor1PinPwm": 4,
"motor2Pin1": 22,
"motor2Pin2": 23,
"motor2PinPwm": 27,
"servodp1pins": "7,13"
}
******
****** Directions and speeds ******
[[100, 100, 20], [60, 60, 30], [0, 30, 30], [100, 0, 100], [0, 30, 30], [60, 60, 30], [100, 100, 20]]
100 100 20
60 60 30
0 30 30
100 0 100
0 30 30
60 60 30
100 100 20
[[20, 100, 100], [30, 60, 60], [30, 30, 0], [100, 0, 100], [30, 30, 0], [30, 60, 60], [20, 100, 100]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
******
****** Thresholds ******
[80, 140]
[60, 80, 100, 140, 160, 180]
******
****** Launching Servod... ******
Board revision: 2
Using hardware: PWM
Using DMA channel: 14
Idle timeout: Disabled
Number of servos: 2
Servo cycle time: 20000us
Pulse increment step size: 10us
Minimum width value: 0 (0us)
Maximum width value: 2000 (20000us)
Output levels: Normal
Using P1 pins: 7,13
Using P5 pins:
Servo mapping:
0 on P1-7 GPIO-4
1 on P1-13 GPIO-27
******
****** Connected client : ******
('192.168.0.15', 35475)
******
x:1 y:3
Je trouve cette partie de code un peu moche :
Code : Tout sélectionner
while index < len(seuilsY) and not(found):
if y<seuilsY[index]:
y=index;
found=True
else:
index=index+1
if not(found):
y=len(seuilsY)
Une recherche par dichotomie, ça aurait été mieux...
Q6 : Il y a quelque chose pour faire ça ?
EDIT : voici un bout de code à modifier, pour que dans le second exemple d'appel, la fonction retourne 1 au lieu de None :
Code : Tout sélectionner
def recherche_dans_tableau_trie(x,t):
if len(t) == 0: return(None)
a = 0
b = len(t)-1
while a <= b:
c = int((a+b)/2)
if x == t[c]: return(c)
elif x < t[c]: b = c - 1
else: a = c + 1
return None
print recherche_dans_tableau_trie(2,[0,1,2,3,4])
print recherche_dans_tableau_trie(2,[0,1,3,4])
print recherche_dans_tableau_trie(2,[])
Q7 : j'imprime les valeurs converties dès qu'elles changent à l'aide de ce bout de code :
Code : Tout sélectionner
#impression de controle uniquement si x ou y a change
convertedValues = 'x:%d y:%d' % (x, y)
if convertedValues!=oldConvertedValues:
print 'x:%d y:%d' % (x, y)
oldConvertedValues=convertedValues
Plutôt que d'imprimer ça de ligne en ligne, je voudrai remplacer la dernière ligne de console.
C'est possible ?
EDIT : il suffit de ne faire qu'un chr(13) et pas de chr(10). Plutôt qu'un loing discourt, démo :
Code : Tout sélectionner
import sys, time
for i in range(1, 6):
sys.stdout.write (str(i) + chr(13))
sys.stdout.flush()
time.sleep (1)