Spaces:
Running
Running
File size: 21,993 Bytes
d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 310edae d9cdcf5 310edae f2f3129 310edae f2f3129 310edae d9cdcf5 310edae d9cdcf5 310edae d9cdcf5 f2f3129 d9cdcf5 310edae 0018c44 d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 0018c44 f2f3129 310edae 0018c44 d9cdcf5 0018c44 d9cdcf5 0018c44 310edae 0018c44 d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 0018c44 d9cdcf5 f2f3129 d9cdcf5 0018c44 d9cdcf5 0018c44 d9cdcf5 0018c44 d9cdcf5 0018c44 d9cdcf5 0018c44 d9cdcf5 f2f3129 d9cdcf5 0018c44 d9cdcf5 f2f3129 d9cdcf5 f2f3129 d9cdcf5 f2f3129 0018c44 f2f3129 0018c44 f2f3129 0018c44 d9cdcf5 b5f6f02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>リアルタイムセンサーデータプロット</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
}
/* ボタンのスタイル */
button {
padding: 10px 20px;
font-size: 16px;
margin: 10px;
cursor: pointer;
}
/* モーダルダイアログのスタイル */
.modal {
display: none; /* 初期状態では非表示 */
position: fixed; /* 固定位置 */
z-index: 1; /* 他の要素より前面に表示 */
left: 0;
top: 0;
width: 100%; /* 幅を全画面に */
height: 100%; /* 高さを全画面に */
overflow: auto; /* 必要に応じてスクロール */
background-color: rgba(0, 0, 0, 0.4); /* 背景を半透明に */
}
.modal-content {
background-color: #fefefe;
margin: 5% auto; /* 上下にマージン、中央揃え */
padding: 20px;
border: 1px solid #888;
width: 90%; /* 幅を90%に */
max-width: 600px; /* 最大幅を600pxに */
border-radius: 8px;
position: relative;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
position: absolute;
right: 20px;
top: 10px;
cursor: pointer;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
}
.regex-container textarea,
.options-container input[type="number"] {
width: 100%;
padding: 8px;
margin: 8px 0;
box-sizing: border-box;
font-size: 16px;
}
.message {
color: red;
font-weight: bold;
margin-top: 10px;
}
.modal-buttons {
text-align: right;
margin-top: 20px;
}
.modal-buttons button {
margin-left: 10px;
}
/* グラフコンテナのスタイル */
#charts {
display: block;
width: 100%;
box-sizing: border-box;
}
.chart-container {
width: 100%;
height: 30vh; /* ビューポート高さの30% */
margin: 10px 0;
position: relative;
}
svg {
width: 100%;
height: 100%;
border: 1px solid #ccc;
box-sizing: border-box;
}
/* Rawデータ表示のスタイル */
#rawDataOutput {
width: 90%;
margin: 20px auto;
text-align: left;
}
#rawDataDisplay {
width: 100%;
max-height: 200px;
overflow: auto;
border: 1px solid #ccc;
padding: 10px;
background-color: #f9f9f9;
white-space: pre-wrap;
}
</style>
<!-- D3.jsの読み込み -->
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<h1>リアルタイムセンサーデータプロット</h1>
<button id="connectButton">シリアルポートに接続</button>
<button id="settingsButton">設定</button>
<div id="charts">
<div class="chart-container">
<h3>左センサー</h3>
<svg id="leftChart"></svg>
</div>
<div class="chart-container">
<h3>中央センサー</h3>
<svg id="centerChart"></svg>
</div>
<div class="chart-container">
<h3>右センサー</h3>
<svg id="rightChart"></svg>
</div>
</div>
<div id="rawDataOutput">
<h2>Rawデータ</h2>
<pre id="rawDataDisplay"></pre>
</div>
<!-- モーダルダイアログ -->
<div id="settingsModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>設定</h2>
<div class="regex-container">
<h3>正規表現設定</h3>
<p>以下の正規表現を使用してシリアルデータを解析します。3つのキャプチャグループ(左、中央、右センサー)を含むように設定してください。</p>
<textarea id="regexInput">Left Sensor:\s*(\d+)\s*\|\s*Center Sensor:\s*(\d+)\s*\|\s*Right Sensor:\s*(\d+)</textarea><br>
<button id="applyRegexButton">適用</button>
<div id="regexMessage" class="message"></div>
</div>
<div class="options-container">
<h3>表示オプション</h3>
<label>
<input type="checkbox" id="includeZeroCheckbox" checked>
Y軸に0を含める
</label>
<br>
<label>
<input type="checkbox" id="useSplineCheckbox" checked>
スプライン曲線を使用
</label>
</div>
<div class="options-container">
<h3>データ保持数</h3>
<input type="number" id="maxPointsInput" value="75" min="10" max="1000">
<button id="applyMaxPointsButton">適用</button>
<div id="maxPointsMessage" class="message"></div>
</div>
<div class="modal-buttons">
<button id="closeSettingsButton">閉じる</button>
</div>
</div>
</div>
<script>
// -------------------------------
// グローバル変数の設定
// -------------------------------
let MAX_POINTS = 75; // データ保持数(変更可能に)
const leftData = [];
const centerData = [];
const rightData = [];
const timeData = [];
let currentTime = 0;
let sensorPattern = /Left Sensor:\s*(\d+)\s*\|\s*Center Sensor:\s*(\d+)\s*\|\s*Right Sensor:\s*(\d+)/;
// -------------------------------
// オプション関連の要素取得
// -------------------------------
const includeZeroCheckbox = document.getElementById('includeZeroCheckbox');
const useSplineCheckbox = document.getElementById('useSplineCheckbox');
const maxPointsInput = document.getElementById('maxPointsInput');
const applyMaxPointsButton = document.getElementById('applyMaxPointsButton');
const maxPointsMessage = document.getElementById('maxPointsMessage');
// -------------------------------
// モーダルダイアログの設定
// -------------------------------
const settingsModal = document.getElementById('settingsModal');
const settingsButton = document.getElementById('settingsButton');
const closeModalSpan = document.querySelector('.modal .close');
const closeSettingsButton = document.getElementById('closeSettingsButton');
// モーダルを開く
settingsButton.onclick = function() {
settingsModal.style.display = "block";
}
// モーダルを閉じる(×ボタン)
closeModalSpan.onclick = function() {
settingsModal.style.display = "none";
}
// モーダルを閉じる(閉じるボタン)
closeSettingsButton.onclick = function() {
settingsModal.style.display = "none";
}
// モーダル外をクリックすると閉じる
window.onclick = function(event) {
if (event.target == settingsModal) {
settingsModal.style.display = "none";
}
}
// -------------------------------
// オプションのイベントリスナー
// -------------------------------
includeZeroCheckbox.addEventListener('change', () => {
updateCharts();
});
useSplineCheckbox.addEventListener('change', () => {
updateLineCurves();
updateCharts();
});
applyMaxPointsButton.addEventListener('click', () => {
const newMax = parseInt(maxPointsInput.value, 10);
if (isNaN(newMax) || newMax < 10 || newMax > 1000) {
maxPointsMessage.style.color = 'red';
maxPointsMessage.textContent = '有効な数値を入力してください(10〜1000)。';
return;
}
// 新しいMAX_POINTSを設定
MAX_POINTS = newMax;
maxPointsMessage.style.color = 'green';
maxPointsMessage.textContent = `データ保持数が${newMax}に設定されました。`;
// データ配列を更新
[leftData, centerData, rightData, timeData].forEach(dataArray => {
while (dataArray.length > MAX_POINTS) {
dataArray.shift();
}
});
// スケールを再設定
for (let key in charts) {
const chart = charts[key];
chart.xScale.domain([0, MAX_POINTS - 1]);
chart.svg.select(".x-axis")
.transition()
.duration(500)
.call(d3.axisBottom(chart.xScale));
}
updateCharts();
});
// -------------------------------
// スプライン曲線の更新関数
// -------------------------------
function updateLineCurves() {
const curveType = useSplineCheckbox.checked ? d3.curveBasis : d3.curveLinear;
for (let key in charts) {
const chart = charts[key];
chart.line.curve(curveType);
// パスを再設定
chart.path
.datum(chart.data)
.attr("d", chart.line);
}
}
// -------------------------------
// D3.jsによるグラフの初期設定
// -------------------------------
const charts = {
left: {
svg: d3.select("#leftChart"),
data: leftData,
color: "red",
path: null,
xScale: null,
yScale: null,
line: null
},
center: {
svg: d3.select("#centerChart"),
data: centerData,
color: "green",
path: null,
xScale: null,
yScale: null,
line: null
},
right: {
svg: d3.select("#rightChart"),
data: rightData,
color: "blue",
path: null,
xScale: null,
yScale: null,
line: null
}
};
function initCharts() {
for (let key in charts) {
const chart = charts[key];
const svgElement = chart.svg.node();
const boundingRect = svgElement.getBoundingClientRect();
const width = boundingRect.width;
const height = boundingRect.height;
// スケールの設定
chart.xScale = d3.scaleLinear().domain([0, MAX_POINTS - 1]).range([50, width - 50]);
chart.yScale = d3.scaleLinear().domain([0, 1023]).range([height - 30, 20]); // マージンを考慮
// 軸の追加
chart.svg.append("g")
.attr("transform", `translate(0,${height - 30})`)
.attr("class", "x-axis")
.call(d3.axisBottom(chart.xScale));
chart.svg.append("g")
.attr("transform", `translate(50,0)`)
.attr("class", "y-axis")
.call(d3.axisLeft(chart.yScale));
// パスの生成
chart.line = d3.line()
.x((d, i) => chart.xScale(i))
.y(d => chart.yScale(d))
.curve(useSplineCheckbox.checked ? d3.curveBasis : d3.curveLinear); // 初期曲線タイプ
chart.path = chart.svg.append("path")
.datum(chart.data)
.attr("fill", "none")
.attr("stroke", chart.color)
.attr("stroke-width", 2)
.attr("d", chart.line);
}
// リサイズイベントの設定
window.addEventListener('resize', () => {
resizeCharts();
});
}
initCharts();
// -------------------------------
// シリアルポートへの接続とデータ読み取り
// -------------------------------
const connectButton = document.getElementById('connectButton');
connectButton.addEventListener('click', async () => {
try {
// シリアルポートのリクエスト
const port = await navigator.serial.requestPort();
// シリアルポートを開く
await port.open({ baudRate: 9600 });
// テキストストリームの作成
const textDecoder = new TextDecoderStream();
const readableStreamClosed = port.readable.pipeTo(textDecoder.writable);
const reader = textDecoder.readable.getReader();
// データの読み取り
while (true) {
const { value, done } = await reader.read();
if (done) {
// 読み取りストリームが終了した場合
break;
}
if (value) {
// 改行でデータを分割
const lines = value.split('\n');
lines.forEach(line => processData(line.trim()));
}
}
} catch (error) {
console.error('シリアルポート接続エラー:', error);
alert('シリアルポートの接続に失敗しました。コンソールを確認してください。');
}
});
// -------------------------------
// 正規表現の適用
// -------------------------------
const applyRegexButton = document.getElementById('applyRegexButton');
const regexInput = document.getElementById('regexInput');
const regexMessage = document.getElementById('regexMessage');
applyRegexButton.addEventListener('click', () => {
const userRegex = regexInput.value.trim();
if (!userRegex) {
regexMessage.textContent = '正規表現を入力してください。';
regexMessage.style.color = 'red';
return;
}
try {
// 新しい正規表現をコンパイル
const newPattern = new RegExp(userRegex);
// テストデータで正規表現を確認(3つのキャプチャグループがあるか)
const testMatch = newPattern.exec('Left Sensor: 123 | Center Sensor: 456 | Right Sensor: 789');
if (!testMatch || testMatch.length < 4) {
throw new Error('正規表現には3つのキャプチャグループが必要です。');
}
// 正規表現を更新
sensorPattern = newPattern;
regexMessage.style.color = 'green';
regexMessage.textContent = '正規表現が正常に適用されました。';
} catch (e) {
console.error('正規表現エラー:', e);
regexMessage.style.color = 'red';
regexMessage.textContent = `正規表現エラー: ${e.message}`;
}
});
// -------------------------------
// データの処理関数
// -------------------------------
let lastLeft = null;
let lastCenter = null;
let lastRight = null;
function processData(line) {
if (!line) return; // 空行は無視
const match = sensorPattern.exec(line);
if (match) {
if (match.length < 4) {
console.warn('正規表現が期待するキャプチャグループを含んでいません。');
return;
}
const left = parseInt(match[1], 10);
const center = parseInt(match[2], 10);
const right = parseInt(match[3], 10);
// データが一定かどうかをチェック
if (left === lastLeft && center === lastCenter && right === lastRight) {
console.log(`データが一定です。Rawデータ: Left=${left}, Center=${center}, Right=${right}`);
appendRawData(left, center, right);
// 一定データの場合、グラフの更新をスキップ
return;
}
// データの追加
leftData.push(left);
centerData.push(center);
rightData.push(right);
timeData.push(currentTime);
currentTime += 1;
// データの保持数を制限
if (leftData.length > MAX_POINTS) {
leftData.shift();
centerData.shift();
rightData.shift();
timeData.shift();
}
// グラフの更新
updateCharts();
// 最後のデータを更新
lastLeft = left;
lastCenter = center;
lastRight = right;
} else {
console.warn('不正なデータ形式:', line);
}
}
// -------------------------------
// Rawデータの表示関数
// -------------------------------
function appendRawData(left, center, right) {
const rawDataDisplay = document.getElementById('rawDataDisplay');
const rawData = `Left=${left}, Center=${center}, Right=${right}\n`;
rawDataDisplay.textContent += rawData;
// スクロールを最新にする
rawDataDisplay.scrollTop = rawDataDisplay.scrollHeight;
}
// -------------------------------
// グラフの更新関数
// -------------------------------
function updateCharts() {
for (let key in charts) {
const chart = charts[key];
const svgElement = chart.svg.node();
const boundingRect = svgElement.getBoundingClientRect();
const width = boundingRect.width;
const height = boundingRect.height;
// スケールの再設定
chart.xScale.range([50, width - 50]);
chart.yScale.range([height - 30, 20]); // マージンを考慮
// Yスケールの更新
let yMin, yMax;
if (includeZeroCheckbox.checked) {
yMin = d3.min(chart.data.concat([0])) || 0;
yMax = d3.max(chart.data.concat([0])) || 1023;
} else {
yMin = d3.min(chart.data) || 0;
yMax = d3.max(chart.data) || 1023;
}
chart.yScale.domain([Math.min(yMin, 0), Math.max(yMax, 0)]);
// Y軸の再描画
chart.svg.select(".y-axis")
.transition()
.duration(500)
.call(d3.axisLeft(chart.yScale));
// パスの更新
chart.path
.datum(chart.data)
.transition()
.duration(500)
.attr("d", chart.line);
}
}
// -------------------------------
// グラフのリサイズ関数
// -------------------------------
function resizeCharts() {
for (let key in charts) {
const chart = charts[key];
const svgElement = chart.svg.node();
const boundingRect = svgElement.getBoundingClientRect();
const width = boundingRect.width;
const height = boundingRect.height;
// スケールの更新
chart.xScale.range([50, width - 50]);
chart.yScale.range([height - 30, 20]); // マージンを考慮
// Yスケールの更新
let yMin, yMax;
if (includeZeroCheckbox.checked) {
yMin = d3.min(chart.data.concat([0])) || 0;
yMax = d3.max(chart.data.concat([0])) || 1023;
} else {
yMin = d3.min(chart.data) || 0;
yMax = d3.max(chart.data) || 1023;
}
chart.yScale.domain([Math.min(yMin, 0), Math.max(yMax, 0)]);
// Y軸の再描画
chart.svg.select(".y-axis")
.transition()
.duration(500)
.call(d3.axisLeft(chart.yScale));
// パスの更新
chart.path
.datum(chart.data)
.transition()
.duration(500)
.attr("d", chart.line);
}
}
</script>
</body>
</html>
|