
% ----------------------------------------------------
% -------------------- Software ----------------------
% ----------------------------------------------------

	The second main objective of this project was the design and development of software to monitor the pedals and to calibrate them in an interactive and intelligent way. This software development was divided in two parts: a program responsible for the interface and a program responsible for monitoring and publishing the data collected from sensors. 

% ------------------------------------------

\section{Development Tools}

	The software was developed under a Linux platform using C/C++ programming and the ROS structure, which provides libraries and tools to help the software development. In addition, the GTK+ toolkit was used to develop the graphical user interfaces, together with the Glade Software, which is a useful software for designing interfaces. In addition, it was necessary to use proprietary firmware libraries in order to communicate with the data acquisition system.

\subsection{Robot Operating System}

% --------------- ROS  ---------------------
	
	The Robot Operating System (ROS) is a flexible software development environment designed especially for the creation of robot software. Due to its modular structure, the ROS is able to assist the large-scale projects, dividing them into smaller modules with a more specific application. Besides reducing the size, it also reduces the complexity of the project by making it easier to debug and improves comprehension \cite{ROS}.
	
	The ROS is organized into packages, which are directories containing software from each module, and allows to establish communication between them. This communication is performed through messages: first, a module (node) publishes a message on a ROS Topic and then, another node subscribes to that topic and automatically starts to receive messages from the node that is publishing. While communicating, both nodes are connected to a topic (Figure \ref{fig:communication_basic_concept}). This communication structure allows to publish simultaneous topics and these topics can be subscribed by multiple nodes. In this way, a module can fulfil multiple tasks and be easily integrated in a simple or multiple research project \cite{ROS_wiki}.

\begin{figure}[htbp]
\centering
  	\includegraphics[width=0.65\textwidth]{images/ROS_basic_concepts.jpg}
  	\caption[Basic ROS communication concept.]{Basic ROS communication concept \cite{ROS_wiki}.}\label{fig:communication_basic_concept}
\end{figure}

	Another important aspect provided by ROS is the ability to store ROS data messages into \textit{bags}. \textit{Bags} are files which store all the requested published data, being considered the primary mechanism in ROS for data logging. These are usually created by the \textit{rosbag} package, which subscribes one or more ROS topics so that the messages are stored as they are received. After recorded, \textit{bags} can be played back and manipulated by tools in the \textit{rosbag} and in the \textit{rxbag} \cite{ROS_wiki}.
	
	Due to the important aspects of ROS, the \textit{Atlas Project} has been migrating every application to this environment.

% ----------------------------------------------------

\subsection{GTK+ and Glade Software}

% ---------- GTK+ and Glade Software -----------------

	One of the most important aspects of an application is the interface that is provided to interact with the user, who expect those interfaces to be graphical. Thus, for this system, a graphical interface to the user was designed using GTK+ and the Glade Software.
	 
	The GTK+, or GIMP Toolkit, is a multi-platform toolkit for creating graphical user interfaces. GTK+ is an object-oriented application programming interface that relies on multiple libraries such as: the GLib Object System (GObject) and the GIMP Drawing Kit (GDK), each providing a specific class or functionality. In order to provide the necessities of building graphical user interfaces, the GTK+ offers a complete set of widgets such as \cite{GTK_foundations}:

\begin{itemize}
\item Windows: normal windows or dialogs;
\item Displays: labels, images, progress bars and status bars;
\item Buttons and toggles: check buttons, radio buttons, toggle buttons and link buttons;
\item Numerical and text data entry and multi-line text editor;
\item Menus, toolbars and layouts.
\end{itemize}	

The GTK+ widgets use the GObject hierarchy system, which is one of the most important aspects of GTK+ (Figure \ref{fig:GTK_hierarchy}) \cite{GTK_foundations}.\\\\
 
\begin{figure}[htbp]
	\centering
  \includegraphics[width=0.65\textwidth]{images/GTK_hierarchy.pdf}
  	\caption[The widget hierarchy of GObject.]{The widget hierarchy of GTK+ \cite{GTK_foundations}.}\label{fig:GTK_hierarchy}
\end{figure}

	On the other hand, the Glade software is a user interface designer which complements the GTK+. Glade is a rapid application development tool that allows quick and easy development of user interfaces for the GTK+ toolkit. The designed user interfaces are saved as XML files and then can be loaded using the \textit{GtkBuilder} in GTK+. With Glade it is possible to separate the user interface layout from the code and this is very useful when developing a more complicated user interface, with a large number of widgets \cite{glade_software}.


% ----------------------------------------------------

\subsection{Firmware Libraries}

% --------------- Firmware Libraries -----------------

	In order to read the data from the sensors, the PhidgetInterfaceKit 8/8/8, which is the main part of the acquisition system, was used. For the communication with this board it was necessary to install the proper drivers and libraries, which are provided by Phidgets.
	
	Before starting to develop the software, it was important to understand how the libraries work and which functions are required to get the data from the board. There are four essential functions for this board to work, which are: 

