Compare commits

...

14 Commits

Author SHA1 Message Date
5a15daab1c travis.yml: Install mraa to /usr
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 23:06:33 +00:00
8734767a5c cmake: disable TGZ from builds when IPK used
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 18:42:31 +00:00
b4535a6403 curieimu: Adds examples for both JS and Python that load MRAA Firmata subplatform
Signed-off-by: deadprogram <ron@hybridgroup.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 17:05:13 +00:00
3d37f957bf cmake: add workaround for cmake 2.7
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 17:05:13 +00:00
3722dfc780 cmake: Fix issues with node v4/v5
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 17:05:13 +00:00
b113a46813 .travis.yml: Make travis test node4/node5 and node0.12
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 17:05:13 +00:00
aa30b59e44 cmake: add BUILDPYTHON3 flag
Use the same methodology as in mraa, by default build for python2, if requested
use python3 for everything

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-22 17:05:13 +00:00
159d2eee07 FindNode.cmake: Improve FindNode cmake module
Use mraa's FindNode since it's alot more robus and can handle NVM better

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-21 17:32:23 +00:00
7fd9f0c508 curieimu: Add WIP for Python example
Signed-off-by: deadprogram <ron@hybridgroup.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-21 17:13:25 +00:00
3ca9bfe80f Updates implementation and examples to use dynamic-friendly interface
Signed-off-by: deadprogram <ron@hybridgroup.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-21 17:13:25 +00:00
90983fde9d Complete accel and gyro implementations for new dynamic friendly interface
Signed-off-by: deadprogram <ron@hybridgroup.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-21 17:13:25 +00:00
ee19daedee curieimu: modify calls to use update methodology to fix python/java/js API
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-18 14:56:23 +00:00
61dbfe4f23 curieimu: Make compilation dependant on mraa_firmata_init
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-18 12:40:32 +00:00
224a915538 curieimu: Plugin to interface with the arduino101's IMU
Internal sensor onboard the Curie/Arduino101 can be accessed via Firmata using
this plugin. You will need mraa compiled with -DFIRMATA=ON for this to work and
be using Firmata with the CurieIMU firmata extension for ExtensibleFirmata

Signed-off-by: Ron Evans <ron@hybridgroup.com>
Signed-off-by: Justin Zemlyansky <jlstigman@live.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
2016-03-18 12:40:32 +00:00
14 changed files with 1175 additions and 50 deletions

View File

@ -1,8 +1,12 @@
language: cpp
compiler:
- gcc
- clang
env:
- CC=gcc CXX=gcc
- CC=clang CXX=clang++
- NODE4=true
- NODE5=true
- NODE012=true
install:
- if [ "${NODE4}" ]; then export CC=gcc-4.8 CXX=g++-4.8; fi
- sudo add-apt-repository --yes ppa:kalakris/cmake
- sudo add-apt-repository --yes ppa:fenics-packages/fenics-exp/swig
- sudo apt-get update -qq
@ -10,12 +14,21 @@ install:
- sudo ln -s /usr/bin/swig3.0 /usr/bin/swig
- sudo update-java-alternatives -s java-8-oracle
before_script:
- export NODE_ROOT_DIR="/home/travis/.nvm/v0.10.36"
- if [ "$CC" = "gcc" ]; then export BUILDJAVA=ON; else export BUILDJAVA=OFF; fi
- export JAVA_HOME=/usr/lib/jvm/java-8-oracle
- if [ "${NODE4}" ]; then nvm install 4.1; export CC=gcc-4.8; export CXX=g++-4.8; export NODE_ROOT_DIR="/home/travis/.nvm/versions/node/`nvm version`"; fi
- if [ "${NODE5}" ]; then nvm install 5; export CC=gcc-4.8; export CXX=g++-4.8; export NODE_ROOT_DIR="/home/travis/.nvm/versions/node/`nvm version`"; fi
- if [ "${NODE012}" ]; then nvm install 0.12; export NODE_ROOT_DIR="/home/travis/.nvm/versions/node/`nvm version`"; fi
script:
- git clone --branch=master https://github.com/intel-iot-devkit/mraa.git
- cd mraa && mkdir build && cd build && cmake -DBUILDSWIGJAVA=$BUILDJAVA -DENABLEEXAMPLES=OFF -DCMAKE_INSTALL_PREFIX:PATH=. -DNODE_ROOT_DIR:PATH=/home/travis/.nvm/v0.10.36/include .. && make && make install
- export PKG_CONFIG_PATH=$PWD/lib/pkgconfig:$PWD/lib/x86_64-linux-gnu/pkgconfig
- export CPLUS_INCLUDE_PATH=$PWD/include
- export LIBRARY_PATH=$PWD/lib:$PWD/lib/x86_64-linux-gnu
- cd ../.. && mkdir build && cd build && cmake -DBUILDSWIGJAVA=$BUILDJAVA -DNODE_ROOT_DIR:PATH=/home/travis/.nvm/v0.10.36/include -DBUILDEXAMPLES=ON -DBUILDJAVAEXAMPLES=$BUILDJAVA .. && make
- git clone https://github.com/intel-iot-devkit/mraa.git
- cd mraa && mkdir build && cd build && cmake -DBUILDSWIGJAVA=$BUILDJAVA -DBUILDSWIGNODE=OFF -DBUILDSWIGPYTHON=OFF -DFIRMATA=ON -DENABLEEXAMPLES=OFF -DCMAKE_INSTALL_PREFIX:PATH=/usr .. && make && sudo make install
- cd ../.. && mkdir build && cd build && cmake -DNODE_ROOT_DIR:PATH="${NODE_ROOT_DIR}" -DBUILDSWIGJAVA=$BUILDJAVA -DBUILDEXAMPLES=ON -DBUILDJAVAEXAMPLES=$BUILDJAVA .. && make
addons:
apt:
sources:
- llvm-toolchain-precise-3.6
- ubuntu-toolchain-r-test
packages:
- clang-3.6
- g++-4.8

