Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions firmware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Infineon のサンプル `mtb-example-psoc-edge-ml-deepcraft-deploy-radar` を
| `D,seq,t_ms,I0,Q0,…,In-1,Qn-1` | ボードから PC | データ本体。`seq` は 1 行ごとに 1 ずつ増え、`t_ms` は起動からの時間、ビンは近い順、値は整数を `scale` で割ったもの。 |
| `E,seq,t_ms,<base64>` | ボードから PC | `M,3` のあとだけ:3 本のアンテナすべて。base64 の中身は int16(リトルエンディアン)を、アンテナ・ビン・実部/虚部の順に 3×24×2 個。値は D の行と同じ平均ですが `scale` をかけません。読み方は [ml/antennas.py](../ml/antennas.py) |
| `Y,t_ms,state,event` | ボードから PC | ボードの AI チップ(Ethos-U55)で動く 2 つのモデルの判定(約 0.5 秒ごと)。`state` は breathing / stopped / moving / empty、`event` は `-` / `no_breath` / `left_bed`。警告のときはボードが自分で LED を点滅させ、SW1 で止まります。モデルは `../ml-env/bin/python -m ml.board_models` で作ります(なければ Y の行は出ません) |
| `H,t_ms,temp_c,humidity` | ボードから PC | 10 秒ごと、ボードの上の温湿度センサー(I2C 0x44、SHT4x)。センサーはレーダーとプロセッサーの横にあるので、部屋より高い「ボードの温度」です(25 °C ほどの部屋で 36 °C) |
| `S,t_ms,text` | ボードから PC | 状態やエラー。例:`S,0,ibuki fw start`。1 分ごとに CPU の使用率も送ります:`S,60000,load dsp 1.5% out 0.0% sleep 95.3% 7158/s queue 246 dropped 0` |
| `B,1` | ボードから PC | ユーザーボタンが押された |
| `L,1` / `L,0` | PC からボード | 警告:RGB LED が赤・緑・青を同時につけて 1 秒に 2 回点滅(ボードの電源などの赤いランプはいつも点いているため、赤だけでは目立ちませんでした)、または消灯 |
Expand Down Expand Up @@ -126,6 +127,7 @@ We use preset P1 from [proj_cm55/radar_settings.h](proj_cm55/radar_settings.h).
| `D,seq,t_ms,I0,Q0,…,In-1,Qn-1` | board to PC | The data. `seq` goes up by one per line, `t_ms` is the board's time since boot, bins are nearest first, and each value is the integer divided by `scale`. |
| `E,seq,t_ms,<base64>` | board to PC | Only after `M,3`: all 3 antennas. The base64 holds int16 values (little-endian), antenna by antenna, bin by bin, Re then Im: 3×24×2 values, averaged like the D line but without `scale`. Read with [ml/antennas.py](../ml/antennas.py). |
| `Y,t_ms,state,event` | board to PC | The decision of the two models running on the board's AI chip (Ethos-U55), about every 0.5 s. `state` is breathing / stopped / moving / empty, `event` is `-` / `no_breath` / `left_bed`. On an alarm the board blinks its LED by itself; SW1 stops it. Build the models with `../ml-env/bin/python -m ml.board_models` (without them there are no Y lines). |
| `H,t_ms,temp_c,humidity` | board to PC | Every 10 s, from the temperature/humidity sensor on the board (I2C 0x44, an SHT4x). It sits beside the radar and the processor, so it reads the board's temperature, warmer than the room (36 °C in a ~25 °C room). |
| `S,t_ms,text` | board to PC | Status and errors, for example `S,0,ibuki fw start`. Once a minute also the CPU use: `S,60000,load dsp 1.5% out 0.0% sleep 95.3% 7158/s queue 246 dropped 0`. |
| `B,1` | board to PC | The user button was pressed. |
| `L,1` / `L,0` | PC to board | Alert: the RGB LED blinks twice a second with red, green and blue on together (the board's power and other red lights are always on, so plain red was easy to miss), or off. |
Expand Down
2 changes: 2 additions & 0 deletions firmware/proj_cm55/ibuki/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [board_io.h](board_io.h) / [board_io.c](board_io.c) | 警告 LED(赤・緑・青を同時に点滅)、ユーザーボタン(`B,1` を送る)、PC からのコマンド(`L,1` / `L,0` / `R,1` / `T,<ms>`) |
| [load.h](load.h) / [load.c](load.c) | CPU がどれだけ忙しいか。1 分ごとに `S,<時刻>,load dsp 1.5% out 0.0% sleep 95.3% 7158/s queue 246 dropped 0` を送ります |
| [ai.h](ai.h) / [ai.c](ai.c) | 学習済みの 2 つのモデルを AI チップ(Ethos-U55)で動かし、警告まで決めます。Y の行を送り、警告のときは LED を自分で点滅させます。アプリと同じ計算で、同じ録画で同じ判定になりました |
| [sensors.h](sensors.h) / [sensors.c](sensors.c) | ボードの I2C のセンサー。起動時に応答するアドレスを報告し(0x18、0x44、0x68、0x77)、10 秒ごとに温湿度(0x44、SHT4x)を H の行で送ります。ほかのセンサーはまだ読んでいません |

## 1 フレームの流れ

Expand Down Expand Up @@ -51,6 +52,7 @@ This turns raw radar data into the lines sent to the PC (C / D / S / B). Infineo
| [board_io.h](board_io.h) / [board_io.c](board_io.c) | Alert LED (blinks red, green and blue together), user button (sends `B,1`), commands from the PC (`L,1` / `L,0` / `R,1` / `T,<ms>`) |
| [load.h](load.h) / [load.c](load.c) | How busy the CPU is: `S,<time>,load dsp 1.5% out 0.0% sleep 95.3% 7158/s queue 246 dropped 0` once a minute |
| [ai.h](ai.h) / [ai.c](ai.c) | Runs the two trained models on the AI chip (Ethos-U55) and decides the alarm: sends Y lines and blinks the LED by itself on an alarm. The same maths as the app; on the same recording it gave the same decisions |
| [sensors.h](sensors.h) / [sensors.c](sensors.c) | The board's I2C sensors: reports at start-up which addresses answer (0x18, 0x44, 0x68, 0x77) and sends the temperature/humidity (0x44, an SHT4x) every 10 s as H lines. The other sensors are not read yet |

## One frame

Expand Down
3 changes: 3 additions & 0 deletions firmware/proj_cm55/ibuki/ibuki.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "lines.h"
#include "load.h"
#include "range.h"
#include "sensors.h"

static uint32_t d_seq;
static uint32_t last_config_ms;
Expand Down Expand Up @@ -159,6 +160,7 @@ void ibuki_init(void)
last_config_ms = ibuki_millis();
load_init();
ai_init();
sensors_init();
}

