Spaces:
Running
Running
Jalon 1
Browse files- reachy_mini_hello_world/main.py +88 -71
reachy_mini_hello_world/main.py
CHANGED
|
@@ -1,75 +1,92 @@
|
|
| 1 |
-
import threading
|
| 2 |
-
from reachy_mini import ReachyMini, ReachyMiniApp
|
| 3 |
-
from reachy_mini.utils import create_head_pose
|
| 4 |
-
import numpy as np
|
| 5 |
import time
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class ReachyMiniHelloWorld(ReachyMiniApp):
|
| 10 |
-
# Optional: URL to a custom configuration page for the app
|
| 11 |
-
# eg. "http://localhost:8042"
|
| 12 |
-
custom_app_url: str | None = "http://0.0.0.0:8042"
|
| 13 |
-
# Optional: specify a media backend ("gstreamer", "gstreamer_no_video", "default", etc.)
|
| 14 |
-
# On the wireless, use gstreamer_no_video to optimise CPU usage if the app does not use video streaming
|
| 15 |
-
request_media_backend: str | None = None
|
| 16 |
-
|
| 17 |
-
def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
|
| 18 |
-
t0 = time.time()
|
| 19 |
-
|
| 20 |
-
antennas_enabled = True
|
| 21 |
-
sound_play_requested = False
|
| 22 |
-
|
| 23 |
-
# You can ignore this part if you don't want to add settings to your app. If you set custom_app_url to None, you have to remove this part as well.
|
| 24 |
-
# === vvv ===
|
| 25 |
-
class AntennaState(BaseModel):
|
| 26 |
-
enabled: bool
|
| 27 |
-
|
| 28 |
-
@self.settings_app.post("/antennas")
|
| 29 |
-
def update_antennas_state(state: AntennaState):
|
| 30 |
-
nonlocal antennas_enabled
|
| 31 |
-
antennas_enabled = state.enabled
|
| 32 |
-
return {"antennas_enabled": antennas_enabled}
|
| 33 |
-
|
| 34 |
-
@self.settings_app.post("/play_sound")
|
| 35 |
-
def request_sound_play():
|
| 36 |
-
nonlocal sound_play_requested
|
| 37 |
-
sound_play_requested = True
|
| 38 |
-
|
| 39 |
-
# === ^^^ ===
|
| 40 |
-
|
| 41 |
-
# Main control loop
|
| 42 |
-
while not stop_event.is_set():
|
| 43 |
-
t = time.time() - t0
|
| 44 |
-
|
| 45 |
-
yaw_deg = 30.0 * np.sin(2.0 * np.pi * 0.2 * t)
|
| 46 |
-
head_pose = create_head_pose(yaw=yaw_deg, degrees=True)
|
| 47 |
-
|
| 48 |
-
if antennas_enabled:
|
| 49 |
-
amp_deg = 25.0
|
| 50 |
-
a = amp_deg * np.sin(2.0 * np.pi * 0.5 * t)
|
| 51 |
-
antennas_deg = np.array([a, -a])
|
| 52 |
-
else:
|
| 53 |
-
antennas_deg = np.array([0.0, 0.0])
|
| 54 |
-
|
| 55 |
-
if sound_play_requested:
|
| 56 |
-
print("Playing sound...")
|
| 57 |
-
reachy_mini.media.play_sound("wake_up.wav")
|
| 58 |
-
sound_play_requested = False
|
| 59 |
-
|
| 60 |
-
antennas_rad = np.deg2rad(antennas_deg)
|
| 61 |
-
|
| 62 |
-
reachy_mini.set_target(
|
| 63 |
-
head=head_pose,
|
| 64 |
-
antennas=antennas_rad,
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
time.sleep(0.02)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
+
import math
|
| 3 |
+
from reachy_mini import ReachyMini
|
| 4 |
+
from reachy_mini.utils import create_head_pose
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def programme_1(mini):
|
| 7 |
+
dimensions = ['roll', 'pitch', 'yaw', 'x', 'y', 'z']
|
| 8 |
+
duree_par_dim = 6.0
|
| 9 |
+
duree_totale = duree_par_dim * 6
|
| 10 |
+
|
| 11 |
+
heure_debut = time.time()
|
| 12 |
+
|
| 13 |
+
while True:
|
| 14 |
+
temps_ecoule = time.time() - heure_debut
|
| 15 |
+
if temps_ecoule >= duree_totale:
|
| 16 |
+
break
|
| 17 |
+
|
| 18 |
+
angle_antennes = 15.0 * math.sin(2.0 * math.pi * temps_ecoule)
|
| 19 |
+
ant_gauche = math.radians(angle_antennes)
|
| 20 |
+
ant_droit = math.radians(-angle_antennes)
|
| 21 |
+
|
| 22 |
+
index_dim = int(temps_ecoule / duree_par_dim)
|
| 23 |
+
dim_actuelle = dimensions[index_dim]
|
| 24 |
+
temps_local = temps_ecoule % duree_par_dim
|
| 25 |
+
|
| 26 |
+
amplitude = 20.0 if dim_actuelle in ['roll', 'pitch', 'yaw'] else 0.03
|
| 27 |
+
|
| 28 |
+
valeur_tete = amplitude * math.sin(math.pi * temps_local)
|
| 29 |
+
|
| 30 |
+
valeurs_pose = {'roll': 0, 'pitch': 0, 'yaw': 0, 'x': 0, 'y': 0, 'z': 0}
|
| 31 |
+
valeurs_pose[dim_actuelle] = valeur_tete
|
| 32 |
+
|
| 33 |
+
pose_tete = create_head_pose(**valeurs_pose, degrees=True)
|
| 34 |
+
mini.set_target(head=pose_tete, antennas=[ant_gauche, ant_droit])
|
| 35 |
+
|
| 36 |
+
time.sleep(0.01)
|
| 37 |
+
|
| 38 |
+
def programme_2(mini, frequence_hz):
|
| 39 |
+
dt = 1.0 / frequence_hz
|
| 40 |
+
|
| 41 |
+
freq_y = 0.25
|
| 42 |
+
freq_pitch = 1.0
|
| 43 |
+
duree_A = 3 * (1.0 / freq_y)
|
| 44 |
+
|
| 45 |
+
freq_cercle = 0.5
|
| 46 |
+
rayon = 0.03
|
| 47 |
+
duree_B = 3 * (1.0 / freq_cercle)
|
| 48 |
+
|
| 49 |
+
duree_totale = duree_A + duree_B
|
| 50 |
+
heure_debut = time.time()
|
| 51 |
+
|
| 52 |
+
while True:
|
| 53 |
+
temps_ecoule = time.time() - heure_debut
|
| 54 |
+
|
| 55 |
+
if temps_ecoule >= duree_totale:
|
| 56 |
+
break
|
| 57 |
+
|
| 58 |
+
valeurs_pose = {'roll': 0, 'pitch': 0, 'yaw': 0, 'x': 0, 'y': 0, 'z': 0}
|
| 59 |
+
|
| 60 |
+
if temps_ecoule < duree_A:
|
| 61 |
+
valeurs_pose['y'] = 0.03 * math.sin(2.0 * math.pi * freq_y * temps_ecoule)
|
| 62 |
+
valeurs_pose['pitch'] = 20.0 * math.sin(2.0 * math.pi * freq_pitch * temps_ecoule)
|
| 63 |
+
else:
|
| 64 |
+
temps_cercle = temps_ecoule - duree_A
|
| 65 |
+
|
| 66 |
+
valeurs_pose['x'] = rayon * math.cos(2.0 * math.pi * freq_cercle * temps_cercle)
|
| 67 |
+
valeurs_pose['y'] = rayon * math.sin(2.0 * math.pi * freq_cercle * temps_cercle)
|
| 68 |
+
|
| 69 |
+
pose_tete = create_head_pose(**valeurs_pose, degrees=True)
|
| 70 |
+
mini.set_target(head=pose_tete)
|
| 71 |
+
|
| 72 |
+
time.sleep(dt)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
choix_prog = input("Quel programme lancer ? (1 ou 2) : ")
|
| 76 |
+
|
| 77 |
+
with ReachyMini() as mini:
|
| 78 |
+
mini.wake_up()
|
| 79 |
+
mini.enable_motors()
|
| 80 |
+
time.sleep(1)
|
| 81 |
+
|
| 82 |
+
if choix_prog == '1':
|
| 83 |
+
programme_1(mini)
|
| 84 |
+
elif choix_prog == '2':
|
| 85 |
+
choix_freq = input("Fréquence d'envoi des commandes en Hz (défaut=100) : ")
|
| 86 |
+
frequence = float(choix_freq) if choix_freq.strip() != "" else 100.0
|
| 87 |
+
programme_2(mini, frequence)
|
| 88 |
+
else:
|
| 89 |
+
print("Choix invalide.")
|
| 90 |
+
|
| 91 |
+
mini.goto_sleep()
|
| 92 |
+
mini.disable_motors()
|