ソースを参照

groveehr: Initial implementation

This module implements support for the Grove Ear-clip heart rate
sensor.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Jon Trulson 10 年 前
コミット
ed15b1042d
共有8 個のファイルを変更した406 個の追加0 個の削除を含む
  1. 3
    0
      examples/CMakeLists.txt
  2. 78
    0
      examples/groveehr.cxx
  3. 60
    0
      examples/javascript/groveehr.js
  4. 5
    0
      src/groveehr/CMakeLists.txt
  5. 129
    0
      src/groveehr/groveehr.cxx
  6. 114
    0
      src/groveehr/groveehr.h
  7. 8
    0
      src/groveehr/jsupm_groveehr.i
  8. 9
    0
      src/groveehr/pyupm_groveehr.i

+ 3
- 0
examples/CMakeLists.txt ファイルの表示

@@ -58,6 +58,7 @@ add_executable (ds1307-example ds1307.cxx)
58 58
 add_executable (a110x-example a110x.cxx)
59 59
 add_executable (gp2y0a21yk-example gp2y0a21yk.cxx)
60 60
 add_executable (grovemoisture-example grovemoisture.cxx)
61
+add_executable (groveehr-example groveehr.cxx)
61 62
 
62 63
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
63 64
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -103,6 +104,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ds1307)
103 104
 include_directories (${PROJECT_SOURCE_DIR}/src/a110x)
104 105
 include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a21yk)
105 106
 include_directories (${PROJECT_SOURCE_DIR}/src/grovemoisture)
107
+include_directories (${PROJECT_SOURCE_DIR}/src/groveehr)
106 108
 
107 109
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
108 110
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -164,3 +166,4 @@ target_link_libraries (ds1307-example ds1307 ${CMAKE_THREAD_LIBS_INIT})
164 166
 target_link_libraries (a110x-example a110x ${CMAKE_THREAD_LIBS_INIT})
165 167
 target_link_libraries (gp2y0a21yk-example gp2y0a21yk ${CMAKE_THREAD_LIBS_INIT})
166 168
 target_link_libraries (grovemoisture-example grovemoisture ${CMAKE_THREAD_LIBS_INIT})
169
+target_link_libraries (groveehr-example groveehr ${CMAKE_THREAD_LIBS_INIT})

+ 78
- 0
examples/groveehr.cxx ファイルの表示

@@ -0,0 +1,78 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include <signal.h>
28
+#include "groveehr.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+
41
+int main()
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+//! [Interesting]
46
+  // Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2
47
+  upm::GroveEHR* heart = new upm::GroveEHR(2);
48
+  
49
+  // set the beat counter to 0, init the clock and start counting beats
50
+  heart->clearBeatCounter();
51
+  heart->initClock();
52
+  heart->startBeatCounter();
53
+
54
+  while (shouldRun)
55
+    {
56
+      // we grab these just for display purposes in this example
57
+      uint32_t millis = heart->getMillis();
58
+      uint32_t beats = heart->beatCounter();
59
+
60
+      // heartRate() requires that at least 5 seconds pass before
61
+      // returning anything other than 0
62
+      int hr = heart->heartRate();
63
+
64
+      // output milliseconds passed, beat count, and computed heart rate
65
+      cout << "Millis: " << millis << " Beats: " << beats;
66
+      cout << " Heart Rate: " << hr << endl;
67
+
68
+      sleep(1);
69
+    }
70
+
71
+  heart->stopBeatCounter();
72
+//! [Interesting]
73
+
74
+  cout << "Exiting..." << endl;
75
+
76
+  delete heart;
77
+  return 0;
78
+}

+ 60
- 0
examples/javascript/groveehr.js ファイルの表示