View File

@ -51,6 +51,7 @@ set (CMAKE_SWIG_FLAGS "")
option (BUILDDOC "Build all doc." OFF)
option (BUILDSWIG "Build swig modules." ON)
option (BUILDSWIGPYTHON "Build swig python modules." ON)
option (BUILDPYTHON3 "Use python3 for building/installing/testing" OFF)
option (BUILDSWIGNODE "Build swig node modules." ON)
option (BUILDEXAMPLES "Build C++ example binaries" OFF)
option (BUILDJAVAEXAMPLES "Build java example jars" OFF)
@ -156,7 +157,7 @@ if (IPK)
endif ()
message (INFO " - Package arch is ${TARGET_ARCH}")
set(CPACK_GENERATOR "DEB" "TGZ")
set(CPACK_GENERATOR "DEB")
set(OPKG_ARCH ${TARGET_ARCH})
set(CPACK_BINARY_DIR ${CMAKE_BINARY_DIR})
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Intel IoT-Devkit") #required
@ -210,10 +211,18 @@ if(BUILDJAVAEXAMPLES)
add_subdirectory (examples/java)
endif()
if(BUILDTESTS)
find_package (PythonInterp REQUIRED)
if (${PYTHONINTERP_FOUND})
enable_testing ()
add_subdirectory (tests)
endif ()
if (BUILDSWIGPYTHON OR BUILDTESTS)
if (BUILDPYTHON3)
set (PYTHONBUILD_VERSION 3)
else ()
set (PYTHONBUILD_VERSION 2.7)
endif ()
find_package (PythonInterp ${PYTHONBUILD_VERSION} REQUIRED)
find_package (PythonLibs ${PYTHONBUILD_VERSION} REQUIRED)
include_directories(${PYTHON_INCLUDE_PATH})
endif ()
if (BUILDTESTS)
enable_testing ()
add_subdirectory (tests)
endif()

View File

