Skip to content

Commit

Permalink
Fix calibration order, add reset (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
DTTerastar authored Oct 6, 2024
1 parent 6e9bfd6 commit 6d0ed07
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 17 deletions.
24 changes: 24 additions & 0 deletions src/Controllers/StateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ void OnDeviceChanged(object? sender, DeviceEventArgs e)
_eventDispatcher.DeviceStateChanged -= OnDeviceChanged;
}
}

[HttpPost("api/state/calibration/reset")]
public async Task<IActionResult> ResetCalibration()
{
try
{
// Reset calibration for all nodes
foreach (var node in _state.Nodes.Values)
{
var nodeSettings = _nsd.Get(node.Id);
nodeSettings.TxRefRssi = null;
nodeSettings.RxAdjRssi = null;
nodeSettings.Absorption = null;
await _nsd.Set(node.Id, nodeSettings);
}

return Ok(new { message = "Calibration reset successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error resetting calibration");
return StatusCode(500, new { error = "An error occurred while resetting calibration" });
}
}
}


6 changes: 3 additions & 3 deletions src/Services/NodeSettingsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public NodeSettings Get(string id)
public async Task Set(string id, NodeSettings ds)
{
var old = Get(id);
if (ds.Absorption != null && ds.Absorption != old.Absorption)
if (ds.Absorption == null || ds.Absorption != old.Absorption)
await mqtt.EnqueueAsync($"espresense/rooms/{id}/absorption/set", $"{ds.Absorption:0.00}");
if (ds.RxAdjRssi != null && ds.RxAdjRssi != old.RxAdjRssi)
if (ds.RxAdjRssi == null || ds.RxAdjRssi != old.RxAdjRssi)
await mqtt.EnqueueAsync($"espresense/rooms/{id}/rx_adj_rssi/set", $"{ds.RxAdjRssi}");
if (ds.TxRefRssi != null && ds.TxRefRssi != old.TxRefRssi)
if (ds.TxRefRssi == null || ds.TxRefRssi != old.TxRefRssi)
await mqtt.EnqueueAsync($"espresense/rooms/{id}/tx_ref_rssi/set", $"{ds.TxRefRssi}");
}

Expand Down
33 changes: 23 additions & 10 deletions src/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ floors:
- id: first
name: First Floor
# Bounds (x,y,z) of map in meters
bounds: [[0, 0, 0], [17, 18, 1.5]]
bounds: [[0, 0, 0], [17, 18, 3]]
rooms:
- name: Powder
points:
Expand Down Expand Up @@ -120,7 +120,7 @@ floors:
- [0, 7.5]
- id: second
name: Second Floor
bounds: [[0, 0, 3.1], [17, 18, 4.6]]
bounds: [[0, 0, 3], [17, 18, 5.5]]
rooms:
- name: Master
points:
Expand Down Expand Up @@ -164,34 +164,43 @@ floors:
# Locations of espresense nodes in meters
nodes:
- name: Master
point: [9.4, 13.7, 3.6]
point: [3.25, 11, 3.2]
floors: ["second"]
- name: Bathroom
point: [0.1, 10, 3.9]
point: [0.05, 9, 3.9]
floors: ["second", "outside"]
- name: Upstairs Hallway
point: [12, 10, 3.2]
point: [13, 7, 3.33]
floors: ["second"]
- name: Garage
point: [0.75, 16.8, 0.5]
floors: ["first", "second", "outside"]
floors: ["first", "outside"]
- name: Office
point: [6.75, 15, 3.6]
point: [6.75, 15.75, 3.6]
floors: ["second"]
- name: Family
point: [0.25, 0.4, 0.3]
floors: ["first", "outside"]
- name: Kitchen
point: [9, 9, 0.85]
point: [9.5, 9, 1]
floors: ["first"]
- name: Nook
point: [9, 2, 0.33]
floors: ["first"]
- name: Dining
point: [16.25, 3, 1.29]
floors: ["first", "outside"]
- name: Basement
point: [16, 13, -1]
floors: ["first", "second", "outside"]
floors: ["first", "outside"]
- name: Laundry
point: [4.6, 7.5, 1]
point: [5.1, 7.5, 1]
floors: ["first"]
- name: Back Stairs
point: [5.2, 7.5, 3.33]
floors: ["first", "second"]
- name: Foyer
point: [10,10.9,0.33]
floors: ["first"]
- name: Mini
stationary: false
Expand All @@ -209,6 +218,10 @@ devices:
- id: "keys:*"
- id: "therm:*"
- id: "iBeacon:*"
- id: "car:*"
- id: "laptop:*"
- id: "itag:f0f010303080"
name: "Piper's Backpack"

