\chapter{Inertial sensor network}

%\epigraph{\emph{`` É fazendo que se aprende a fazer aquilo que se deve aprender a fazer.''}}%
%         {Aristóteles}

This chapter explains the sensor network architecture, the hardware project and assembly, as well as the software and communication protocols.



\section{Architecture}

The network has the typical master, multi-slave structure fig(\ref{fig:sens_net_arch}), where it uses Controller Area Network bus (CANbus) to establish communication between the master and the slaves.
Each slave module is composed by one Microchip PIC18LF2580-I/ML microcontroller, one STMicroelectronics LPR503AL analog two-axis gyroscope, one STMicroelectronics LIS331DLH digital three axis accelerometer, one Texas Instruments SN65H233EP CANbus transceiver and several accessory passive components. This sensors are cutting edge technology and and just recentli started to spread in the electronics market

On startup, the slave modules load some configuration parameters from their EEPROM and initiate the digital accelerometer configuration over an I$^{2}$C\textsuperscript{\scriptsize{\texttrademark}} bus communication, with the microcontroller as the master and the accelerometer as the slave. After configuration the accelerometer generates one interrupt signal which triggers the microcontroller software to immediately initiate the data reading process from the digital accelerometer. After this acquisition, the microcontroller initiates the analog to digital conversion to read the analog gyroscope data, and then sends all the data to the master module that is listening for data in the CANbus.

Before going to small scale, the entire setup was implemented and tested using breadboard fig(\ref{fig:breadboard}) and standard components, where the software was developed and debugged.

The master module, similarly to slave modules, loads configuration parameters from its EEPROM on startup, creates a database for storing the latest data from each module and then starts the main cycle, waiting for data arriving by CANbus. After data has arrived, an interrupt is triggered to execute the procedure to read the data and store it in the database. One of the EEPROM parameters is the time interval at which the master device sends the data from the active modules to one remote computer by RS-232 communication protocol.

The communication between devices is carried out by four wires: two for the CANbus and two for the power supply. The two CANbus wires have to be terminated with one 120$\Omega$ resistor at each end to comply with CANbus specifications.

The power is supplied by a buck regulator fig(\ref{fig:power_supply}, \ref{fig:sply_sch}, \ref{fig:splyTOP} \& \ref{fig:splyBOTTOM}) which can supply up to 3A at 3.3V with an input from 4.5 to 18V.

\begin{figure}
  \centering
  % Requires \usepackage{graphicx}
  \includegraphics[width=1\textwidth]{sensor_network_sch}\\
  \caption{Sensor network architecture}
  \label{fig:sens_net_arch}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \includegraphics[width=0.9\textwidth]{sply_sch}\\
  \caption{Power supply PCB schematic}\label{fig:sply_sch}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{splyTOP}
    \label{fig:splyTOP}}
  \hfill
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{splyBOTTOM}
    \label{fig:splyBOTTOM}}
  \caption{Power supply PCB layout: top (a) and bottom (c)}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.7\textwidth]{breadboard}\\
  \caption{Breadboard setup for prototyping the circuit}\label{fig:breadboard}
\end{figure}


\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.7\textwidth]{power_suply}\\
  \caption{Power supply for the sensors network}\label{fig:power_supply}
\end{figure}


\section{Sensor specification}

\begin{table}
    \caption{LPR503AL gyroscope mechanical characteristics\cite{ST_gyro}}
    \label{tab:gyro_mec}
    %\begin{tabularx}{\textwidth}{|c|*{6}{x{1cm}|}}
    \begin{tabular}{cc}
    \centering
    \includegraphics[width=0.9\textwidth]{Gyro_mec}
    \end{tabular}
\end{table}

\begin{table}
    \caption{LIS331DLH accelerometer mechanical characteristics\cite{ST_acc}}
    \label{tab:acc_mec}
    %\begin{tabularx}{\textwidth}{|c|*{6}{x{1cm}|}}
    \begin{tabular}{cc}
    \centering
    \includegraphics[width=0.9\textwidth]{Acc_mec}
    \end{tabular}
\end{table}


\section{Slave}



\subsection{Hardware}

The entire hardware setup was developed around the integrated circuits needed for this device, trying to keep it as compact as possible fig(\ref{fig:module_scale}). All components, except the CAN transceiver are presented in surface mount packages for space optimization and the number of components was reduced to the minimum. Besides the integrated circuits there are only two voltage stabilization capacitors, two I$^{2}$C\textsuperscript{\scriptsize{\texttrademark}} bus pull-up resistors, one low pass filter and one high pass filter for each analog gyroscope axis and two status LEDs as shown in figures \ref{fig:pcb_sch} , \ref{fig:slave_front} and \ref{fig:slave_back}.

\begin{figure}
  % Requires \usepackage{graphicx}
  \includegraphics[width=1.0\textwidth]{sch}\\
  \caption{Sensor PCB schematic}\label{fig:pcb_sch}
\end{figure}

To keep the device as small as possible a third layer was added to the PCB to carry power, bus lines and device programming lines fig(\ref{fig:sens_midle}). The programming port was implemented based in the USB type A connectors, applying the male layout contacts to the PCB, which makes the connection as simple as inserting the tip of the module inside the modified USB type A female which is connected to the programming hardware as seen in figure \ref{fig:pgm1} and \ref{fig:pgm2}.

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \subfloat[]{
    \includegraphics[width=0.34\textwidth]{sens_TOP}
    \label{fig:sens_TOP}}
  \hfill
  \subfloat[]{
    \includegraphics[width=0.29\textwidth]{sens_midle}
    \label{fig:sens_midle}}
  \hfill
  \subfloat[]{
    \includegraphics[width=0.3\textwidth]{sens_bottom}
    \label{fig:sens_bottom}}
  \caption{Sensor PCB layout: top (a), middle (b) and bottom (c)}
