Static Library Util.a

Table of contents
4.Specification requirements
5.Solutions suggestions
6.The magnetic fields sensors
7.The inclination measurement system
8.The gyroscope
9.The data acquisition system
10.Communication system
11.The power supply
12.Realisation of the PCB
13.The embedded system
14.Static Library Util.a
17.Sensors controller commands
19.Future improvements

14.           Static Library Util.a

Viewport and XCompass are two programs used to manage the Smart Compass card throw the serial port. To do so, they share the same static library: util.a

Figure 26

This library contains the classes and functions used by the programs

  • To control of the serial port (class Cport)

  • To display the notifications and the errors and log them in a file (class Clog)

  • To construct and interpret the data received and send to the Smart Compass (class CNavigation).

  • Others common functions are in the file util.cpp

Figure 27

14.1.                Cport

Cport class contains the functions used to handle the serial port.

14.1.1. Access port on Linux

The way to access the port on Linux is the same way to access a file. It can be open, read, write and close by the same function. Linux names its serial ports in the UNIX tradition. The first serial port has the file name /dev/ttyS0, the second serial port has the file name /dev/ttyS1, and so on. Once open, a termios structure can alter the characteristic of the port.

14.1.2. Termios structure

This structure gathers the characteristic to access on a serial device (including the standard output: cout, cin, cerr) under Linux. Many option can be setup there. The most common are the baudrate, the frame type (parity, stop bits, data bits) and the flow control.

A Cport’s subclass is Cbuffer. It emulate a circular buffer that store the last data received from the port.

Excluding the methods used to receive and send the data (char, string, structure), the Cport class can execute a shell command throw a pipe (ex: setserial, sz …).

14.2.                Clog

Clog class saves and displays the data from the programs. A member function searches for keyword and highlights them with one of the following colours (red, green, yellow, blue, purple, grey and cyan) or form (bold, underlined, blink, inverse). Once this class called, a pointer to file is created and a list of keyword is loaded from the file “HIGHLIGHT”. Each message send to the static member write() is saved in the opened file and sent to the std::cout output with the appropriate colour.

            This class also deals with the errors thanks to the static member function error_detected(). The error message is display and the errno error if exist, is commented.

            If the class is launch by an Xwindow program, the method m_ouput allow adding some specific function. Therefore, pointers to three functions are passed:

  • To output in a multi-line text edit

  • To open a messagebox after each notificationalert

  • To advance a curser while progressing a task

14.3.                CModule

The Cmodule class is an abstract class that defines the structure to the different modules of the Card (magneto, accelero, gyro, power and compensation). Each of these modules shares the same behaviour.

14.3.1.  Middle-level methods

The module can request value from the card. They have to be calibrating first, then they can be execute (1). The instruction is then send to the command method that create the request frame and insert the headers (2). Then, the corresponding structure is send to the micro controller where the frame is interpreted and executed. The reply is also a structure that is received (3) and transmits back to the command that save the value into the module (4). The process is similar for the calibrate method exec that except that the value saved is the average value of high number of value (5). The initialised data can be saved or loaded from / to a file (6).

 The methods exec() and calibrate() do not return directly the value but register them in private structure inside the class. Therefore, to access the data, the virtual function getInit() and getVal() have to be called (7).

Figure 28

14.3.2.  Calibration

The calibration is essential for the rest of the measurement. The initial values are used many times in the calculation of the azimuth. Therefore, we should have the most accurate values as possible. The calibration’s program has been realized in this purpose. The process of the calibration is different regarding the module:

  • For the magneto sensors, the initial values are the averages voltage read after a negative and a positive pulse

  • For magnetic field compensation, we have to make a first series a measurement in an initial direction, and then a second with the card turn of 180º. The values of the magnetic field interference are the two series.

  • For all the other modules, the calibration of the module is done in two steps:

    • A pre-calibration to determine the tolerance

    • The calibration that calculates the average.

The calibration is done according to the following diagram:

Figure 29

