Skip to content

fookang/Fall-Detector

Repository files navigation

Fall Detection System

A real-time fall detection system built for the STM32L4S5-IoT01A Discovery Board (B-L4S5I-IOT01) using FreeRTOS, accelerometer/gyroscope sensors, and cloud connectivity.

Table of Contents

Overview

This project implements a fall detection system that monitors accelerometer and gyroscope data to detect fall events in real-time. When a fall is detected, the system:

  • Activates visual alerts (LED blinking)
  • Triggers audio alerts (buzzer)
  • Sends data to cloud via WiFi for remote monitoring
  • Provides real-time visualization through web dashboard

Features

Hardware Features

  • Real-time sensor monitoring at 50 Hz sampling rate
  • Multi-sensor fusion: LSM6DSL accelerometer and gyroscope
  • WiFi connectivity via Inventek ES-WIFI module
  • Visual feedback: LED2 blinks at different rates (500ms normal, 50ms on fall)
  • Audio alerts: Buzzer with customizable patterns
  • User reset: Push button to clear fall detection state

Software Features

  • FreeRTOS multi-tasking architecture with 6 concurrent tasks
  • Optimized signal processing: Assembly-language moving average filter
  • State machine-based fall detection with configurable thresholds
  • Cloud data transmission via HTTP POST
  • Live monitoring dashboard with real-time charts
  • Local proxy server for testing without cloud dependency

System Architecture

FreeRTOS Tasks

  1. SensorTask (Priority: 3 - Highest)

    • Samples accelerometer and gyroscope at 50 Hz
    • Applies 4-point moving average filter (Assembly optimized)
    • Publishes sensor data to queue
  2. DetectionTask (Priority: 2)

    • Implements fall detection state machine
    • Monitors for low-g followed by high-g and gyroscope spike
    • Updates fall_detected flag
    • Distributes detection results to WiFi and UART tasks
  3. BlinkTask (Priority: 1)

    • Controls LED2 blink rate based on fall state
    • 500ms period: Normal operation
    • 50ms period: Fall detected
  4. BuzzerTask (Priority: 1)

    • Monitors fall detection flag
    • Plays alert patterns when fall detected
    • Provides audio feedback
  5. WifiTask (Priority: 1)

    • Maintains WiFi connection
    • Sends fall detection data to cloud via HTTP POST
    • Handles connection errors and retries
  6. UartTask (Priority: 1)

    • Outputs diagnostic information via UART1
    • Logs sensor readings and detection events

Architecture Diagram

System Architecture Diagram

Hardware Requirements

  • Board: STM32L4S5-IoT01A Discovery Kit (B-L4S5I-IOT01)
  • Microcontroller: STM32L4S5VIT6 (ARM Cortex-M4)
  • Sensors:
    • LSM6DSL 3D accelerometer and gyroscope
  • WiFi Module: Inventek ISM43362-M3G-L44
  • Peripherals:
    • User button (On board)
    • LED2 (On board)
    • Buzzer (PWM-controlled)
  • UART: UART1 for logging (115200 baud)

Software Components

Key Files

  • main.c: System initialization and main entry point
  • freertos.c: Task definitions and RTOS configuration
  • fall_cloud.c: WiFi connectivity and HTTP client
  • buzzer.c: Buzzer control functions
  • mov_avg.s: Assembly-optimized moving average filter
  • fall_detect.h: Fall detection thresholds and constants

Cloud Backend (fall-cloud)

  • Platform: Vercel (serverless)
  • Runtime: Node.js
  • API: REST endpoint at /api/data
  • Storage: In-memory (global variable)
  • Authentication: Simple API key (X-Ingest-Key header)

Local Proxy Server (fall-proxy)

  • Framework: Express.js
  • Port: 3000
  • Features:
    • In-memory circular buffer (500 samples)
    • Live charting with Chart.js
    • Optional forwarding to Vercel
    • No-cache headers for real-time updates

Configuration

WiFi Settings

Edit CG2028_Assignment/Core/Inc/wifi_config.h:

#define WIFI_SSID       "your_ssid"
#define WIFI_PASS       "your_password"
#define WIFI_SECURITY   ES_WIFI_SEC_WPA2

#define SERVER_IP0 3    // AWS server IP
#define SERVER_IP1 107
#define SERVER_IP2 208
#define SERVER_IP3 198

Fall Detection Thresholds

Edit CG2028_Assignment/Core/Inc/fall_detect.h:

#define LOW_G 6.0f          // Low-g threshold (m/s²)
#define HIGH_G 18.0f        // High-g threshold (m/s²)
#define GYRO_SPIKE 1800.0f  // Gyroscope spike (dps)
#define MAX_GAP 1000        // Maximum time gap (ms)

Cloud Server

Edit CG2028_Assignment/Core/Src/fall_cloud.c:

#define SERVER_PORT 3000
#define API_PATH    "/api/data"
#define INGEST_KEY  "devkey"
#define SERVER_HOST_STR "3.107.208.198"  // Your server IP

Proxy Server

Edit fall-proxy/proxy.js:

const PORT = 3000;
const MAX_SAMPLES = 500;
const FORWARD_TO_VERCEL = true;
const VERCEL_INGEST_URL = "https://fall-cloud.vercel.app/api/data";
const VERCEL_INGEST_KEY = "devkey";

Usage

Running the System

  1. Power on the board and ensure WiFi is available

  2. Monitor UART output (115200 baud) for debug logs:

    Blink Task
    Sensor Task
    Uart Task
    WIFI START
    wifi: RegisterBusIO
    wifi: Init
    wifi: Connect
    ...
    
  3. Observe LED behavior:

    • Slow blink (500ms): Normal operation
    • Fast blink (50ms): Fall detected
  4. View live data at http://localhost:3000 or your Vercel URL

  5. Reset fall state: Press the blue user button