\begin{itemize}
\item void \textit{start()};
\item int \textit{AttachHandler()};
\item int \textit{DetachHandler()};
\item int \textit{ErrorHandler()};
\end{itemize}	

	For example, by defining a class on a source file named \textit{PhidgetClass} and adding these functions to it, when the \textit{start()} function is called, the connected PhidgetInterfaceKit 8/8/8 will be attached, becoming ready to communicate with the computer. After the board is attached it is possible to read the data from the sensors using the function \textit{CPhidgetInterfaceKitget\_getSensorValue(ID, sensor\_index, \&Sensor1)}. This function requires three parameters: 1) the ID of the board, which is known when the board is attached, 2) the sensor index (which ranges from 0 to 7), which are the ports where the sensors are connected in the board, and 3) a variable, for example \textit{Sensor1}, where the sensor value is saved.


% ----------------------------------------------------



\section{Interface}
	
% --------------- Interface Program ------------------

	As mentioned, the program responsible for the interface was written in C/C++ and uses Glade and GTK+. It is responsible for the communication between the user and the system. This interface was intended to calibrate the sensors as well as to monitor the data received from them. To accomplish this, it was necessary to create a main source file responsible for two tasks: the interface and the communication between the program and the acquisition system (Figure \ref{fig:program_scheme}).
	
	The interface was designed using the Glade software and then loaded using the GTK+ in the main file. The interface is composed by three menus: 1) the \textit{Main menu} shown in Figure \ref{fig:main_menu}, where the user an choose either to create a new calibration or to load an existing one, 2) the \textit{New calibration menu} shown in Figure \ref{fig:new_calib}, where the user can create a new calibration and 3) the \textit{Load calibration menu} shown in Figure \ref{fig:load_calib}, where the user can load an existing calibration and monitor the pedals. 

\begin{figure}[!htbp]
\centering
  	\includegraphics[width=0.5\textwidth]{images/start_up_interface.jpg}
  	\caption[Interface main menu.]{Interface main menu.}\label{fig:main_menu}
\end{figure}

	By selecting the \textit{New calibration menu}, a dialog box, where it is possible to enter the username and the number of pedals to calibrate, is shown. By clicking in \textit{Continue}, the new calibration window emerges and the system is ready to create a new calibration. In order to do that, it is necessary to press the \textit{Start} button and simply follow the instructions (Figure \ref{fig:calib_scheme}). In case of failure, mostly due to the wrong orientation of the infrared (IR) sensors, a function was added to test that condition and issue a warning if is detected. After completing the calibration process, a XML file is created and a save file dialog is open so the user can choose where to save the calibration file. This XML file is organized into categories: the main category is 1) the root element, which is the \textit{calibration}, 2) the \textit{calibration} has two children named \textit{pedal}, 3) the \textit{pedal} has 2 children, which are the \textit{name} and the \textit{sensor}, and finally 4) the \textit{sensor} has 4 children that are \textit{name}, \textit{type}, \textit{min} and \textit{max}, as shown in Figure \ref{fig:xml_example}. 
	
\begin{figure}[H]
        \centering
        \subfloat[Selection menu for user and pedals.]{\includegraphics[width=0.4\textwidth]{images/new_user_interface.jpg}}
        \qquad
        \subfloat[New calibration menu]{\includegraphics[width=0.4\textwidth]{images/new_calib_interface.jpg}}
        \caption[Interface new calibration menu.]{Interface new calibration menu.}\label{fig:new_calib}
\end{figure}


\begin{figure}[htbp]
\centering
  	\includegraphics[width=0.4\textwidth]{images/calibration_scheme.pdf}
  	\caption[Calibration instructions scheme.]{Calibration instructions scheme.}\label{fig:calib_scheme}
\end{figure}

\begin{figure}[htbp]
\centering
  	\includegraphics[width=0.6\textwidth]{images/xml_calibration_file.pdf}
  	\caption{Example of a XML calibration file.}\label{fig:xml_example}
\end{figure}	

	Afterwards, it is possible to load the calibration from the file that was created. When the \textit{Load calibration menu} is selected, an open file dialog is shown and the user can select the calibration file he wants to load. When the calibration file is loaded, the data from sensors is automatically calibrated and displayed.  

\begin{figure}[htbp]
\centering
  	\includegraphics[width=0.5\textwidth]{images/load_calib_interface.jpg}
  	\caption[Interface load calibration menu.]{Interface load calibration menu.}\label{fig:load_calib}
