Bladeren bron

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>
Mihai Tudor Panu 10 jaren geleden
bovenliggende
commit
b2ffcdd9ea

BIN
docs/images/groverotary.jpeg Bestand weergeven


+ 57
- 0
examples/groverotary.cxx Bestand weergeven

@@ -0,0 +1,57 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.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 <iomanip>
28
+#include "grove.h"
29
+
30
+using namespace std;
31
+
32
+int main ()
33
+{
34
+//! [Interesting]
35
+    // Instantiate a rotary sensor on analog pin A0
36
+    upm::GroveRotary* knob = new upm::GroveRotary(0);
37
+
38
+    // Print sensor name to confirm it initialized properly
39
+    cout << knob->name() << endl;
40
+
41
+    while(true) {
42
+        float abs_value = knob->abs_value(); // Absolute raw value
43
+        float abs_deg = knob->abs_deg();     // Absolute degrees
44
+        float abs_rad = knob->abs_rad();     // Absolute radians
45
+        float rel_value = knob->rel_value(); // Relative raw value
46
+        float rel_deg = knob->rel_deg();     // Relative degrees
47
+        float rel_rad = knob->rel_rad();     // Relative radians
48
+
49
+        fprintf(stdout, "Absolute: %4d raw %5.2f deg = %3.2f rad Relative: %4d raw %5.2f deg %3.2f rad\n",
50
+                (int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg, rel_rad);
51
+
52
+        usleep(2500000); // Sleep for 2.5s
53
+    }
54
+//! [Interesting]
55
+    delete knob;
56
+    return 0;
57
+}

+ 49
- 0
examples/javascript/groverotary.js Bestand weergeven

@@ -0,0 +1,49 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.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
+//setup/Initialization
26
+var upm_grove = require('jsupm_grove');
27
+
28
+//setup access analog input Analog pin #0 (A0)
29
+var groveRotary = new upm_grove.GroveRotary(0);
30
+
31
+loop();
32
+
33
+function loop()
34
+{
35
+    var abs = groveRotary.abs_value();
36
+    var absdeg = groveRotary.abs_deg();
37
+    var absrad = groveRotary.abs_rad();
38
+
39
+    var rel = groveRotary.rel_value();
40
+    var reldeg = groveRotary.rel_deg();
41
+    var relrad = groveRotary.rel_rad();
42
+
43
+    //write the knob value to the console in different formats
44
+    console.log("Abs: " + abs + " " + Math.round(parseInt(absdeg)) + " " + absrad.toFixed(3));
45
+    console.log("Rel: " + rel + " " + Math.round(parseInt(reldeg)) + " " + relrad.toFixed(3));
46
+
47
+    //wait 2 s and call function again
48
+    setTimeout(loop, 2000);
49
+}

+ 45
- 0
examples/python/groverotary.py Bestand weergeven

@@ -0,0 +1,45 @@
1
+# Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
2
+# Copyright (c) 2014 Intel Corporation.
3
+#
4
+# Permission is hereby granted, free of charge, to any person obtaining
5
+# a copy of this software and associated documentation files (the
6
+# "Software"), to deal in the Software without restriction, including
7
+# without limitation the rights to use, copy, modify, merge, publish,
8
+# distribute, sublicense, and/or sell copies of the Software, and to
9
+# permit persons to whom the Software is furnished to do so, subject to
10
+# the following conditions:
11
+#
12
+# The above copyright notice and this permission notice shall be
13
+# included in all copies or substantial portions of the Software.
14
+#
15
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+from time import sleep
24
+import pyupm_grove as grove
25
+
26
+# New knob on AIO pin 0
27
+knob = grove.GroveRotary(0)
28
+
29
+# Loop indefinitely
30
+while True:
31
+
32
+    # Read values
33
+    abs = knob.abs_value()
34
+    absdeg = knob.abs_deg()
35
+    absrad = knob.abs_rad()
36
+
37
+    rel = knob.rel_value()
38
+    reldeg = knob.rel_deg()
39
+    relrad = knob.rel_rad()
40
+
41
+    print "Abs values: %4d" % int(abs) , " raw %4d" % int(absdeg), "deg = %5.2f" % absrad , " rad ",
42
+    print "Rel values: %4d" % int(rel) , " raw %4d" % int(reldeg), "deg = %5.2f" % relrad , " rad"
43
+
44
+    # Sleep for 2.5 s
45
+    sleep(2.5)

+ 45
- 0
src/grove/grove.cxx Bestand weergeven

@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3 4
  * Copyright (c) 2014 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -115,3 +116,47 @@ float GroveLight::raw_value()
115 116
 {
116 117
     return (float) mraa_aio_read(m_aio);
117 118
 }
119
+
120
+//// GroveRotary ////
121
+
122
+GroveRotary::GroveRotary(unsigned int pin)
123
+{
124
+    mraa_init();
125
+    m_aio = mraa_aio_init(pin);
126
+    m_name = "Rotary Angle Sensor";
127
+}
128
+
129
+GroveRotary::~GroveRotary()
130
+{
131
+    mraa_aio_close(m_aio);
132
+}
133
+
134
+float GroveRotary::abs_value()
135
+{
136
+    return (float) mraa_aio_read(m_aio);
137
+}
138
+
139
+float GroveRotary::abs_deg()
140
+{
141
+    return GroveRotary::abs_value() * (float) m_max_angle / 1023.0;
142
+}
143
+
144
+float GroveRotary::abs_rad()
145
+{
146
+    return GroveRotary::abs_deg() * M_PI / 180.0;
147
+}
148
+
149
+float GroveRotary::rel_value()
150
+{
151
+    return GroveRotary::abs_value() - 512.0;
152
+}
153
+
154
+float GroveRotary::rel_deg()
155
+{
156
+    return GroveRotary::rel_value() * (float) m_max_angle / 1023.0;
157
+}
158
+
159
+float GroveRotary::rel_rad()
160
+{
161
+    return GroveRotary::rel_deg() * M_PI / 180.0;
162
+}

+ 65
- 0
src/grove/grove.h Bestand weergeven

@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3 4
  * Copyright (c) 2014 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -135,4 +136,68 @@ class GroveLight: public Grove {
135 136
         mraa_aio_context m_aio;
136 137
 };
137 138
 
139
+/**
140
+ * @brief C++ API for Grove Rotary Angle Sensor (Knob)
141
+ *
142
+ * Very basic UPM module for Grove rotary angle sensor (knob) on analog. Provides
143
+ * a set of functions to read the absolute pin value, degrees or radians and another
144
+ * to do the same relative to the center of the knob's range.
145
+ *
146
+ * @ingroup grove analog
147
+ * @snippet groverotary.cxx Interesting
148
+ * @image html groverotary.jpeg
149
+ */
150
+class GroveRotary: public Grove {
151
+    public:
152
+        /**
153
+         * Grove rotary angle sensor constructor
154
+         *
155
+         * @param pin number of analog pin to use
156
+         */
157
+        GroveRotary(unsigned int pin);
158
+        /**
159
+         * GroveRotary Destructor
160
+         */
161
+        ~GroveRotary();
162
+        /**
163
+         * Get absolute raw value from AIO pin
164
+         *
165
+         * @return the unsigned value from the ADC
166
+         */
167
+        float abs_value();
168
+        /**
169
+         * Get absolute raw degrees from AIO pin
170
+         *
171
+         * @return the unsigned degrees from the ADC
172
+         */
173
+        float abs_deg();
174
+        /**
175
+         * Get absolute raw radians from AIO pin
176
+         *
177
+         * @return the unsigned radians from the ADC
178
+         */
179
+        float abs_rad();
180
+        /**
181
+         * Get the relative value from the pin
182
+         *
183
+         * @return the signed value from the ADC
184
+         */
185
+        float rel_value();
186
+        /**
187
+         * Get relative degrees from AIO pin
188
+         *
189
+         * @return the signed degrees from the ADC
190
+         */
191
+        float rel_deg();
192
+        /**
193
+         * Get relative radians from AIO pin
194
+         *
195
+         * @return the signed radians from the ADC
196
+         */
197
+        float rel_rad();
198
+    private:
199
+        mraa_aio_context m_aio;
200
+        static const int m_max_angle = 300;
201
+};
202
+
138 203
 }