Blog Post:

Embedded Accelerometer Visualization with QP, Zephyr, and Nordic Thingy91

Introduction

In embedded systems design, the Publish and Subscribe (P&S) architecture provides a powerful framework for decoupling components, facilitating modular design, and improving system scalability by creating separation for message publisher and subscribers. This case study examines the development of a real-time accelerometer visualization system using the Nordic Thingy91 nRF9160, a cellular IoT prototyping platform. The Thingy91 is integrated with the real-time embedded framework QP (Quantum Platform) and the open-sourced real-time operating system Zephyr RTOS.

The architectural decisions made throughout the project are guided by the principles of P&S, which help reduce technical complexity, improve maintainability, and enhance development efficiency. By leveraging QP’s active object model and Zephyr’s robust real-time capabilities, this project demonstrates how an event-driven architecture can streamline data processing and visualization in embedded systems.

Note that while Zephyr was chosen for its real-time capabilities, the decision was largely influenced by Nordic Semiconductor’s comprehensive development ecosystem, specifically the availability of the nRF Connect SDK.

Application Overview

The system’s primary function is to collect real-time data from the ADXL362 accelerometer on the Thingy91. The system communicates with the 3-axis accelerometer via the serial peripheral interface (SPI) communication protocol. The system visualizes the gravity vector as a plane in a 3D wireframe plot using a custom QView script, which is a graphical user-interface that runs on python. The QView script runs on a host computer and is connected to the Nordic Thingy91 through a universal asynchronous receiver/transmitter (UART) serial connection and the underlying QP/Spy software tracing system. The system architecture is based on Zephyr RTOS, for hardware management, and QP’s active object pattern to manage state transitions and event handling.

Hardware Architecture

Nordic Semiconductor Logo

Embedded Platform

The Nordic Thingy91 nRF9160 was selected for this project due to its advanced capabilities in wireless communication and sensor integration. The nRF9160 is a System-in-Package (SiP) that combines LTE-M and NB-IoT connectivity, making it ideal for low-power, wide-area network applications crucial in IoT deployments. Its built-in support for cellular networks enables reliable data transmission over long distances, allowing for remote monitoring and control of devices with minimal local infrastructure.

The Thingy91’s comprehensive suite of onboard sensors—including the ADXL362 accelerometer—provides a rich environment for prototyping various IoT applications, from environmental monitoring to smart city solutions. The device’s energy efficiency, combined with Nordic’s strong development ecosystem, online training courses, and robust software support, positions the Thingy91 as an excellent choice for developers looking to create scalable and efficient embedded IoT solutions.

Streamlining Development

Zephyr screenshot from file directory

Operating System

Zephyr RTOS was chosen for this project due to its lightweight architecture, modular design, and extensive support for a wide range of hardware platforms, including the Nordic Thingy91. As an open-source real-time operating system, Zephyr provides essential features such as multi-threading, inter-thread communication, and real-time scheduling, making it well-suited for embedded applications that require reliable and timely processing. However, a key driver in choosing Zephyr was its integration with the nRF Connect SDK, which streamlined development by offering pre-configured libraries, sample applications, and device drivers specifically tailored for Nordic hardware.

Zephyr’s built-in device drivers and support for various communication protocols simplify integration with sensors like the ADXL362 accelerometer, allowing for rapid development and prototyping. The RTOS’s scalability ensures that it can accommodate future enhancements, making it a strategic choice for projects that may evolve in complexity or scope. Moreover, Zephyr’s active community and comprehensive documentation facilitate troubleshooting and feature implementation, further streamlining the development process for software architects and engineers alike.

Getting started VS Code start-up setup page.

Development Tools

In addition to leveraging QP and Zephyr RTOS, we utilized the nRF Connect extension for Visual Studio Code to streamline the development process for the Nordic Thingy91 project. This extension provides a robust set of tools for managing Nordic Semiconductor’s hardware, including easy access to sample applications, libraries, and debugging tools. The integration within VSCode allows for seamless project setup and configuration, significantly enhancing the development experience. Nordic’s training courses on the nRF Connect SDK and Zephyr RTOS were also invaluable resources that helped fast-track our team’s onboarding with Nordic hardware and software tools, ensuring that we could leverage the platform to its fullest potential.

Software Architecture

The Value of Publish and Subscribe Architecture

