update devkit server and model files

This commit is contained in:
lenn
2026-06-02 09:43:05 +08:00
parent 0812142359
commit 78c4445b93
40 changed files with 66301 additions and 316 deletions

View File

@@ -27,6 +27,14 @@ message PztAngleResponse {
uint32 dts_ms = 4;
bool ok = 5;
string message = 6;
float magnitude = 7;
int32 state = 8;
float cop_x = 9;
float cop_y = 10;
float base_x = 11;
float base_y = 12;
float total_press = 13;
float threshold = 14;
}
message ProcessRequest {

View File

@@ -2,11 +2,26 @@
//!
//! 仅在 `devkit` feature 启用时编译。
use serde::Serialize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use tauri::State;
#[cfg(feature = "devkit")]
use tauri::AppHandle;
use crate::devkit::{DevKitConfig, DevKitState, DevKitStatusSnapshot, ExportProcessResult};
use crate::devkit::{
proto::SensorFrame, DevKitConfig, DevKitState, DevKitStatusSnapshot, ExportProcessResult,
};
static REPLAY_SEQ_COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DevKitReplayFramePushResult {
pub seq: u64,
pub timestamp_ms: u64,
pub dts_ms: u32,
}
#[tauri::command]
pub fn devkit_status(state: State<'_, DevKitState>) -> DevKitStatusSnapshot {
@@ -51,3 +66,55 @@ pub async fn devkit_process_export(
let use_xlsx = save_as_xlsx.unwrap_or(config.save_as_xlsx);
state.process_export(&csv_path, use_xlsx).await
}
#[tauri::command]
pub fn devkit_push_replay_frame(
state: State<'_, DevKitState>,
values: Vec<i32>,
dts_ms: u32,
seq: Option<u64>,
) -> Result<DevKitReplayFramePushResult, String> {
if values.len() != 84 {
return Err(format!("InvalidReplayMatrixLength: {}", values.len()));
}
if !state.running.load(Ordering::Relaxed) {
return Err("NotRunning".to_string());
}
let timestamp_ms = now_millis();
let seq = seq.unwrap_or_else(|| build_replay_seq(timestamp_ms));
let resultant_force = values.iter().copied().sum::<i32>().max(0) as f64;
let matrix = values
.into_iter()
.map(|value| value.max(0) as u32)
.collect::<Vec<_>>();
state.push_frame(SensorFrame {
seq,
timestamp_ms,
rows: 12,
cols: 7,
matrix,
resultant_force,
dts_ms,
});
Ok(DevKitReplayFramePushResult {
seq,
timestamp_ms,
dts_ms,
})
}
fn now_millis() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|duration| duration.as_millis() as u64)
.unwrap_or_default()
}
fn build_replay_seq(timestamp_ms: u64) -> u64 {
let counter = REPLAY_SEQ_COUNTER.fetch_add(1, Ordering::Relaxed) % 1000;
timestamp_ms.saturating_mul(1000).saturating_add(counter)
}

View File

