1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 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 <iostream>
27 #include <string>
28 #include <mraa/aio.h>
29 
30 // EZ series is volts/512
31 #define MAXSONAREZ_RES  512
32 
33 namespace upm {
34   /**
35    * @brief MaxSonar-EZ Family of Ultrasonic Rangers library
36    * @defgroup maxsonarez libupm-maxsonarez
37    * @ingroup seeed analog sound
38    */
39 
40   /**
41    * @library maxsonarez
42    * @sensor maxsonarez
43    * @comname LV-MaxSonar-EZ Ultrasonic Ranger
44    * @altname EZ1, EZ2, EZ3, EZ4
45    * @type sound
46    * @man sparkfun
47    * @web https://www.sparkfun.com/products/8502
48    * @con analog
49    *
50    * @brief API for the LV-MaxSonar-EZ Family of Ultrasonic Rangers
51    *
52    * Sensors of this family return an analog voltage corresponding to the
53    * distance of an object from the sensor in inches. They have a
54    * resolution of about 9.7 millivolts per inch with an analog reference voltage of 5.0 V. The
55    * differences between various versions (EZ1, EZ2, etc.) are
56    * related to the narrowness of the beam angle.
57    *
58    * This class supports these sensors with an analog input only.
59    *
60    * This driver was developed using an LV-MaxSonar-EZ3 ultrasonic ranger.
61    *
62    * @image html maxsonarez.jpg
63    * <br><em>LV-MaxSonar-EZ Ultrasonic Ranger image provided by SparkFun* under
64    * <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
65    * CC BY-NC-SA-3.0</a>.</em>
66    *
67    * @snippet maxsonarez.cxx Interesting
68    */
69 
70   class MAXSONAREZ {
71   public:
72 
73     /**
74      * MAXSONAREZ constructor
75      *
76      * @param pin Analog pin to use
77      * @param aref Analog reference voltage; default is 5.0 V
78      */
79     MAXSONAREZ(int pin, float aref=5.0);
80 
81     /**
82      * MAXSONAREZ destructor
83      */
84     ~MAXSONAREZ();
85 
86     /**
87      * Gets the distance to the object in inches
88      *
89      * @return Distance to the object in inches
90      */
91     int inches();
92 
93   private:
94     mraa_aio_context m_aio;
95     float m_aref;
96     // ADC resolution
97     int m_aRes;
98     // computed volts per inch
99     float m_vI;
100   };
101 }
102 
103 
104