@@ -0,0 +1,60 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*global */
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2014 Intel Corporation.
7
+*
8
+* Permission is hereby granted, free of charge, to any person obtaining
9
+* a copy of this software and associated documentation files (the
10
+* "Software"), to deal in the Software without restriction, including
11
+* without limitation the rights to use, copy, modify, merge, publish,
12
+* distribute, sublicense, and/or sell copies of the Software, and to
13
+* permit persons to whom the Software is furnished to do so, subject to
14
+* the following conditions:
15
+*
16
+* The above copyright notice and this permission notice shall be
17
+* included in all copies or substantial portions of the Software.
18
+*
19
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+*/
27
+
28
+// Load heart rate sensor module
29
+var heartRateSensor = require('jsupm_groveehr');
30
+// Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2
31
+var myHeartRateSensor = new heartRateSensor.GroveEHR(2);
32
+
33
+// set the beat counter to 0, init the clock and start counting beats
34
+myHeartRateSensor.clearBeatCounter();
35
+myHeartRateSensor.initClock();
36
+myHeartRateSensor.startBeatCounter();
37
+
38
+setInterval(readHeartRate, 1000);
39
+
40
+function readHeartRate()
41
+{
42
+	// we grab these just for display purposes in this example
43
+	var millis = myHeartRateSensor.getMillis();
44
+	var beats = myHeartRateSensor.beatCounter();
45
+
46
+	// heartRate() requires that at least 5 seconds pass before
47
+	// returning anything other than 0
48
+	var hr = myHeartRateSensor.heartRate();
49
+
50
+	// output milliseconds passed, beat count, and computed heart rate
51
+	console.log("Millis: " + millis + " Beats: " + beats +
52
+	            " Heart Rate: " + hr);
53
+}
54
+
55
+// Print message when exiting
56
+process.on('SIGINT', function()
57
+{
58
+	console.log("Exiting...");
59
+	process.exit(0);
60
+});

+ 5
- 0
src/groveehr/CMakeLists.txt ファイルの表示

@@ -0,0 +1,5 @@
1
+set (libname "groveehr")
2
+set (libdescription "upm grove ear-clip heart rate sensor module")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 129
- 0
src/groveehr/groveehr.cxx ファイルの表示

@@ -0,0 +1,129 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <iostream>
26
+
27
+#include "groveehr.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+GroveEHR::GroveEHR(int pin)
33
+{
34
+  mraa_init();
35
+
36
+  if ( !(m_gpio = mraa_gpio_init(pin)) )
37
+    {
38
+      cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
39
+      return;
40
+    }
41
+
42
+  mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
43
+
44
+  initClock();
45
+  m_beatCounter = 0;
46
+}
47
+
48
+GroveEHR::~GroveEHR()
49
+{
50
+  mraa_gpio_close(m_gpio);
51
+}
52
+
53
+void GroveEHR::initClock()
54
+{
55
+  gettimeofday(&m_startTime, NULL);
56
+}
57
+
58
+uint32_t GroveEHR::getMillis()
59
+{
60
+  struct timeval elapsed, now;
61
+  uint32_t elapse;
62
+
63
+  // get current time
64
+  gettimeofday(&now, NULL);
65
+
66
+  // compute the delta since m_startTime
67
+  if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 ) 
68
+    {
69
+      elapsed.tv_usec += 1000000;
70
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
71
+    } 
72
+  else 
73
+    {
74
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
75
+    }
76
+
77
+  elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
78
+
79
+  // never return 0
80
+  if (elapse == 0)
81
+    elapse = 1;
82
+
83
+  return elapse;
84
+}
85
+
86
+void GroveEHR::clearBeatCounter()
87
+{
88
+  m_beatCounter = 0;
89
+}
90
+
91
+void GroveEHR::startBeatCounter()
92
+{
93
+  // install our interrupt handler
94
+  mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING, 
95
+                &beatISR, this);
96
+}
97
+
98
+void GroveEHR::stopBeatCounter()
99
+{
100
+  // remove the interrupt handler
101
+  mraa_gpio_isr_exit(m_gpio);
102
+}
103
+
104
+uint32_t GroveEHR::beatCounter()
105
+{
106
+  return m_beatCounter;
107
+}
108
+
109
+void GroveEHR::beatISR(void *ctx)
110
+{
111
+  upm::GroveEHR *This = (upm::GroveEHR *)ctx;
112
+  This->m_beatCounter++;
113
+}
114
+
115
+int GroveEHR::heartRate()
116
+{
117
+  uint32_t millis = getMillis();
118
+  uint32_t beats = beatCounter();
119
+  
120
+  float heartRate = 0;
121
+  // wait at least 5 seconds before attempting to compute the
122
+  // heart rate
123
+  if (millis > 5000)
124
+    {
125
+      heartRate = (float(beats) / (float(millis) / 1000.0)) * 60.0;
126
+    }
127
+
128
+  return int(heartRate);
129
+}

