Skip to content

About

Non blocking ATmega328P firmware for an ultrasonic radar system with motorized scanning and ranging.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

45 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Radar System

A non-blocking firmware architecture for an ATmega328P-based ultrasonic radar system with motorized scanning and ultrasonic ranging.

The system uses a servo-mounted HC-SR04 ultrasonic sensor to scan the environment and sends measurement data to a PC for visualization through a web-based radar dashboard.

Features

  • Bare-metal C firmware
  • Non-blocking firmware architecture
  • ATmega328P microcontroller
  • Servo-based 180° scanning
  • HC-SR04 ultrasonic ranging
  • Input Capture Unit (ICU) for echo pulse measurement
  • Timer/interrupt-driven operation
  • Modular peripheral drivers
  • UART communication
  • Python-based serial data acquisition
  • WebSocket-based real-time dashboard
  • Browser-based radar visualization
  • State-machine-based radar control

System Architecture

                ┌──────────────────────┐
                │      ATmega328P      │
                │                      │
                │  Radar Controller    │
                │          │           │
                │          ▼           │
                │     Servo Motor      │
                │          │           │
                │          ▼           │
                │      HC-SR04         │
                │          │           │
                │          ▼           │
                │     ICU / Timer      │
                │          │           │
                │          ▼           │
                │        UART          │
                └──────────┬───────────┘
                           │
                       USB Serial
                           │
                           ▼
                ┌──────────────────────┐
                │   Python Acquisition │
                │   radar_data_acq.py  │
                └──────────┬───────────┘
                           │
                       WebSocket
                           │
                           ▼
                ┌──────────────────────┐
                │    FastAPI Server    │
                │      server.py       │
                └──────────┬───────────┘
                           │
                           ▼
                ┌──────────────────────┐
                │   Browser Dashboard  │
                │    Radar Display     │
                └──────────────────────┘

Firmware Architecture

Firmware is organised into layers to keep Hardware specific code and application logic seperate.


           main.c
             |
       radar_control.c
               |
               |———> Send_data.c
               |———> sweep_control.c
               |———> obstacle_detection.c
                                |
                                |———> servo.c
                                |———> icu.c
                                |———> uart.c
                                |———> gpio.c
                                |———> timer.c
                                         |
                                     ATmega328P
                                          

Project Structure

radar_system/
     |
     |———> app/
     |      |———> radar_control.c
     |
     |———> driver/
     |        |
     |        |———> gpio.c
     |        |———> timer.c
     |        |———> servo.c
     |        |———> icu.c
     |        |———> uart.c
     |
     |
     |———> src/
     |      |———> obstacle_detection.c
     |      |———> sweep_control.c
     |      |———> send_data.c
     |
     |———> inc/
     |      |———> goio.h
     |      |———> timer.h
     |      |———> servo.h
     |      |———> icu.h
     |      |———> obstacle_detection.h
     |      |———> radar_control.h
     |      |———> send_data.h
     |      |———> uart.h
     |      |———> sweep_control.h
     |
     |———> radar_dashboard/
     |             |———> radar_data_acq.py
     |             |———> index.html
     |             |———> server.py
     |             |———> static/
     |                     |———> radar.js
     |                     |———> style.css
     |
     |———> main.c
     |———> makefile
    

Layer Responsibility

Layer Responsibility
driver/ Hardware-specific peripheral driver
src/ Specific task services built using hardware drivers
inc/ Public interface and definition
app/ Application to run services sequentially
main.c Firmware entry point
radar_dashboard/ PC side data acquisition and visualization

Hardware Used

Hardware Application
ATmega328P Main MCU
SG90 Servo Radar sensing positioning
HCSR04 Ultrasonic detection

Peripherals and Application

The firmware configures and uses Built-in Hardware peripherals for different applications.

8 bit Timer/Counter Timer0

Timer0 is used in timer driver for generating hardware interrupt based precise and non blocking delays.

Check ATmega328P-HAL-Timer-Driver

16 bit Timer/Counter Timer1