@@ -1,7 +1,10 @@
use crate::serial_core::codecs::tactile_a::{
export_recording_csv, TactileACodec, TactileACsvImporter, TactileAHandler,
export_recording_csv, TactileACodec, TactileACsvImporter, TactileADataPacket, TactileAHandler,
};
use crate::serial_core::error::SerialError;
use crate::serial_core::model::HudSpatialForce;
#[cfg(feature = "multi-dim")]
use crate::serial_core::multi_dim_force::PztProcessor;
use crate::serial_core::record::CsvImporter;
use crate::serial_core::serial::{PollMode, TactileAPollRequester};
use crate::serial_core::{serial, TactileARecording};
@@ -44,6 +47,7 @@ pub struct SerialExportResponse {
pub struct SerialImportFrame {
pub data: Vec<i32>,
pub dts_ms: u64,
pub spatial_force: Option<HudSpatialForce>,
}
#[derive(Serialize)]
@@ -322,13 +326,7 @@ pub fn serial_import_csv(
let channel_count = packets.first().map(|item| item.data.len()).unwrap_or(0);
let frame_count = packets.len();
let frames = packets
.into_iter()
.map(|packet| SerialImportFrame {
data: packet.data,
dts_ms: packet.dts_ms,
})
.collect();
let frames = build_import_frames(packets);
Ok(SerialImportResponse {
file_name,
@@ -353,6 +351,44 @@ pub fn serial_import_csv_from_path(file_path: String) -> Result<SerialImportResp
serial_import_csv(file_name, csv_content)
}
fn build_import_frames(packets: Vec<TactileADataPacket>) -> Vec<SerialImportFrame> {
#[cfg(feature = "multi-dim")]
let mut pzt_processor = PztProcessor::new();
packets
.into_iter()
.map(|packet| {
#[cfg(feature = "multi-dim")]
let spatial_force = replay_spatial_force(&mut pzt_processor, &packet.data);
#[cfg(not(feature = "multi-dim"))]
let spatial_force = None;
SerialImportFrame {
data: packet.data,
dts_ms: packet.dts_ms,
spatial_force,
}
})
.collect()
}
#[cfg(feature = "multi-dim")]
fn replay_spatial_force(processor: &mut PztProcessor, values: &[i32]) -> Option<HudSpatialForce> {
let pzt_values = values.iter().map(|value| *value as f32).collect::<Vec<f32>>();
let analysis = processor.get_pzt_analysis(&pzt_values).ok()?;
if !PztProcessor::should_report(&analysis) {
return None;
}
Some(HudSpatialForce {
angle_deg: analysis.angle_deg,
magnitude: analysis.magnitude,
confidence: analysis.confidence,
})
}
fn resolve_record_for_export(
state: &State<'_, SerialConnectionState>,
) -> Result<SharedTactileRecording, SerialError> {

View File

@@ -24,6 +24,14 @@ struct DevKitPztAngleEvent {
timestamp_ms: u64,
dts_ms: u32,
angle: f32,
magnitude: f32,
state: i32,
cop_x: f32,
cop_y: f32,
base_x: f32,
base_y: f32,
total_press: f32,
threshold: f32,
}
// ── DevKit 配置 ────────────────────────────────────────────────────
@@ -276,12 +284,28 @@ async fn run_grpc_upload(
timestamp_ms: message.timestamp_ms,
dts_ms: message.dts_ms,
angle: message.angle,
magnitude: message.magnitude,
state: message.state,
cop_x: message.cop_x,
cop_y: message.cop_y,
base_x: message.base_x,
base_y: message.base_y,
total_press: message.total_press,
threshold: message.threshold,
};
::log::debug!(
"python pzt angle: seq={} dts_ms={} angle={:.2}",
"python pzt angle: seq={} dts_ms={} angle={:.2} magnitude={:.2} state={} cop=({:.2},{:.2}) base=({:.2},{:.2}) total_press={:.2} threshold={:.2}",
message.seq,
message.dts_ms,
message.angle
message.angle,
message.magnitude,
message.state,
message.cop_x,
message.cop_y,
message.base_x,
message.base_y,
message.total_press,
message.threshold
);
app.emit("devkit_pzt_angle", payload)?;
} else {

View File

@@ -154,7 +154,8 @@ pub fn run() {
commands::devkit::devkit_stop,
commands::devkit::devkit_get_config,
commands::devkit::devkit_set_config,
commands::devkit::devkit_process_export
commands::devkit::devkit_process_export,
commands::devkit::devkit_push_replay_frame
]);
#[cfg(not(feature = "devkit"))]

View File

@@ -3,7 +3,7 @@ use fern::{
Dispatch,
};
use log::debug;
use std::{path::{Path, PathBuf}, time::SystemTime};
use std::{path::PathBuf, time::SystemTime};
fn log_directory() -> PathBuf {
let base_dir = std::env::var_os("LOCALAPPDATA")
@@ -67,6 +67,7 @@ pub fn setup_logger() {
Dispatch::new()
.level(log::LevelFilter::Debug)
.level_for("h2", log::LevelFilter::Info)
.chain(console_config)
.chain(file_config)
.apply()

View File

@@ -17,7 +17,6 @@ const ACTIVE_CELL_MIN_VALUE: f32 = 18.0;
const ACTIVE_CELL_PEAK_RATIO: f32 = 0.14;
const MIN_ACTIVE_CELLS: usize = 3;
const ANCHOR_LERP_ALPHA: f32 = 0.018;
const VECTOR_SMOOTHING_ALPHA: f32 = 0.16;
const REPORT_MAGNITUDE_ENTER: f32 = 0.12;
@@ -29,6 +28,12 @@ const REPORT_HOLD_FRAMES: usize = 10;
const ASYMMETRY_WEIGHT: f32 = 1.1;
const DRIFT_WEIGHT: f32 = 0.65;
const MOTION_WEIGHT: f32 = 0.25;
const EDGE_ASYMMETRY_DAMPING: f32 = 0.35;
const EDGE_INWARD_ROLLING_BIAS: f32 = 0.55;
const EDGE_START_COP_THRESHOLD: f32 = 0.45;
const EDGE_START_BIAS_WEIGHT: f32 = 1.1;
const ROLLING_FRICTION_ALPHA: f32 = 0.68;
const ROLLING_FRICTION_MIN_MAGNITUDE: f32 = 0.05;
#[derive(Debug, Clone, Copy)]
pub struct PztSpatialAnalysis {
@@ -50,6 +55,8 @@ pub struct PztProcessor {
anchor_cop_y: Option<f32>,
last_cop_x: Option<f32>,
last_cop_y: Option<f32>,
edge_start_bias_x: f32,
edge_start_bias_y: f32,
smoothed_x: f32,
smoothed_y: f32,
report_active: bool,
@@ -84,6 +91,8 @@ impl PztProcessor {
anchor_cop_y: None,
last_cop_x: None,
last_cop_y: None,
edge_start_bias_x: 0.0,
edge_start_bias_y: 0.0,
smoothed_x: 0.0,
smoothed_y: 0.0,
report_active: false,
@@ -100,6 +109,8 @@ impl PztProcessor {
self.anchor_cop_y = None;
self.last_cop_x = None;
self.last_cop_y = None;
self.edge_start_bias_x = 0.0;
self.edge_start_bias_y = 0.0;
self.smoothed_x = 0.0;
self.smoothed_y = 0.0;
}
@@ -273,6 +284,112 @@ impl PztProcessor {
(angle, magnitude)
}
fn contact_touches_edge(stats: &ContactStats) -> bool {
stats.min_row == 0
|| stats.max_row == SENSOR_ROWS - 1
|| stats.min_col == 0
|| stats.max_col == SENSOR_COLS - 1
}
fn damp_edge_asymmetry(
stats: &ContactStats,
kinematic_x: f32,
kinematic_y: f32,
) -> (f32, f32) {
let mut asymmetry_x = stats.asymmetry_x * ASYMMETRY_WEIGHT;
let mut asymmetry_y = stats.asymmetry_y * ASYMMETRY_WEIGHT;
if stats.min_col == 0 && asymmetry_x < 0.0 {
asymmetry_x = -asymmetry_x * EDGE_INWARD_ROLLING_BIAS;
}
if stats.max_col == SENSOR_COLS - 1 && asymmetry_x > 0.0 {
asymmetry_x = -asymmetry_x * EDGE_INWARD_ROLLING_BIAS;
}
if stats.min_row == 0 && asymmetry_y < 0.0 {
asymmetry_y = -asymmetry_y * EDGE_INWARD_ROLLING_BIAS;
}
if stats.max_row == SENSOR_ROWS - 1 && asymmetry_y > 0.0 {
asymmetry_y = -asymmetry_y * EDGE_INWARD_ROLLING_BIAS;
}
if Self::contact_touches_edge(stats) {
let opposing_dot = asymmetry_x * kinematic_x + asymmetry_y * kinematic_y;
let kinematic_mag = (kinematic_x * kinematic_x + kinematic_y * kinematic_y).sqrt();
if opposing_dot < 0.0 && kinematic_mag >= ROLLING_FRICTION_MIN_MAGNITUDE {
asymmetry_x *= EDGE_ASYMMETRY_DAMPING;
asymmetry_y *= EDGE_ASYMMETRY_DAMPING;
}
}
(asymmetry_x, asymmetry_y)
}
fn edge_start_bias(stats: &ContactStats) -> (f32, f32) {
let center_x = (SENSOR_COLS - 1) as f32 * 0.5;
let center_y = (SENSOR_ROWS - 1) as f32 * 0.5;
let normalized_x = ((stats.cop_x - center_x) / center_x.max(1.0)).clamp(-1.0, 1.0);
let normalized_y = ((stats.cop_y - center_y) / center_y.max(1.0)).clamp(-1.0, 1.0);
let mut bias_x = 0.0;
let mut bias_y = 0.0;
if stats.min_col == 0 || stats.max_col == SENSOR_COLS - 1 {
bias_x = Self::edge_start_axis_bias(normalized_x);
}
if stats.min_row == 0 || stats.max_row == SENSOR_ROWS - 1 {
bias_y = Self::edge_start_axis_bias(normalized_y);
}
(bias_x, bias_y)
}
fn edge_start_axis_bias(normalized_axis: f32) -> f32 {
let distance = normalized_axis.abs();
if distance <= EDGE_START_COP_THRESHOLD {
return 0.0;
}
let strength = ((distance - EDGE_START_COP_THRESHOLD) / (1.0 - EDGE_START_COP_THRESHOLD))
.clamp(0.0, 1.0);
-normalized_axis.signum() * strength * EDGE_START_BIAS_WEIGHT
}
fn apply_rolling_friction(
previous_x: f32,
previous_y: f32,
current_x: f32,
current_y: f32,
) -> (f32, f32) {
let previous_mag = (previous_x * previous_x + previous_y * previous_y).sqrt();
let current_mag = (current_x * current_x + current_y * current_y).sqrt();
if previous_mag < ROLLING_FRICTION_MIN_MAGNITUDE
|| current_mag < ROLLING_FRICTION_MIN_MAGNITUDE
{
return (current_x, current_y);
}
let dot = previous_x * current_x + previous_y * current_y;
if dot >= 0.0 {
return (current_x, current_y);
}
let mixed_x = current_x * (1.0 - ROLLING_FRICTION_ALPHA)
+ previous_x * ROLLING_FRICTION_ALPHA;
let mixed_y = current_y * (1.0 - ROLLING_FRICTION_ALPHA)
+ previous_y * ROLLING_FRICTION_ALPHA;
if mixed_x * previous_x + mixed_y * previous_y >= 0.0 {
return (mixed_x, mixed_y);
}
let keep_mag = previous_mag.min(current_mag) * 0.5;
(
previous_x / previous_mag * keep_mag,
previous_y / previous_mag * keep_mag,
)
}
fn update_contact_state(&mut self, raw_frame: &[f32], frame: &[f32]) -> bool {
if self.contact_active {
if Self::is_contact_exit_frame(frame) {
@@ -376,6 +493,9 @@ impl PztProcessor {
self.anchor_cop_y = Some(stats.cop_y);
self.last_cop_x = Some(stats.cop_x);
self.last_cop_y = Some(stats.cop_y);
let (edge_start_bias_x, edge_start_bias_y) = Self::edge_start_bias(&stats);
self.edge_start_bias_x = edge_start_bias_x;
self.edge_start_bias_y = edge_start_bias_y;
return Ok(self.stabilize_report(Self::weak_contact_analysis()));
};
@@ -388,18 +508,25 @@ impl PztProcessor {
let motion_x = stats.cop_x - last_x;
let motion_y = stats.cop_y - last_y;
let combined_x = stats.asymmetry_x * ASYMMETRY_WEIGHT
+ drift_x * DRIFT_WEIGHT
+ motion_x * MOTION_WEIGHT;
let combined_y = stats.asymmetry_y * ASYMMETRY_WEIGHT
+ drift_y * DRIFT_WEIGHT
+ motion_y * MOTION_WEIGHT;
let kinematic_x = drift_x * DRIFT_WEIGHT + motion_x * MOTION_WEIGHT;
let kinematic_y = drift_y * DRIFT_WEIGHT + motion_y * MOTION_WEIGHT;
let edge_bias_x = self.edge_start_bias_x;
let edge_bias_y = self.edge_start_bias_y;
let (asymmetry_x, asymmetry_y) =
Self::damp_edge_asymmetry(&stats, kinematic_x + edge_bias_x, kinematic_y + edge_bias_y);
let combined_x = asymmetry_x + kinematic_x + edge_bias_x;
let combined_y = asymmetry_y + kinematic_y + edge_bias_y;
let (combined_x, combined_y) = Self::apply_rolling_friction(
self.smoothed_x,
self.smoothed_y,
combined_x,
combined_y,
);
self.smoothed_x += (combined_x - self.smoothed_x) * VECTOR_SMOOTHING_ALPHA;
self.smoothed_y += (combined_y - self.smoothed_y) * VECTOR_SMOOTHING_ALPHA;
self.anchor_cop_x = Some(anchor_x + drift_x * ANCHOR_LERP_ALPHA);
self.anchor_cop_y = Some(anchor_y + drift_y * ANCHOR_LERP_ALPHA);
self.last_cop_x = Some(stats.cop_x);
self.last_cop_y = Some(stats.cop_y);
@@ -446,7 +573,7 @@ impl PztProcessor {
#[cfg(test)]
mod tests {
use super::{PztProcessor, SENSOR_COLS, SENSOR_ROWS};
use super::{ContactStats, PztProcessor, SENSOR_COLS, SENSOR_ROWS};
fn index(row: usize, col: usize) -> usize {
row * SENSOR_COLS + col
@@ -460,6 +587,23 @@ mod tests {
frame
}
fn stats_touching_bottom_edge() -> ContactStats {
ContactStats {
total: 1000.0,
peak: 300.0,
active_total: 900.0,
active_cells: 6,
min_row: SENSOR_ROWS - 2,
max_row: SENSOR_ROWS - 1,
min_col: 2,
max_col: 4,
cop_x: 3.0,
cop_y: 10.5,
asymmetry_x: 0.0,
asymmetry_y: 1.0,
}
}
#[test]
fn idle_frame_does_not_report_contact() {
let mut processor = PztProcessor::new();
@@ -524,4 +668,29 @@ mod tests {
let analysis = processor.get_pzt_analysis(&weak).unwrap();
assert!(analysis.reportable);
}
#[test]
fn bottom_edge_outward_gradient_is_turned_inward() {
let stats = stats_touching_bottom_edge();
let (_asymmetry_x, asymmetry_y) = PztProcessor::damp_edge_asymmetry(&stats, 0.0, -0.2);
assert!(asymmetry_y < 0.0);
assert!(asymmetry_y > -1.1);
}
#[test]
fn bottom_edge_start_adds_fixed_upward_bias() {
let stats = stats_touching_bottom_edge();
let (_bias_x, bias_y) = PztProcessor::edge_start_bias(&stats);
assert!(bias_y < 0.0);
}
#[test]
fn rolling_friction_resists_one_frame_reversal() {
let (x, y) = PztProcessor::apply_rolling_friction(0.4, 0.0, -0.6, 0.0);
assert!(x > 0.0);
assert_eq!(y, 0.0);
}
}

View File

@@ -9,7 +9,6 @@ use crate::serial_core::multi_dim_force::PztProcessor;
use crate::serial_core::record::Recording;
use crate::serial_core::record::{FrameTiming, RecordedFrame};
use anyhow::Result;
use log::debug;
use std::future::pending;
#[cfg(feature = "devkit")]
use std::sync::atomic::Ordering;
@@ -311,15 +310,14 @@ where
drop(record);
if let Some(vals) = decode_res {
#[cfg(feature = "multi-dim")]
let mut spatial_force = None;
#[cfg(not(feature = "multi-dim"))]
let spatial_force = None;
#[cfg(feature = "multi-dim")]
{
let pzt_values = vals.iter().map(|value| *value as f32).collect::<Vec<f32>>();
if let Ok(analysis) = pzt_processor.get_pzt_analysis(&pzt_values) {
debug!(
"spatial force: angle={:.2}°, magnitude={:.2}, dx={:.2}, dy={:.2}",
analysis.angle_deg, analysis.magnitude, analysis.planar_x, analysis.planar_y
);
if PztProcessor::should_report(&analysis) {
spatial_force = Some(HudSpatialForce {
angle_deg: analysis.angle_deg,