@ -1,4 +1,12 @@
find_program (NODE_EXECUTABLE NAMES node
# Macro to add directory to NODEJS_INCLUDE_DIRS if it exists and is not /usr/include
macro(add_include_dir dir)
if (IS_DIRECTORY ${dir} AND NOT ${dir} STREQUAL "/usr/include")
set(NODEJS_INCLUDE_DIRS ${NODEJS_INCLUDE_DIRS} ${dir})
endif()
endmacro()
find_program (NODEJS_EXECUTABLE NAMES node nodejs
HINTS
$ENV{NODE_DIR}
PATH_SUFFIXES bin
@ -7,26 +15,50 @@ find_program (NODE_EXECUTABLE NAMES node
include (FindPackageHandleStandardArgs)
find_path (NODE_ROOT_DIR "node/node.h" "src/node.h"
PATHS /usr/include/nodejs /usr/local/include/nodejs)
# If compat-libuv package exists, it must be at start of include path
find_path (UV_ROOT_DIR "uv.h" PATHS /usr/include/compat-libuv010 NO_DEFAULT_PATH)
if (UV_ROOT_DIR)
# set (NODEJS_INCLUDE_DIRS ${UV_ROOT_DIR})
add_include_dir(${UV_ROOT_DIR})
endif()
set (NODE_INCLUDE_DIRS
${NODE_ROOT_DIR}/src
${NODE_ROOT_DIR}/node
${NODE_ROOT_DIR}/deps/v8/include
${NODE_ROOT_DIR}/deps/uv/include
# Now look for node. Flag an error if not found
find_path (NODE_ROOT_DIR "include/node/node.h" "include/src/node.h"
PATHS /usr/include/nodejs /usr/local/include/nodejs /usr/local/include)
if (NODE_ROOT_DIR)
add_include_dir(${NODE_ROOT_DIR}/include/src)
add_include_dir(${NODE_ROOT_DIR}/include/node)
add_include_dir(${NODE_ROOT_DIR}/include/deps/v8/include)
add_include_dir(${NODE_ROOT_DIR}/include/deps/uv/include)
else()
unset(NODEJS_INCLUDE_DIRS)
message(ERROR " - node.h not found")
endif()
# Check that v8.h is in NODEJS_INCLUDE_DIRS
find_path (V8_ROOT_DIR "v8.h" PATHS ${NODEJS_INCLUDE_DIRS})
if (NOT V8_ROOT_DIR)
unset(NODEJS_INCLUDE_DIRS)
message(ERROR " - v8.h not found")
endif()
# Check that uv.h is in NODEJS_INCLUDE_DIRS
find_path (UV_ROOT_DIR "uv.h" PATHS ${NODEJS_INCLUDE_DIRS})
if (NOT UV_ROOT_DIR)
unset(NODEJS_INCLUDE_DIRS)
message(ERROR " - uv.h not found")
endif()
find_package_handle_standard_args (Nodejs DEFAULT_MSG
NODEJS_EXECUTABLE
NODEJS_INCLUDE_DIRS
)
find_package_handle_standard_args (Node DEFAULT_MSG
NODE_EXECUTABLE
NODE_INCLUDE_DIRS
)
if (NODE_EXECUTABLE)
execute_process(COMMAND ${NODE_EXECUTABLE} --version
if (NODEJS_EXECUTABLE)
execute_process(COMMAND ${NODEJS_EXECUTABLE} --version
OUTPUT_VARIABLE _VERSION
RESULT_VARIABLE _NODE_VERSION_RESULT)
execute_process(COMMAND ${NODE_EXECUTABLE} -e "console.log(process.versions.v8)"
execute_process(COMMAND ${NODEJS_EXECUTABLE} -e "console.log(process.versions.v8)"
OUTPUT_VARIABLE _V8_VERSION
RESULT_VARIABLE _V8_RESULT)
if (NOT _NODE_VERSION_RESULT AND NOT _V8_RESULT)
@ -55,7 +87,11 @@ if (NODE_EXECUTABLE)
set (V8_VERSION_STRING "3.28.72")
message ("defaulted to node 0.10.30")
endif ()
message ("INFO - Node version is " ${NODE_VERSION_STRING} "INFO - Node using v8 " ${V8_VERSION_STRING})
string (REGEX REPLACE "\n" "" NODE_VERSION_STRING ${NODE_VERSION_STRING})
string (REGEX REPLACE "\n" "" V8_VERSION_STRING ${V8_VERSION_STRING})
message ("INFO - Node version is " ${NODE_VERSION_STRING})
message ("INFO - Node using v8 " ${V8_VERSION_STRING})
mark_as_advanced (NODEJS_EXECUTABLE)
endif ()
mark_as_advanced (NODE_EXECUTABLE)

View File

@ -240,6 +240,9 @@ add_example (rhusb)
add_example (apds9930)
add_example (kxcjk1013)
add_example (ssd1351)
if (HAVE_FIRMATA)
add_example (curieimu)
endif ()
# These are special cases where you specify example binary, source file and module(s)
include_directories (${PROJECT_SOURCE_DIR}/src)

81
examples/c++/curieimu.cxx Normal file
View File

@ -0,0 +1,81 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Author: Ron Evans (@deadprogram)
* Author: Justin Zemlyansky (@JustInDevelopment)
* 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 <iostream>
#include "curieimu.hpp"
#include "mraa.h"
#include "mraa/firmata.h"
#include <math.h>
int
main(int argc, char **argv)
{
//! [Interesting]
mraa_init();
mraa_add_subplatform(MRAA_GENERIC_FIRMATA, "/dev/ttyACM0");
upm::CurieImu* sensor = new upm::CurieImu();
std::cout << "temperature is: " << (sensor->getTemperature() * pow(0.5, 9) + 23) << std::endl;
sensor->updateAccel();
int x = sensor->getAccelX(),
y = sensor->getAccelY(),
z = sensor->getAccelZ();
printf("accelerometer is: %d, %d, %d\n", x, y, z);
sensor->updateGyro();
int a = sensor->getGyroX(),
b = sensor->getGyroY(),
c = sensor->getGyroZ();
printf("gyroscope is: %d, %d, %d\n", a, b, c);
int axis, direction;
sensor->enableShockDetection(true);
for(int i=0; i<300; i++) {
if (sensor->isShockDetected()) {
sensor->getNextShock();
axis = sensor->getAxis();
direction = sensor->getDirection();
printf("shock data is: %d, %d\n", axis, direction);
}
usleep(10000);
}
sensor->updateMotion();
int m = sensor->getAccelX(),
n = sensor->getAccelY(),
o = sensor->getAccelZ(),
p = sensor->getGyroX(),
q = sensor->getGyroY(),
r = sensor->getGyroZ();
printf("motion is: %d, %d, %d, %d, %d, %d\n", m, n, o, p, q, r);
delete sensor;
return 0;
}

View File

@ -0,0 +1,52 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*
* Author: Ron Evans (@deadprogram)
* 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.
*/
var mraa = require('mraa');
console.log('MRAA Version: ' + mraa.getVersion());
// open connection to Firmata
mraa.addSubplatform(mraa.GENERIC_FIRMATA, "/dev/ttyACM0");
var curieImu = require('jsupm_curieimu');
var myCurie = new curieImu.CurieImu();
var outputStr;
var myInterval = setInterval(function()
{
myCurie.updateAccel();
outputStr = "accel: x " + myCurie.getAccelX()
+ " - y " + myCurie.getAccelY()
+ " - z " + myCurie.getAccelZ();
console.log(outputStr);
}, 500);
// Print message when exiting
process.on('SIGINT', function()
{
clearInterval(myInterval);
console.log("Exiting");
process.exit(0);
});

View File

@ -0,0 +1,56 @@
#!/usr/bin/python
# Author: Ron Evans (@deadprogram)
# 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
import mraa
print (mraa.getVersion())
# open connection to Firmata
mraa.addSubplatform(mraa.GENERIC_FIRMATA, "/dev/ttyACM0")
import time, sys, signal, atexit
import pyupm_curieimu as curieimu
sensor = curieimu.CurieImu()
## Exit handlers ##
def SIGINTHandler(signum, frame):
raise SystemExit
def exitHandler():
print "Exiting"
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
while(1):
sensor.updateAccel();
outputStr = "acc: gX {0} - gY {1} - gZ {2}".format(
sensor.getAccelX(), sensor.getAccelY(),
sensor.getAccelZ())
print outputStr
time.sleep(1)

View File

@ -46,16 +46,28 @@ endmacro()
macro(upm_SWIG_NODE)
if (BUILDSWIGNODE AND BUILDSWIG)
# SWIG treats SWIG_FLAGS as a list and not a string so semicolon seperation is
# required. This hardcodes V8_VERSION to be <10 but I assume that's not going
# to be a problem for a little while! SWIG uses a padded SWIG_V8 version which
# we hack together from our findnode module.
set (V8_VERSION_HEX 0x0${V8_VERSION_MAJOR}${V8_VERSION_MINOR}${V8_VERSION_PATCH})
string (LENGTH "${V8_VERSION_HEX}" V8_VERSION_HEX_length)
while (V8_VERSION_HEX_length LESS 8)
set (V8_VERSION_HEX "${V8_VERSION_HEX}0")
string (LENGTH "${V8_VERSION_HEX}" V8_VERSION_HEX_length)
endwhile ()
set_property (SOURCE jsupm_${libname}.i PROPERTY SWIG_FLAGS "-node" "-DV8_VERSION=${V8_VERSION_HEX}")
set_source_files_properties (jsupm_${libname}.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties (jsupm_${libname}.i PROPERTIES SWIG_FLAGS "-node")
swig_add_module (jsupm_${libname} javascript jsupm_${libname}.i ${module_src})
swig_link_libraries (jsupm_${libname} ${MRAA_LIBRARIES} ${NODE_LIBRARIES})
target_include_directories ( ${SWIG_MODULE_jsupm_${libname}_REAL_NAME}
PUBLIC
"${NODE_INCLUDE_DIRS}"
)
)
set_target_properties (jsupm_${libname} PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DBUILDING_NODE_EXTENSION -DSWIG_V8_VERSION=0x0${V8_DEFINE_STRING}"
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DBUILDING_NODE_EXTENSION -DSWIG_V8_VERSION=${V8_VERSION_HEX}"
PREFIX ""
SUFFIX ".node"
)
@ -67,10 +79,24 @@ macro(upm_SWIG_NODE)
endif()
set_property (TARGET jsupm_${libname} PROPERTY CXX_STANDARD 11)
set_property (TARGET jsupm_${libname} PROPERTY CXX_STANDARD_REQUIRED ON)
if (CMAKE_VERSION VERSION_LESS "3.1")
if (CMAKE_COMPILER_IS_GNUCXX)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7")
message (FATAL_ERROR " FATAL ERROR: GNU gcc compiler is also too old (need 4.7+, but ${CMAKE_CXX_COMPILER_VERSION}) and does not support C++11 standard.")
endif ()
set (UPM_CXX11_WORKAROUND_OPTION "-std=gnu++11")
else ()
set (UPM_CXX11_WORKAROUND_OPTION "-std=c++11")
endif ()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${UPM_CXX11_WORKAROUND_OPTION} ")
endif ()
endif()
createpackagejson(${libname})
set (NODE_MODULE_INSTALL_PATH ${NODE_ROOT_DIR}/lib/node_modules/jsupm_${libname}/)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}.node
DESTINATION lib/node_modules/jsupm_${libname} COMPONENT ${libname})
DESTINATION ${NODE_MODULE_INSTALL_PATH} COMPONENT ${libname})
endif()
endmacro()
@ -139,30 +165,25 @@ macro(upm_doxygen)
endmacro()
if (SWIG_FOUND)
if(BUILDSWIGPYTHON)
find_package (PythonLibs)
string (REPLACE "." ";" PYTHON_VERSION_LIST ${PYTHONLIBS_VERSION_STRING})
list (GET PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
list (GET PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
endif(BUILDSWIGPYTHON)
if(BUILDSWIGNODE)
if (BUILDSWIGNODE)
if(NOT NODE_FOUND)
find_package(Node)
endif()
if(SWIG_VERSION VERSION_LESS 3.0.5 AND NODE_VERSION_STRING VERSION_GREATER 0.12)
message("WARNING - SWIG 3.0.5+ required for building with nodejs 0.12. Current version is ${SWIG_VERSION}")
endif()
find_path (NODE_ROOT_DIR "node/node.h")
find_path (NODE_ROOT_DIR "include/node/node.h")
set (NODE_INCLUDE_DIRS
${NODE_ROOT_DIR}/src
${NODE_ROOT_DIR}/node
${NODE_ROOT_DIR}/deps/v8/include
${NODE_ROOT_DIR}/deps/uv/include
${NODE_ROOT_DIR}/include/src
${NODE_ROOT_DIR}/include/node
${NODE_ROOT_DIR}/include/deps/v8/include
${NODE_ROOT_DIR}/include/deps/uv/include
)
macro(createpackagejson)
configure_file (${PROJECT_SOURCE_DIR}/src/package.json.in ${CMAKE_CURRENT_BINARY_DIR}/package.json @ONLY)
set (NODE_MODULE_INSTALL_PATH ${NODE_ROOT_DIR}/lib/node_modules/jsupm_${libname}/)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/package.json
DESTINATION lib/node_modules/jsupm_${libname} COMPONENT ${libname})
DESTINATION ${NODE_MODULE_INSTALL_PATH} COMPONENT ${libname})
endmacro()
endif(BUILDSWIGNODE)

View File

@ -0,0 +1,11 @@
include (CheckFunctionExists)
include (CheckLibraryExists)
include (CheckIncludeFiles)
check_library_exists (${MRAA_LIBRARIES} mraa_firmata_init "" HAVE_FIRMATA)
if (HAVE_FIRMATA)
set (libname "curieimu")
set (libdescription "upm Curie IMU via Firmata")
set (module_src ${libname}.cpp)
set (module_h ${libname}.hpp)
upm_module_init ()
endif ()

458
src/curieimu/curieimu.cpp Normal file
View File

@ -0,0 +1,458 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Author: Ron Evans (@deadprogram)
* Author: Justin Zemlyansky (@JustInDevelopment)
* 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 <iostream>
#include <string>
#include <string.h>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "curieimu.hpp"
using namespace upm;
static CurieImu* awaitingReponse;
CurieImu::CurieImu (int subplatformoffset)
{
m_firmata = mraa_firmata_init(FIRMATA_CURIE_IMU);
if (m_firmata == NULL) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_firmata_init() failed");
return;
}
if (pthread_mutex_init(&m_responseLock, NULL)) {
throw std::runtime_error(std::string(__FUNCTION__) +
": pthread_mutex_init(m_responseLock) failed");
return;
}
if (pthread_cond_init(&m_responseCond, NULL)) {
throw std::runtime_error(std::string(__FUNCTION__) +
": pthread_cond_init(m_responseCond) failed");
return;
}
}
CurieImu::~CurieImu()
{
pthread_mutex_destroy(&m_responseLock);
pthread_cond_destroy(&m_responseCond);
}
void
CurieImu::lock()
{
pthread_mutex_lock(&m_responseLock);
}
void
CurieImu::unlock()
{
pthread_mutex_unlock(&m_responseLock);
}
void
CurieImu::waitForResponse()
{
awaitingReponse = this;
pthread_cond_wait(&m_responseCond, &m_responseLock);
}
void
CurieImu::proceed()
{
pthread_cond_broadcast(&m_responseCond);
}
void
CurieImu::setResults(uint8_t* buf, int length)
{
m_results = new char(length);
memcpy((void*)m_results, (void*)buf, length);
}
/*
* Handles a single syncronous response being returned from Firmata
*
* @param buffer the data beinig returned from Firmata
* @param length length of results buffer
*/
static void
handleSyncResponse(uint8_t* buf, int length)
{
awaitingReponse->setResults(buf, length);
awaitingReponse->proceed();
}
/*
* Handles asyncronous responses being returned from Firmata
*
* @param buffer the data beinig returned from Firmata
* @param length length of results buffer
*/
static void
handleAsyncResponses(uint8_t* buf, int length)
{
awaitingReponse->setResults(buf, length);
awaitingReponse->processResponse();
}
void
CurieImu::processResponse()
{
switch(m_results[2]) {
case FIRMATA_CURIE_IMU_SHOCK_DETECT:
{
IMUDataItem* item = new IMUDataItem();
item->axis = m_results[3];
item->direction = m_results[4];
m_shockData.push(item);
break;
}
case FIRMATA_CURIE_IMU_STEP_COUNTER:
{
int count = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7));
m_stepData.push(count);
break;
}
case FIRMATA_CURIE_IMU_TAP_DETECT:
{
IMUDataItem* item = new IMUDataItem();
item->axis = m_results[3];
item->direction = m_results[4];
m_tapData.push(item);
break;
}
}
return;
}
int16_t*
CurieImu::getAccel()
{
return &m_accel[0];
}
int16_t
CurieImu::getAccelX()
{
return m_accel[X];
}
int16_t
CurieImu::getAccelY()
{
return m_accel[Y];
}
int16_t
CurieImu::getAccelZ()
{
return m_accel[Z];
}
int16_t*
CurieImu::getGyro()
{
return &m_gyro[0];
}
int16_t
CurieImu::getGyroX()
{
return m_gyro[X];
}
int16_t
CurieImu::getGyroY()
{
return m_gyro[Y];
}
int16_t
CurieImu::getGyroZ()
{
return m_gyro[Z];
}
int16_t*
CurieImu::getMotion()
{
return &m_motion[0];
}
void
CurieImu::updateAccel()
{
char message[4];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_READ_ACCEL;
message[3] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleSyncResponse);
mraa_firmata_write_sysex(m_firmata, &message[0], 4);
waitForResponse();
m_accel[0] = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7));
m_accel[1] = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7));
m_accel[2] = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7));
delete m_results;
unlock();
return;
}
void
CurieImu::updateGyro()
{
char message[4];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_READ_GYRO;
message[3] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleSyncResponse);
mraa_firmata_write_sysex(m_firmata, &message[0], 4);
waitForResponse();
m_gyro[0] = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7));
m_gyro[1] = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7));
m_gyro[2] = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7));
delete m_results;
unlock();
return;
}
void
CurieImu::updateMotion()
{
char message[4];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_READ_MOTION;
message[3] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleSyncResponse);
mraa_firmata_write_sysex(m_firmata, &message[0], 4);
waitForResponse();
m_motion[0] = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7));
m_motion[1] = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7));
m_motion[2] = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7));
m_motion[3] = ((m_results[9] & 0x7f) | ((m_results[10] & 0x7f) << 7));
m_motion[4] = ((m_results[11] & 0x7f) | ((m_results[12] & 0x7f) << 7));
m_motion[5] = ((m_results[13] & 0x7f) | ((m_results[13] & 0x7f) << 7));
for (int i=0; i<3; i++)
m_accel[i] = m_motion[i];
for (int i=0; i<3; i++)
m_gyro[i] = m_motion[i+3];
delete m_results;
unlock();
return;
}
int16_t
CurieImu::getTemperature()
{
char message[4];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_READ_TEMP;
message[3] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleSyncResponse);
mraa_firmata_write_sysex(m_firmata, &message[0], 4);
waitForResponse();
int16_t result;
result = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7));
result += ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)) << 8;
delete m_results;
unlock();
return result;
}
int16_t
CurieImu::getAxis()
{
return m_axis;
}
int16_t
CurieImu::getDirection()
{
return m_direction;
}
void
CurieImu::enableShockDetection(bool enable)
{
char message[5];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_SHOCK_DETECT;
message[3] = enable;
message[4] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleAsyncResponses);
mraa_firmata_write_sysex(m_firmata, &message[0], 5);
awaitingReponse = this;
unlock();
return;
}
bool
CurieImu::isShockDetected()
{
return (m_shockData.size() > 0);
}
void
CurieImu::getNextShock()
{
if (m_shockData.size() > 0) {
IMUDataItem* item = m_shockData.front();
m_axis = item->axis;
m_direction = item->direction;
m_shockData.pop();
delete item;
}
}
void
CurieImu::enableStepCounter(bool enable)
{
char message[5];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_STEP_COUNTER;
message[3] = enable;
message[4] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleAsyncResponses);
mraa_firmata_write_sysex(m_firmata, &message[0], 5);
awaitingReponse = this;
unlock();
return;
}
bool
CurieImu::isStepDetected()
{
return (m_stepData.size() > 0);
}
int16_t
CurieImu::getStepCount()
{
int16_t count = 0;
if (m_stepData.size() > 0) {
count = m_stepData.front();
m_stepData.pop();
}
return count;
}
void
CurieImu::enableTapDetection(bool enable)
{
char message[5];
message[0] = FIRMATA_START_SYSEX;
message[1] = FIRMATA_CURIE_IMU;
message[2] = FIRMATA_CURIE_IMU_TAP_DETECT;
message[3] = enable;
message[4] = FIRMATA_END_SYSEX;
lock();
mraa_firmata_response_stop(m_firmata);
mraa_firmata_response(m_firmata, handleAsyncResponses);
mraa_firmata_write_sysex(m_firmata, &message[0], 5);
awaitingReponse = this;
unlock();
return;
}
bool
CurieImu::isTapDetected()
{
return (m_tapData.size() > 0);
}
void
CurieImu::getNextTap()
{
if (m_tapData.size() > 0) {
IMUDataItem* item = m_tapData.front();
m_axis = item->axis;
m_direction = item->direction;
m_tapData.pop();
delete item;
}
}