Timer1 is used in the icu driver where the peripheral is configured in ICU mode for precise edge detection. The Input Capture Unit (ICU)has selectable edges Rising/Falling which can be programmed to trigger on a rising (low to high) or falling edge (high to low).

Check ATmega328P-HAL-ICU-Driver

8 bit Timer/Counter Timer2

Timer2 is configured in CTCmode with a prescaler 8 and a top values OCR2A = 199.The the Timer2 Interrupt is enabled (OCIA bit of the TIMSK2 register is set) and global interruptis enabled. This timer peripheral is used for generating accurate and precise pulse timing for servo motor positioning in servo driver.

USART

The USART/UART Peripheral of ATmega328P is used in the uart driver for establishing serial communication with other peripherals and devices. Check ATmega328P-HAL-UART-Driver

          Peripheral
               |
               |———> Timer0
               |        |———> System timing / non blocking delays 
               |
               |———> Timer1
               |        |———> ICU
               |
               |———> Timer2
               |        |———> Servo Pulse timing
               |
               |———> Usart/Uart
                         |———> Serial communication/ Radar Data transmission 

The firmware avoids using blocking delays for radar operation. Timing is handled by Hardware peripherals, interrupts and state machine progression.

Drivers

Driver Purpose
goio GPIO pin configuration
timer Creating Non blocking delays and system timing
uart Radar data transmission with PC visualization
servo Controlling servo pulse and positioning
icu Measuring distance using icu hardware on rising and falling edge

The peripheral drivers are implemented as reusable bare-metal C modules. Deatiled driver documentation is maintained separately in the ATmega328P HAL repository.

Radar Application Layer

The Radar Application is a state-machine driven high level application specific code layer built to sequentially run the services using state based progression and keep the system non blocking. The Application layer also acts as cooperative superloop, in which every service gets required CPU cycles to complete its specified task without blocking the CPU.

State-Machine

The Application uses the following states to run the services one by one in required sequence.

typedef enum
{
    RADAR_MOVE,
    RADAR_SETTLE,
    RADAR_TIGGER,
    RADAR_ECHO,
    RADAR_SEND
}RADAR_STATE_t;
  • RADAR_MOVE: Update the current step to move the servo.
  • RADAR_SETTLE: Wait for servo shaft on which the ultrasonic sensor is mounted to reach the required postion.
  • RADAR_TRIGGER: Trigger ultrasonic sesnor once and then change the state.
  • RADAR_ECHO: Update ongoing measurement status, if measurement is done, changes the state.
  • RADAR_SEND: Send the Radar data, step and measurement over serial.
  • RADAR_UPDATE: Update the next step.

Internal Setup

RADAR_t RADAR_control = {
    .RADAR_settle_wait_Ref = 0,   // Timer reference for settlement wait
    .RADAR_settle_time = 100,   // Settle time for RADAR / Sweep speed [Can be changed as desired]
    .SERVO_pin = 5   // Servo Pin [Can be changed to any GPIO Pin]
};
  • SERVO_pin: The current application use PD5 Pin of ATmega328P for servo pulse generation, any GPIO Pin can be used for the task. servo driver

  • RADAR_settle_time: Wait time for the Radar to reach the required position in milliseconds. Can also be considered as RADAR SPEED and can be changed by the user to any desired 32-bit value.

At RADAR_settle_time values below 20 the servo makes unpredictable movement behaviour

Radar Service Layer

The Radar system uses different service layers for different tasks.

Sweep Control

The Sweep control (sweep_control.c) service layer is responsible for the movement and positioning of the servo. This service layer uses servo driver for communicating with the hardware registers. The service layer only implements the required service logic for Radar system rather than directly manipulating hardware registers for each opertion. The goal was to seperate register level hardware control and service/application logic.

Responsibilities:

  • Acts as a bridge for communication between the high level application and low level servo driver.
  • Changes direction of sweep at extreme values (0 and 180 degrees).
  • Updates the current step value in servo driver according to application for generating pulse.