The P&S model plays a central role in this project by decoupling message producers (accelerometer sensor readings) from message consumers (visualization tools and data processing logic). This separation of concerns allows for parallel development of system components, simplifies testing, and supports future scalability with minimal refactoring. For software architects, this results in clear technical and operational advantages, as components can evolve independently without affecting the overall system.

In the case of this project, the P&S model enables efficient communication between the SPI interface, the accelerometer manager, and the visualization framework. Each component subscribes to relevant signals without requiring direct knowledge of how the data is produced or consumed, adhering to key principles of modularity and reducing interdependencies between system parts.

Event-Driven Design with QP

At the core of the system is QP’s active object model, which encapsulates the logic for periodic data acquisition, SPI communication, and state management in an autonomous software object. The accelerometer manager is implemented as an active object, which subscribes to SPI transaction completion signals and periodically publishes SPI data acquisition events. This event-driven architecture is essential in an embedded context, where timing and resource constraints demand efficient, non-blocking system designs.

QP’s Contribution to Modularity and Parallel Development

The behavior of each part of the system (accelerometer, SPI, UART serial) is encapsulated by QP’s active object pattern. This level of modularity allows developers to work on different system components in parallel, minimizing bottlenecks during development and enabling faster prototyping.

Additionally, QP supports event-driven task management, which simplifies the process of integrating new features or extending functionality. By leveraging QP’s active objects and publish-subscribe mechanisms, the development team can quickly incorporate additional sensors, communication protocols, or even cloud-based services without disrupting the core system architecture.

Software

Real-Time SPI Communication and Data Handling

The system uses SPI to communicate with the ADXL362 accelerometer. Every SPI transaction is initiated by publishing an event and completed by subscribing to a corresponding completion signal. This allows the system to remain responsive, with real-time data processing occurring as SPI transactions finish.

Periodic Sampling with Event-Driven Architecture

A key feature of the system is its ability to handle periodic accelerometer sampling using QP’s event-driven model. The QTimeEvt timer triggers periodic reads from the accelerometer, which are processed through a series of state transitions. In the collect_sample state, the SPI transaction is initiated, and upon completion, the data is processed and visualized.

This model allows the system to handle real-time data without continuously polling the sensor, reducing system load and improving performance. Each SPI transaction is handled independently, ensuring that the system can efficiently manage data collection even under heavy processing demands.

This code snippet demonstrates how the collect_sample state initiates and processes SPI transactions. By using QP’s event-driven and run-to-complete model, the system handles data acquisition asynchronously, avoiding potential delays or resource contention.

Data Visualization with Custom QView Script

QView script gif

To provide real-time feedback, a custom QView script was developed to visualize the accelerometer data. The script leverages Tkinter and Matplotlib to create a graphical interface that displays the accelerometer’s movement in real-time using a 3D wireframe plot.

Modular Data Visualization

The decoupled nature of the system allows the visualization component to operate independently of the data collection process. The QView script subscribes to the processed data and updates the visual representation accordingly. This approach simplifies the integration of additional features, such as different visualization techniques or additional sensor data, without modifying the underlying data acquisition logic.

The separation of concerns between data acquisition and visualization ensures that the system remains scalable and easy to maintain. Future visualizations or additional data streams can be integrated with minimal impact on the system’s core functionality.

Advantages for Software Development and Project Management

This architecture demonstrates the technical, operational, and project management benefits of the P&S model in embedded systems. By decoupling components and utilizing an event-driven architecture, the system reduces the complexity of managing real-time data processing.

Key Takeaways for Software Architects

  • Modularity: Components operate independently, allowing for parallel development and easier maintenance.
  • Scalability: The system can be extended with new features or sensors without requiring significant refactoring.
  • Efficiency: Event-driven processing reduces system load and improves real-time performance by avoiding continuous polling.
  • Maintainability: The decoupled architecture minimizes technical debt, making future updates or enhancements more manageable.

Conclusion

This real-time accelerometer visualization project demonstrates the power of the P&S model in embedded systems development. By combining QP’s active object pattern, Zephyr RTOS, and a decoupled architecture, we’ve created a modular and efficient system that can scale easily and handle real-time data processing with minimal latency.

For software development projects, especially those involving complex embedded systems, adopting a P&S architecture can significantly reduce complexity, streamline testing, and simplify project management, enabling faster development cycles and easier future upgrades.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get in Touch

If you have a product design that you would like to discuss, a technical problem in need of a solution, or if you just wish you could add more capabilities to your existing engineering team, please contact us.