307
src/curieimu/curieimu.hpp Normal file
View File

@ -0,0 +1,307 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Author: Ron Evans (@deadprogram)
* Author: Justin Zemlyansky (@JustInDevelopment)
* 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.
*/
#pragma once
#include <mraa/firmata.h>
#include <queue>
namespace upm {
/**
* @brief CurieIMU sensor for Genuino 101 running Firmata
* @defgroup curieimu libupm-curieimu
* @ingroup firmata
*/
/**
* @library curieimu
* @sensor curieimu
* @comname Curie IMU sensor over Genuino 101 running Firmata
* @altname Curie Firmata IMU
* @type firmata
* @man firmata imu genuino
* @con firmata
*
* @brief API for the Curie IMU via Firmata
*
* Curie IMU is a 6-axxis acclerometer
*
* This module has been tested on an Genuino 101 running ConfigurableFirmata with CurieIMU
*
* @snippet curieimu.cxx Interesting
*/
#define FIRMATA_START_SYSEX 0xF0
#define FIRMATA_END_SYSEX 0xF7
#define FIRMATA_CURIE_IMU 0x11
#define FIRMATA_CURIE_IMU_READ_ACCEL 0x00
#define FIRMATA_CURIE_IMU_READ_GYRO 0x01
#define FIRMATA_CURIE_IMU_READ_TEMP 0x02
#define FIRMATA_CURIE_IMU_SHOCK_DETECT 0x03
#define FIRMATA_CURIE_IMU_STEP_COUNTER 0x04
#define FIRMATA_CURIE_IMU_TAP_DETECT 0x05
#define FIRMATA_CURIE_IMU_READ_MOTION 0x06
#define X 0
#define Y 1
#define Z 2
struct IMUDataItem {
int axis;
int direction;
};
class CurieImu {
public:
/**
* Instantiates a CurieImu object
*
* @param subplatformoffset Subplatform offset
*/
CurieImu (int subplatform_offset=512);
/**
* Destructor for CurieImu object
*/
~CurieImu();
/**
* Updates the latest accelerometer readings by calling Firmata
*/
void updateAccel();
/**
* Updates the latest gyroscope readings by calling Firmata
*/
void updateGyro();
/**
* Updates the both the latest accelerometer & gyroscope readings
* by calling Firmata
*/
void updateMotion();
/**
* Returns last accelerometer reading X, Y, and Z axis
*
* @return pointer to array with X-axis, Y-axis & Z-axis value
*/
int16_t* getAccel();
/**
* Returns last accelerometer reading X axis
*
* @return X-axis value
*/
int16_t getAccelX();
/**
* Returns last accelerometer reading Y axis
*
* @return Y-axis value
*/
int16_t getAccelY();
/**
* Returns last accelerometer reading Z axis
*
* @return Z-axis value
*/
int16_t getAccelZ();
/**
* Read gyroscope X, Y, and Z axis
*
* @return pointer to array with X-axis, Y-axis & Z-axis value
*/
int16_t* getGyro();
/**
* Returns last gyroscope reading X axis
*
* @return X-axis value
*/
int16_t getGyroX();
/**
* Returns last gyroscope reading Y axis
*
* @return Y-axis value
*/
int16_t getGyroY();
/**
* Returns last gyroscope reading Z axis
*
* @return Z-axis value
*/
int16_t getGyroZ();
/**
* Reads the internal temperature
*
* @return 16-bit integer containing the scaled temperature reading
*/
int16_t getTemperature();
/**
* Reads the X, Y, and Z axis of both gyroscope and accelerometer
*
* @return pointer to array with X-axis, Y-axis & Z-axis values for
* accelerometer, and then X-axis, Y-axis & Z-axis values for
* gyroscope
*/
int16_t* getMotion();
/**
* Returns last shock or tap axis reading
*
* @return axis value
*/
int16_t getAxis();
/**
* Returns last shock or tap direction reading
*
* @return direction value
*/
int16_t getDirection();
/**
* Turns shock detection notifications on/off
*
* @param enable enables/disables notifications
*/
void enableShockDetection(bool enable);
/**
* Has there been a shock detected?
*
* @return true if any unprocessed shock notifications are in the queue
*/
bool isShockDetected();
/**
* Gets shock detect data from queue. Then m_axis gets axis data, and
* m_direction gets direction data
*/
void getNextShock();
/**
* Turns step counter notifications on/off
*
* @param enable enables/disables notifications
*/
void enableStepCounter(bool enable);
/**
* Has there been a step detected?
*
* @return true if any unprocessed step notifications are in the queue
*/
bool isStepDetected();
/**
* Gets step count data from queue
*
* @return the total number of steps taken
*/
int16_t getStepCount();
/**
* Turns tap detection notifications on/off
*
* @param enable enables/disables notifications
*/
void enableTapDetection(bool enable);
/**
* Has there been a tap detected?
*
* @return true if any unprocessed tap notifications are in the queue
*/
bool isTapDetected();
/**
* Gets tap detect data from queue. Then m_axis gets axis data, and
* m_direction gets direction data
*/
void getNextTap();
/**
* Locks responses from Firmata
*/
void lock();
/**
* Unlocks responses from Firmata
*/
void unlock();
/**
* Wait for a response from Firmata before proceeding
*/
void waitForResponse();
/**
* Proceed with original function call now that response
* from Firmata has been received
*/
void proceed();
/**
* Set results being returned from Firmata for processing
*
* @param buf is the buffer
* @param length is the length of results buffer
*/
void setResults(uint8_t* buf, int length);
/**
* Processes asyncronous responses returned from Firmata
*/
void processResponse();
private:
mraa_firmata_context m_firmata;
pthread_mutex_t m_responseLock;
pthread_cond_t m_responseCond;
char* m_results;
std::queue<IMUDataItem*> m_shockData;
std::queue<int> m_stepData;
std::queue<IMUDataItem*> m_tapData;
int16_t m_accel[3];
int16_t m_gyro[3];
int16_t m_motion[6];
int16_t m_axis;
int16_t m_direction;
};
}