Expected Behavior

Normal Operation

  • LED blinks every 500ms
  • Sensor data streams to UART
  • Data posted to cloud every detection cycle

Fall Detected

  • LED blinks rapidly (50ms)
  • Buzzer sounds alert pattern
  • Fall flag sent to cloud immediately
  • UART prints sample stream in the format: t_ms, acc_mag, gyro_mag Fall

Reset (Button Press)

  • Fall state cleared
  • LED returns to slow blink
  • Buzzer stops
  • Reset message sent to cloud

Monitoring Tools

UART Terminal

Connect via serial terminal (PuTTY, minicom, screen, Tera Term)

Web Dashboard

  • Proxy server: Real-time chart of acc_mag and gyro_mag
  • Vercel dashboard: Simple status display
  • Auto-refreshes every 500ms-1000ms

Fall Detection Algorithm

The system uses a two-stage state machine:

Stage 1: Free Fall Detection (Low-G)

if (acc_mag < LOW_G):
    enter_lowg_state()
    start_timer()

Stage 2: Impact Detection (High-G + Rotation)

if (saw_lowg AND time_since_lowg < MAX_GAP):
    if (acc_mag > HIGH_G AND gyro_mag > GYRO_SPIKE):
        fall_detected = true

Algorithm Parameters

  • LOW_G: 6.0 m/s² (≈0.61g) - free fall threshold
  • HIGH_G: 18.0 m/s² (≈1.84g) - impact threshold
  • GYRO_SPIKE: 1800 dps - rotational velocity threshold
  • MAX_GAP: 1000 ms - maximum time between stages

Signal Processing

  1. Raw sensor data acquired at 50 Hz
  2. 4-point moving average filter (Assembly optimized)
  3. Magnitude calculation: sqrt(x² + y² + z²)
  4. Unit conversion to m/s² and dps

Assembly Optimization

The moving average filter is implemented in ARM assembly for efficiency:

mov_avg:
  PUSH {r2-r4, lr}
  MOV r2, #0          @ sum = 0
  MOV r3, #0          @ i = 0
loop:
  CMP r3, r0          @ i >= N?
  BGE end
  LDR r4, [r1], #4    @ load and increment
  ADD r2, r2, r4      @ accumulate
  ADD r3, r3, #1      @ i++
  B loop
end:
  ASR r0, r2, #2      @ divide by 4 (shift right 2)
  POP {r2-r4, pc}

Cloud Infrastructure

Data Flow

  1. POST /api/data (from STM32)

    • Headers: X-Ingest-Key: devkey
    • Body: JSON
      {
        "t_ms": 123456,
        "acc_mag": 9.8,
        "gyro_mag": 45.2,
        "fall": 0
      }
  2. GET /api/data (dashboard polling)

    • Response:
      {
        "latest": {
          "t_ms": 123456,
          "acc_mag": 9.8,
          "gyro_mag": 45.2,
          "fall": 0
        }
      }

Project Structure

Assignment/
├── CG2028_Assignment/          # Main embedded project
│   ├── Core/
│   │   ├── Inc/                # Header files
│   │   │   ├── main.h
│   │   │   ├── fall_detect.h  # Detection thresholds
│   │   │   ├── fall_cloud.h   # WiFi/HTTP API
│   │   │   ├── buzzer.h       # Buzzer control
│   │   │   ├── wifi_config.h  # WiFi credentials
│   │   │   └── FreeRTOSConfig.h
│   │   ├── Src/                # Source files
│   │   │   ├── main.c          # Entry point, init
│   │   │   ├── freertos.c      # Task implementations
│   │   │   ├── fall_cloud.c    # WiFi & HTTP client
│   │   │   ├── buzzer.c        # Buzzer driver
│   │   │   ├── mov_avg.s       # Assembly filter
│   │   │   └── stm32l4xx_*.c   # HAL/BSP
│   │   └── Startup/
│   │       └── startup_stm32l4s5vitx.s
│   ├── Drivers/                # STM32 HAL & BSP
│   │   ├── BSP/
│   │   │   └── B-L4S5I-IOT01/  # Board support
│   │   ├── CMSIS/              # ARM CMSIS
│   │   └── STM32L4xx_HAL_Driver/
│   ├── Middlewares/
│   │   └── Third_Party/
│   │       └── FreeRTOS/       # Real-time OS
│   ├── fall-server/            # Legacy/local server prototype
│   │   ├── package.json
│   │   └── server.js
│   ├── Debug/                  # Build output
│   └── *.ld                    # Linker scripts
│
├── CG2028_Assignment_Test/     # Test project (moving avg validation)
│   └── (similar structure)
│
├── fall-cloud/                 # Vercel serverless backend
│   ├── api/
│   │   └── data.js            # Serverless function
│   ├── public/
│   │   └── index.html         # Simple dashboard
│   ├── package.json
│   └── vercel.json            # Vercel config
│
├── fall-proxy/                 # Local Express proxy/dashboard
│   ├── proxy.js               # Server with live charts
│   └── package.json
│
├── fall-dashboard/             # (Reserved for future expansion)
│
├── Diagram/                    # PlantUML system diagrams
│   ├── system_arch.puml       # Architecture diagram
│   ├── fall_detection.puml    # Detection algorithm
│   ├── interrupt.puml         # ISR flow
│   └── real_time.puml         # Real-time constraints
│
└── README.md                   # This file

Authors

CG2028 Assignment, Semester 2, AY 2025/26

  • Foo Kang
  • Yeo Teck Ian Bryan

Course: CG2028 - Computer Organization and Architecture
Institution: National University of Singapore (NUS)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages