o2: Added O2 sensor C source

Added the C source for the O2 sensor with necessary changes to cmake,
examples, docs.

    * Renamed all files with groveo2 to o2
    * Replaced all instances of groveo2 with o2
    * Added C source for o2 sensor
    * Updated all cmake files
    * Added C example for o2 sensor

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2016-09-07 10:18:06 -07:00
parent fe318a78d2
commit f9878b3fe6
19 changed files with 514 additions and 53 deletions

View File

@ -170,7 +170,7 @@ add_example (otp538u)
add_example (grovecollision)
add_example (groveelectromagnet)
add_example (groveemg)
add_example (groveo2)
add_example (o2)
add_example (grovegsr)
add_example (ina132)
add_example (l298)

View File

@ -24,7 +24,7 @@
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include "groveo2.hpp"
#include "o2.hpp"
using namespace std;
@ -42,8 +42,8 @@ int main(int argc, char **argv)
//! [Interesting]
// The was tested with the O2 Oxygen Concentration Sensor Module
// Instantiate a GroveO2 on analog pin A0
upm::GroveO2 *O2 = new upm::GroveO2(0);
// Instantiate a O2 on analog pin A0
upm::O2 *O2 = new upm::O2(0);
while (shouldRun)
{
cout << "The output voltage is: " << O2->voltageValue() << "mV" << endl;

View File

@ -96,6 +96,7 @@ add_example (hka5)
add_example (dfrorp)
add_example (vdiv)
add_example (mqx)
add_example (o2)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

75
examples/c/o2.c Normal file
View File

@ -0,0 +1,75 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 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 <signal.h>
#include "o2.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a o2 sensor on analog pin A0
o2_context sensor = o2_init(0);
if (!sensor)
{
printf("o2_init() failed.\n");
return -1;
}
// Every half a second, sample the sensor output
while (shouldRun)
{
float raw_volts = 0.0;
float o2_percent = 0.0;
o2_get_raw_volts(sensor, &raw_volts);
o2_get_value(sensor, &o2_percent);
printf("O2 raw volts: %0.03f v, o2: %0.03f %\n",
raw_volts, o2_percent);
usleep(500000);
}
//! [Interesting]
printf("Exiting\n");
o2_close(sensor);
return 0;
}

View File

@ -44,7 +44,7 @@ add_example(GroveMDSample grovemd)
add_example(GroveMoistureSample grovemoisture)
add_example(GroveMQ3 gas)
add_example(GroveMQ9 gas)
add_example(GroveO2Example groveo2)
add_example(O2Example o2)
add_example(GroveQTouch at42qt1070)
add_example(GroveRelaySample grove)
add_example(GroveRotarySample grove)

View File