\end{figure}

	In order to create a new calibration or to load an existing one, it is necessary to read the data from the acquisition system in real-time. However, the interface is loaded by using the GTK+, which is a blocking function and thus blocks computer processing. To make it possible the use of other functions at the same time as the GTK+, it was necessary to create a thread, making the computer processor work in parallel instead of working in a single process. In this way, it is possible to run the interface and at the same time run the functions responsible for collecting the data from the acquisition system.
			
	In order to access the sensor values, another set of functions using the Phidgets library was created inside this thread. Among this set of functions, two were responsible for the reading rate: one at 40Hz to read the infrared (IR) sensor values, and one at 100 Hz to read the load cell (LC) and the force sensitive resistor (FSR) values. These reading rates had to be different due to the different performance of the sensors. Inside these functions, there are other functions responsible for the actual reading of the data and, if a calibration file is loaded, they process the data calibration: \textit{readIRSensors}, \textit{calibrateIRSensors}, \textit{readLCSensors}, \textit{calibrateLCSensors}, \textit{readFSRSensors} and \textit{calibrateFSRSensors}.\\


\begin{figure}[H]
\centering
  	\includegraphics[width=\textwidth]{images/program_scheme.pdf}
  	\caption[Code organization diagram.]{Code organization diagram.}\label{fig:program_scheme}
\end{figure}

% ----------------------------------------------------


\section{Monitoring and Publishing}

% ------------------ Monitor and Publish--------------

	As mentioned, besides the interface, another program was created. This program was developed to be responsible for monitoring and publishing data on ROS. Thus, in order to launch this program an existing calibration is required.
	
	The monitoring and publishing program is meant to be launched when the user wants to monitor the pedals and when a calibration file already exists. Similarly to the interface, this program also has functions responsible for reading the data from the sensors. However, instead of launching an interface, it loads the calibration XML file and with the calibration data as a basis, the received data is calibrated and published on ROS. This program publishes two types of messages: 1) a default message with the type sensor\_msgs/range, which is the type of message published by the infrared (IR) sensors and 2) a pressure\_cells/SenVal, which is the type of message published by the load cells (LC) and the force sensitive resistors (FSR) (Figure \ref{fig:rqt_graph}).

\begin{figure}[H]
\centering
  	\includegraphics[width=0.8\textwidth]{images/rqt_graph.pdf}
  	\caption[Published messages on ROS.]{Published messages on ROS.}\label{fig:rqt_graph}
\end{figure}
	
	These messages can be displayed using the ROS rviz, which is a ROS package that allows the visualization of specific messages: range type for the IR messages and markers type for the SenVal messages. For instance, in Figure \ref{fig:rviz}) displayed messages are presented: the green color represents the LC values, the blue color the FSR values and the red color the IR sensor values.
	

\begin{figure}[H]
\centering
  	\includegraphics[width=0.6\textwidth]{images/rviz.jpg}
  	\caption[Published data displayed on Rviz.]{Published data displayed on Rviz.}\label{fig:rviz}
\end{figure}

	In addition, it is also possible to launch several packages together with the one developed in this system. For instance, it would be interesting to also know the car speed in this system, hence, it is possible to launch an odometry package, in order to measure the car speed and rpm. In order to launch a package or several packages, it is necessary to create a \textit{launch file}, which is XML configuration file where is it possible to specify which packages the user want to launch, among other options. An example of a launch file is shown in Figure \ref{fig:launch_file}.
	
\begin{figure}[htbp]
\centering
  	\includegraphics[width=\textwidth]{images/launch_file_example.pdf}
  	\caption{Launch file example used to record the data from the monitoring system.}\label{fig:launch_file}
\end{figure}	
	
% ----------------------------------------------------


\section{Future developments}

% ----------------------------------------------------

	As mentioned before, it is possible to use other electronic devices as acquisition boards, such as an Arduino or a Raspberry Pi, for a final solution approach.
	
	In case of using an Arduino, instead of Phidgets libraries, it would be necessary to develop a code to read the data from the sensors. In order to do that, the code should be able to read the Arduino analogical pins where the sensors are connected to. The Arduino code is implemented in C/C++ and there are many libraries available that facilitate the code development; thus, to read an analogical pin it is just required to use a simple function --- \textit{analogRead()}.
	
	Also, both the developed programs, the interface and the monitoring and publishing, would require some changes in order to communicate with the Arduino instead of the Phidgets board. The communication with the Arduino is much easier, due to the fact that the Arduino communicates by serial port. To access the data from the Arduino, it is necessary to replace the Phidget class to a serial read function. To implement this serial read function, it is possible to use the boost/asio library that provides the \textit{asio::read()} function, which reads the data from an established serial connection.
		
	In addition, by adding a bluetooth module to the Arduino, it would be possible to communicate with the system using a device with bluetooth, such as a smartphone or tablet. With this module and an user interface application for Android, developed using the Android software development kit, it would be possible to interact with the system using a smartphone or a tablet, instead of a computer.
			
	In case of using a Raspberry Pi, it would be possible to create a WiFi network to communicate with the user gadgets, such as smartphone, tablet, laptop or other devices supporting WiFi communication. With the WiFi connection, and developing an user interface using HTML and PHP languages, the user interface would be accessible from every platform that has a web browser. This approach would be an asset to make the system even more flexible and robust. 


% ----------------------------------------------------