View File

@ -0,0 +1,42 @@
%module javaupm_curieimu
%include "../upm.i"
%{
#include "curieimu.hpp"
%}
%typemap(jni) int16_t* "jshortArray"
%typemap(jstype) int16_t* "short[]"
%typemap(jtype) int16_t* "short[]"
%typemap(javaout) int16_t* {
return $jnicall;
}
%typemap(out) int16_t *getAccel {
$result = JCALL1(NewShortArray, jenv, 3);
JCALL4(SetShortArrayRegion, jenv, $result, 0, 3, (jshort*)$1);
}
%typemap(out) int16_t *getGyro {
$result = JCALL1(NewShortArray, jenv, 3);
JCALL4(SetShortArrayRegion, jenv, $result, 0, 3, (jshort*)$1);
}
%typemap(out) int16_t *getMotion {
$result = JCALL1(NewShortArray, jenv, 6);
JCALL4(SetShortArrayRegion, jenv, $result, 0, 6, (jshort*)$1);
}
%include "curieimu.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_curieimu");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}

View File

@ -0,0 +1,15 @@
%module jsupm_curieimu
%include "../upm.i"
%include "../carrays_int16_t.i"
// Adding this typemap because SWIG is converting int16 into a short by default
// This forces SWIG to convert it correctly
%typemap(out) int16_t* {
jsresult = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 | 0 );
}
%{
#include "curieimu.hpp"
%}
%include "curieimu.hpp"

View File

@ -0,0 +1,21 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_curieimu
%include "../upm.i"
%include "../carrays_int16_t.i"
%include "stdint.i"
%feature("autodoc", "3");
#ifdef DOXYGEN
%include "curieupm_doc.i"
#endif
%typemap(out) int16_t* {
$result = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 | 0 );
}
%include "curieimu.hpp"
%{
#include "curieimu.hpp"
%}