forked from tanchou/Verilog
ultrasonic commands commencer et tester mais non fonctionnel donc début de testbench pour pouvoir debuguer
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
import serial
|
||||
|
||||
# === Configuration ===
|
||||
PORT = 'COM7' # Remplace par le port série de ton FPGA (ex: '/dev/ttyUSB0' sur Linux)
|
||||
BAUDRATE = 115200 # À adapter selon ton uart_tx_fifo
|
||||
TIMEOUT = 1 # En secondes
|
||||
|
||||
# === Connexion série ===
|
||||
ser = serial.Serial(PORT, BAUDRATE, timeout=TIMEOUT)
|
||||
print(f"Ouvert sur {PORT} à {BAUDRATE} bauds.")
|
||||
|
||||
try:
|
||||
while True:
|
||||
data = ser.read(1) # Lire 1 octet
|
||||
if data:
|
||||
value = int.from_bytes(data, byteorder='little')
|
||||
print(f"Distance mesurée : {value} cm")
|
||||
except KeyboardInterrupt:
|
||||
print("\nArrêté par l'utilisateur.")
|
||||
finally:
|
||||
ser.close()
|
@@ -0,0 +1,68 @@
|
||||
import serial
|
||||
import time
|
||||
import struct
|
||||
|
||||
# === Paramètres de communication ===
|
||||
SERIAL_PORT = "COM6" # Modifie selon ton système, ex. "/dev/ttyUSB0" sur Linux
|
||||
BAUDRATE = 115200 # Change si différent
|
||||
TIMEOUT = 2 # secondes
|
||||
|
||||
# === Commandes (doivent correspondre aux valeurs Verilog) ===
|
||||
CMD_STOP = 3
|
||||
CMD_ONE = 1
|
||||
CMD_CONTINUOUS = 2
|
||||
|
||||
def send_command(ser, command):
|
||||
"""Envoie une commande au FPGA"""
|
||||
ser.write(bytes([command]))
|
||||
|
||||
def read_distance(ser):
|
||||
"""Lit 2 octets et les convertit en distance (int16)"""
|
||||
data = ser.read(2)
|
||||
if len(data) != 2:
|
||||
return None
|
||||
# Interprétation little-endian (LSB, MSB)
|
||||
lsb, msb = data[0], data[1]
|
||||
distance = msb << 8 | lsb
|
||||
return distance
|
||||
|
||||
def main():
|
||||
with serial.Serial(SERIAL_PORT, BAUDRATE, timeout=TIMEOUT) as ser:
|
||||
print("Connexion ouverte sur", SERIAL_PORT)
|
||||
|
||||
mode = input("Mode (one / continuous / stop) ? ").strip().lower()
|
||||
|
||||
if mode == "one":
|
||||
send_command(ser, CMD_ONE)
|
||||
print("Mesure unique demandée. Attente résultat...")
|
||||
time.sleep(0.05)
|
||||
distance = read_distance(ser)
|
||||
if distance is not None:
|
||||
print(f"Distance mesurée : {distance} cm")
|
||||
else:
|
||||
print("Erreur : distance non reçue.")
|
||||
|
||||
elif mode == "continuous":
|
||||
send_command(ser, CMD_CONTINUOUS)
|
||||
print("Mesures continues (CTRL+C pour stopper) :")
|
||||
try:
|
||||
while True:
|
||||
distance = read_distance(ser)
|
||||
if distance is not None:
|
||||
print(f"Distance : {distance} cm")
|
||||
else:
|
||||
print("... (aucune donnée reçue)")
|
||||
time.sleep(0.1)
|
||||
except KeyboardInterrupt:
|
||||
send_command(ser, CMD_STOP)
|
||||
print("\nMesure continue arrêtée.")
|
||||
|
||||
elif mode == "stop":
|
||||
send_command(ser, CMD_STOP)
|
||||
print("Commande STOP envoyée.")
|
||||
|
||||
else:
|
||||
print("Commande invalide.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user