mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
ppd42ns: Initial implementation of the Grove dust sensor
jrvandr: removed unnecessary mraa_init() Signed-off-by: Zion Orent <zorent@ics.com> Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:
parent
df390358f2
commit
7d40425391
@ -69,6 +69,7 @@ add_executable (mpr121-example mpr121.cxx)
|
||||
add_executable (ublox6-example ublox6.cxx)
|
||||
add_executable (yg1006-example yg1006.cxx)
|
||||
add_executable (wt5001-example wt5001.cxx)
|
||||
add_executable (ppd42ns-example ppd42ns.cxx)
|
||||
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||
@ -125,6 +126,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/mpr121)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/ublox6)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/yg1006)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/wt5001)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/ppd42ns)
|
||||
|
||||
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
||||
@ -197,3 +199,4 @@ target_link_libraries (mpr121-example mpr121 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (ublox6-example ublox6 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (yg1006-example yg1006 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (wt5001-example wt5001 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (ppd42ns-example ppd42ns ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
52
examples/javascript/ppd42ns.js
Normal file
52
examples/javascript/ppd42ns.js
Normal file
@ -0,0 +1,52 @@
|
||||
/*jslint node:true, vars:true, bitwise:true, unparam:true */
|
||||
/*jshint unused:true */
|
||||
/*global */
|
||||
/*
|
||||
* Author: Zion Orent <zorent@ics.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// Load dust sensor module
|
||||
var dustSensor = require('jsupm_ppd42ns');
|
||||
// Instantiate a dust sensor on digital pin D8
|
||||
var myDustSensor = new dustSensor.PPD42NS(8);
|
||||
|
||||
var data;
|
||||
|
||||
// Continue until user ends program
|
||||
var notice = "This program will give readings ";
|
||||
notice += "every 30 seconds until you stop it"
|
||||
console.log(notice);
|
||||
while(1)
|
||||
{
|
||||
data = myDustSensor.getData();
|
||||
console.log("Low pulse occupancy: " + data.lowPulseOccupancy);
|
||||
console.log("Ratio: " + data.ratio);
|
||||
console.log("Concentration: " + data.concentration);
|
||||
}
|
||||
|
||||
// Print message when exiting
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
63
examples/ppd42ns.cxx
Normal file
63
examples/ppd42ns.cxx
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Author: Zion Orent <zorent@ics.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include "ppd42ns.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int shouldRun = true;
|
||||
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
}
|
||||
|
||||
|
||||
int main ()
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
// Instantiate a dust sensor on GPIO pin D8
|
||||
upm::PPD42NS* dust = new upm::PPD42NS(8);
|
||||
dustData data;
|
||||
cout << "This program will give readings every 30 seconds until you stop it" << endl;
|
||||
while (shouldRun)
|
||||
{
|
||||
data = dust->getData();
|
||||
cout << "Low pulse occupancy: " << data.lowPulseOccupancy << endl;
|
||||
cout << "Ratio: " << data.ratio << endl;
|
||||
cout << "Concentration: " << data.concentration << endl;
|
||||
}
|
||||
//! [Interesting]
|
||||
|
||||
cout << "Exiting" << endl;
|
||||
|
||||
delete dust;
|
||||
return 0;
|
||||
}
|
5
src/ppd42ns/CMakeLists.txt
Normal file
5
src/ppd42ns/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
set (libname "ppd42ns")
|
||||
set (libdescription "upm ppd42ns dust sensor module")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_h ${libname}.h)
|
||||
upm_module_init()
|
8
src/ppd42ns/jsupm_ppd42ns.i
Normal file
8
src/ppd42ns/jsupm_ppd42ns.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_ppd42ns
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "ppd42ns.h"
|
||||
%}
|
||||
|
||||
%include "ppd42ns.h"
|
129
src/ppd42ns/ppd42ns.cxx
Normal file
129
src/ppd42ns/ppd42ns.cxx
Normal file
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include "ppd42ns.h"
|
||||
|
||||
using namespace upm;
|
||||
|
||||
PPD42NS::PPD42NS(int pin)
|
||||
{
|
||||
m_gpio = mraa_gpio_init(pin);
|
||||
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
|
||||
}
|
||||
|
||||
PPD42NS::~PPD42NS()
|
||||
{
|
||||
mraa_gpio_close(m_gpio);
|
||||
}
|
||||
|
||||
dustData PPD42NS::getData()
|
||||
{
|
||||
dustData data;
|
||||
struct timespec printData_start={0,0};
|
||||
struct timespec printData_now={0,0};
|
||||
clock_gettime(CLOCK_MONOTONIC, &printData_start);
|
||||
clock_gettime(CLOCK_MONOTONIC, &printData_now);
|
||||
double low_pulse_occupancy = 0;
|
||||
double pulse_check_time = 30;
|
||||
|
||||
// Keep reading dust data until 30 seconds have passed
|
||||
double start_time, end_time;
|
||||
while (m_timediff(printData_start, printData_now) < pulse_check_time)
|
||||
{
|
||||
start_time = m_timediff(printData_start, printData_now);
|
||||
end_time = pulse_check_time - start_time;
|
||||
low_pulse_occupancy += pulseIn_polyfill(0, end_time);
|
||||
clock_gettime(CLOCK_MONOTONIC, &printData_now);
|
||||
}
|
||||
|
||||
// Store dust data
|
||||
double ratio = low_pulse_occupancy / (pulse_check_time * 1000 * 10.0); // Integer percentage 0=>100
|
||||
double concentration = (1.1 * pow(ratio,3)) - (3.8 * pow(ratio, 2)) + (520 * ratio) + 0.62; // using spec sheet curve
|
||||
data.lowPulseOccupancy = (int)low_pulse_occupancy;
|
||||
data.ratio = ratio;
|
||||
data.concentration = concentration;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
// Mimicking Arduino's pulseIn function
|
||||
// return how long it takes a pin to go from HIGH to LOW or LOW to HIGH
|
||||
double PPD42NS::pulseIn_polyfill(bool high_low_value, double end_time)
|
||||
{
|
||||
struct timespec pulseIn_start={0,0};
|
||||
struct timespec pulseIn_now={0,0};
|
||||
struct timespec pulsetime_start={0,0};
|
||||
struct timespec pulsetime_end={0,0};
|
||||
|
||||
int pin_val = 5; // some non-zero, non-1 number
|
||||
bool started_timing = false;
|
||||
bool ended_timing = false;
|
||||
clock_gettime(CLOCK_MONOTONIC, &pulseIn_start);
|
||||
|
||||
// run through this loop until either:
|
||||
// a) we detect a change in pulse
|
||||
// b) we've hit 30 seconds
|
||||
while (!ended_timing && (m_timediff(pulseIn_start, pulseIn_now) < end_time))
|
||||
{
|
||||
pin_val = (bool)mraa_gpio_read(m_gpio);
|
||||
if (pin_val == high_low_value && !started_timing)
|
||||
{
|
||||
clock_gettime(CLOCK_MONOTONIC, &pulsetime_start);
|
||||
started_timing = true;
|
||||
//std::cout << "Started counting pulse change" << std::endl;
|
||||
usleep(50);
|
||||
}
|
||||
else if (started_timing && pin_val != high_low_value)
|
||||
{
|
||||
clock_gettime(CLOCK_MONOTONIC, &pulsetime_end);
|
||||
ended_timing = true;
|
||||
//std::cout << "Ended counting pulse change" << (m_timediff(pulseIn_start, pulseIn_now)) << std::endl;
|
||||
}
|
||||
else
|
||||
usleep(50);
|
||||
clock_gettime(CLOCK_MONOTONIC, &pulseIn_now);
|
||||
}
|
||||
|
||||
double low_pulse_occupancy = 0;
|
||||
|
||||
// if we ended due to detecting a pulse change and not due to hitting 30 seconds
|
||||
if (started_timing && ended_timing)
|
||||
low_pulse_occupancy = m_timediff(pulsetime_start, pulsetime_end);
|
||||
//std::cout << "Low pulse occupancy is " << low_pulse_occupancy << " seconds" << std::endl;
|
||||
|
||||
return (low_pulse_occupancy * 1000000); // convert to microseconds
|
||||
}
|
||||
|
||||
double PPD42NS::m_timediff(timespec time1, timespec time2)
|
||||
{
|
||||
return ((double)time2.tv_sec + 1.0e-9*time2.tv_nsec) -
|
||||
((double)time1.tv_sec + 1.0e-9*time1.tv_nsec);
|
||||
}
|
78
src/ppd42ns/ppd42ns.h
Normal file
78
src/ppd42ns/ppd42ns.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <mraa/aio.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int lowPulseOccupancy;
|
||||
double ratio;
|
||||
double concentration;
|
||||
} dustData;
|
||||
|
||||
namespace upm {
|
||||
|
||||
/**
|
||||
* @brief C++ API for the Dust Sensor
|
||||
*
|
||||
* UPM module for the Dust Sensor
|
||||
*
|
||||
* @ingroup gpio
|
||||
* @snippet ppd42ns.cxx Interesting
|
||||
*/
|
||||
class PPD42NS {
|
||||
public:
|
||||
/**
|
||||
* Dust Sensor Constructor
|
||||
*
|
||||
* @param pin digital pin to use
|
||||
*/
|
||||
PPD42NS(int pin);
|
||||
/**
|
||||
* Dust Sensor Destructor
|
||||
*/
|
||||
~PPD42NS();
|
||||
/**
|
||||
* Print dust concentration
|
||||
*
|
||||
* @return struct dustData contains data from dust sensor
|
||||
*/
|
||||
dustData getData();
|
||||
|
||||
private:
|
||||
mraa_gpio_context m_gpio;
|
||||
/**
|
||||
* Return the amount of time it takes a pin to go from HIGH to LOW or LOW to HIGH
|
||||
*
|
||||
* @param highLowValue int do we measure from HIGH to LOW or LOW to HIGH. highLowValue is "from" value
|
||||
*/
|
||||
double pulseIn_polyfill(bool highLowValue, double endTime);
|
||||
double m_timediff(timespec time1, timespec time2);
|
||||
};
|
||||
}
|
||||
|
||||
|
9
src/ppd42ns/pyupm_ppd42ns.i
Normal file
9
src/ppd42ns/pyupm_ppd42ns.i
Normal file
@ -0,0 +1,9 @@
|
||||
%module pyupm_ppd42ns
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "ppd42ns.h"
|
||||
%{
|
||||
#include "ppd42ns.h"
|
||||
%}
|
Loading…
x
Reference in New Issue
Block a user