# Devices to NOT track
exclude_devices:
Expand Down
44 changes: 42 additions & 2 deletions src/ui/src/lib/CalibrationMatrix.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { calibration } from '$lib/stores';
import { RadioGroup, RadioItem } from '@skeletonlabs/skeleton';
import { popup } from '@skeletonlabs/skeleton';
import { getToastStore } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
enum DataPoint {
ErrorPercent = 0,
Expand Down Expand Up @@ -48,14 +50,51 @@
let rxColumns: Array<string> = [];
$: {
const matrix = $calibration?.matrix ?? {};
const rxSet = new Set<string>();
Object.values($calibration?.matrix ?? {}).forEach((n1) => {
Object.keys(matrix).forEach((key) => rxSet.add(key));
Object.values(matrix).forEach((n1) => {
Object.keys(n1).forEach((key) => rxSet.add(key));
});
rxColumns = Array.from(rxSet);
}
let data_point: DataPoint = 0;
const toastStore = getToastStore();
const modalStore = getModalStore();
async function resetCalibration() {
const confirmed = await new Promise(resolve => {
modalStore.trigger({
type: 'confirm',
title: 'Reset Calibration',
body: 'Are you sure you want to reset the calibration? This will reset rx_adj_rssi, tx_ref_rssi, and absorption for all nodes. This action cannot be undone.',
response: (r: boolean) => resolve(r)
});
});
if (!confirmed) return;
try {
const response = await fetch('/api/state/calibration/reset', { method: 'POST' });
if (response.ok) {
toastStore.trigger({
message: 'Calibration reset successfully',
background: 'variant-filled-success'
});
} else {
const errorText = await response.text();
throw new Error(`Server error ${response.status}: ${errorText}`);
}
} catch (error) {
console.error('Error resetting calibration:', error);
toastStore.trigger({
message: `Failed to reset calibration: ${error.message}`,
background: 'variant-filled-error'
});
}
}
</script>

{#if $calibration?.matrix}
Expand All @@ -76,7 +115,7 @@
<div class="card p-2">
{#if $calibration?.matrix}
<header>
<div class="flex justify-center p-2">
<div class="flex justify-between items-center p-2">
<RadioGroup active="variant-filled-primary" hover="hover:variant-soft-primary">
<RadioItem bind:group={data_point} name="justify" value={0}>Error %</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={1}>Error (m)</RadioItem>
Expand All @@ -85,6 +124,7 @@
<RadioItem bind:group={data_point} name="justify" value={4}>Tx Rssi Ref</RadioItem>
<RadioItem bind:group={data_point} name="justify" value={5}>Variance (m)</RadioItem>
</RadioGroup>
<button class="btn variant-filled-warning" on:click={resetCalibration}> Reset Calibration </button>
</div>
</header>
<section class="p-4 pt-0">
Expand Down
4 changes: 2 additions & 2 deletions src/ui/src/lib/stores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readable, writable, derived } from 'svelte/store';
import { base } from '$app/paths';
import type { Device, Config, Node } from './types';
import type { Device, Config, Node, CalibrationResponse } from './types';

export const showAll: SvelteStore<boolean> = writable(false);
export const config = writable<Config>();
Expand Down Expand Up @@ -131,7 +131,7 @@ export const nodes = readable<Node[]>([], function start(set) {
};
});

export const calibration = readable({}, function start(set) {
export const calibration = readable<CalibrationResponse>({matrix: {}}, function start(set) {
async function fetchAndSet() {
const response = await fetch(`${base}/api/state/calibration`);
var data = await response.json();
Expand Down
20 changes: 20 additions & 0 deletions src/ui/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ export interface Release {
name: string;
}

export interface CalibrationMatrix {
[txName: string]: {
[rxName: string]: {
tx_ref_rssi?: number;
rx_adj_rssi?: number;
absorption?: number;
expected: number;
actual: number;
rssi: number;
err: number;
percent: number;
var?: number;
};
};
}

export interface CalibrationResponse {
matrix: CalibrationMatrix;
}

export function isNode(d: Device | Node | null): d is Node {
return (d as Node)?.telemetry !== undefined;
}

0 comments on commit 6d0ed07

Please sign in to comment.