\end{figure}

The assembling of the modules was accomplished using hot air from a rework station and solder paste; some difficulties where encountered due to the very small size of the components, and sometimes a preheating of the PCB was required to ensure the correct amount of solder paste, and avoid short-circuit between component pads.

This sensor module dimensions are 21x12x7mm as seen in figure \ref{fig:sens_TOP}. It is considerably smaller than all market available solutions and this allows it to be used in places until now unreachable to this kind of sensors.



\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.8\textwidth]{module_scale}\\
  \caption{Slave size compared to coin}
  \label{fig:module_scale}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.8\textwidth]{module_front}\\
  \caption{Slave front view}\label{fig:slave_front}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.8\textwidth]{module_back}\\
  \caption{Slave back view}\label{fig:slave_back}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{pgm1}
    \label{fig:pgm1}}
  \hfill
  \subfloat[]{
    \includegraphics[width=0.3\textwidth]{pgm2}
    \label{fig:pgm2}}
  \caption{Programming port}
\end{figure}



\subsection{Software}

The slave module software fig(\ref{fig:slave_flowchart}) was developed in C language and compiled with MPLAB\textsuperscript{\scriptsize{\textregistered}} C18 C Compiler Lite in MPLAB\textsuperscript{\scriptsize{\textregistered}} IDE environment.
The software has one main routine which configures the I$^{2}$C\textsuperscript{\scriptsize{\texttrademark}} communications and peripheral device writing configuration bytes to the accelerometer. It also configures the device hardware for interrupts, analog to digital conversion and IO digital pins. After that, the main routine enters an infinite loop responsible for the status LEDs handling. The yellow LED blinks every second and the red LED is on when the system is in error mode, that can be caused by unsuccessful CANbus communication or lack of data from the accelerometer. While in the infinite loop, the accelerometer triggers an interrupt every time it has a new set of data available for reading. The device then jumps to the interrupt service routine which calls for I$^{2}$C\textsuperscript{\scriptsize{\texttrademark}} handling functions to read the six bytes of acceleration data form the accelerometer, then calls for analog to digital handling functions to read the data from the analog gyroscope, and after the collection of all the data it sends it to the CANbus for the master to collect it, store and then send to a remote computer.

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.5\textwidth]{slave_software}\\
  \caption{Slave module software flowchart}\label{fig:slave_flowchart}
\end{figure}

\section{Master}



\subsection{Hardware}

The master device hardware fig(\ref{fig:master_top} \& \ref{fig:master_bottom}) is based in the same PCB as the slave device except it does not include inertial sensor or filters, and it is attached to one interface PCB fig(\ref{fig:max_TOP} \& \ref{fig:max_BOTTOM}), which contains the DB9 connector and the Maxim MAX3233ECWP TTL to RS-232 transceiver fig(\ref{fig:max_sch}).

\begin{figure}
  % Requires \usepackage{graphicx}
  \includegraphics[width=0.9\textwidth]{max_sch}\\
  \caption{Master PCB schematic}\label{fig:max_sch}
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{max_TOP}
    \label{fig:max_TOP}}
  \hfill
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{max_BOTTOM}
    \label{fig:max_BOTTOM}}
  \caption{Master PCB layout: top (a) and bottom (c)}
\end{figure}

\subsection{Software}

Similarly to the slave software, the master software fig(\ref{fig:master_flowchart}) was also developed in C language and compiled with MPLAB\textsuperscript{\scriptsize{\textregistered}} C18 C Compiler Lite in MPLAB\textsuperscript{\scriptsize{\textregistered}} IDE environment.
The software has one main routine which creates the database in the device second memory bank with capacity to store fourteen simultaneous slave devices, and clears the memory setting all the values to zero. In sequence, EEPROM values are loaded to global variables to define the rate at which the device sends all the data stored in memory to one remote computer, and then configures IO pins, timers, CAN module and interrupts, before entering the infinite loop responsible for status LEDs actuation, sending the data over RS-232 fig(\ref{fig:RS232_protocol}) and cleaning the database from expired entries caused by modules who have been disconnected. When data is received from CANbus one interrupt is generated and the device executes the interrupt service routine. This routine reads the new data; searches the table to find the entry which this new data came from; if the entry already exists the data is updated; if it's from new module, the routine searches for empty database entries to store the new module data. If the database is full, the master module enters error mode, but still handling the remaining slave modules.

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{master_top}
    \label{fig:master_top}}
  \hfill
  \subfloat[]{
    \includegraphics[width=0.4\textwidth]{master_bottom}
    \label{fig:master_bottom}}
  \caption{Master module top (a) and bottom (b) view }
\end{figure}

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=0.6\textwidth]{master_software}\\
  \caption{Master module software flowchart}\label{fig:master_flowchart}
\end{figure}

\section{RS-232 communication Protocol}
This protocol fig(\ref{fig:RS232_protocol}) is used to send the data from the master device database to any remote device. It works as a data stream, and the send timing can be modified by changing the 0x00 address in the device EEPROM memory.

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=1\textwidth]{RS232_protocol}\\
  \caption{Master module RS-232 protocol}\label{fig:RS232_protocol}
\end{figure}


\section{CAN communication Protocol}

This protocol fig(\ref{fig:CAN_protocol}) is used between slave devices and the master module. Given that CAN protocol requires at least eleven bit addressing, and to keep the messages as short as possible, some data is sent in the available address bits, followed by the six data bits containing the sensor measures values.

\begin{figure}
  % Requires \usepackage{graphicx}
  \centering
  \includegraphics[width=1\textwidth]{CAN_protocol}\\
  \caption{Sensor network CAN protocol}\label{fig:CAN_protocol}
\end{figure}