void ibuki_on_frame(const float* frame)
Expand Down Expand Up @@ -188,6 +190,7 @@ void ibuki_poll(void)
last_config_ms = now;
}
load_poll(now);
sensors_poll(now);
line_pump();
board_io_poll(now);
}
Expand Down
129 changes: 129 additions & 0 deletions firmware/proj_cm55/ibuki/sensors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* The board's other sensors (see sensors.h), with the PDL's I2C controller functions. */
#include "sensors.h"

#include "cybsp.h"
#include "ibuki.h"
#include "lines.h"

#define I2C_TIMEOUT_MS (5U)

static cy_stc_scb_i2c_context_t i2c;
static bool i2c_ok;

/* true if a chip acknowledges its address */
static bool answers(uint32_t address)
{
bool ack = Cy_SCB_I2C_MasterSendStart(CYBSP_I2C_CONTROLLER_HW, address, CY_SCB_I2C_WRITE_XFER,
I2C_TIMEOUT_MS, &i2c) == CY_SCB_I2C_SUCCESS;
(void)Cy_SCB_I2C_MasterSendStop(CYBSP_I2C_CONTROLLER_HW, I2C_TIMEOUT_MS, &i2c);
return ack;
}

/* write one command byte, wait, read n bytes; false if the chip doesn't answer */
static bool command_read(uint32_t address, uint8_t cmd, uint32_t wait_ms, uint8_t* out, uint32_t n)
{
bool ok = Cy_SCB_I2C_MasterSendStart(CYBSP_I2C_CONTROLLER_HW, address, CY_SCB_I2C_WRITE_XFER,
I2C_TIMEOUT_MS, &i2c) == CY_SCB_I2C_SUCCESS
&& Cy_SCB_I2C_MasterWriteByte(CYBSP_I2C_CONTROLLER_HW, cmd, I2C_TIMEOUT_MS, &i2c) == CY_SCB_I2C_SUCCESS;
(void)Cy_SCB_I2C_MasterSendStop(CYBSP_I2C_CONTROLLER_HW, I2C_TIMEOUT_MS, &i2c);
if (!ok)
{
return false;
}
Cy_SysLib_Delay(wait_ms);
ok = Cy_SCB_I2C_MasterSendStart(CYBSP_I2C_CONTROLLER_HW, address, CY_SCB_I2C_READ_XFER,
I2C_TIMEOUT_MS, &i2c) == CY_SCB_I2C_SUCCESS;
for (uint32_t i = 0U; ok && (i < n); i++)
{
ok = Cy_SCB_I2C_MasterReadByte(CYBSP_I2C_CONTROLLER_HW, (i + 1U < n) ? CY_SCB_I2C_ACK : CY_SCB_I2C_NAK,
&out[i], I2C_TIMEOUT_MS, &i2c) == CY_SCB_I2C_SUCCESS;
}
(void)Cy_SCB_I2C_MasterSendStop(CYBSP_I2C_CONTROLLER_HW, I2C_TIMEOUT_MS, &i2c);
return ok;
}

