enc03r: C port; FTI; C++ wraps C

The API for this driver has changed.  See docs/apichanges.md.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2017-01-27 17:58:35 -07:00
parent f914159e21
commit 1bbb9386b7
15 changed files with 758 additions and 165 deletions

View File

@ -1,5 +1,9 @@
set (libname "enc03r")
set (libdescription "Enc03r single axis analog gyro module")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()
upm_mixed_module_init (NAME enc03r
DESCRIPTION "Single-axis Analog Gyroscope"
C_HDR enc03r.h
C_SRC enc03r.c
CPP_HDR enc03r.hpp
CPP_SRC enc03r.cxx
FTI_SRC enc03r_fti.c
CPP_WRAPS_C
REQUIRES mraa)

158
src/enc03r/enc03r.c Normal file
View File

@ -0,0 +1,158 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014-2017 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 <stdio.h>
#include <string.h>
#include <assert.h>
#include "enc03r.h"
enc03r_context enc03r_init(int pin, float aref)
{
// make sure MRAA is initialized
int mraa_rv;
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
{
printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
return NULL;
}
enc03r_context dev =
(enc03r_context)malloc(sizeof(struct _enc03r_context));
if (!dev)
return NULL;
// zero out context
memset((void *)dev, 0, sizeof(struct _enc03r_context));
dev->a_ref = aref;
if ( !(dev->aio = mraa_aio_init(pin)) )
{
printf("%s: mraa_aio_init() failed.\n", __FUNCTION__);
enc03r_close(dev);
return NULL;
}
dev->offset = 0.0;
dev->scale = 1.0;
dev->a_res = (float)(1 << mraa_aio_get_bit(dev->aio)) - 1.0;
dev->calibrationValue = 0.0;
return dev;
}
void enc03r_close(enc03r_context dev)
{
assert(dev != NULL);
if (dev->aio)
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t enc03r_update(enc03r_context dev)
{
assert(dev != NULL);
float val = (float)mraa_aio_read(dev->aio);
if (val < 0)
{
printf("%s: mraa_aio_read() failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
dev->normalized = val / dev->a_res;
// from seeed studio example
dev->angular_velocity =
((val - dev->calibrationValue) * (dev->a_ref * 1000.0)
/ dev->a_res / 0.67);
return UPM_SUCCESS;
}
upm_result_t enc03r_calibrate(const enc03r_context dev,
unsigned int samples)
{
assert(dev != NULL);
float total = 0.0;
for (int i=0; i<samples; i++)
{
int val = mraa_aio_read(dev->aio);
if (val < 0)
{
printf("%s: mraa_aio_read() failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
total += (float)val;
upm_delay_ms(2);
}
dev->calibrationValue = total / (float)samples;
return UPM_SUCCESS;
}
float enc03r_calibration_value(const enc03r_context dev)
{
assert(dev != NULL);
return dev->calibrationValue;
}
float enc03r_angular_velocity(const enc03r_context dev)
{
assert(dev != NULL);
return dev->angular_velocity * dev->scale + (dev->offset * dev->scale);
}
void enc03r_set_offset(const enc03r_context dev, float offset)
{
assert(dev != NULL);
dev->offset = offset;
}
void enc03r_set_scale(const enc03r_context dev, float scale)
{
assert(dev != NULL);
dev->scale = scale;
}
float enc03r_get_normalized(const enc03r_context dev)
{
assert(dev != NULL);
return dev->normalized;
}

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
* Copyright (c) 2014-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -31,52 +31,49 @@
using namespace upm;
using namespace std;
ENC03R::ENC03R(int pin, float vref)
ENC03R::ENC03R(int pin, float aref) :
m_enc03r(enc03r_init(pin, aref))
{
if ( !(m_aio = mraa_aio_init(pin)) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_aio_init() failed, invalid pin?");
return;
}
m_vref = vref;
m_calibrationValue = 0;
if (!m_enc03r)
throw std::runtime_error(string(__FUNCTION__)
+ ": enc03r_init() failed");
}
ENC03R::~ENC03R()
{
mraa_aio_close(m_aio);
}
unsigned int ENC03R::value()
{
int x = mraa_aio_read(m_aio);
if (x == -1) throw std::out_of_range(std::string(__FUNCTION__) +
": Failed to do an aio read.");
return (unsigned int) x;
enc03r_close(m_enc03r);
}
void ENC03R::calibrate(unsigned int samples)
{
int val;
float total = 0.0;
for (unsigned int i=0; i<samples; i++)
{
val = mraa_aio_read(m_aio);
if (val == -1) throw std::out_of_range(std::string(__FUNCTION__) +
": Failed to do an aio read.");
total += (float)val;
usleep(2000);
}
m_calibrationValue = total / (float)samples;
if (enc03r_calibrate(m_enc03r, samples))
throw std::runtime_error(string(__FUNCTION__)
+ ": enc03r_calibrate() failed");
}
double ENC03R::angularVelocity(unsigned int val)
void ENC03R::update()
{
// from seeed studio example
//return (((double)(val-m_calibrationValue)*(m_vref*1000.0))/1023.0/0.67);
return (((double)(val-m_calibrationValue)*(m_vref*1000.0))/685.41);
if (enc03r_update(m_enc03r))
throw std::runtime_error(string(__FUNCTION__)
+ ": enc03r_update() failed");
}
float ENC03R::angularVelocity()
{
return enc03r_angular_velocity(m_enc03r);
}
void ENC03R::setOffset(float offset)
{
enc03r_set_offset(m_enc03r, offset);
}
void ENC03R::setScale(float scale)
{
enc03r_set_scale(m_enc03r, scale);
}
float ENC03R::getNormalized()
{
return enc03r_get_normalized(m_enc03r);
}

159
src/enc03r/enc03r.h Normal file
View File

@ -0,0 +1,159 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014-2017 Intel Corporation.
emacs . *
* 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 <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <mraa/aio.h>
#include <upm.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file enc03r.h
* @library enc03r
* @brief Generic API for AT command based UART devices
*
*/
/**
* Device context
*/
typedef struct _enc03r_context {
mraa_aio_context aio;
// determined by calibrate();
float calibrationValue;
// our computed value
float angular_velocity;
// analog reference voltage
float a_ref;
// analog ADC resolution (max value)
float a_res;
// offset
float offset;
// scale
float scale;
// normalized ADC value
float normalized;
} *enc03r_context;
/**
* ENC03R sensor constructor
*
* @param pin Analog pin to use
* @param vref Reference voltage to use; default is 5.0 V
* @return Device context
*/
enc03r_context enc03r_init(int pin, float aref);
/**
* ENC03R destructor
*
* @param dev Device context
*/
void enc03r_close(enc03r_context dev);
/**
* Update the internal state with the current reading. This
* function must be called prior to calling
* enc03r_angular_velocity().
*
* @param dev Device context
* @return UPM result
*/
upm_result_t enc03r_update(enc03r_context dev);
/**
* Calibrates the sensor by determining an analog reading over many
* samples with no movement of the sensor. This must be done
* before attempting to use the sensor.
*
* @param dev Device context
* @param samples Number of samples to use for calibration
* @return UPM result
*/
upm_result_t enc03r_calibrate(const enc03r_context dev,
unsigned int samples);
/**
* Returns the currently stored calibration value
*
* @param dev Device context
* @return Current calibration value
*/
float enc03r_calibration_value(const enc03r_context dev);
/**
* Return the computed Angular Velocity in degrees per second.
* You must have called encr03r_update() prior to calling this
* function.
*
* @param dev Device context
* @return Computed angular velocity
*/
float enc03r_angular_velocity(const enc03r_context dev);
/**
* Set sensor offset. The offste is applied to the return value
* before scaling. Default is 0.
*
* @param dev Device context
* @param scale Scale to apply to value
*/
void enc03r_set_offset(const enc03r_context dev, float offset);
/**
* Set sensor scale. The return value is scaled by this value
* before the offset is applied. Default is 1.0.
*
* @param dev Device context
* @param scale Offset to apply to value
*/
void enc03r_set_scale(const enc03r_context dev, float scale);
/**
* Get a normalized ADC value from the sensor. The return value
* will be between 0.0 (indicating no voltage) and 1.0 indicating
* max voltage (aref). encr03r_update() must be called prior to
* calling this function.
*
* @param dev Device context
* @return The normalized reading from the ADC.
*/
float enc03r_get_normalized(const enc03r_context dev);
#ifdef __cplusplus
}
#endif

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
* Copyright (c) 2014-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -24,92 +24,116 @@
#pragma once
#include <string>
#include <mraa/aio.h>
#include "enc03r.h"
namespace upm {
/**
* @brief ENC03R Single Axis Gyro library
* @defgroup enc03r libupm-enc03r
* @ingroup seeed analog compass robok
*/
/**
* @library enc03r
* @sensor enc03r
* @comname ENC03R Single Axis Gyro
* @altname Grove Single Axis Analog Gyro
* @type compass
* @man seeed
* @con analog
* @kit robok
*
* @brief API for the ENC03R Single Axis Analog Gyro
*
* UPM module for the ENC03R single axis analog gyro.
* This gyroscope measures x-axis angular velocity, that is
* how fast the sensor is rotating around the x-axis.
* Calibration of the sensor is necessary for accurate readings.
*
* @image html enc03r.jpg
* @snippet enc03r.cxx Interesting
*/
class ENC03R {
public:
/**
* @brief ENC03R Single Axis Gyro library
* @defgroup enc03r libupm-enc03r
* @ingroup seeed analog compass robok
*/
/**
* ENC03R sensor constructor
* @library enc03r
* @sensor enc03r
* @comname ENC03R Single Axis Gyro
* @altname Grove Single Axis Analog Gyro
* @type compass
* @man seeed
* @con analog
* @kit robok
*
* @param pin Analog pin to use
* @param vref Reference voltage to use; default is 5.0 V
*/
ENC03R(int pin, float vref=5.0);
/**
* ENC03R destructor
*/
~ENC03R();
/**
* Calibrates the sensor by determining an analog reading over many
* samples with no movement of the sensor. This must be done
* before attempting to use the sensor.
* @brief API for the ENC03R Single Axis Analog Gyro
*
* @param samples Number of samples to use for calibration
*/
void calibrate(unsigned int samples);
/**
* Returns the raw value of the sensor
* UPM module for the ENC03R single axis analog gyro.
* This gyroscope measures x-axis angular velocity, that is
* how fast the sensor is rotating around the x-axis.
* Calibration of the sensor is necessary for accurate readings.
*
* @return Raw value of the sensor
* @image html enc03r.jpg
* @snippet enc03r.cxx Interesting
*/
unsigned int value();
class ENC03R {
public:
/**
* Returns the currently stored calibration value
*
* @return Current calibration value
*/
float calibrationValue() { return m_calibrationValue; };
/**
* ENC03R sensor constructor
*
* @param pin Analog pin to use
* @param vref Reference voltage to use; default is 5.0 V
*/
ENC03R(int pin, float aref=5.0);
/**
* Computes angular velocity based on the value and stored calibration
* reference.
*
* @param val Value to use to compute angular velocity
* @return Computed angular velocity
*/
double angularVelocity(unsigned int val);
/**
* ENC03R destructor
*/
~ENC03R();
private:
// determined by calibrate();
float m_calibrationValue;
/**
* Calibrates the sensor by determining an analog reading over many
* samples with no movement of the sensor. This must be done
* before attempting to use the sensor.
*
* @param samples Number of samples to use for calibration
*/
void calibrate(unsigned int samples);
// reference voltage
float m_vref;
mraa_aio_context m_aio;
};
/**
* Update the internal state with the current reading. This
* function must be called prior to calling
* angularVelocity().
*
* @param dev Device context
*/
void update();
/**
* Returns the currently stored calibration value
*
* @return Current calibration value
*/
float calibrationValue() { return enc03r_calibration_value(m_enc03r); };
/**
* Computes angular velocity based on the value and stored calibration
* reference.
*
* @param val Value to use to compute angular velocity
* @return Computed angular velocity
*/
float angularVelocity();
/**
* Set sensor offset. The offste is applied to the return value
* before scaling. Default is 0.
*
* @param scale Scale to apply to value
*/
void setOffset(float offset);
/**
* Set sensor scale. The return value is scaled by this value
* before the offset is applied. Default is 1.0.
*
* @param dev Device context
* @param scale Offset to apply to value
*/
void setScale(float scale);
/**
* Get a normalized ADC value from the sensor. The return
* value will be between 0.0 (indicating no voltage) and 1.0
* indicating max voltage (aref). update() must be called
* prior to calling this function.
*
* @return The normalized reading from the ADC.
*/
float getNormalized();
protected:
enc03r_context m_enc03r;
private:
};
}

102
src/enc03r/enc03r_fti.c Normal file
View File

@ -0,0 +1,102 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 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 "enc03r.h"
#include "upm_fti.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_enc03r_name[] = "ENC03R";
const char upm_enc03r_description[] = "Analog Single Axis Gyroscope";
const upm_protocol_t upm_enc03r_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_enc03r_category[] = {UPM_GYROSCOPE};
// forward declarations
const void* upm_enc03r_get_ft(upm_sensor_t sensor_type);
void* upm_enc03r_init_name();
void upm_enc03r_close(void *dev);
upm_result_t upm_enc03r_get_value(void *dev, float *value);
const upm_sensor_descriptor_t upm_enc03r_get_descriptor()
{
upm_sensor_descriptor_t usd;
usd.name = upm_enc03r_name;
usd.description = upm_enc03r_description;
usd.protocol_size = 1;
usd.protocol = upm_enc03r_protocol;
usd.category_size = 1;
usd.category = upm_enc03r_category;
return usd;
}
static const upm_sensor_ft ft =
{
.upm_sensor_init_name = &upm_enc03r_init_name,
.upm_sensor_close = &upm_enc03r_close,
};
static const upm_gyroscope_ft gft =
{
.upm_gyroscope_get_value = &upm_enc03r_get_value
};
const void* upm_enc03r_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft;
case UPM_GYROSCOPE:
return &gft;
default:
return NULL;
}
}
void* upm_enc03r_init_name()
{
return NULL;
}
void upm_enc03r_close(void *dev)
{
enc03r_close((enc03r_context)dev);
}
upm_result_t upm_enc03r_get_value(void *dev, float *value)
{
if (enc03r_update((enc03r_context)dev))
return UPM_ERROR_OPERATION_FAILED;
value[0] = enc03r_angular_velocity((enc03r_context)dev);
value[1] = 0.0;
value[2] = 0.0;
return UPM_SUCCESS;
}