comparison web/calibrate/Light9Calibrate.ts @ 2417:ae4b90efb55a

start calibration tool
author drewp@bigasterisk.com
date Mon, 20 May 2024 01:28:12 -0700
parents
children e3af0ac507c8
comparison
equal deleted inserted replaced
2416:61dc5bc8ce2e 2417:ae4b90efb55a
1 import debug from "debug";
2 import { css, html, LitElement } from "lit";
3 import { customElement, query, state } from "lit/decorators.js";
4 import { CollectorClient } from "../collector/CollectorClient";
5 import { getTopGraph } from "../RdfdbSyncedGraph";
6 import { SyncedGraph } from "../SyncedGraph";
7 import { Light9Camera } from "./Light9Camera";
8 import { XyPlot } from "./XyPlot";
9 export { RdfdbSyncedGraph } from "../RdfdbSyncedGraph";
10 export { Light9Camera } from "./Light9Camera";
11 export { XyPlot } from "./XyPlot";
12 debug.enable("*");
13 const log = debug("calibrate");
14
15 async function sleep(ms: number) {
16 return new Promise((resolve) => setTimeout(resolve, ms));
17 }
18
19 @customElement("light9-calibrate")
20 export class Light9Calibrate extends LitElement {
21 graph!: SyncedGraph;
22 static styles = [
23 css`
24 button {
25 min-height: 3em;
26 min-width: 10em;
27 }
28 `,
29 ];
30 collector: CollectorClient = new CollectorClient("calibrate");
31 @query("light9-camera", true) cam?: Light9Camera;
32 @query("xy-plot", true) plot?: XyPlot;
33 @state() device: string;
34 constructor() {
35 super();
36 this.device = "http://light9.bigasterisk.com/theater/vet/device/parR3";
37 getTopGraph().then((g) => {
38 this.graph = g;
39 });
40 }
41
42 render() {
43 return html`<rdfdb-synced-graph></rdfdb-synced-graph>
44 <h1>Calibrate</h1>
45 <light9-camera></light9-camera>
46
47 <p>Device to calibrate: [ ${this.device} ]</p>
48
49 <ol>
50 <li><button @click=${this.setToFull}>Set to full</button></li>
51 <li><button @click=${this.findSafeExposure}>Find safe exposure</button></li>
52 <li><button @click=${this.setToZero}>Set to 0</button></li>
53 <li><button @click=${this.markTare}>Mark tare</button></li>
54 <li><button @click=${this.calibrateLoop}>Calibrate loop</button></li>
55 </ol>
56 <xy-plot label="zebra pixels vs exposure"></xy-plot>
57 r/g/b/r*g*b lines ,x=send y=seen `;
58 }
59
60 async withButtonSpinner(ev: MouseEvent, fn: () => Promise<void>) {
61 const btn = ev.target as HTMLButtonElement;
62 try {
63 btn.disabled = true;
64 await fn();
65 } finally {
66 btn.disabled = false;
67 }
68 }
69 async setToFull(ev: MouseEvent) {
70 await this.withButtonSpinner(ev, async () => {
71 this.collector.updateSettings([
72 /// device,attr,value
73 [this.device, "http://light9.bigasterisk.com/color", "#ffffff"],
74 [this.device, "http://light9.bigasterisk.com/white", 1],
75 ]);
76 });
77 }
78
79 async findSafeExposure(ev: MouseEvent) {
80 await this.withButtonSpinner(ev, async () => {
81
82 const gatherSample = async (expo: number) => {
83 await this.cam?.set("exposureTime", expo);
84 const settleUntil = Date.now() + 1000;
85 let miny = this.cam?.saturatedPixelCount!;
86 while (Date.now() < settleUntil) {
87 await sleep(50);
88 miny = Math.min(miny, this.cam?.saturatedPixelCount!);
89 }
90 this.plot!.insertPoint(expo, miny);
91 };
92
93 // todo: drive around without big skips, gradually slower, looking for the max workable expo
94 let fixedSteps = 8;
95 const expoMin = 1;
96 const expoMax = 200;
97 let expo = 0;
98 const data=this.plot!.data;
99 while (data.length < 20) {
100 if (data.length < fixedSteps + 1) {
101 expo = expoMin + ((expoMax - expoMin) / fixedSteps) * data.length;
102 } else {
103 let x2 = data.findIndex(([_, y]) => y > 2);
104 if (x2 < 1) x2 = 1;
105 const x1 = x2 - 1;
106 log(JSON.stringify([x1, data[x1], x2, data[x2]]));
107 expo = (data[x1][0] + data[x2][0]) / 2;
108 log(data);
109 }
110 await gatherSample(expo);
111 }
112 });
113 }
114 async setToZero(ev: MouseEvent) {
115 await this.withButtonSpinner(ev, async () => {
116 this.collector.updateSettings([
117 [this.device, "http://light9.bigasterisk.com/color", "#000000"],
118 [this.device, "http://light9.bigasterisk.com/white", 0],
119 ]);
120 });
121 }
122 async markTare(ev: MouseEvent) {
123 await this.withButtonSpinner(ev, async () => {});
124 }
125 async calibrateLoop(ev: MouseEvent) {
126 await this.withButtonSpinner(ev, async () => {});
127 }
128 }