#define SHT_ADDRESS (0x44U) /* answered with a Sensirion SHT4x serial number and valid CRCs */
#define CLIMATE_MS (10000U) /* one reading every 10 s */
static uint32_t last_climate_ms;

/* the CRC-8 the SHT4x puts after every 2 data bytes (polynomial 0x31, start 0xFF) */
static uint8_t crc8(const uint8_t* d)
{
uint8_t c = 0xFFU;
for (uint32_t i = 0U; i < 2U; i++)
{
c ^= d[i];
for (uint32_t b = 0U; b < 8U; b++)
{
c = (c & 0x80U) ? (uint8_t)((c << 1) ^ 0x31U) : (uint8_t)(c << 1);
}
}
return c;
}

/* "H,t_ms,<temperature C>,<relative humidity %>" from one high-precision measurement (0xFD,
* ~9 ms). The sensor sits on the board, beside the radar and the processor, so it reads the
* board's temperature: warmer than the room (36 C in a ~25 C room on 2026-10-28). */
static void send_climate(uint32_t now)
{
uint8_t b[6];
if (!command_read(SHT_ADDRESS, 0xFDU, 10U, b, 6U) || (crc8(&b[0]) != b[2]) || (crc8(&b[3]) != b[5]))
{
return; /* no reading this time */
}
int32_t t10 = -450 + (int32_t)((1750U * ((uint32_t)b[0] << 8 | b[1])) / 65535U);
int32_t h10 = -60 + (int32_t)((1250U * ((uint32_t)b[3] << 8 | b[4])) / 65535U);
h10 = (h10 < 0) ? 0 : ((h10 > 1000) ? 1000 : h10);
char* p = line_start();
*p++ = 'H';
*p++ = ',';
p = line_put_u32(p, now);
*p++ = ',';
if (t10 < 0)
{
*p++ = '-';
t10 = -t10;
}
p = line_put_tenths(p, (uint32_t)t10);
*p++ = ',';
p = line_put_tenths(p, (uint32_t)h10);
line_send(p);
}

void sensors_poll(uint32_t now_ms)
{
if (i2c_ok && ((now_ms - last_climate_ms) >= CLIMATE_MS))
{
last_climate_ms = now_ms;
send_climate(now_ms);
}
}

void sensors_init(void)
{
i2c_ok = Cy_SCB_I2C_Init(CYBSP_I2C_CONTROLLER_HW, &CYBSP_I2C_CONTROLLER_config, &i2c) == CY_SCB_I2C_SUCCESS;
if (!i2c_ok)
{
ibuki_status("i2c: init failed");
return;
}
Cy_SCB_I2C_Enable(CYBSP_I2C_CONTROLLER_HW);
char* p = line_start();
*p++ = 'S';
*p++ = ',';
p = line_put_u32(p, ibuki_millis());
p = line_put_text(p, ",i2c:");
for (uint32_t a = 0x08U; a < 0x78U; a++) /* the usual 7-bit address range */
{
if (answers(a))
{
static const char hex[] = "0123456789abcdef";
*p++ = ' ';
*p++ = '0';
*p++ = 'x';
*p++ = hex[a >> 4];
*p++ = hex[a & 15U];
}
}
line_send(p);
}
12 changes: 12 additions & 0 deletions firmware/proj_cm55/ibuki/sensors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* The board's other sensors on its I2C bus (CYBSP_I2C_CONTROLLER, SCB0). Found on our board:
* 0x18, 0x44, 0x68, 0x77. 0x44 is the temperature/humidity sensor (an SHT4x: its serial number
* and measurements carry valid CRCs); the others are not read yet. */
#ifndef IBUKI_SENSORS_H
#define IBUKI_SENSORS_H

