mirror of
https://forge.apps.education.fr/blender-edutech/ropy.git
synced 2024-01-27 08:23:20 +01:00
Détection automatique de la carte sur le port série : microbit, uno et mega
This commit is contained in:
parent
526c9011bf
commit
b7a2f6f050
BIN
ropy-31.blend
BIN
ropy-31.blend
Binary file not shown.
@ -8,8 +8,8 @@
|
||||
<worldPosition.z>20.22315788269043</worldPosition.z>
|
||||
</cam>
|
||||
<screen>
|
||||
<width>960</width>
|
||||
<height>540</height>
|
||||
<width>1280</width>
|
||||
<height>720</height>
|
||||
</screen>
|
||||
</config>
|
||||
<mission>
|
||||
|
@ -77,7 +77,7 @@ card_description.update({"radar-card" : [rp_radar_title, rp_radar_text, rp_radar
|
||||
# Jumeau numérique
|
||||
rp_twins_title="Jumeau numérique"
|
||||
rp_twins_text=""" Via une liaison série, Ropy peut être \n le jumeau numérique d'un robot réel. \n
|
||||
rp_jumeau(port, vitesse) \n -> Active le jumeau réel par la liaison \n série (port et vitesse en baud) \n
|
||||
rp_jumeau(port, vitesse=115200) \n -> Active le jumeau réel par la liaison \n série. Si le port n'est pas spécifié, il \n est recherché automatiquement. \n
|
||||
rp_serie_msg(texte) \n -> Envoi un message \n \n texte=rp_serie_rcpt() \n -> Reçoit un message"""
|
||||
|
||||
# Message envoyé (asynchrone) : \n avancer : a, reculer : r, droite : d, \n gauche g, marquer : m et forer : f \n\n\n """
|
||||
|
106
rp_lib.py
106
rp_lib.py
@ -9,6 +9,9 @@ import math
|
||||
import mathutils
|
||||
import random
|
||||
import serial # Liaison série (jumeau numérique)
|
||||
from serial.tools.list_ports import comports
|
||||
|
||||
# import serial.tools.list_ports.comports as list_com_ports
|
||||
|
||||
import rp_map1 as rp_map # Map definition
|
||||
|
||||
@ -42,6 +45,10 @@ debug_thread = scene.objects['Terrain']['debug_thread']
|
||||
# Jumeau numérique
|
||||
twins_serial = None
|
||||
|
||||
# CH340 : arduino Uno
|
||||
# MICROBITPID = 516
|
||||
# MICROBITVID = 3368
|
||||
|
||||
# UPBGE constants
|
||||
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
|
||||
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
|
||||
@ -1110,42 +1117,96 @@ def rp_tempo (duration):
|
||||
# Jumeau numérique
|
||||
###############################################################################
|
||||
|
||||
# Paramétrage de la communication avec la carte de communication (Arduino, Micro:bit)
|
||||
##
|
||||
# Activation de la communication avec la carte de communication (Arduino, Micro:bit)
|
||||
# Vitesse : 115200 -> 7 fps, 38400 -> 6 fps, 9600 -> 2 fps
|
||||
##
|
||||
|
||||
def rp_jumeau(port, speed):
|
||||
def rp_jumeau(port="auto", speed=115200):
|
||||
global twins_serial
|
||||
scene.objects['Points-Twins'].setVisible(True,True)
|
||||
scene.objects['Points-Twins-text'].setVisible(True,False)
|
||||
twins_serial = getSerialOrNone(port,speed)
|
||||
|
||||
# Recherche automatique du port
|
||||
if port=="auto" or port=="microbit" or port=="uno" or port=="mega":
|
||||
[device,board] =serial_autoget_port(port)
|
||||
else:
|
||||
device = port
|
||||
board=""
|
||||
|
||||
# Mise en place de la communication
|
||||
twins_serial = serial_getSerialOrNone(device,speed)
|
||||
if twins_serial is not None:
|
||||
# twins_serial.set_buffer_size(rx_size = 12800, tx_size = 12800)
|
||||
scene.objects['Commands']['twins'] = True
|
||||
scene.objects['Commands']['twins_close'] = False
|
||||
scene.objects['Commands']['twins_port'] = port
|
||||
scene.objects['Commands']['twins_port'] = device
|
||||
scene.objects['Commands']['twins_speed'] = speed
|
||||
scene.objects['Commands']['twins_readline'] = ""
|
||||
scene.objects['Points-Twins-text']['Text'] = "Connection ouverte :\n"+port+" - "+str(speed)+" baud"
|
||||
if board =="":
|
||||
scene.objects['Points-Twins-text']['Text'] = "Connection ouverte :\n"+device+" - "+str(speed)+" baud"
|
||||
else:
|
||||
scene.objects['Points-Twins-text']['Text'] = "Connection ouverte :\n"+board+"\n"+device+" - "+str(speed)+" baud"
|
||||
rp_tempo (0.1)
|
||||
print (twins_serial)
|
||||
else:
|
||||
scene.objects['Commands']['twins'] = False
|
||||
scene.objects['Points-Twins-text']['Text'] = "Port "+port+" pas prêt"
|
||||
scene.objects['Points-Twins-text']['Text'] = "Port "+device+" pas prêt"
|
||||
|
||||
# # ser = serial.Serial('/dev/ttyACM0',38400) # 6 fps
|
||||
# # ser = serial.Serial('/dev/ttyACM0',9600) # 2 fps
|
||||
# #serial_port='COM5' # Windows
|
||||
# # serial_port='/dev/ttyACM0' # GNU/Linux
|
||||
# # 115200 -> 7 fps
|
||||
# # 38400 -> 6 fps
|
||||
# # 9600 -> 2 fps
|
||||
##
|
||||
# Recherche automatique du port
|
||||
##
|
||||
|
||||
def getSerialOrNone(port,speed):
|
||||
def serial_autoget_port(port):
|
||||
# USB Vendor ID, USB Product ID
|
||||
board={'microbit' :[3368, 516],
|
||||
'uno' :[9025, 67],
|
||||
'mega' :[9025, 66]}
|
||||
if port=="auto" or port=="microbit":
|
||||
for com in comports():
|
||||
if com.vid == board["microbit"][0] and com.pid == board["microbit"][1]:
|
||||
return [com.device,"Micro:bit"]
|
||||
if port=="auto" or port=="uno":
|
||||
for com in comports():
|
||||
if com.vid == board["uno"][0] and com.pid == board["uno"][1]:
|
||||
return [com.device,"Arduino Uno"]
|
||||
if port=="auto" or port=="mega":
|
||||
for com in comports():
|
||||
if com.vid == board["mega"][0] and com.pid == board["mega"][1]:
|
||||
return [com.device,"Arduino Mega"]
|
||||
return None
|
||||
|
||||
##
|
||||
# Affiche la liste des ports (communication série)
|
||||
##
|
||||
|
||||
def rp_serie_ports():
|
||||
for com in comports():
|
||||
print ("Name : "+str(com.name)+"\n"
|
||||
+" Device : "+str(com.device)+"\n"
|
||||
+" Hardware ID : "+str(com.hwid)+"\n"
|
||||
+" USB Vendor ID : "+str(com.vid)+"\n"
|
||||
+" USB Product ID : "+str(com.pid)+"\n"
|
||||
+" USB device location : "+str(com.location)+"\n"
|
||||
+" USB manufacturer : "+str(com.manufacturer)+"\n"
|
||||
+" USB product : "+str(com.product)+"\n"
|
||||
+" Interface-specific : "+str(com.interface))
|
||||
|
||||
##
|
||||
# Création de l'objet serial (communication série)
|
||||
##
|
||||
|
||||
def serial_getSerialOrNone(port,speed):
|
||||
try:
|
||||
# return serial.Serial(port,speed, bytesize=100)
|
||||
return serial.Serial(port,speed)
|
||||
except:
|
||||
return None
|
||||
|
||||
##
|
||||
# Fermeture de la communication série
|
||||
##
|
||||
|
||||
def rp_jumeau_close():
|
||||
global twins_serial
|
||||
twins_serial.close() # Fermer proprement le port série
|
||||
@ -1172,18 +1233,27 @@ def rp_jumeau_config(speed, temps_avancer, temps_tourner):
|
||||
serial_msg5 = "FC\n"
|
||||
twins_serial.write(serial_msg5.encode())
|
||||
|
||||
##
|
||||
# Envoi d'un message vers la communication série
|
||||
##
|
||||
|
||||
def rp_serie_msg(text):
|
||||
global twins_serial
|
||||
text2= text+"\n"
|
||||
scene.objects['Points-Twins-text']['Text'] = "Communication ...\nEnvoi message : "+text
|
||||
twins_serial.write(text2.encode())
|
||||
|
||||
##
|
||||
# Mise en écoute de jumeau numérique (figeage de la scène)
|
||||
##
|
||||
|
||||
def twins_listen(cont):
|
||||
global twins_serial
|
||||
if scene.objects['Commands']['twins']:
|
||||
if scene.objects['Commands']['twins_readline'] != "":
|
||||
scene.objects['Points-Twins-text']['Text'] = "Écoute de la connection\nfigeage de la scène\nMessage reçu : "+scene.objects['Commands']['twins_readline']
|
||||
scene.objects['Points-Twins-text']['Text'] = "Écoute de la connection\nfigeage de la scène...\nMessage reçu : "+scene.objects['Commands']['twins_readline']
|
||||
else:
|
||||
scene.objects['Points-Twins-text']['Text'] = "Écoute de la connection\nfigeage de la scène"
|
||||
scene.objects['Points-Twins-text']['Text'] = "Écoute de la connection\nfigeage de la scène..."
|
||||
if cont.sensors['Property'].positive:
|
||||
if scene.objects['Commands']['twins_listen'] :
|
||||
serial_msg = twins_serial.readline()
|
||||
@ -1192,6 +1262,10 @@ def twins_listen(cont):
|
||||
# scene.objects['Points-Twins-text']['Text'] = "Message reçu : "+str(serial_msg)
|
||||
scene.objects['Commands']['twins_listen'] = False
|
||||
|
||||
##
|
||||
# Réception d'un message de la communication série
|
||||
##
|
||||
|
||||
def rp_serie_rcpt():
|
||||
# scene.objects['Points-Twins-text']['Text'] = "Écoute de la \nconnection\n figeage de \n la scène"
|
||||
scene.objects['Commands']['twins_readline'] = ""
|
||||
|
@ -38,8 +38,6 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
||||
|
||||
def init():
|
||||
|
||||
print ("init")
|
||||
|
||||
# Mémorisation de la position de la tablette du store
|
||||
scene.objects["Store"]['init_lx']=scene.objects["Store"].worldPosition.x
|
||||
scene.objects["Store"]['init_ly']=scene.objects["Store"].worldPosition.y
|
||||
|
@ -59,9 +59,10 @@ void loop() {
|
||||
// digitalWrite(v, HIGH);
|
||||
|
||||
if (serial_msg_complet) {
|
||||
Serial.println(serial_msg); // Echo
|
||||
if (serial_msg =="S\n") bt_num=true; // S pour Set
|
||||
if (serial_msg =="R\n") bt_num=false; // R pour Reset
|
||||
|
||||
if (serial_msg =="S\n") bt_num=true; // S pour Set
|
||||
if (serial_msg =="R\n") bt_num=false; // R pour Reset
|
||||
|
||||
// Serial.println("Echo : "+serial_msg);
|
||||
serial_msg = "";
|
||||
|
@ -28,7 +28,8 @@ from rp_lib import * # Bibliothèque Ropy
|
||||
|
||||
def commandes():
|
||||
|
||||
rp_jumeau('/dev/ttyACM0', 115200)
|
||||
# rp_serie_ports() # Affichage de la liste des ports série
|
||||
rp_jumeau() # Vitesse 115200 baud
|
||||
|
||||
rp_serie_msg("S") # Allumer led
|
||||
rp_tempo(2)
|
||||
|
@ -28,7 +28,8 @@ from rp_lib import * # Bibliothèque Ropy
|
||||
|
||||
def commandes():
|
||||
|
||||
rp_jumeau('/dev/ttyACM1', 115200)
|
||||
# rp_serie_ports() # Affichage de la liste des ports série
|
||||
rp_jumeau() # Vitesse 115200 baud
|
||||
|
||||
rp_serie_msg("Press a button !") # Envoyer un message
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user