+ 114
- 0
src/groveehr/groveehr.h ファイルの表示

@@ -0,0 +1,114 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+#pragma once
25
+
26
+#include <string>
27
+#include <stdint.h>
28
+#include <sys/time.h>
29
+#include <mraa/gpio.h>
30
+
31
+namespace upm {
32
+
33
+  /**
34
+   * @brief C++ API for the Grove Ear-clip Heart Rate sensor
35
+   *
36
+   * UPM module for the GroveEHR sensor.  It is used to measure your
37
+   * heartbeat.
38
+   *
39
+   * @ingroup gpio
40
+   * @snippet groveehr.cxx Interesting
41
+   */
42
+  class GroveEHR {
43
+  public:
44
+    /**
45
+     * GroveEHR constructor
46
+     *
47
+     * @param pin digital pin to use
48
+     */
49
+    GroveEHR(int pin);
50
+    /**
51
+     * GroveEHR Destructor
52
+     */
53
+    ~GroveEHR();
54
+    /**
55
+     * Return the number of milliseconds elapsed since initClock()
56
+     * was last called.
57
+     *
58
+     * @return elapsed milliseconds
59
+     */
60
+    uint32_t getMillis();
61
+
62
+    /**
63
+     * Reset the Clock
64
+     *
65
+     */
66
+    void initClock();
67
+
68
+    /**
69
+     * Reset the beat counter to 0.  The beat Counter should be
70
+     * stopped via stopBeatCounter() prior to calling this function.
71
+     *
72
+     */
73
+    void clearBeatCounter();
74
+
75
+    /**
76
+     * Start the beat counter
77
+     *
78
+     */
79
+    void startBeatCounter();
80
+
81
+    /**
82
+     * Stop the beat counter
83
+     *
84
+     */
85
+    void stopBeatCounter();
86
+
87
+    /**
88
+     * Get the beat Counter
89
+     *
90
+     * @return the beat counter
91
+     */
92
+    uint32_t beatCounter();
93
+
94
+    /**
95
+     * Beat Interrupt Service Routine
96
+     *
97
+     */
98
+    static void beatISR(void *ctx);
99
+
100
+    /**
101
+     * Compute the heart rate
102
+     *
103
+     * @return the computed heart rate
104
+     */
105
+    int heartRate();
106
+
107
+  private:
108
+    volatile uint32_t m_beatCounter;
109
+    struct timeval m_startTime;
110
+    mraa_gpio_context m_gpio;
111
+  };
112
+}
113
+
114
+

+ 8
- 0
src/groveehr/jsupm_groveehr.i ファイルの表示

@@ -0,0 +1,8 @@
1
+%module jsupm_groveehr
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "groveehr.h"
6
+%}
7
+
8
+%include "groveehr.h"

+ 9
- 0
src/groveehr/pyupm_groveehr.i ファイルの表示

@@ -0,0 +1,9 @@
1
+%module pyupm_groveehr
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "groveehr.h"
7
+%{
8
+    #include "groveehr.h"
9
+%}