#include <stdint.h>

void sensors_init(void); /* starts the bus, reports which chips answer: "S,t,i2c: 0x.. 0x.." */
void sensors_poll(uint32_t now_ms); /* every 10 s: "H,t_ms,<temperature C>,<humidity %>" */

#endif /* IBUKI_SENSORS_H */
4 changes: 2 additions & 2 deletions pc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.venv/bin/python pc/ibuki/server.py
```

http://localhost:8000/api/health を開いて、`board_connected` が `true` になっていれば大丈夫です。`board_health` には、ボードが 1 分ごとに送る CPU の使用率(`load`)と、レーダーの再起動の回数・最後の理由(`restarts`、`last_restart`)が入ります。ボードが手元にないときは、録画を再生できます:
http://localhost:8000/api/health を開いて、`board_connected` が `true` になっていれば大丈夫です。`board_health` には、ボードが 1 分ごとに送る CPU の使用率(`load`)と、レーダーの再起動の回数・最後の理由(`restarts`、`last_restart`)、ボードの温湿度(`climate`)が入ります。ボードが手元にないときは、録画を再生できます:

```
.venv/bin/python pc/ibuki/server.py --replay recordings/<ファイル>.csv --loop
Expand Down Expand Up @@ -186,7 +186,7 @@ The easiest way is `./ibuki.command` in the repo folder: it starts the server an
.venv/bin/python pc/ibuki/server.py
```

Then open http://localhost:8000/api/health and check that `board_connected` is `true`. `board_health` shows what the board reports about itself: its CPU use once a minute (`load`) and radar restarts (`restarts`, `last_restart`). If you don't have the board with you, replay a recording instead:
Then open http://localhost:8000/api/health and check that `board_connected` is `true`. `board_health` shows what the board reports about itself: its CPU use once a minute (`load`) radar restarts (`restarts`, `last_restart`) and the board's temperature and humidity (`climate`). If you don't have the board with you, replay a recording instead:

```
.venv/bin/python pc/ibuki/server.py --replay recordings/<file>.csv --loop
Expand Down
15 changes: 14 additions & 1 deletion pc/ibuki/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def __init__(self):
self.load: dict | None = None
self.restarts = 0
self.last_restart: dict | None = None
self.climate: dict | None = None

def feed_climate(self, text: str):
"""H,t_ms,<temperature C>,<humidity %> from the sensor on the board. It sits beside the
radar and the processor, so it reads the board's temperature, warmer than the room."""
p = text.split(",")
try:
self.climate = {"board_temp_c": float(p[2]), "humidity_pct": float(p[3]), "board_t_ms": int(p[1])}
except (IndexError, ValueError):
pass

def feed(self, t_ms: int, text: str):
m = LOAD_RE.search(text)
Expand All @@ -132,7 +142,8 @@ def feed(self, t_ms: int, text: str):
self.last_restart = {"reason": reason, "board_t_ms": t_ms, "unix_s": round(time.time(), 1)}

def as_dict(self) -> dict:
return {"load": self.load, "restarts": self.restarts, "last_restart": self.last_restart}
return {"load": self.load, "restarts": self.restarts, "last_restart": self.last_restart,
"climate": self.climate}


class Pairing:
Expand Down Expand Up @@ -426,6 +437,8 @@ async def handle_line(self, text: str):
log("button pressed (B,1)")
self.set_led(False) # a parent saw it: acknowledge
self.siren.stop()
elif text.startswith("H,"):
self.board_health.feed_climate(text)
elif isinstance(m, Status):
self.last_board_status = m.text
self.board_health.feed(m.t_ms, m.text)
Expand Down
4 changes: 3 additions & 1 deletion pc/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ class BoardHealthTest(unittest.TestCase):
def test_load_and_restart_lines(self):
from ibuki.server import BoardHealth
h = BoardHealth()
self.assertEqual(h.as_dict(), {"load": None, "restarts": 0, "last_restart": None})
self.assertEqual(h.as_dict(), {"load": None, "restarts": 0, "last_restart": None, "climate": None})
h.feed_climate("H,10000,36.3,36.7")
self.assertEqual(h.as_dict()["climate"], {"board_temp_c": 36.3, "humidity_pct": 36.7, "board_t_ms": 10000})
h.feed(60000, "load dsp 1.5% out 0.0% sleep 95.3% 7158/s queue 246 dropped 0")
h.feed(25162, "radar restart: fifo overflow 0x00000000")
h.feed(1013, "radar initialized P1") # other status text changes nothing
Expand Down
Loading