When the calibration is finished, the initialized values are saved on a calibration file chosen by the user. If the file does not exist, a new one is created. This file could be loaded at anytime. In addition, the initial values are load each time ‘Xcompass’ or ‘viewport’ start. Nevertheless for optimized result a calibration have to be done every day.

14.3.3.  Request the value

The command method provides an easy way to send and receive any type of data. In addition, this method deals with the header. If errors occur during the communication or in the microcontroller, the error code is interpreted there and forwarded to the Clog class to be registered, commented and notified.

14.3.4.  Direct control:

The different value from the sensors can be access through the method getValue() from the corresponding module: magneto, gyro, accelero, power that returns respectively the magnetic fields, the rotation velocity, the acceleration and the voltage of the system.

Some control methods are Usage(), resetSystem(), pingSystem()

14.3.5.  Matter of inheritance

The C++ language allows the multiple inheritances in contrast of other object oriented language like the Java. However, the method is a little tricky when a great child (like Cnavigation) inherits from two children (ex: CMagneto and Cgyro) that have the same parent (CModule). In that case, the parent is duplicated and two distinct instance of this object co-exist. It means that if a variable is change by a child, the modification does not occur on the second. This problem does not really affect the methods, especially virtual methods that have no implementation.

Figure 30

To by-pass this problem, the solution implemented have been create a private inner class (Common) which the only instance have a static linkage. In that way, all the shared data that should have a space dedicated, are not duplicated and can be access by any child.

Figure 31

14.4.                CNavigation

This class is specific to the Smart Compass card. It holds the object of the different module and implements the methods that have to receive information from more than one module. Consequently, CNavigation gathers the method to calibrate the different modules, to calculate the angle between the card and magnetic north.

14.4.1. Calculation of the azimuth

The method getNorth() calculate from the different parameters, the angle between the heading direction and the magnetic north.

The calculation of azimuth tries to compensate the different source of errors.

Many kinds of errors may occur during the measurement:

Figure 32


Equation 9

  • Magnetic error:

  • The earth field may be distorted by other magnetic fields or by nearby ferrous materials. These errors can be classified into two types:

    • Deterministic interference: The interference source is at a fixed position relative to the compass and its magnitude is constant over the time. The effect will be to shift circle’s centre of the interference fields. This error can be reduced by a calibration: the shift of the centre can be compensating by electro-magnetic feedback.

    • Non-Deterministic interference: the interference source happens from time to time with unknown magnitude: for example the robot passes close to a speaker or a computer. This kind of error cannot be compensating. However, once detected, the lecture of the angle can be switch on the integration of the gyro value over a short period.

  • Drift:

  • The most important error from gyro is a slow drift of the average value during the time. This error is non-deterministic and its value can be some m°/s per minute. A way to compensate this error can be to add a numerical low-pass filter with Fc = ~0.1Hz

    This error lead to an important deviation as the gyro velocity is integrated to obtain the angle.

  • Tilt:

  • The inclination leads to an error due to the measurement of the component of the field.

  • Saturation:

  • Saturation can appear from:

    • The ADCs Max386 and Atmega32, which cannot read a value outside the range [0 V– 4,096 V]

    • The amplifier INA2126 has an output range: [0,65 V – 4,35 V]

    Saturation may appear in the magneto-sensor if the magnitude of the field detected is twice the earth field.

  • Numerisation:

    • Quantification:

    • The numerisation of the analogical values by the ADC produce a noise from measure. This noise can be modelize by the following function where Va is the tension from the sensor and Vn the value read.


      Equation 10

    • Sampling:

    • The noise is directly dependent of the read frequency

      Shannon theorem: Fe ³ 2 * Fmax

      => Problem for the gyro.

  • Gaussian white noise:

  • Voltage and movement noise are measured. Refer to the section.

      In regards to these perturbations, the following algorithm has been implemented to determine the corresponding situation.

      Figure 33

      14.5.                Util.cpp

      This file contains some useful functions:

      • Conversion method (ASCII to hexadecimal or binary, int to string)

      • A non blocking keyboard input reader.

contact - link to this site