@ -22,15 +22,15 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import upm_groveo2.GroveO2;
import upm_o2.O2;
public class GroveO2Example {
public class O2Example {
public static void main(String[] args) {
// TODO Auto-generated method stub
//! [Interesting]
// Initializing the Grove O2 sensor on the A) analog pin
GroveO2 o2 = new GroveO2(0);
O2 o2 = new O2(0);
while(true){
System.out.println("The output voltage is: "+o2.voltageValue());

View File

@ -21,15 +21,15 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var groveO2_lib = require("jsupm_groveo2");
var O2_lib = require("jsupm_o2");
// The was tested with the O2 Oxygen Concentration Sensor Module
// Instantiate a GroveO2 on analog pin A0
var groveO2_obj = new groveO2_lib.GroveO2(0);
// Instantiate a O2 on analog pin A0
var O2_obj = new O2_lib.O2(0);
var myInterval = setInterval(function()
{
console.log("The output voltage is: " + roundNum(groveO2_obj.voltageValue(), 5) + "mV");
console.log("The output voltage is: " + roundNum(O2_obj.voltageValue(), 5) + "mV");
}, 100);
function roundNum(num, decimalPlaces)
@ -42,9 +42,9 @@ function roundNum(num, decimalPlaces)
process.on('SIGINT', function()
{
clearInterval(myInterval);
groveO2_obj = null;
groveO2_lib.cleanUp();
groveO2_lib = null;
O2_obj = null;
O2_lib.cleanUp();
O2_lib = null;
console.log("Exiting...");
process.exit(0);
});

View File

@ -22,11 +22,11 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import time, sys, signal, atexit
import pyupm_groveo2 as upmGroveo2
import pyupm_o2 as upmO2
# This was tested with the O2 Oxygen Concentration Sensor Module
# Instantiate a GroveO2 on analog pin A0
myGroveO2 = upmGroveo2.GroveO2(0)
# Instantiate a O2 on analog pin A0
myO2 = upmO2.O2(0)
## Exit handlers ##
@ -34,7 +34,7 @@ myGroveO2 = upmGroveo2.GroveO2(0)
def SIGINTHandler(signum, frame):
raise SystemExit
# This lets you run code on exit, including functions from myGroveO2
# This lets you run code on exit, including functions from myO2
def exitHandler():
print "Exiting"
sys.exit(0)
@ -46,6 +46,6 @@ signal.signal(signal.SIGINT, SIGINTHandler)
while(1):
print "The output voltage is: {0}mV".format(
myGroveO2.voltageValue())
myO2.voltageValue())
time.sleep(.1)

View File

@ -1,5 +0,0 @@
set (libname "groveo2")
set (libdescription "upm groveO2 oxygen concentration sensor module")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()

View File

@ -1,8 +0,0 @@
%module jsupm_groveo2
%include "../upm.i"
%{
#include "groveo2.hpp"
%}
%include "groveo2.hpp"

8
src/o2/CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
upm_mixed_module_init (NAME o2
DESCRIPTION "upm O2 oxygen concentration sensor module"
C_HDR o2.h
C_SRC o2.c
CPP_HDR o2.hpp
CPP_SRC o2.cxx
FTI_SRC o2_fti.c
REQUIRES mraa)

View File

@ -1,16 +1,16 @@
%module javaupm_groveo2
%module javaupm_o2
%include "../upm.i"
%{
#include "groveo2.hpp"
#include "o2.hpp"
%}
%include "groveo2.hpp"
%include "o2.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_groveo2");
System.loadLibrary("javaupm_o2");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);

8
src/o2/jsupm_o2.i Normal file
View File

@ -0,0 +1,8 @@
%module jsupm_o2
%include "../upm.i"
%{
#include "o2.hpp"
%}
%include "o2.hpp"

131
src/o2/o2.c Normal file
View File

@ -0,0 +1,131 @@
/*
* Author:
* Copyright (c) 2015 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 <string.h>
#include <stdlib.h>
#include "o2.h"
o2_context o2_init(int16_t pin)
{
o2_context dev = (o2_context)malloc(sizeof(struct _o2_context));
if(dev == NULL) return NULL;
/* Init aio pin */
dev->aio = mraa_aio_init(pin);
if(dev->aio == NULL) {
free(dev);
return NULL;
}
/* Set defaults */
dev->m_aRef = 5.0;
dev->m_offset = 0.0;
dev->m_scale = 1.0;
return dev;
}
void o2_close(o2_context dev)
{
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t o2_set_aref(const o2_context dev, float aref)
{
dev->m_aRef = aref;
return UPM_SUCCESS;
}
float o2_get_aref(const o2_context dev)
{
return dev->m_aRef;
}
upm_result_t o2_set_offset(const o2_context dev, float offset)
{
dev->m_offset = offset;
return UPM_SUCCESS;
}
float o2_get_offset(const o2_context dev)
{
return dev->m_offset;
}
upm_result_t o2_set_scale(const o2_context dev, float scale)
{
dev->m_scale = scale;
return UPM_SUCCESS;
}
float o2_get_scale(const o2_context dev)
{
return dev->m_scale;
}
upm_result_t o2_get_counts(const o2_context dev, int *value)
{
/* Read counts */
*value = mraa_aio_read(dev->aio);
if (*value < 0) return UPM_ERROR_OPERATION_FAILED;
return UPM_SUCCESS;
}
upm_result_t o2_get_raw_volts(const o2_context dev, float *value)
{
/* Read normalized adc value */
*value = mraa_aio_read_float(dev->aio);
if (*value < 0.0) return UPM_ERROR_OPERATION_FAILED;
/* Convert normalized value to voltage via aRef */
*value *= dev->m_aRef;
return UPM_SUCCESS;
}
upm_result_t o2_get_value(const o2_context dev, float *value)
{
/* Read normalized value */
*value = mraa_aio_read_float(dev->aio);
if (*value < 0.0)
return UPM_ERROR_OPERATION_FAILED;
/* Apply raw scale */
*value *= dev->m_scale;
/* Convert to %oxygen
Datasheet for grove o2 shows a linear response for the sensor. Assuming
20.5% oxygen @ 25 celsius, with an gain = 1 + 12k/100 = 121, a
dynamic range of 0->25% oxygen, and opamp rails of 0->3.3v (the grove o2
sensor uses a high-accuracy 3.3v regulator),
*/
*value *= 25 * dev->m_aRef/3.3;
*value += dev->m_offset;
return UPM_SUCCESS;
}

View File

@ -26,12 +26,12 @@
#include <string>
#include <stdexcept>
#include "groveo2.hpp"
#include "o2.hpp"
using namespace upm;
using namespace std;
GroveO2::GroveO2(int pin)
O2::O2(int pin)
{
if ( !(m_aio = mraa_aio_init(pin)) )
{
@ -41,12 +41,12 @@ GroveO2::GroveO2(int pin)
}
}
GroveO2::~GroveO2()
O2::~O2()
{
mraa_aio_close(m_aio);
}
float GroveO2::voltageValue()
float O2::voltageValue()
{
int val = mraa_aio_read(m_aio);
if (val == -1) return -1.0f;

135
src/o2/o2.h Normal file
View File

@ -0,0 +1,135 @@
/*
* Author:
* Copyright (c) 2015 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 "upm.h"
#include "mraa/aio.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* device context
*/
typedef struct _o2_context {
/* mraa aio pin context */
mraa_aio_context aio;
/* Analog voltage reference */
float m_aRef;
/* Scale */
float m_scale;
/* Offset in sensor units */
float m_offset;
} *o2_context;
/**
* Initialize analog sensor
* @param pin is Analog pin
* @return sensor context as void pointer
*/
o2_context o2_init(int16_t pin);
/**
* Analog sensor destructor
* @param sensor context pointer deallocate memory
*/
void o2_close(o2_context dev);
/**
* Set sensor adc vref.
* @param dev sensor context pointer
* @param aref Voltage reference routed to ADC ref pin
* @return Function result code
*/
upm_result_t o2_set_aref(const o2_context dev, float aref);
/**
* Get sensor adc vref
* @param dev sensor context pointer
* @return Current adc vref
*/
float o2_get_aref(const o2_context dev);
/**
* Set sensor offset. This offset is applied to the return value:
* counts = counts * scale + offset * scale
* @param dev sensor context pointer
* @param offset count offset value used
* @return Function result code
*/
upm_result_t o2_set_offset(const o2_context dev, float offset);
/**
* Get sensor offset
* @param dev sensor context pointer
* @return Current sensor offset
*/
float o2_get_offset(const o2_context dev);
/**
* Set sensor scale. This scale is applied to the return value:
* counts = counts * scale + offset * scale
* @param dev sensor context pointer
* @param scale count scale value used
* @return Function result code
*/
upm_result_t o2_set_scale(const o2_context dev, float scale);
/**
* Get sensor scale
* @param dev sensor context pointer
* @return Current sensor scale
*/
float o2_get_scale(const o2_context dev);
/**
* Read raw adc counts from sensor
* @param dev sensor context pointer
* @param *value Raw adc value
* @return Function result code
*/
upm_result_t o2_get_counts(const o2_context dev, int *value);
/**
* Read voltage from sensor
* @param dev sensor context pointer
* @param *value Voltage (v)
* @return Function result code
*/
upm_result_t o2_get_raw_volts(const o2_context dev, float *value);
/**
* Read value from sensor
* @param dev sensor context pointer
* @param *value O2 percentage
* @return Function result code
*/
upm_result_t o2_get_value(const o2_context dev, float *value);
#ifdef __cplusplus
}
#endif

View File

@ -29,13 +29,13 @@
namespace upm {
/**
* @brief Grove O2 Oxygen Gas Sensor library
* @defgroup groveo2 libupm-groveo2
* @defgroup o2 libupm-o2
* @ingroup seeed analog gaseous
*/
/**
* @library groveo2
* @sensor groveo2
* @library o2
* @sensor o2
* @comname Grove O2 Sensor
* @type gaseous
* @man seeed
@ -45,21 +45,21 @@ namespace upm {
*
* The Grove O2 Oxygen Gas sensor measures the oxygen concentration in the air
*
* @image html groveo2.jpg
* @snippet groveo2.cxx Interesting
* @image html o2.jpg
* @snippet o2.cxx Interesting
*/
class GroveO2 {
class O2 {
public:
/**
* Grove O2 Oxygen Gas sensor constructor
*
* @param pin Analog pin to use
*/
GroveO2(int pin);
O2(int pin);
/**
* GroveO2 destructor
* O2 destructor
*/
~GroveO2();
~O2();
/**
* Measures O2 from the sensor
*

116
src/o2/o2_fti.c Normal file
View File

@ -0,0 +1,116 @@
/*
* Author:
* Copyright (c) 2015 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 "o2.h"
#include "upm_fti.h"
#include "fti/upm_sensor.h"
#include "fti/upm_raw.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_o2_name[] = "O2";
const char upm_o2_description[] = "Analog oxygen sensor";
const upm_protocol_t upm_o2_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_o2_category[] = {UPM_RAW};
// forward declarations
const void* upm_o2_get_ft(upm_sensor_t sensor_type);
void* upm_o2_init_str(const char* protocol, const char* params);
void upm_o2_close(void* dev);
const upm_sensor_descriptor_t upm_o2_get_descriptor();
upm_result_t upm_o2_set_offset(const void* dev, float offset);
upm_result_t upm_o2_set_scale(const void* dev, float scale);
upm_result_t upm_o2_get_value(const void* dev, float *value);
/* This sensor implementes 2 function tables */
/* 1. Generic base function table */
static const upm_sensor_ft ft_gen =
{
.upm_sensor_init_name = &upm_o2_init_str,
.upm_sensor_close = &upm_o2_close,
.upm_sensor_get_descriptor = &upm_o2_get_descriptor
};
/* 2. RAW function table */
static const upm_raw_ft ft_raw =
{
.upm_raw_set_offset = &upm_o2_set_offset,
.upm_raw_set_scale = &upm_o2_set_scale,
.upm_raw_get_value = &upm_o2_get_value
};
const void* upm_o2_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft_gen;
case UPM_RAW:
return &ft_raw;
default:
return NULL;
}
}
void* upm_o2_init_str(const char* protocol, const char* params)
{
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILE__);
return o2_init(0);
}
void upm_o2_close(void* dev)
{
o2_close((o2_context)dev);
}
const upm_sensor_descriptor_t upm_o2_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_o2_name;
usd.description = upm_o2_description;
usd.protocol_size = 1;
usd.protocol = upm_o2_protocol;
usd.category_size = 1;
usd.category = upm_o2_category;
return usd;
}
upm_result_t upm_o2_set_offset(const void* dev, float offset)
{
return o2_set_offset((o2_context)dev, offset);
}
upm_result_t upm_o2_set_scale(const void* dev, float scale)
{
return o2_set_scale((o2_context)dev, scale);
}
upm_result_t upm_o2_get_value(const void* dev, float *value)
{
return o2_get_value((o2_context)dev, value);
}

View File

@ -1,11 +1,11 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_groveo2
%module pyupm_o2
%include "../upm.i"
%feature("autodoc", "3");
%include "groveo2.hpp"
%include "o2.hpp"
%{
#include "groveo2.hpp"
#include "o2.hpp"
%}