Pressure
HAL Example
Device Compatibility
Overview
The pressure sensor reports values for:
- Pressure
- Altitude
- Temperature
Code Example
Below is an example of how to interface with the pressure sensor in MATRIX HAL.
Pressure sensor function references can be found here.
The following section shows how to receive data from the pressure sensor. You can download this example here.
The command below will compile the example. Be sure to pass in your C++ file and desired output file.
g++ -o YOUR_OUTPUT_FILE YOUR_CPP_FILE -std=c++11 -lmatrix_creator_hal
Include Statements
To begin working with the pressure sensor you need to include these header files.
// System calls #include <unistd.h> // Input/output streams and functions #include <iostream> // Interfaces with pressure sensor #include "matrix_hal/pressure_sensor.h" // Holds data from pressure sensor #include "matrix_hal/pressure_data.h" // Communicates with MATRIX device #include "matrix_hal/matrixio_bus.h"
Initial Setup
You'll then need to setup MatrixIOBus
in order to communicate with the hardware on your MATRIX device.
int main() { // Create MatrixIOBus object for hardware communication matrix_hal::MatrixIOBus bus; // Initialize bus and exit program if error occurs if (!bus.Init()) return false;
Main Setup
Now we will create our PressureData
and PressureSensor
object and use it to receive data from the pressure sensor.
// The following code is part of main() // Create PressureData object matrix_hal::PressureData pressure_data; // Create PressureSensor object matrix_hal::PressureSensor pressure_sensor; // Set pressure_sensor to use MatrixIOBus bus pressure_sensor.Setup(&bus); // Endless loop while (true) { // Overwrites pressure_data with new data from pressure sensor pressure_sensor.Read(&pressure_data); // Altitude output is represented in meters float altitude = pressure_data.altitude; // Pressure output is represented in Pa float pressure = pressure_data.pressure; // Temperature output is represented in Celsius float temperature = pressure_data.temperature; // Clear console std::system("clear"); // Output sensor data to console std::cout << " [ Pressure Sensor Output ]" << std::endl; std::cout << " [ Altitude (m) : " << altitude << " ] [ Pressure (Pa) : " << pressure << " ] [ Temperature (Celsius) : " << temperature << " ]" << std::endl; // Sleep for 20000 microseconds usleep(20000); } return 0; }