Measure expected Move colors in tracker frames

This commit is contained in:
Meta Vens 2026-07-13 01:18:07 +01:00
commit 249db3b42f
4 changed files with 74 additions and 3 deletions

View file

@ -4880,6 +4880,11 @@ fn run_move_tracker_worker(options: Options) -> Result<()> {
let mut observation_count = 0u64;
let mut last_observation_at = String::new();
let mut last_calibration = Instant::now();
let mut last_image_evidence = Instant::now() - Duration::from_secs(1);
let mut image_mean_rgb = Vec::new();
let mut image_peak_rgb = Vec::new();
let mut color_evidence_move_ids = Vec::new();
let mut color_evidence_pixel_counts = Vec::new();
let stdout = std::io::stdout();
let mut writer = stdout.lock();
loop {
@ -4888,13 +4893,21 @@ fn run_move_tracker_worker(options: Options) -> Result<()> {
tracker.observe_connected();
last_calibration = Instant::now();
}
for (identity, color) in tracker_colors(&roster, &program) {
tracker.set_expected_color(&identity, color);
let expected_colors = tracker_colors(&roster, &program);
for (identity, color) in &expected_colors {
tracker.set_expected_color(identity, *color);
}
let observations = tracker.update();
update_count = update_count.saturating_add(1);
observation_count = observation_count.saturating_add(observations.len() as u64);
if !observations.is_empty() { last_observation_at = timestamp()?; }
if last_image_evidence.elapsed() >= Duration::from_millis(250) {
if let Some((_, _, rgb)) = tracker.rgb_image() {
(image_mean_rgb, image_peak_rgb, color_evidence_move_ids, color_evidence_pixel_counts) =
summarize_tracker_rgb(&rgb, &expected_colors);
}
last_image_evidence = Instant::now();
}
let health = MuninnMoveTrackerHealthRecord {
health_id: format!("muninn:{}:{}:move-tracker-health", options.host_id, source.camera_id),
host_id: options.host_id.clone(), camera_id: source.camera_id.clone(), camera_index,
@ -4903,6 +4916,9 @@ fn run_move_tracker_worker(options: Options) -> Result<()> {
calibrated_controller_count: tracker.tracked_controller_count() as u32, update_count, observation_count,
latest_observation_count: observations.len() as u32, last_observation_at: last_observation_at.clone(),
detail: "private subprocess worker".to_string(), updated_at: timestamp()?,
image_mean_rgb: image_mean_rgb.clone(), image_peak_rgb: image_peak_rgb.clone(),
color_evidence_move_ids: color_evidence_move_ids.clone(),
color_evidence_pixel_counts: color_evidence_pixel_counts.clone(),
};
let frame = MoveTrackerWorkerFrame {
health,
@ -4912,9 +4928,34 @@ fn run_move_tracker_worker(options: Options) -> Result<()> {
}).collect(),
};
write_move_tracker_worker_frame(&mut writer, &frame)?;
thread::sleep(Duration::from_millis(4));
}
}
#[cfg(feature = "psmoveapi-tracker")]
fn summarize_tracker_rgb(rgb: &[u8], expected: &[(String, [u8; 3])]) -> (Vec<u32>, Vec<u32>, Vec<String>, Vec<u32>) {
let mut sums = [0u64; 3];
let mut peaks = [0u32; 3];
let mut counts = vec![0u32; expected.len()];
let pixels = rgb.len() / 3;
for pixel in rgb.chunks_exact(3) {
for channel in 0..3 { sums[channel] += u64::from(pixel[channel]); peaks[channel] = peaks[channel].max(u32::from(pixel[channel])); }
let max = *pixel.iter().max().unwrap_or(&0);
let min = *pixel.iter().min().unwrap_or(&0);
if max < 40 || max.saturating_sub(min) < 30 { continue; }
let pixel_norm = (pixel.iter().map(|value| f64::from(*value).powi(2)).sum::<f64>()).sqrt();
for (index, (_, color)) in expected.iter().enumerate() {
let color_norm = (color.iter().map(|value| f64::from(*value).powi(2)).sum::<f64>()).sqrt();
let dot = pixel.iter().zip(color).map(|(left, right)| f64::from(*left) * f64::from(*right)).sum::<f64>();
if pixel_norm > 0.0 && color_norm > 0.0 && dot / (pixel_norm * color_norm) >= 0.985 {
counts[index] = counts[index].saturating_add(1);
}
}
}
let means = if pixels == 0 { vec![0, 0, 0] } else { sums.iter().map(|sum| (*sum / pixels as u64) as u32).collect() };
(means, peaks.to_vec(), expected.iter().map(|(id, _)| id.clone()).collect(), counts)
}
#[cfg(feature = "psmoveapi-tracker")]
fn tracker_colors(roster: &[String], program: &MuninnMoveHueProgramRecord) -> Vec<(String, [u8; 3])> {
let now_ns = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos() as i128;

View file

@ -43,6 +43,13 @@ mod linux {
height: c_int,
}
#[repr(C)]
struct TrackerRgbImage {
data: *const u8,
width: c_int,
height: c_int,
}
#[link(name = "psmoveapi")]
#[link(name = "psmoveapi_tracker")]
unsafe extern "C" {
@ -79,6 +86,7 @@ mod linux {
radius: *mut c_float,
) -> c_int;
fn psmove_tracker_get_camera_info(tracker: *mut c_void) -> *const CameraInfo;
fn psmove_tracker_get_image(tracker: *mut c_void) -> TrackerRgbImage;
}
struct Controller {
@ -237,6 +245,18 @@ mod linux {
}
observations
}
pub fn rgb_image(&self) -> Option<(u32, u32, Vec<u8>)> {
let image = unsafe { psmove_tracker_get_image(self.tracker) };
if image.data.is_null() || image.width <= 0 || image.height <= 0 {
return None;
}
let byte_len = (image.width as usize)
.checked_mul(image.height as usize)?
.checked_mul(3)?;
let bytes = unsafe { core::slice::from_raw_parts(image.data, byte_len) };
Some((image.width as u32, image.height as u32, bytes.to_vec()))
}
}
impl Drop for PsmoveApiTracker {
@ -292,6 +312,9 @@ impl PsmoveApiTracker {
pub fn update(&mut self) -> Vec<PsmoveApiObservation> {
Vec::new()
}
pub fn rgb_image(&self) -> Option<(u32, u32, Vec<u8>)> { None }
}
#[cfg(test)]

View file

@ -26,7 +26,7 @@ fn main() -> Result<()> {
records.sort_by(|left, right| left.camera_id.cmp(&right.camera_id));
for record in records {
println!(
"camera={} index={} state={} backend={}/{} size={}x{} exposure={} calibrated={} updates={} observations={} latest={} last_observation={} updated={} detail={}",
"camera={} index={} state={} backend={}/{} size={}x{} exposure={} calibrated={} updates={} observations={} latest={} last_observation={} mean_rgb={:?} peak_rgb={:?} color_pixels={:?} updated={} detail={}",
record.camera_id,
record.camera_index,
record.state,
@ -40,6 +40,9 @@ fn main() -> Result<()> {
record.observation_count,
record.latest_observation_count,
record.last_observation_at,
record.image_mean_rgb,
record.image_peak_rgb,
record.color_evidence_move_ids.iter().cloned().zip(record.color_evidence_pixel_counts.iter().copied()).collect::<Vec<_>>(),
record.updated_at,
record.detail,
);

View file

@ -1216,6 +1216,10 @@ pub struct MuninnMoveTrackerHealthRecord {
#[cultcache(key = 14)] pub last_observation_at: String,
#[cultcache(key = 15)] pub detail: String,
#[cultcache(key = 16)] pub updated_at: String,
#[cultcache(key = 17, default)] pub image_mean_rgb: Vec<u32>,
#[cultcache(key = 18, default)] pub image_peak_rgb: Vec<u32>,
#[cultcache(key = 19, default)] pub color_evidence_move_ids: Vec<String>,
#[cultcache(key = 20, default)] pub color_evidence_pixel_counts: Vec<u32>,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]