forked from emlid/Navio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++: Examples: add Multithread Examples
- Loading branch information
1 parent
a63641b
commit 5965781
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CC = g++ | ||
NAVIO = ../../Navio/Common | ||
INCLUDES = -I ../../Navio | ||
|
||
all: | ||
$(CC) $(INCLUDES) threaded_baro.cpp $(NAVIO)/MS5611.cpp $(NAVIO)/I2Cdev.cpp $(NAVIO)/Util.cpp -lpthread -o threaded_baro | ||
|
||
clean: | ||
rm threaded_baro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Provided to you by Emlid Ltd (c) 2014. | ||
twitter.com/emlidtech || www.emlid.com || [email protected] | ||
Example: Get pressure from MS5611 barometer onboard of Navio shield for Raspberry Pi | ||
using a different thread, to update pressure info in the background | ||
To run this example navigate to the directory containing it and run following commands: | ||
make | ||
sudo ./threaded_baro | ||
*/ | ||
|
||
#include <Common/MS5611.h> | ||
#include <Common/Util.h> | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <pthread.h> | ||
|
||
void * acquireBarometerData(void * barom) | ||
{ | ||
MS5611* barometer = (MS5611*)barom; | ||
|
||
while (true) { | ||
barometer->refreshPressure(); | ||
usleep(10000); // Waiting for pressure data ready | ||
barometer->readPressure(); | ||
|
||
barometer->refreshTemperature(); | ||
usleep(10000); // Waiting for temperature data ready | ||
barometer->readTemperature(); | ||
|
||
barometer->calculatePressureAndTemperature(); | ||
|
||
//sleep(0.5); | ||
} | ||
|
||
pthread_exit(NULL); | ||
} | ||
|
||
int main() | ||
{ | ||
if (check_apm()) { | ||
return 1; | ||
} | ||
|
||
MS5611 baro; | ||
baro.initialize(); | ||
|
||
pthread_t baro_thread; | ||
|
||
if(pthread_create(&baro_thread, NULL, acquireBarometerData, (void *)&baro)) | ||
{ | ||
printf("Error: Failed to create barometer thread\n"); | ||
return 0; | ||
} | ||
|
||
while(true) | ||
{ | ||
printf("Temperature(C): %f Pressure(millibar): %f\n", baro.getTemperature(), baro.getPressure()); | ||
sleep(1); | ||
} | ||
|
||
pthread_exit(NULL); | ||
|
||
return 1; | ||
} |