chromapi 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chromapi/__init__.py +2 -0
- chromapi/__main__.py +6 -0
- chromapi/assets/audio/alert.wav +0 -0
- chromapi/assets/audio/chromapi_scream.wav +0 -0
- chromapi/assets/audio/confused.wav +0 -0
- chromapi/assets/audio/dizzy.wav +0 -0
- chromapi/assets/audio/excited.wav +0 -0
- chromapi/assets/audio/greeting.wav +0 -0
- chromapi/assets/audio/happy.wav +0 -0
- chromapi/assets/audio/no.wav +0 -0
- chromapi/assets/audio/purring.wav +0 -0
- chromapi/assets/audio/sad.wav +0 -0
- chromapi/assets/audio/scared.wav +0 -0
- chromapi/assets/audio/sleepy.wav +0 -0
- chromapi/assets/audio/squeak.wav +0 -0
- chromapi/assets/audio/surprised.wav +0 -0
- chromapi/assets/audio/thinking.wav +0 -0
- chromapi/assets/audio/upset.wav +0 -0
- chromapi/assets/audio/yes.wav +0 -0
- chromapi/assets/favicon.ico +0 -0
- chromapi/assets/firmware/chromapi_stm32_core_v2.0.0.hex +2222 -0
- chromapi/assets/firmware/flash.sh +88 -0
- chromapi/assets/moves/README.md +48 -0
- chromapi/assets/moves/angry.json +1 -0
- chromapi/assets/moves/confused.json +1 -0
- chromapi/assets/moves/dizzy.json +1 -0
- chromapi/assets/moves/excited.json +1 -0
- chromapi/assets/moves/greeting.json +1 -0
- chromapi/assets/moves/happy.json +1 -0
- chromapi/assets/moves/joy.json +1 -0
- chromapi/assets/moves/kick.json +1 -0
- chromapi/assets/moves/metadata.jsonl +15 -0
- chromapi/assets/moves/no.json +1 -0
- chromapi/assets/moves/purring.json +1 -0
- chromapi/assets/moves/sad.json +1 -0
- chromapi/assets/moves/scared.json +1 -0
- chromapi/assets/moves/surprised.json +1 -0
- chromapi/assets/moves/tired.json +1 -0
- chromapi/assets/moves/yes.json +1 -0
- chromapi/chromapi.py +1460 -0
- chromapi/cli.py +106 -0
- chromapi/config/config.yaml +118 -0
- chromapi/estimation/__init__.py +6 -0
- chromapi/estimation/attitude.py +107 -0
- chromapi/hardware/__init__.py +2 -0
- chromapi/hardware/battery.py +54 -0
- chromapi/hardware/fan.py +165 -0
- chromapi/hardware/motherboard_bridge.py +345 -0
- chromapi/input/__init__.py +2 -0
- chromapi/input/gamepad.py +379 -0
- chromapi/input/pairing.py +98 -0
- chromapi/kinematics/__init__.py +10 -0
- chromapi/kinematics/kromatics.py +324 -0
- chromapi/kinematics/poses.py +29 -0
- chromapi/kinematics/topology.py +26 -0
- chromapi/kinematics/types.py +21 -0
- chromapi/locomotion/__init__.py +7 -0
- chromapi/locomotion/balance.py +284 -0
- chromapi/locomotion/gait.py +595 -0
- chromapi/locomotion/recorded_move.py +290 -0
- chromapi/media/__init__.py +2 -0
- chromapi/media/audio.py +424 -0
- chromapi/media/led.py +461 -0
- chromapi/model/LICENSE +438 -0
- chromapi/model/mjcf/additional.xml +21 -0
- chromapi/model/mjcf/assets/18650_li_ion_battery.msh +0 -0
- chromapi/model/mjcf/assets/18650_li_ion_battery.part +13 -0
- chromapi/model/mjcf/assets/battery_holder.msh +0 -0
- chromapi/model/mjcf/assets/battery_holder.part +13 -0
- chromapi/model/mjcf/assets/camera_imx219_d160.msh +0 -0
- chromapi/model/mjcf/assets/camera_imx219_d160.part +13 -0
- chromapi/model/mjcf/assets/chromapi_logo.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo.part +13 -0
- chromapi/model/mjcf/assets/chromapi_logo_circle.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo_inner.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_logo_outer.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_motherboard.msh +0 -0
- chromapi/model/mjcf/assets/chromapi_motherboard.part +13 -0
- chromapi/model/mjcf/assets/composant2_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant2_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/composant3_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant3_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/composant4_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/composant4_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1_1.msh +0 -0
- chromapi/model/mjcf/assets/connector_f_1__corps1_1.part +13 -0
- chromapi/model/mjcf/assets/corps14.msh +0 -0
- chromapi/model/mjcf/assets/corps14.part +13 -0
- chromapi/model/mjcf/assets/fan_nf_a4x10.msh +0 -0
- chromapi/model/mjcf/assets/fan_nf_a4x10.part +13 -0
- chromapi/model/mjcf/assets/foot.msh +0 -0
- chromapi/model/mjcf/assets/foot.part +13 -0
- chromapi/model/mjcf/assets/left_coxa.msh +0 -0
- chromapi/model/mjcf/assets/left_coxa.part +13 -0
- chromapi/model/mjcf/assets/left_femur.msh +0 -0
- chromapi/model/mjcf/assets/left_femur.part +13 -0
- chromapi/model/mjcf/assets/left_tibia.msh +0 -0
- chromapi/model/mjcf/assets/left_tibia.part +13 -0
- chromapi/model/mjcf/assets/microswitch.msh +0 -0
- chromapi/model/mjcf/assets/microswitch.part +13 -0
- chromapi/model/mjcf/assets/power_switch.msh +0 -0
- chromapi/model/mjcf/assets/power_switch.part +13 -0
- chromapi/model/mjcf/assets/raspberry_pi_4_model_b.msh +0 -0
- chromapi/model/mjcf/assets/raspberry_pi_4_model_b.part +13 -0
- chromapi/model/mjcf/assets/right_coxa.msh +0 -0
- chromapi/model/mjcf/assets/right_coxa.part +13 -0
- chromapi/model/mjcf/assets/right_femur.msh +0 -0
- chromapi/model/mjcf/assets/right_femur.part +13 -0
- chromapi/model/mjcf/assets/right_tibia.msh +0 -0
- chromapi/model/mjcf/assets/right_tibia.part +13 -0
- chromapi/model/mjcf/assets/robot_case_bottom.msh +0 -0
- chromapi/model/mjcf/assets/robot_case_bottom.part +13 -0
- chromapi/model/mjcf/assets/robot_case_top.msh +0 -0
- chromapi/model/mjcf/assets/robot_case_top.part +13 -0
- chromapi/model/mjcf/assets/rpi_battery_bracket.msh +0 -0
- chromapi/model/mjcf/assets/rpi_battery_bracket.part +13 -0
- chromapi/model/mjcf/assets/speaker.msh +0 -0
- chromapi/model/mjcf/assets/speaker.part +13 -0
- chromapi/model/mjcf/assets/vis_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/vis_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/vis_1__corps1_1.msh +0 -0
- chromapi/model/mjcf/assets/vis_1__corps1_1.part +13 -0
- chromapi/model/mjcf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.msh +0 -0
- chromapi/model/mjcf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.part +13 -0
- chromapi/model/mjcf/assets/ws2812b_cob_pixel_ring_27mm.msh +0 -0
- chromapi/model/mjcf/assets/ws2812b_cob_pixel_ring_27mm.part +13 -0
- chromapi/model/mjcf/config.json +14 -0
- chromapi/model/mjcf/robot.xml +639 -0
- chromapi/model/mjcf/scene.xml +24 -0
- chromapi/model/urdf/assets/18650_li_ion_battery.part +13 -0
- chromapi/model/urdf/assets/18650_li_ion_battery.stl +0 -0
- chromapi/model/urdf/assets/battery_holder.part +13 -0
- chromapi/model/urdf/assets/battery_holder.stl +0 -0
- chromapi/model/urdf/assets/camera_imx219_d160.part +13 -0
- chromapi/model/urdf/assets/camera_imx219_d160.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo.part +13 -0
- chromapi/model/urdf/assets/chromapi_logo.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_circle.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_inner.stl +0 -0
- chromapi/model/urdf/assets/chromapi_logo_outer.stl +0 -0
- chromapi/model/urdf/assets/chromapi_motherboard.part +13 -0
- chromapi/model/urdf/assets/chromapi_motherboard.stl +0 -0
- chromapi/model/urdf/assets/composant2_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant2_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/composant3_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant3_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/composant4_1__corps1.part +13 -0
- chromapi/model/urdf/assets/composant4_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/connector_f_1__corps1.part +13 -0
- chromapi/model/urdf/assets/connector_f_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/connector_f_1__corps1_1.part +13 -0
- chromapi/model/urdf/assets/connector_f_1__corps1_1.stl +0 -0
- chromapi/model/urdf/assets/corps14.part +13 -0
- chromapi/model/urdf/assets/corps14.stl +0 -0
- chromapi/model/urdf/assets/fan_nf_a4x10.part +13 -0
- chromapi/model/urdf/assets/fan_nf_a4x10.stl +0 -0
- chromapi/model/urdf/assets/foot.part +13 -0
- chromapi/model/urdf/assets/foot.stl +0 -0
- chromapi/model/urdf/assets/left_coxa.part +13 -0
- chromapi/model/urdf/assets/left_coxa.stl +0 -0
- chromapi/model/urdf/assets/left_femur.part +13 -0
- chromapi/model/urdf/assets/left_femur.stl +0 -0
- chromapi/model/urdf/assets/left_tibia.part +13 -0
- chromapi/model/urdf/assets/left_tibia.stl +0 -0
- chromapi/model/urdf/assets/microswitch.part +13 -0
- chromapi/model/urdf/assets/microswitch.stl +0 -0
- chromapi/model/urdf/assets/power_switch.part +13 -0
- chromapi/model/urdf/assets/power_switch.stl +0 -0
- chromapi/model/urdf/assets/raspberry_pi_4_model_b.part +13 -0
- chromapi/model/urdf/assets/raspberry_pi_4_model_b.stl +0 -0
- chromapi/model/urdf/assets/right_coxa.part +13 -0
- chromapi/model/urdf/assets/right_coxa.stl +0 -0
- chromapi/model/urdf/assets/right_femur.part +13 -0
- chromapi/model/urdf/assets/right_femur.stl +0 -0
- chromapi/model/urdf/assets/right_tibia.part +13 -0
- chromapi/model/urdf/assets/right_tibia.stl +0 -0
- chromapi/model/urdf/assets/robot_case_bottom.part +13 -0
- chromapi/model/urdf/assets/robot_case_bottom.stl +0 -0
- chromapi/model/urdf/assets/robot_case_top.part +13 -0
- chromapi/model/urdf/assets/robot_case_top.stl +0 -0
- chromapi/model/urdf/assets/rpi_battery_bracket.part +13 -0
- chromapi/model/urdf/assets/rpi_battery_bracket.stl +0 -0
- chromapi/model/urdf/assets/speaker.part +13 -0
- chromapi/model/urdf/assets/speaker.stl +0 -0
- chromapi/model/urdf/assets/vis_1__corps1.part +13 -0
- chromapi/model/urdf/assets/vis_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/vis_1__corps1_1.part +13 -0
- chromapi/model/urdf/assets/vis_1__corps1_1.stl +0 -0
- chromapi/model/urdf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.part +13 -0
- chromapi/model/urdf/assets/vis_a_tete_tronconique_jis_b_1111_m3x4_h_acier_inoxydable_a2_standard_v1_1__corps1.stl +0 -0
- chromapi/model/urdf/assets/ws2812b_cob_pixel_ring_27mm.part +13 -0
- chromapi/model/urdf/assets/ws2812b_cob_pixel_ring_27mm.stl +0 -0
- chromapi/model/urdf/config.json +8 -0
- chromapi/model/urdf/robot.urdf +2587 -0
- chromapi/utils/calibrate_imu_mount.py +80 -0
- chromapi/utils/calibrate_servo_centers.py +432 -0
- chromapi/utils/flash.py +141 -0
- chromapi/utils/health.py +210 -0
- chromapi/utils/report.py +75 -0
- chromapi/utils/setup_motors.py +189 -0
- chromapi/utils/sim_server.py +126 -0
- chromapi/web/__init__.py +2 -0
- chromapi/web/access.py +167 -0
- chromapi/web/app.py +1136 -0
- chromapi/web/camera.py +202 -0
- chromapi/web/static/fonts/varela-round.woff2 +0 -0
- chromapi/web/static/index.html +1079 -0
- chromapi/web/static/logo.png +0 -0
- chromapi/web/static/motherboard_front.png +0 -0
- chromapi/web/static/socket.io.min.js +7 -0
- chromapi-1.0.0.dist-info/METADATA +222 -0
- chromapi-1.0.0.dist-info/RECORD +219 -0
- chromapi-1.0.0.dist-info/WHEEL +5 -0
- chromapi-1.0.0.dist-info/entry_points.txt +2 -0
- chromapi-1.0.0.dist-info/licenses/LICENSE +201 -0
- chromapi-1.0.0.dist-info/licenses/src/chromapi/model/LICENSE +438 -0
- chromapi-1.0.0.dist-info/top_level.txt +1 -0
chromapi/media/led.py
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""LED ring animations and moods: each mood is a ring animation played with a sound.
|
|
3
|
+
|
|
4
|
+
:class:`LedRing` renders at ~20 fps in a background thread: a persistent animation (off, idle breathing),
|
|
5
|
+
a temporary flash over it (a mood), and a torch (plain white, over everything) for the camera. Works in
|
|
6
|
+
simulation too, where the ring material takes the mean colour.
|
|
7
|
+
|
|
8
|
+
Usage::
|
|
9
|
+
|
|
10
|
+
ring = LedRing(robot)
|
|
11
|
+
ring.start()
|
|
12
|
+
ring.set_persistent(render_idle)
|
|
13
|
+
ring.play_mood("happy") # sound + animation
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import colorsys
|
|
17
|
+
import logging
|
|
18
|
+
import math
|
|
19
|
+
import random
|
|
20
|
+
import threading
|
|
21
|
+
import time
|
|
22
|
+
import wave
|
|
23
|
+
from dataclasses import dataclass
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
from typing import Any, Callable, Dict, List, Optional, Tuple
|
|
26
|
+
|
|
27
|
+
from chromapi.chromapi import AUDIO_DIR
|
|
28
|
+
from chromapi.hardware.motherboard_bridge import LED_RING_COUNT
|
|
29
|
+
|
|
30
|
+
logger = logging.getLogger(__name__)
|
|
31
|
+
|
|
32
|
+
Color = Tuple[int, int, int]
|
|
33
|
+
RenderFn = Callable[[Any, float], None] # (robot, t [s]) -> sets the ring
|
|
34
|
+
|
|
35
|
+
BRIGHTNESS = 0.35 # global scale: full brightness is blinding and draws ~1 A from the 5 V rail
|
|
36
|
+
IDLE = (182, 5, 252) # violet-pink, Chromapi's default idle colour
|
|
37
|
+
_idle: Color = IDLE
|
|
38
|
+
_rainbow = False
|
|
39
|
+
RAINBOW_IDLE_S = 4.0 # [s] per turn
|
|
40
|
+
TORCH_LEVEL = 200 # white colour for the torch
|
|
41
|
+
FPS = 20.0 # [Hz] the animations stay smooth; each frame costs the shared serial bus
|
|
42
|
+
TOP_HALF = {14, 15, 16, 17, 0, 1, 2, 3, 4} # LED 0 is at the top of the ring
|
|
43
|
+
RING_CLOCKWISE = True # LED indices clockwise, seen from the front
|
|
44
|
+
BOTTOM_HALF = {5, 6, 7, 8, 9, 10, 11, 12, 13}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _dim(r: float, g: float, b: float) -> Color:
|
|
48
|
+
return (int(r * BRIGHTNESS), int(g * BRIGHTNESS), int(b * BRIGHTNESS))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def dimmed(color: Color) -> Color:
|
|
52
|
+
"""``color`` at the ring's brightness."""
|
|
53
|
+
return _dim(*color)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def idle_color() -> Color:
|
|
57
|
+
"""The idle colour."""
|
|
58
|
+
return _rainbow_rgb(2 * math.pi * time.monotonic() / RAINBOW_IDLE_S) if _rainbow else _idle
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _rainbow_rgb(a: float) -> Color:
|
|
62
|
+
"""Rainbow colour at angle ``a`` [rad]."""
|
|
63
|
+
return tuple(int(127.5 * (1 + math.sin(a + phase))) for phase in (0.0, 2.094, 4.188)) # type: ignore[return-value]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _idle_leds(t: float) -> List[Color]:
|
|
67
|
+
"""The idle colour of each LED at ``t`` [s]."""
|
|
68
|
+
if _rainbow:
|
|
69
|
+
return [_rainbow_rgb(2 * math.pi * (t / RAINBOW_IDLE_S + i / LED_RING_COUNT)) for i in range(LED_RING_COUNT)]
|
|
70
|
+
return [_idle] * LED_RING_COUNT
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def idle_rainbow() -> bool:
|
|
74
|
+
"""The idle colour is the rainbow."""
|
|
75
|
+
return _rainbow
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def set_idle_color(color: Optional[Color] = None) -> None:
|
|
79
|
+
"""Set :func:`idle_color` (None: :data:`IDLE`), ending the rainbow."""
|
|
80
|
+
global _idle, _rainbow
|
|
81
|
+
_idle = IDLE if color is None else tuple(int(min(max(c, 0), 255)) for c in color) # type: ignore[assignment]
|
|
82
|
+
_rainbow = False
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def set_idle_rainbow() -> None:
|
|
86
|
+
"""Make the idle colour a slowly turning rainbow."""
|
|
87
|
+
global _rainbow
|
|
88
|
+
_rainbow = True
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def accent_color(color: Color) -> Color:
|
|
92
|
+
"""A pale neighbour of ``color``, 50 degrees round the hue wheel, on the brighter side."""
|
|
93
|
+
h, _, _ = colorsys.rgb_to_hsv(*(c / 255.0 for c in color))
|
|
94
|
+
candidates = [colorsys.hsv_to_rgb((h + shift) % 1.0, 0.6, 1.0) for shift in (50 / 360, -50 / 360)]
|
|
95
|
+
best = max(candidates, key=lambda rgb: 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2])
|
|
96
|
+
return tuple(int(round(c * 255)) for c in best) # type: ignore[return-value]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _from_top(i: int) -> int:
|
|
100
|
+
"""Distance of LED ``i`` from the top of the ring, in LEDs."""
|
|
101
|
+
return min(i, LED_RING_COUNT - i)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def render_off(robot: Any, t: float) -> None:
|
|
105
|
+
"""All off."""
|
|
106
|
+
robot.set_led_all(0, 0, 0)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
LISTENING_LEDS_PER_S = 18.0 # [LED/s] one turn a second
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def render_listening(robot: Any, t: float) -> None:
|
|
113
|
+
"""Someone listens through the microphone: an accent comet over the dimmed idle colour."""
|
|
114
|
+
head = int(t * LISTENING_LEDS_PER_S) % LED_RING_COUNT
|
|
115
|
+
colors = []
|
|
116
|
+
for i, base in enumerate(_idle_leds(t)):
|
|
117
|
+
d = (head - i) % LED_RING_COUNT
|
|
118
|
+
k = 1.0 - d / 4 * 0.75 if d < 4 else 0.0 # head and a 3-LED tail
|
|
119
|
+
colors.append(_dim(*(m * k + c * 0.3 * (1.0 - k) for m, c in zip(accent_color(base), base))))
|
|
120
|
+
robot.set_led_bulk(colors)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def render_torch(robot: Any, t: float) -> None:
|
|
124
|
+
"""Plain white, to light the camera's view."""
|
|
125
|
+
robot.set_led_all(TORCH_LEVEL, TORCH_LEVEL, TORCH_LEVEL)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def render_idle(robot: Any, t: float) -> None:
|
|
129
|
+
"""Slow breathing in the idle colour, or the spinning rainbow."""
|
|
130
|
+
if _rainbow:
|
|
131
|
+
robot.set_led_bulk([_dim(*c) for c in _idle_leds(t)])
|
|
132
|
+
return
|
|
133
|
+
k = 0.3 + 0.7 * (math.sin(t * math.pi) + 1.0) / 2.0
|
|
134
|
+
robot.set_led_all(*_dim(*(c * k for c in _idle)))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def render_rainbow(robot: Any, t: float) -> None:
|
|
138
|
+
"""Spinning rainbow."""
|
|
139
|
+
robot.set_led_bulk([_dim(*_rainbow_rgb(t * 6.0 + 2 * math.pi * i / LED_RING_COUNT)) for i in range(LED_RING_COUNT)])
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def render_sad(robot: Any, t: float) -> None:
|
|
143
|
+
"""Blue to cyan towards the bottom, slow pulse."""
|
|
144
|
+
k = 0.5 + 0.5 * (math.sin(t * 0.4 * math.pi) + 1.0) / 2.0
|
|
145
|
+
robot.set_led_bulk([_dim(0, 255 * _from_top(i) / (LED_RING_COUNT / 2) * k, 255 * k) for i in range(LED_RING_COUNT)])
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def render_angry(robot: Any, t: float) -> None:
|
|
149
|
+
"""Red to orange, slow pulse, a frowning brow (LEDs off) at the top."""
|
|
150
|
+
k = 0.5 + 0.5 * (math.sin(t * 0.4 * math.pi) + 1.0) / 2.0
|
|
151
|
+
brow = {16, 17, 0, 1, 2}
|
|
152
|
+
depth = max(_from_top(i) for i in brow) + 1
|
|
153
|
+
colors: List[Color] = []
|
|
154
|
+
for i in range(LED_RING_COUNT):
|
|
155
|
+
if i in brow:
|
|
156
|
+
colors.append((0, 0, 0))
|
|
157
|
+
continue
|
|
158
|
+
x = (_from_top(i) - depth) / (LED_RING_COUNT / 2 - depth)
|
|
159
|
+
colors.append(_dim(255 * k, 140 * x * k, 0))
|
|
160
|
+
robot.set_led_bulk(colors)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _pulse_wave(robot: Any, t: float, rgb: Color, every_other: bool = False) -> None:
|
|
164
|
+
k = 0.4 + 0.6 * abs(math.sin(t * 4.0 * math.pi))
|
|
165
|
+
colors: List[Color] = []
|
|
166
|
+
for i in range(LED_RING_COUNT):
|
|
167
|
+
w = k * (0.8 + 0.2 * math.sin(i + t * 10.0))
|
|
168
|
+
colors.append((0, 0, 0) if every_other and i % 2 else _dim(rgb[0] * w, rgb[1] * w, rgb[2] * w))
|
|
169
|
+
robot.set_led_bulk(colors)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def render_yes(robot: Any, t: float) -> None:
|
|
173
|
+
"""Green fast pulsing wave."""
|
|
174
|
+
_pulse_wave(robot, t, (0, 255, 0))
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def render_no(robot: Any, t: float) -> None:
|
|
178
|
+
"""Red fast pulsing wave."""
|
|
179
|
+
_pulse_wave(robot, t, (255, 0, 0))
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def render_surprised(robot: Any, t: float) -> None:
|
|
183
|
+
"""White fast pulsing wave, every other LED."""
|
|
184
|
+
_pulse_wave(robot, t, (255, 255, 255), every_other=True)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def render_alert(robot: Any, t: float) -> None:
|
|
188
|
+
"""Whole ring blinking red."""
|
|
189
|
+
robot.set_led_all(*(_dim(255, 0, 0) if math.sin(t * 6 * math.pi) > 0 else (0, 0, 0)))
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def render_scared(robot: Any, t: float) -> None:
|
|
193
|
+
"""Pale blue, even and odd LEDs alternating fast."""
|
|
194
|
+
even_on = int(t * 9.0) % 2 == 0
|
|
195
|
+
robot.set_led_bulk([_dim(200, 230, 255) if (i % 2 == 0) == even_on else (0, 0, 0) for i in range(LED_RING_COUNT)])
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
THINKING_CYCLE_S = 2.2 # [s] per lap
|
|
199
|
+
THINKING_DOTS = 4
|
|
200
|
+
THINKING_GAP_S = 0.4 # [s] between dots
|
|
201
|
+
THINKING_EASE = 0.55 # 0: even speed, 1: stops at the top
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def render_thinking(robot: Any, t: float) -> None:
|
|
205
|
+
"""A loading spinner: blue to indigo dots chasing round the ring, slower at the top."""
|
|
206
|
+
lit = [0.0] * LED_RING_COUNT
|
|
207
|
+
colors = [(0.0, 0.0, 0.0)] * LED_RING_COUNT
|
|
208
|
+
for j in range(THINKING_DOTS):
|
|
209
|
+
s = ((t - j * THINKING_GAP_S) / THINKING_CYCLE_S) % 1.0
|
|
210
|
+
lap = s - THINKING_EASE * math.sin(2 * math.pi * s) / (2 * math.pi)
|
|
211
|
+
pos = lap * LED_RING_COUNT
|
|
212
|
+
mix = j / (THINKING_DOTS - 1)
|
|
213
|
+
rgb = (75 * mix, 0.0, 255 * (1 - mix) + 130 * mix)
|
|
214
|
+
for i in range(LED_RING_COUNT):
|
|
215
|
+
d = abs((pos - i + LED_RING_COUNT / 2) % LED_RING_COUNT - LED_RING_COUNT / 2)
|
|
216
|
+
k = max(0.0, 1.0 - d) # glide between two LEDs
|
|
217
|
+
if k > lit[i]:
|
|
218
|
+
lit[i], colors[i] = k, (rgb[0] * k, rgb[1] * k, rgb[2] * k)
|
|
219
|
+
robot.set_led_bulk([_dim(*c) for c in colors])
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def render_purring(robot: Any, t: float) -> None:
|
|
223
|
+
"""Top half in the idle colour, with a fast purr."""
|
|
224
|
+
k = 0.85 + 0.15 * math.sin(t * 14.0 * math.pi)
|
|
225
|
+
robot.set_led_bulk([_dim(*(c * k for c in base)) if i in TOP_HALF else (0, 0, 0)
|
|
226
|
+
for i, base in enumerate(_idle_leds(t))])
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def render_excited(robot: Any, t: float) -> None:
|
|
230
|
+
"""Bright yellow strobe."""
|
|
231
|
+
k = 0.4 + 0.6 * abs(math.sin(t * 10.0 * math.pi))
|
|
232
|
+
robot.set_led_all(*_dim(255 * k, 255 * k, 0))
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
QUESTION_HOOK = range(-3, 7) # LED steps clockwise from the top
|
|
236
|
+
QUESTION_DOT = 9
|
|
237
|
+
QUESTION_WRITE_S = 0.7 # [s]
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _led_at(step: int) -> int:
|
|
241
|
+
"""The LED ``step`` places clockwise from the top (LED 0)."""
|
|
242
|
+
return (step if RING_CLOCKWISE else -step) % LED_RING_COUNT
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def render_confused(robot: Any, t: float) -> None:
|
|
246
|
+
"""A cyan question mark, written then glowing."""
|
|
247
|
+
glow = 0.75 + 0.25 * math.cos(max(t - QUESTION_WRITE_S - 0.2, 0.0) * 2.0 * math.pi)
|
|
248
|
+
colors = [(0, 0, 0)] * LED_RING_COUNT
|
|
249
|
+
for n, step in enumerate(QUESTION_HOOK):
|
|
250
|
+
k = min(max(t / QUESTION_WRITE_S * len(QUESTION_HOOK) - n, 0.0), 1.0)
|
|
251
|
+
colors[_led_at(step)] = _dim(0, 255 * k * glow, 255 * k * glow)
|
|
252
|
+
if t >= QUESTION_WRITE_S + 0.2:
|
|
253
|
+
colors[_led_at(QUESTION_DOT)] = _dim(0, 255 * glow, 255 * glow)
|
|
254
|
+
robot.set_led_bulk(colors)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def render_squeak(robot: Any, t: float) -> None:
|
|
258
|
+
"""Idle colour to its accent, 4 beats per squeak."""
|
|
259
|
+
mix = 0.5 + 0.5 * math.sin((t % 0.342) / 0.342 * 4 * 2 * math.pi)
|
|
260
|
+
robot.set_led_bulk([_dim(*(a + (b - a) * mix for a, b in zip(base, accent_color(base)))) for base in _idle_leds(t)])
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def render_joy(robot: Any, t: float) -> None:
|
|
264
|
+
"""A pink smile: the top half lit."""
|
|
265
|
+
robot.set_led_bulk([_dim(220, 0, 220) if i in TOP_HALF else (0, 0, 0) for i in range(LED_RING_COUNT)])
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def render_dizzy(robot: Any, t: float) -> None:
|
|
269
|
+
"""A spinning yellow comet, 0.4 turn/s like the chassis in the dizzy gesture."""
|
|
270
|
+
head = int(t * 0.4 * LED_RING_COUNT) % LED_RING_COUNT
|
|
271
|
+
colors = []
|
|
272
|
+
for i in range(LED_RING_COUNT):
|
|
273
|
+
d = (head - i) % LED_RING_COUNT
|
|
274
|
+
k = 1.0 - d / 5 * 0.8 if d < 5 else 0.0
|
|
275
|
+
colors.append(_dim(255 * k, 255 * k, 0))
|
|
276
|
+
robot.set_led_bulk(colors)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def render_tired(robot: Any, t: float) -> None:
|
|
280
|
+
"""Low orange eyelids (bottom half), blinking now and then."""
|
|
281
|
+
blink = random.random() < 0.05
|
|
282
|
+
robot.set_led_bulk([_dim(255, 100, 0) if i in BOTTOM_HALF and not blink else (0, 0, 0) for i in range(LED_RING_COUNT)])
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
BATTERY_FLASH_S = 3.0 # [s] how long the ring shows the charge
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def battery_color(percent: float) -> Color:
|
|
289
|
+
"""The motherboard status LED's colour for ``percent`` (firmware ``Battery_Led_Update``, same thresholds).
|
|
290
|
+
|
|
291
|
+
Red (10 %) -> orange (20 %) -> yellow (30 %, "low") -> green (60 %) -> green-cyan (100 %), on a 0-50 scale there.
|
|
292
|
+
"""
|
|
293
|
+
p = min(max(percent, 0.0), 100.0)
|
|
294
|
+
if p >= 60.0:
|
|
295
|
+
rgb = (0.0, 50.0, 50.0 * (p - 60.0) / 40.0)
|
|
296
|
+
elif p >= 30.0:
|
|
297
|
+
rgb = (50.0 * (60.0 - p) / 30.0, 50.0, 0.0)
|
|
298
|
+
elif p >= 20.0:
|
|
299
|
+
rgb = (50.0, 15.0 + 35.0 * (p - 20.0) / 10.0, 0.0)
|
|
300
|
+
else:
|
|
301
|
+
rgb = (50.0, 15.0 * max(p - 10.0, 0.0) / 10.0, 0.0)
|
|
302
|
+
return _dim(*(c * 255.0 / 50.0 for c in rgb))
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def render_battery(percent: float) -> RenderFn:
|
|
306
|
+
"""The charge on the ring: as many LEDs as the charge (from the top), in the status LED's colour.
|
|
307
|
+
|
|
308
|
+
Under 10 % they blink at 1 Hz, as the status LED does. At least one LED stays lit, so 0 % still shows something.
|
|
309
|
+
"""
|
|
310
|
+
lit = max(1, round(LED_RING_COUNT * min(max(percent, 0.0), 100.0) / 100.0))
|
|
311
|
+
color = battery_color(percent)
|
|
312
|
+
|
|
313
|
+
def render(robot: Any, t: float) -> None:
|
|
314
|
+
on = percent >= 10.0 or int(2.0 * t) % 2 == 0
|
|
315
|
+
robot.set_led_bulk([color if on and i < lit else (0, 0, 0) for i in range(LED_RING_COUNT)])
|
|
316
|
+
|
|
317
|
+
return render
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
@dataclass(frozen=True)
|
|
321
|
+
class Mood:
|
|
322
|
+
"""A sound played with a ring animation."""
|
|
323
|
+
|
|
324
|
+
name: str
|
|
325
|
+
render: RenderFn
|
|
326
|
+
sound: str # file in AUDIO_DIR
|
|
327
|
+
emoji: str # for the web app
|
|
328
|
+
min_s: float = 0.0 # [s] minimum animation length
|
|
329
|
+
gain: float = 1.0 # sound volume factor, for a sound quieter on the small speaker than the others
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
MOODS: List[Mood] = [
|
|
333
|
+
Mood("scream", render_rainbow, "chromapi_scream.wav", "📣"),
|
|
334
|
+
Mood("happy", render_rainbow, "happy.wav", "😄"),
|
|
335
|
+
Mood("sad", render_sad, "sad.wav", "😢"),
|
|
336
|
+
Mood("angry", render_angry, "upset.wav", "😠"),
|
|
337
|
+
Mood("surprised", render_surprised, "surprised.wav", "😮"),
|
|
338
|
+
Mood("joy", render_joy, "greeting.wav", "🥰"),
|
|
339
|
+
Mood("dizzy", render_dizzy, "dizzy.wav", "😵"),
|
|
340
|
+
Mood("tired", render_tired, "sleepy.wav", "😴"),
|
|
341
|
+
Mood("yes", render_yes, "yes.wav", "👍"),
|
|
342
|
+
Mood("no", render_no, "no.wav", "👎"),
|
|
343
|
+
Mood("alert", render_alert, "alert.wav", "🚨"),
|
|
344
|
+
Mood("scared", render_scared, "scared.wav", "😨"),
|
|
345
|
+
Mood("thinking", render_thinking, "thinking.wav", "🤔", min_s=2 * THINKING_CYCLE_S + 0.4),
|
|
346
|
+
Mood("purring", render_purring, "purring.wav", "😺", gain=3.5),
|
|
347
|
+
Mood("excited", render_excited, "excited.wav", "🤩"),
|
|
348
|
+
Mood("confused", render_confused, "confused.wav", "❓", min_s=2.0),
|
|
349
|
+
Mood("squeak", render_squeak, "squeak.wav", "🐭"),
|
|
350
|
+
]
|
|
351
|
+
MOODS_BY_NAME: Dict[str, Mood] = {mood.name: mood for mood in MOODS}
|
|
352
|
+
PAD_MOOD_COLORS: Dict[str, Color] = {
|
|
353
|
+
"scream": (255, 0, 80), "happy": (255, 210, 0), "sad": (0, 100, 255), "angry": (255, 140, 0),
|
|
354
|
+
"surprised": (255, 255, 255), "joy": (220, 0, 220), "dizzy": (255, 255, 0), "tired": (255, 120, 0),
|
|
355
|
+
"yes": (0, 255, 0), "no": (255, 0, 0), "alert": (255, 51, 0), "scared": (200, 230, 255),
|
|
356
|
+
"thinking": (75, 0, 130), "purring": (255, 105, 180), "excited": (255, 255, 0), "confused": (0, 229, 255),
|
|
357
|
+
"squeak": (180, 180, 180),
|
|
358
|
+
}
|
|
359
|
+
MIN_FLASH_S = 1.2 # [s] a short sound still gets a visible animation
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def sound_duration_s(path: Path) -> float:
|
|
363
|
+
"""Length of a .wav [s], 0 if it cannot be read."""
|
|
364
|
+
try:
|
|
365
|
+
with wave.open(str(path), "rb") as w:
|
|
366
|
+
return w.getnframes() / float(w.getframerate())
|
|
367
|
+
except (OSError, wave.Error):
|
|
368
|
+
return 0.0
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
class _OnChange:
|
|
372
|
+
"""Forwards a frame to the robot only when it differs from the last one: a still ring costs nothing."""
|
|
373
|
+
|
|
374
|
+
def __init__(self, robot: Any) -> None:
|
|
375
|
+
self.robot = robot
|
|
376
|
+
self.last: Any = None
|
|
377
|
+
|
|
378
|
+
def set_led_all(self, r: int, g: int, b: int) -> None:
|
|
379
|
+
if self.last != (r, g, b):
|
|
380
|
+
self.robot.set_led_all(r, g, b)
|
|
381
|
+
self.last = (r, g, b)
|
|
382
|
+
|
|
383
|
+
def set_led_bulk(self, colors: List[Color]) -> None:
|
|
384
|
+
if self.last != colors:
|
|
385
|
+
self.robot.set_led_bulk(colors)
|
|
386
|
+
self.last = colors
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
class LedRing:
|
|
390
|
+
"""Background ring renderer: persistent animation, temporary flash over it, the listening sign over that, torch over everything."""
|
|
391
|
+
|
|
392
|
+
def __init__(self, robot: Any) -> None:
|
|
393
|
+
"""No thread until :meth:`start`."""
|
|
394
|
+
self.robot = robot
|
|
395
|
+
self._out = _OnChange(robot)
|
|
396
|
+
self.torch = False
|
|
397
|
+
self.listening = False # Chromapi's microphone is streamed to a page: shown, whatever else plays
|
|
398
|
+
self._persistent: RenderFn = render_off
|
|
399
|
+
self._flash: Optional[Tuple[RenderFn, float]] = None # (animation, until [s])
|
|
400
|
+
self._stop = threading.Event()
|
|
401
|
+
self._thread: Optional[threading.Thread] = None
|
|
402
|
+
|
|
403
|
+
def start(self) -> None:
|
|
404
|
+
"""Start rendering."""
|
|
405
|
+
self._stop.clear()
|
|
406
|
+
self._thread = threading.Thread(target=self._loop, name="chromapi-led", daemon=True)
|
|
407
|
+
self._thread.start()
|
|
408
|
+
|
|
409
|
+
def stop(self) -> None:
|
|
410
|
+
"""Stop rendering and turn the ring off."""
|
|
411
|
+
self._stop.set()
|
|
412
|
+
if self._thread is not None:
|
|
413
|
+
self._thread.join(timeout=1.0)
|
|
414
|
+
render_off(self.robot, 0.0) # always sent, whatever the last frame was
|
|
415
|
+
|
|
416
|
+
def set_persistent(self, render: RenderFn) -> None:
|
|
417
|
+
"""The animation shown when nothing else plays."""
|
|
418
|
+
self._persistent = render
|
|
419
|
+
|
|
420
|
+
def flash(self, render: RenderFn, duration_s: float) -> None:
|
|
421
|
+
"""Show ``render`` for ``duration_s``, then go back to the persistent animation."""
|
|
422
|
+
self._flash = (render, time.monotonic() + duration_s)
|
|
423
|
+
|
|
424
|
+
def play_mood(self, name: str, duration_s: float = 0.0) -> None:
|
|
425
|
+
"""A mood's sound and its animation, for as long as the sound, or ``duration_s`` [s] (a gesture) if longer."""
|
|
426
|
+
mood = MOODS_BY_NAME[name]
|
|
427
|
+
path = AUDIO_DIR / mood.sound
|
|
428
|
+
self.flash(mood.render, max(MIN_FLASH_S, mood.min_s, sound_duration_s(path), duration_s))
|
|
429
|
+
self.robot.play_sound(path, gain=mood.gain)
|
|
430
|
+
|
|
431
|
+
def _loop(self) -> None:
|
|
432
|
+
t0 = time.monotonic()
|
|
433
|
+
while not self._stop.is_set():
|
|
434
|
+
now = time.monotonic()
|
|
435
|
+
flash = self._flash
|
|
436
|
+
if flash is not None and now >= flash[1]:
|
|
437
|
+
self._flash = flash = None
|
|
438
|
+
render = (render_torch if self.torch else render_listening if self.listening
|
|
439
|
+
else flash[0] if flash else self._persistent)
|
|
440
|
+
try:
|
|
441
|
+
render(self._out, now - t0)
|
|
442
|
+
except Exception: # a lost frame on the bus: the next one will do
|
|
443
|
+
logger.debug("LED frame failed", exc_info=True)
|
|
444
|
+
self._stop.wait(1.0 / FPS)
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
#: The ring after a crash (``chromapi led error``).
|
|
448
|
+
ERROR_RGB: Color = (255, 0, 0)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def error_main() -> int:
|
|
452
|
+
"""Light the ring in :data:`ERROR_RGB`."""
|
|
453
|
+
from chromapi.hardware.motherboard_bridge import BridgeClient
|
|
454
|
+
|
|
455
|
+
bridge = BridgeClient()
|
|
456
|
+
if not bridge.connect():
|
|
457
|
+
return 1
|
|
458
|
+
try:
|
|
459
|
+
return 0 if bridge.set_led_color_all(*_dim(*ERROR_RGB)) else 1
|
|
460
|
+
finally:
|
|
461
|
+
bridge.close()
|