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