forked from worldcoin/orb-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanual-mirror-calibration.rs
More file actions
254 lines (245 loc) · 9.29 KB
/
manual-mirror-calibration.rs
File metadata and controls
254 lines (245 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#![warn(unsafe_op_in_unsafe_fn)]
#![warn(clippy::pedantic)]
use agentwire::{port, BrokerFlow};
use eyre::{bail, Result};
use futures::{channel::mpsc, prelude::*};
use orb::{
agents::{camera, mirror},
async_main,
brokers::{Orb, OrbPlan},
calibration::Calibration,
config::Config,
consts::{
CALIBRATION_FILE_PATH, IR_CAMERA_FRAME_RATE, RGB_FPS, RGB_REDUCED_HEIGHT, RGB_REDUCED_WIDTH,
},
ext::mpsc::SenderExt as _,
mcu, monitor,
plans::biometric_capture::{IR_TARGET_MEAN, MIN_SHARPNESS},
ui::{self, Engine},
};
use std::{
io::{prelude::*, stdin, stdout, Stdout},
sync::Arc,
task::{Context, Poll},
thread,
};
use termion::{
event::Key,
input::TermRead,
raw::{IntoRawMode, RawTerminal},
};
use tokio::sync::Mutex;
enum Command {
Recalibrate(Calibration),
SwitchEye(bool),
Quit(Calibration),
EyePidControllerToggle(bool),
ThermalCameraToggle(bool),
ThermalCameraCalibrate,
}
struct Plan {
command_rx: mpsc::Receiver<Command>,
command: Option<Command>,
}
impl OrbPlan for Plan {
fn poll_extra(&mut self, _orb: &mut Orb, cx: &mut Context<'_>) -> Result<BrokerFlow> {
match self.command_rx.poll_next_unpin(cx) {
Poll::Ready(command @ Some(_)) => {
self.command = command;
Ok(BrokerFlow::Break)
}
Poll::Ready(None) => bail!("channel closed unexpectedly"),
Poll::Pending => Ok(BrokerFlow::Continue),
}
}
}
impl Plan {
#[allow(clippy::unused_async)]
async fn new(calibration: Calibration) -> Self {
let (command_tx, command_rx) = mpsc::channel(100);
spawn_command_thread(command_tx, calibration);
Self { command_rx, command: None }
}
async fn run(&mut self, orb: &mut Orb) -> Result<()> {
orb.main_mcu.send(mcu::main::Input::FrameRate(IR_CAMERA_FRAME_RATE)).await?;
orb.disable_ir_led().await?;
orb.main_mcu.send(mcu::main::Input::LiquidLens(None)).await?;
#[cfg(feature = "livestream")]
orb.enable_livestream()?;
orb.enable_ir_net().await?;
orb.enable_rgb_net(true).await?;
orb.start_ir_eye_camera().await?;
orb.start_ir_face_camera().await?;
orb.start_rgb_camera(RGB_FPS).await?;
orb.start_thermal_camera().await?;
orb.start_depth_camera().await?;
orb.start_ir_auto_exposure(IR_TARGET_MEAN).await?;
orb.start_ir_auto_focus(MIN_SHARPNESS, false).await?;
orb.enable_eye_tracker()?;
orb.enable_mirror()?;
orb.set_fisheye(RGB_REDUCED_WIDTH, RGB_REDUCED_HEIGHT, false).await?;
let mut calibration = loop {
orb.run(self).await?;
match self.command.take().expect("command should be set") {
Command::Quit(calibration) => {
break calibration;
}
Command::Recalibrate(calibration) => {
orb.mirror
.enabled()
.expect("mirror should be enabled")
.send(port::Input::new(mirror::Command::Recalibrate(calibration)))
.await?;
}
Command::SwitchEye(target_left_eye) => {
orb.set_target_left_eye(target_left_eye).await?;
}
Command::EyePidControllerToggle(enable_eye_pid_controller) => {
if enable_eye_pid_controller {
orb.enable_eye_pid_controller()?;
} else if orb.eye_pid_controller.is_enabled() {
orb.stop_eye_pid_controller().await?;
}
}
Command::ThermalCameraToggle(enable_thermal_camera) => {
if enable_thermal_camera {
orb.start_thermal_camera().await?;
} else {
orb.stop_thermal_camera().await?;
}
}
Command::ThermalCameraCalibrate => {
if let Some(thermal_camera) = orb.thermal_camera.enabled() {
thermal_camera
.send(port::Input::new(camera::thermal::Command::FscCalibrate))
.await?;
}
}
}
};
orb.stop_eye_tracker().await?;
if orb.eye_pid_controller.is_enabled() {
if let Some(mirror_offset) = orb.stop_eye_pid_controller().await? {
calibration.mirror.phi_offset_degrees += mirror_offset.phi_degrees;
calibration.mirror.theta_offset_degrees += mirror_offset.theta_degrees;
}
}
orb.stop_ir_auto_focus().await?;
#[cfg(feature = "livestream")]
orb.disable_livestream();
if orb.thermal_camera.is_enabled() {
orb.stop_thermal_camera().await?;
}
orb.stop_depth_camera().await?;
orb.stop_rgb_camera().await?;
orb.stop_ir_face_camera().await?;
orb.stop_ir_eye_camera().await?;
orb.disable_agents();
calibration.store(CALIBRATION_FILE_PATH).await?;
Ok(())
}
}
fn spawn_command_thread(mut command_tx: mpsc::Sender<Command>, mut calibration: Calibration) {
fn recalibrate(
stdout: &RawTerminal<Stdout>,
command_tx: &mut mpsc::Sender<Command>,
calibration: &Calibration,
) {
command_tx.send_now(Command::Recalibrate(calibration.clone())).unwrap();
stdout.suspend_raw_mode().unwrap();
println!("{}", serde_json::to_string_pretty(&calibration).unwrap());
stdout.activate_raw_mode().unwrap();
}
thread::spawn(move || {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
write!(
stdout,
"{}{}q to exit, arrow keys to move, space to switch the active eye, p to run \
eye_pid_controller, t to toggle thermal camera, T to calibrate thermal camera.{}",
termion::clear::All,
termion::cursor::Goto(1, 1),
termion::cursor::Goto(1, 3)
)
.unwrap();
stdout.flush().unwrap();
let mut target_left_eye = false;
let mut enable_eye_pid_controller = false;
let mut enable_thermal_camera = true;
for c in stdin.keys() {
write!(stdout, "{}{}", termion::cursor::Goto(1, 3), termion::clear::AfterCursor)
.unwrap();
match c.unwrap() {
Key::Char('q') => {
drop(stdout);
command_tx.send_now(Command::Quit(calibration)).unwrap();
break;
}
Key::Char(' ') => {
target_left_eye = !target_left_eye;
command_tx.send_now(Command::SwitchEye(target_left_eye)).unwrap();
}
Key::Char('p') => {
enable_eye_pid_controller = !enable_eye_pid_controller;
command_tx
.send_now(Command::EyePidControllerToggle(enable_eye_pid_controller))
.unwrap();
}
Key::Char('t') => {
enable_thermal_camera = !enable_thermal_camera;
command_tx
.send_now(Command::ThermalCameraToggle(enable_thermal_camera))
.unwrap();
}
Key::Char('T') => {
command_tx.send_now(Command::ThermalCameraCalibrate).unwrap();
}
Key::Left => {
calibration.mirror.phi_offset_degrees -= 0.1;
recalibrate(&stdout, &mut command_tx, &calibration);
}
Key::Right => {
calibration.mirror.phi_offset_degrees += 0.1;
recalibrate(&stdout, &mut command_tx, &calibration);
}
Key::Up => {
calibration.mirror.theta_offset_degrees -= 0.1;
recalibrate(&stdout, &mut command_tx, &calibration);
}
Key::Down => {
calibration.mirror.theta_offset_degrees += 0.1;
recalibrate(&stdout, &mut command_tx, &calibration);
}
_ => {}
}
write!(stdout, "{}", termion::cursor::Goto(1, 3)).unwrap();
stdout.flush().unwrap();
}
});
}
fn main() -> Result<()> {
async_main(run())
}
async fn run() -> Result<()> {
let ui = ui::Jetson::spawn();
let config = Arc::new(Mutex::new(Config::load_or_default().await));
config.lock().await.propagate_to_ui(&ui);
let cpu_monitor = Box::new(monitor::cpu::Jetson::spawn());
let main_mcu = mcu::main::Jetson::spawn()?;
let net_monitor = monitor::net::Jetson::spawn(Arc::clone(&config))?;
ui.pause();
orb::short_lived_token::wait_for_token().await;
Config::download_and_store(Arc::clone(&config)).await?;
let mut orb = Box::pin(
Orb::builder()
.config(config)
.ui(Box::new(ui))
.main_mcu(Box::new(main_mcu))
.net_monitor(Box::new(net_monitor))
.cpu_monitor(cpu_monitor)
.build(),
)
.await?;
Plan::new(orb.calibration().clone()).await.run(&mut orb).await?;
Ok(())
}