The servo driver internally uses a step value for generating pulse for the servo motor.

1 step is approximately 100 microseconds. The servo driver uses a tick_100us variable which is increamented on every Timer2 interrupt fire which gives 1 tick_100us = 100 microseconds = 1 step value.

Obstacle detection

The higher level Radar application uses Obstacle Detection (obstacle_detection.c) service layer for distance measurement and obstacle detection. The service layer only implements the application specific logic and uses ICU Driver for accessing the hardware.

The Radar uses a HCSR04 ultrasonic sensor for measuring distance and obstacles in its range. Current Architecture restricts its range to 200cm, however the actual range of the sensor is found to be ~400cm.

Responsibilities:

  • Bridge for communication between ICU driver and Application layer.
  • Trigger a pulse for measurement
  • Update a completion flag when measuremnt is done
  • Return the measuremnet result in CM.

Radar Dashboard

The current postion and angle is visualized on web based dashboard.

              ATmega328P 
                  |
                  | uart
                  |
          radar_data_acq.py
                  |
           step, distance
                  |
              server.py
                  |
        FastAPI & HTTP + Websocket
                  |
              WebSocket
                  |
               Browser
                  |
              radar.js
                  |
                  |
            Radar Canvas (visualisation)
       

Data Transmission

The current position and step data is transmitted from the MCU to the PC over serial in the format (step, distance). The PC runs a python program radar_data_acq.py which receives the transmitted data using pyserial and parse it in required format, step: , distance:.

Data Broadcasting

The parsed data is then broadcasted over the dashboard using a python server server.py. The data is continuously updated and broadcast on the dashboard. The python backend server.py acts as the bridge between the embedded system and the browser.

Main Functions (Backend)

  • Serves the web application: FastAPI serves the HTML, CSS and Js files required by the dashboard.

  • Stream Radar measurements: A backgroud task continuously reads measurements from radar_data_acq.py. Each measurement is converted into JSON and broadcasted to all the connected WebSocket Clients/devices.

Main function (Frontend)

The browser connects to the backend through a WebSocket connection.

  • Parse JSON message.
  • Convert servo steps into an angle
  • Update current step, angle and distance
  • Draw detected obstacles on the radar canvas
  • Update the sweep visualisation
Angles is derived from firmware step value using,
angle = (step - 5) * 9

Complete Data Flow

          HCSR04
            |
            |> Echo timing
            |
        Atmega328P
            |
            |> step, distance
            |
     radar_data_acq.py
            |
            |> structured data
            |
        server.py
            |
            |> JSON over WebSocket
            |
        radar.js
            | 
            |> angle, distance
            |
      Radar Dashboard

The embedded firmware is responsible for measurements and control, while the host software is responsible for data transport and visualisation.

Build & Flash

The firmware is built using AVR-GCC toolchain and Make.

Clone repository

git clone github.com/ashish4hub/radar_system

Build

Move to radar_system repository and and run:

make

This compiles the source files and generate the firmware artifacts, including the .hex file.

Flash

Connect the ATmega328P programmer/ usb interface and run:

make flash

The generated .hex firmware image is programmed into the ATmega328P using avrdude.

Clean

Remove the generated build artifacts:

make clean

Requirements

  • avr-gcc
  • avr-libc
  • avrdude
  • make

Python Server & Dashboard

Requirements

  • Python 3
  • pip
  • Serial connection with ATmega328P
  • A wifi connection

Setup

create a virtual environment:

python3 -m venv .venv source .venv/bin/activate

Install required Python packages

pip install -r fastapi uvicorn[standard] pyserial

Run

From the radar_dashboard directory run:

python server.py

The server starts on port 8000

  • On host device
http://localhost:8000
  • On other device connected to same network (wifi)
http://<host_ip>:8000

License

This project is licensed under MIT License. See the License for details.

Check pd for circuit schematic and prototype

About

Non blocking ATmega328P firmware for an ultrasonic radar system with motorized scanning and ranging.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages