mirror of
https://github.com/eclipse/upm.git
synced 2025-03-14 20:47:30 +03:00
grove: rotary angle sensor
Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
parent
e91b69230c
commit
b2ffcdd9ea
BIN
docs/images/groverotary.jpeg
Normal file
BIN
docs/images/groverotary.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
57
examples/groverotary.cxx
Normal file
57
examples/groverotary.cxx
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Author: Mihai Tudor Panu <mihai.t.panu@intel.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 <iomanip>
|
||||
#include "grove.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main ()
|
||||
{
|
||||
//! [Interesting]
|
||||
// Instantiate a rotary sensor on analog pin A0
|
||||
upm::GroveRotary* knob = new upm::GroveRotary(0);
|
||||
|
||||
// Print sensor name to confirm it initialized properly
|
||||
cout << knob->name() << endl;
|
||||
|
||||
while(true) {
|
||||
float abs_value = knob->abs_value(); // Absolute raw value
|
||||
float abs_deg = knob->abs_deg(); // Absolute degrees
|
||||
float abs_rad = knob->abs_rad(); // Absolute radians
|
||||
float rel_value = knob->rel_value(); // Relative raw value
|
||||
float rel_deg = knob->rel_deg(); // Relative degrees
|
||||
float rel_rad = knob->rel_rad(); // Relative radians
|
||||
|
||||
fprintf(stdout, "Absolute: %4d raw %5.2f deg = %3.2f rad Relative: %4d raw %5.2f deg %3.2f rad\n",
|
||||
(int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg, rel_rad);
|
||||
|
||||
usleep(2500000); // Sleep for 2.5s
|
||||
}
|
||||
//! [Interesting]
|
||||
delete knob;
|
||||
return 0;
|
||||
}
|
49
examples/javascript/groverotary.js
Normal file
49
examples/javascript/groverotary.js
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Author: Mihai Tudor Panu <mihai.t.panu@intel.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.
|
||||
*/
|
||||
|
||||
//setup/Initialization
|
||||
var upm_grove = require('jsupm_grove');
|
||||
|
||||
//setup access analog input Analog pin #0 (A0)
|
||||
var groveRotary = new upm_grove.GroveRotary(0);
|
||||
|
||||
loop();
|
||||
|
||||
function loop()
|
||||
{
|
||||
var abs = groveRotary.abs_value();
|
||||
var absdeg = groveRotary.abs_deg();
|
||||
var absrad = groveRotary.abs_rad();
|
||||
|
||||
var rel = groveRotary.rel_value();
|
||||
var reldeg = groveRotary.rel_deg();
|
||||
var relrad = groveRotary.rel_rad();
|
||||
|
||||
//write the knob value to the console in different formats
|
||||
console.log("Abs: " + abs + " " + Math.round(parseInt(absdeg)) + " " + absrad.toFixed(3));
|
||||
console.log("Rel: " + rel + " " + Math.round(parseInt(reldeg)) + " " + relrad.toFixed(3));
|
||||
|
||||
//wait 2 s and call function again
|
||||
setTimeout(loop, 2000);
|
||||
}
|
45
examples/python/groverotary.py
Normal file
45
examples/python/groverotary.py
Normal file
@ -0,0 +1,45 @@
|
||||
# Author: Mihai Tudor Panu <mihai.t.panu@intel.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.
|
||||
|
||||
from time import sleep
|
||||
import pyupm_grove as grove
|
||||
|
||||
# New knob on AIO pin 0
|
||||
knob = grove.GroveRotary(0)
|
||||
|
||||
# Loop indefinitely
|
||||
while True:
|
||||
|
||||
# Read values
|
||||
abs = knob.abs_value()
|
||||
absdeg = knob.abs_deg()
|
||||
absrad = knob.abs_rad()
|
||||
|
||||
rel = knob.rel_value()
|
||||
reldeg = knob.rel_deg()
|
||||
relrad = knob.rel_rad()
|
||||
|
||||
print "Abs values: %4d" % int(abs) , " raw %4d" % int(absdeg), "deg = %5.2f" % absrad , " rad ",
|
||||
print "Rel values: %4d" % int(rel) , " raw %4d" % int(reldeg), "deg = %5.2f" % relrad , " rad"
|
||||
|
||||
# Sleep for 2.5 s
|
||||
sleep(2.5)
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
||||
* Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
@ -115,3 +116,47 @@ float GroveLight::raw_value()
|
||||
{
|
||||
return (float) mraa_aio_read(m_aio);
|
||||
}
|
||||
|
||||
//// GroveRotary ////
|
||||
|
||||
GroveRotary::GroveRotary(unsigned int pin)
|
||||
{
|
||||
mraa_init();
|
||||
m_aio = mraa_aio_init(pin);
|
||||
m_name = "Rotary Angle Sensor";
|
||||
}
|
||||
|
||||
GroveRotary::~GroveRotary()
|
||||
{
|
||||
mraa_aio_close(m_aio);
|
||||
}
|
||||
|
||||
float GroveRotary::abs_value()
|
||||
{
|
||||
return (float) mraa_aio_read(m_aio);
|
||||
}
|
||||
|
||||
float GroveRotary::abs_deg()
|
||||
{
|
||||
return GroveRotary::abs_value() * (float) m_max_angle / 1023.0;
|
||||
}
|
||||
|
||||
float GroveRotary::abs_rad()
|
||||
{
|
||||
return GroveRotary::abs_deg() * M_PI / 180.0;
|
||||
}
|
||||
|
||||
float GroveRotary::rel_value()
|
||||
{
|
||||
return GroveRotary::abs_value() - 512.0;
|
||||
}
|
||||
|
||||
float GroveRotary::rel_deg()
|
||||
{
|
||||
return GroveRotary::rel_value() * (float) m_max_angle / 1023.0;
|
||||
}
|
||||
|
||||
float GroveRotary::rel_rad()
|
||||
{
|
||||
return GroveRotary::rel_deg() * M_PI / 180.0;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
||||
* Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
@ -135,4 +136,68 @@ class GroveLight: public Grove {
|
||||
mraa_aio_context m_aio;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief C++ API for Grove Rotary Angle Sensor (Knob)
|
||||
*
|
||||
* Very basic UPM module for Grove rotary angle sensor (knob) on analog. Provides
|
||||
* a set of functions to read the absolute pin value, degrees or radians and another
|
||||
* to do the same relative to the center of the knob's range.
|
||||
*
|
||||
* @ingroup grove analog
|
||||
* @snippet groverotary.cxx Interesting
|
||||
* @image html groverotary.jpeg
|
||||
*/
|
||||
class GroveRotary: public Grove {
|
||||
public:
|
||||
/**
|
||||
* Grove rotary angle sensor constructor
|
||||
*
|
||||
* @param pin number of analog pin to use
|
||||
*/
|
||||
GroveRotary(unsigned int pin);
|
||||
/**
|
||||
* GroveRotary Destructor
|
||||
*/
|
||||
~GroveRotary();
|
||||
/**
|
||||
* Get absolute raw value from AIO pin
|
||||
*
|
||||
* @return the unsigned value from the ADC
|
||||
*/
|
||||
float abs_value();
|
||||
/**
|
||||
* Get absolute raw degrees from AIO pin
|
||||
*
|
||||
* @return the unsigned degrees from the ADC
|
||||
*/
|
||||
float abs_deg();
|
||||
/**
|
||||
* Get absolute raw radians from AIO pin
|
||||
*
|
||||
* @return the unsigned radians from the ADC
|
||||
*/
|
||||
float abs_rad();
|
||||
/**
|
||||
* Get the relative value from the pin
|
||||
*
|
||||
* @return the signed value from the ADC
|
||||
*/
|
||||
float rel_value();
|
||||
/**
|
||||
* Get relative degrees from AIO pin
|
||||
*
|
||||
* @return the signed degrees from the ADC
|
||||
*/
|
||||
float rel_deg();
|
||||
/**
|
||||
* Get relative radians from AIO pin
|
||||
*
|
||||
* @return the signed radians from the ADC
|
||||
*/
|
||||
float rel_rad();
|
||||
private:
|
||||
mraa_aio_context m_aio;
|
||||
static const int m_max_angle = 300;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user