mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
ADXRS610Gyroscope: Add string based constructor
Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
parent
c7cca92fa7
commit
055c12f080
@ -24,39 +24,85 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "upm_string_parser.hpp"
|
||||||
#include "adxrs610.hpp"
|
#include "adxrs610.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace upm;
|
using namespace upm;
|
||||||
|
|
||||||
ADXRS610::ADXRS610(int dPin, int tPin, float aref) :
|
ADXRS610::ADXRS610(int dPin, int tPin, float aref) :
|
||||||
m_aioData(dPin), m_aioTemp(tPin)
|
m_aioData(new mraa::Aio(dPin)), m_aioTemp(new mraa::Aio(tPin))
|
||||||
{
|
{
|
||||||
// ADC resolution of data and temp should be the same...
|
// ADC resolution of data and temp should be the same...
|
||||||
m_aRes = (1 << m_aioData.getBit());
|
m_aRes = (1 << m_aioData->getBit());
|
||||||
m_aref = aref;
|
m_aref = aref;
|
||||||
|
|
||||||
setZeroPoint(calibrateZeroPoint());
|
setZeroPoint(calibrateZeroPoint());
|
||||||
setDeadband(0.0);
|
setDeadband(0.0);
|
||||||
|
|
||||||
m_centerVolts = aref / 2.0;
|
m_centerVolts = aref / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ADXRS610::ADXRS610(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
|
||||||
|
{
|
||||||
|
if(mraaIo == NULL)
|
||||||
|
{
|
||||||
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": Failed to allocate memory for internal member");
|
||||||
|
}
|
||||||
|
if(!mraaIo->aios.empty())
|
||||||
|
{
|
||||||
|
m_aioData = &mraaIo->aios[0];
|
||||||
|
m_aioTemp = &mraaIo->aios[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": mraa_i2c_init() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> upmTokens;
|
||||||
|
|
||||||
|
if (!mraaIo->getLeftoverStr().empty()) {
|
||||||
|
upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADC resolution of data and temp should be the same...
|
||||||
|
m_aref = 5.0;
|
||||||
|
m_aRes = (1 << m_aioData->getBit());
|
||||||
|
setZeroPoint(calibrateZeroPoint());
|
||||||
|
setDeadband(0.0);
|
||||||
|
|
||||||
|
for (std::string tok : upmTokens) {
|
||||||
|
if(tok.substr(0,5) == "aref:") {
|
||||||
|
m_aref = std::stof(tok.substr(5));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_centerVolts = m_aref / 2.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ADXRS610::~ADXRS610()
|
ADXRS610::~ADXRS610()
|
||||||
{
|
{
|
||||||
|
if(mraaIo != NULL)
|
||||||
|
delete mraaIo;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
delete m_aioData;
|
||||||
|
delete m_aioTemp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float ADXRS610::getDataVolts()
|
float ADXRS610::getDataVolts()
|
||||||
{
|
{
|
||||||
int val = m_aioData.read();
|
int val = m_aioData->read();
|
||||||
|
|
||||||
return(float(val) * (m_aref / float(m_aRes)));
|
return(float(val) * (m_aref / float(m_aRes)));
|
||||||
}
|
}
|
||||||
|
|
||||||
float ADXRS610::getTemperatureVolts()
|
float ADXRS610::getTemperatureVolts()
|
||||||
{
|
{
|
||||||
int val = m_aioTemp.read();
|
int val = m_aioTemp->read();
|
||||||
|
|
||||||
return(float(val) * (m_aref / float(m_aRes)));
|
return(float(val) * (m_aref / float(m_aRes)));
|
||||||
}
|
}
|
||||||
@ -75,7 +121,7 @@ float ADXRS610::calibrateZeroPoint(unsigned int samples)
|
|||||||
float ADXRS610::getTemperature()
|
float ADXRS610::getTemperature()
|
||||||
{
|
{
|
||||||
float tempV = getTemperatureVolts();
|
float tempV = getTemperatureVolts();
|
||||||
|
|
||||||
// nominal 2.5 volts at 25C
|
// nominal 2.5 volts at 25C
|
||||||
if (tempV > m_centerVolts)
|
if (tempV > m_centerVolts)
|
||||||
return (m_temperatureNom + ((tempV - m_centerVolts) / m_temperatureCoeff));
|
return (m_temperatureNom + ((tempV - m_centerVolts) / m_temperatureCoeff));
|
||||||
@ -91,7 +137,7 @@ float ADXRS610::getAngularVelocity()
|
|||||||
if (dataV < (m_zeroPoint + m_deadband) &&
|
if (dataV < (m_zeroPoint + m_deadband) &&
|
||||||
dataV > (m_zeroPoint - m_deadband))
|
dataV > (m_zeroPoint - m_deadband))
|
||||||
return 0.0;
|
return 0.0;
|
||||||
|
|
||||||
if (dataV > m_zeroPoint)
|
if (dataV > m_zeroPoint)
|
||||||
return ((dataV - m_zeroPoint) / m_degreeCoeff);
|
return ((dataV - m_zeroPoint) / m_degreeCoeff);
|
||||||
else
|
else
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mraa/aio.hpp>
|
#include <mraa/aio.hpp>
|
||||||
|
#include <mraa/initio.hpp>
|
||||||
|
|
||||||
// volts per degree / second (typ)
|
// volts per degree / second (typ)
|
||||||
#define m_degreeCoeff 0.006
|
#define m_degreeCoeff 0.006
|
||||||
@ -81,6 +82,13 @@ namespace upm {
|
|||||||
*/
|
*/
|
||||||
ADXRS610(int dPin, int tPin, float aref=5.0);
|
ADXRS610(int dPin, int tPin, float aref=5.0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates ADXRS610 Gyroscope based on a given string.
|
||||||
|
*
|
||||||
|
* @param initStr string containing specific information for ADXRS610 initialization.
|
||||||
|
*/
|
||||||
|
ADXRS610(std::string initStr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ADXRS610 destructor
|
* ADXRS610 destructor
|
||||||
*/
|
*/
|
||||||
@ -125,7 +133,7 @@ namespace upm {
|
|||||||
* (setZeroPoint()).
|
* (setZeroPoint()).
|
||||||
*
|
*
|
||||||
* @param samples the number of samples to take an average over.
|
* @param samples the number of samples to take an average over.
|
||||||
* The default is 50.
|
* The default is 50.
|
||||||
* @return the average of the reading over samples times.
|
* @return the average of the reading over samples times.
|
||||||
*/
|
*/
|
||||||
float calibrateZeroPoint(unsigned int samples=50);
|
float calibrateZeroPoint(unsigned int samples=50);
|
||||||
@ -154,8 +162,9 @@ namespace upm {
|
|||||||
float getAngularVelocity();
|
float getAngularVelocity();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mraa::Aio m_aioData;
|
mraa::Aio* m_aioData = NULL;
|
||||||
mraa::Aio m_aioTemp;
|
mraa::Aio* m_aioTemp = NULL;
|
||||||
|
mraa::MraaIo* mraaIo = NULL;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float m_aref;
|
float m_aref;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user