From: Kiveisha Yevgeniy Date: Mon, 25 Aug 2014 23:25:11 +0000 (+0000) Subject: ecs1030: added new current sensor (works on atmega328 but on Galileo not accurate... X-Git-Url: http://review.tizen.org/git/?p=contrib%2Fupm.git;a=commitdiff_plain;h=5c4de1b9269794ca0433fd0f3dca919b59560d87 ecs1030: added new current sensor (works on atmega328 but on Galileo not accurate, need calibration and circuit) Signed-off-by: Kiveisha Yevgeniy --- diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e33f668..718bf1b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable (nrf8001-broadcast-example nrf8001_broadcast.cxx) add_executable (nrf8001-helloworld-example nrf8001_helloworld.cxx) add_executable (lpd8806-example lpd8806-example.cxx) add_executable (mlx90614-example mlx90614-example.cxx) +add_executable (ecs1030-example ecs1030-example.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -53,6 +54,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/max5487) include_directories (${PROJECT_SOURCE_DIR}/src/nrf8001) include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806) include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614) +include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -84,3 +86,4 @@ target_link_libraries (nrf8001-broadcast-example nrf8001 ${CMAKE_THREAD_LIBS_INI target_link_libraries (nrf8001-helloworld-example nrf8001 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lpd8806-example lpd8806 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mlx90614-example mlx90614 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (ecs1030-example ecs1030 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/ecs1030-example.cxx b/examples/ecs1030-example.cxx new file mode 100644 index 0000000..40a7dc5 --- /dev/null +++ b/examples/ecs1030-example.cxx @@ -0,0 +1,61 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 +#include +#include +#include +#include "ecs1030.h" + +int is_running = 0; +upm::ECS1030 *sensor = NULL; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + is_running = 1; + } +} + +//! [Interesting] +int +main(int argc, char **argv) +{ + sensor = new upm::ECS1030(0); + signal(SIGINT, sig_handler); + + while (!is_running) { + std::cout << "I = " << sensor->getCurrency_A () << ", Power = " << sensor->getPower_A () << std::endl; + std::cout << "I = " << sensor->getCurrency_B () << ", Power = " << sensor->getPower_B () << std::endl; + } + + std::cout << "exiting application" << std::endl; + + delete sensor; + + return 0; +} +//! [Interesting] diff --git a/src/ecs1030/CMakeLists.txt b/src/ecs1030/CMakeLists.txt new file mode 100644 index 0000000..ccbe8dc --- /dev/null +++ b/src/ecs1030/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "ecs1030") +set (libdescription "Non-invasive current sensor") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/ecs1030/ecs1030.cxx b/src/ecs1030/ecs1030.cxx new file mode 100644 index 0000000..f55c2f1 --- /dev/null +++ b/src/ecs1030/ecs1030.cxx @@ -0,0 +1,101 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 +#include +#include + +#include "ecs1030.h" + +using namespace upm; + +struct ECS1030Exception : public std::exception { + std::string message; + ECS1030Exception (std::string msg) : message (msg) { } + ~ECS1030Exception () throw () { } + const char* what() const throw () { return message.c_str(); } +}; + +ECS1030::ECS1030 (uint8_t pinNumber) { + m_dataPinCtx = mraa_aio_init(pinNumber); + if (m_dataPinCtx == NULL) { + throw ECS1030Exception ("GPIO failed to initilize"); + } + + m_calibration = 111.1; +} + +ECS1030::~ECS1030 () { + mraa_result_t error = MRAA_SUCCESS; + + error = mraa_aio_close (m_dataPinCtx); + if (error != MRAA_SUCCESS) { + } +} + +double +ECS1030::getCurrency_A () { + int sensorValue = 0; + float rLoad = 0; + float volt = 0; + float rms = 0; + + for (int i = 0; i < NUMBER_OF_SAMPLES; i++) { + sensorValue = mraa_aio_read (m_dataPinCtx); + volt = (VOLT_M * sensorValue) - 2.5; + volt = volt * volt; + rms = rms + volt; + usleep (DELAY_MS); + } + + rms = rms / (float)NUMBER_OF_SAMPLES; + rms = sqrt(rms); + return rms / R_LOAD; +} + +double +ECS1030::getCurrency_B () { + double sumCurrency = 0; + + for (int i = 0; i < NUMBER_OF_SAMPLES; i++) { + m_lastSample = m_sample; + m_sample = mraa_aio_read (m_dataPinCtx); + m_lastFilter = m_filteredSample; + m_filteredSample = 0.996 * (m_lastFilter + m_sample - m_lastSample); + sumCurrency += (m_filteredSample * m_filteredSample); + } + + double ratio = m_calibration * ((SUPPLYVOLTAGE / 1000.0) / (ADC_RESOLUTION)); + return ( ratio * sqrt(sumCurrency / NUMBER_OF_SAMPLES) ); +} + +double +ECS1030::getPower_A () { + return 220.0 * getCurrency_A (); +} + +double +ECS1030::getPower_B () { + return 220.0 * getCurrency_B (); +} diff --git a/src/ecs1030/ecs1030.h b/src/ecs1030/ecs1030.h new file mode 100644 index 0000000..5588db1 --- /dev/null +++ b/src/ecs1030/ecs1030.h @@ -0,0 +1,97 @@ +/* + * Author: Yevgeniy Kiveisha + * 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. + */ +#pragma once + +#include +#include +#include +#include + +#define NUMBER_OF_SAMPLES 500 +#define ADC_RESOLUTION 1024 +#define SUPPLYVOLTAGE 5100 +#define CURRENT_RATIO 2000.0 + +#define HIGH 1 +#define LOW 0 + +#define TRUE HIGH +#define FALSE LOW + +namespace upm { + class ECS1030 { + public: + static const uint8_t DELAY_MS = 20000 / NUMBER_OF_SAMPLES; /* 1/50Hz is 20ms period */ + static const uint8_t VOLT_M = 5.1 / 1023; + static const uint8_t R_LOAD = 2000.0 / CURRENT_RATIO; + + /** + * Instanciates a ECS1030 (current sensor) object + * + * @param pinNumber number of the data pin + */ + ECS1030 (uint8_t pinNumber); + + /** + * ECS1030 object destructor, basicaly it close the GPIO. + */ + ~ECS1030 (); + + /** + * Return currency data for the sampled period + */ + double getCurrency_A (); + + /** + * Return power data for the sampled period + */ + double getPower_A (); + + /** + * Return currency data for the sampled period + */ + double getCurrency_B (); + + /** + * Return power data for the sampled period + */ + double getPower_B (); + + /** + * Return name of the component + */ + std::string name() { + return m_name; + } + private: + std::string m_name; + mraa_aio_context m_dataPinCtx; + + double m_calibration; + int m_lastSample; + double m_lastFilter; + int m_sample; + double m_filteredSample; + }; +} diff --git a/src/ecs1030/jsupm_ecs1030.i b/src/ecs1030/jsupm_ecs1030.i new file mode 100644 index 0000000..12abd2c --- /dev/null +++ b/src/ecs1030/jsupm_ecs1030.i @@ -0,0 +1,8 @@ +%module jsupm_ecs1030 +%include "../upm.i" + +%{ + #include "ecs1030.h" +%} + +%include "ecs1030.h" diff --git a/src/ecs1030/pyupm_ecs1030.i b/src/ecs1030/pyupm_ecs1030.i new file mode 100644 index 0000000..d39b79a --- /dev/null +++ b/src/ecs1030/pyupm_ecs1030.i @@ -0,0 +1,11 @@ +%module pyupm_ecs1030 +%include "../upm.i" + +%include "stdint.i" + +%feature("autodoc", "3"); + +%include "ecs1030.h" +%{ + #include "ecs1030.h" +%}