Skip to content

bugparty/openpowermonitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

170 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Power Monitor

A power monitoring system based on the INA228 current/voltage/power sensor and Raspberry Pi Pico (RP2040), with a custom UART communication protocol and PC-side software to control and collect data.

Features

  • INA228 sensor driver for RP2040
  • Interactive command-based configuration of INA228 (set/get config)
  • Raw data acquisition plus derived/processed values on the PC side
  • Save collected and processed data to JSON for offline analysis
  • Custom UART protocol with CRC error detection
  • Retransmission and timeout handling
  • Real-time data streaming with 1 kHz sample rates
  • PC-side protocol simulator with fault injection capabilities
  • Comprehensive test suite using Google Test

Project Structure

powermonitor/
├── device/              # Raspberry Pi Pico firmware
│   ├── INA228.cpp/hpp  # INA228 sensor driver
│   ├── powermonitor.cpp # Main device firmware
│   └── CMakeLists.txt  # Pico SDK build configuration
├── pc_client/          # PC-side serial client
├── pc_sim/             # PC-side simulator and tests
│   ├── pc_sim_main.cpp # Simulator demo entry
│   └── CMakeLists.txt  # Host build configuration with GTest
├── protocol/           # Protocol implementation
│   ├── crc16_ccitt_false.cpp
│   ├── frame_builder.cpp
│   ├── parser.cpp
│   └── unpack.cpp
├── sim/                # Simulation infrastructure
│   ├── event_loop.cpp  # Discrete event simulator
│   └── virtual_link.cpp # Virtual UART link with fault injection
├── node/               # Protocol node implementations
│   ├── pc_node.cpp     # PC-side protocol logic
│   ├── device_node.cpp # Device-side protocol logic
│   └── ina228_model.cpp # INA228 behavior model
└── docs/               # Documentation
    ├── pc_client/
    │   ├── overview.md
    │   └── implementation_plan.md
    ├── pc_sim/
    │   ├── simulator_tests.md
    │   └── state_machine_tests.md
    ├── protocol/
    │   ├── uart_protocol.md
    │   └── parser_design.md
    └── device/
        └── time_sync.md

Building

Using workflow.ps1 (Recommended)

The workflow.ps1 PowerShell script provides a unified interface for building, testing, and flashing:

# Run tests
pwsh workflow.ps1

# Clean build and run tests
pwsh workflow.ps1 -Clean

# Build device firmware only
pwsh workflow.ps1 -BuildDevice

# Build and flash device firmware to Pico (Windows only)
pwsh workflow.ps1 -FlashPico

# Generate Visual Studio solution
pwsh workflow.ps1 -GenerateSolution

# Show all options
pwsh workflow.ps1 -Help

Requirements for device firmware:

  • Set PICO_SDK_PATH environment variable to your Pico SDK installation
  • For -FlashPico: Windows OS with Pico in BOOTSEL mode (RPI-RP2 drive)

Windows (Visual Studio)

If you prefer using Visual Studio, generate a VS solution with CMake, then set the PC app as the startup project and press Run.

# Let CMake pick the default generator (often Visual Studio on Windows)
cmake -S . -B build_vs
  • Open the generated solution in build_vs/.
  • Set powermonitor as the Startup Project (PC-side interactive client).
  • Build and run (F5).

Device Firmware

Requires Pico SDK installation.

cd device
cmake -B build
cmake --build build

# Flash to Pico
# Copy build/powermonitor.uf2 to your Pico in BOOTSEL mode

Testing

There are two test tracks:

  • PC simulator / host-side tests (GoogleTest): protocol framing/parsing, state machines, and fault-injection scenarios.

    • Recommended: ./test.sh
    • Or run directly: ./build/pc_sim/pc_sim_test
    • Details: docs/pc_sim/simulator_tests.md and docs/pc_sim/state_machine_tests.md
  • Real device tests (serial/on-device): step-by-step validation against actual hardware.

    • Test plan: docs/pc_client/device_serial_tests.md

Protocol Simulator Configuration

Edit pc_sim/pc_sim_main.cpp to adjust simulation parameters:

  • LinkConfig: Fault injection settings

    • drop_prob: Packet drop probability (0.0-1.0)
    • flip_prob: Bit flip probability (0.0-1.0)
    • min_delay_us, max_delay_us: Transmission delay range
    • min_chunk, max_chunk: Byte chunking for fragmentation
  • INA228 Model: Edit node/ina228_model.cpp to change simulated voltage/current/temperature waveforms

PC Tools

ASIO serial demo

a tool to test asio serial api

Web timeline viewer (PC client JSON)

A lightweight browser viewer is available at web_viewer/. It loads the JSON generated by powermonitor and renders voltage/current/power timeline tracks with logic-analyzer-style interactions:

  • Zoom timeline with mouse wheel
  • Pan timeline by dragging
  • Hover to inspect point values

Run locally:

npm install
npm run dev
# opens http://localhost:5173/web_viewer/index.html
# optional auto-load:
# http://localhost:5173/web_viewer/index.html?data=/build/pc_client/output/powermonitor_20260218_005711.json

Validation for web-only changes:

# workflow.ps1 is not required when only files under web_viewer/ are changed
npm run build:web

Protocol Overview

The communication protocol uses framed messages with the following features:

  • Frame format: SOF(0xAA 0x55) + VER + TYPE + FLAGS + SEQ + LEN(LE) + MSGID + DATA + CRC16(CCITT-FALSE)
  • CRC16-CCITT-FALSE error detection
  • Sequence numbering for reliable delivery
  • ACK/NACK responses with automatic retransmission
  • Configurable timeout and retry mechanisms

See docs/protocol/uart_protocol.md for detailed protocol specification.

Hardware

  • MCU: Raspberry Pi Pico (RP2040) / Adafruit Feather RP2040
  • Sensor: Texas Instruments INA228 (I2C power monitor)
  • Interface: USB CDC (UART over USB)

License

See individual source files for license information.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors