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,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
@ -36,44 +36,45 @@ bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a ENC03R on analog pin A0
upm::ENC03R *gyro = new upm::ENC03R(0);
// The first thing we need to do is calibrate the sensor.
cout << "Please place the sensor in a stable location, and do not" << endl;
cout << "move it while calibration takes place." << endl;
cout << "This may take a couple of minutes." << endl;
// Instantiate a ENC03R on analog pin A0
upm::ENC03R *gyro = new upm::ENC03R(0);
gyro->calibrate(CALIBRATION_SAMPLES);
cout << "Calibration complete. Reference value: "
<< gyro->calibrationValue() << endl;
// The first thing we need to do is calibrate the sensor.
cout << "Please place the sensor in a stable location, and do not" << endl;
cout << "move it while calibration takes place." << endl;
cout << "This may take a couple of minutes." << endl;
// Read the input and print both the raw value and the angular velocity,
// waiting 0.1 seconds between readings
while (shouldRun)
gyro->calibrate(CALIBRATION_SAMPLES);
cout << "Calibration complete. Reference value: "
<< gyro->calibrationValue() << endl;
// Read the input and print both the raw value and the angular velocity,
// waiting 0.1 seconds between readings
while (shouldRun)
{
unsigned int val = gyro->value();
double av = gyro->angularVelocity(val);
gyro->update();
cout << "Raw value: " << val << ", "
<< "angular velocity: " << av << " deg/s" << endl;
usleep(100000);
cout << "Angular velocity: "
<< gyro->angularVelocity()
<< " deg/s"
<< endl;
usleep(100000);
}
//! [Interesting]
cout << "Exiting" << endl;
cout << "Exiting" << endl;
delete gyro;
return 0;
delete gyro;
return 0;
}

View File

@ -147,6 +147,7 @@ add_example (speaker)
add_example (cjq4435)
add_example (hmc5883l)
add_example (wfs)
add_example (enc03r)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

90
examples/c/enc03r.c Normal file
View File

@ -0,0 +1,90 @@
/*
* 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 <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <upm_utilities.h>
#include <upm_platform.h>
#include "enc03r.h"
bool shouldRun = true;
// The number of samples used for calibration
#if defined(UPM_PLATFORM_ZEPHYR)
# define CALIBRATION_SAMPLES 500
#else
# define CALIBRATION_SAMPLES 1000
#endif
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a ENC03R on analog pin A0
enc03r_context sensor = enc03r_init(0, 5.0);
if (!sensor)
{
printf("%s: enc03r_init() failed\n", __FUNCTION__);
return 1;
}
// The first thing we need to do is calibrate the sensor.
printf("Please place the sensor in a stable location, and do not\n");
printf("move it while calibration takes place.\n");
printf("This may take a little time to complete.\n");
enc03r_calibrate(sensor, CALIBRATION_SAMPLES);
printf("Calibration complete. Reference value: %f\n\n",
enc03r_calibration_value(sensor));
// Read the input and print both the raw value and the angular velocity,
// waiting 0.1 seconds between readings
while (shouldRun)
{
enc03r_update(sensor);
printf("Angular velocity: %f deg/s\n",
enc03r_angular_velocity(sensor));
upm_delay_ms(100);
}
printf("Exiting\n");
enc03r_close(sensor);
//! [Interesting]
return 0;
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//NOT TESTED!!!
public class ENC03RSample {
private static final long CALIBRATION_SAMPLES = 1000;
@ -42,14 +42,15 @@ public class ENC03RSample {
// Read the input and print both the raw value and the angular velocity,
// waiting 1 second between readings
while (true) {
long val = gyro.value();
double av = gyro.angularVelocity(val);
gyro.update();
System.out.println("Raw value: " + val + ", angular velocity: " + av + " deg/s");
System.out.println("Angular velocity: "
+ gyro.angularVelocity()
+ " deg/s");
Thread.sleep(1000);
Thread.sleep(100);
}
// ! [Interesting]
}
}
}

View File

@ -30,8 +30,8 @@ var myGyro = new analogGyro.ENC03R(0);
var CALIBRATION_SAMPLES = 1000;
console.log("Please place the sensor in a stable location,\n" +
"and do not move it while calibration takes place.\n" +
"This may take a couple of minutes.");
"and do not move it while calibration takes place.\n" +
"This may take a couple of minutes.");
myGyro.calibrate(CALIBRATION_SAMPLES);
console.log("Calibration complete. Reference value: " +
@ -41,23 +41,22 @@ console.log("Calibration complete. Reference value: " +
// waiting 0.1 seconds between readings
setInterval(function()
{
var gyroVal = myGyro.value();
var outputStr = "Raw value: " + gyroVal + ", " +
"angular velocity: " +
roundNum(myGyro.angularVelocity(gyroVal), 5) + " deg/s";
console.log(outputStr);
myGyro.update();
var outputStr = "Angular velocity: " +
roundNum(myGyro.angularVelocity(), 5) + " deg/s";
console.log(outputStr);
}, 100);
function roundNum(num, decimalPlaces)
{
var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) /
var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) /
Math.pow(10, decimalPlaces));
}
// Print message when exiting
process.on('SIGINT', function()
{
console.log("Exiting...");
process.exit(0);
console.log("Exiting...");
process.exit(0);
});

View File

@ -55,10 +55,9 @@ def main():
print("Reference value: ", myAnalogGyro.calibrationValue())
while(1):
gyroVal = myAnalogGyro.value();
outputStr = ("Raw value: {0}, "
"angular velocity: {1}"
" deg/s".format(gyroVal, myAnalogGyro.angularVelocity(gyroVal)))
myAnalogGyro.update();
outputStr = ("Angular velocity: {0}"
" deg/s".format(myAnalogGyro.angularVelocity()))
print(outputStr)
time.sleep(.1)