mb1242.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Trey Henrichsen
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Driver for the Maxbotix I2CXL-MaxSonar-EZ series sonar.
34  * This has been made for and tested with the MB1242 sonar module,
35  * but should work for the MB1202, MB1212, MB1222, and MB1232.
36  */
37 #ifndef I2C_SONAR_H
38 #define I2C_SONAR_H
39 
40 #include "i2c.h"
41 
42 #include <cstdint>
43 
44 // These 3 constants come from the spec sheet for the sonar
45 #define MB1242_DEFAULT_ADDRESS 112
46 #define MB1242_DEFAULT_REGISTER 0xFF
47 #define MB1242_PING_COMMAND 81
48 
49 // Uncomment this to return raw distances instead of calibrated distances
50 // #define MB1242_RAW
51 
52 #define MB1242_UPDATE_WAIT_MILLIS 50 // minimum time between calls of async_update that actually do something
53 // 50 ms is chosen because it will read only once per two calls to async_update, at max,
54 // and the spec sheet for the sonar recomends waiting 100 ms in between pings
55 
56 class I2CSonar
57 {
58 private:
59  uint32_t last_update_ms_; // The last time that async_update was called
60  uint32_t last_callback_ms_; // The last time the sensor responded
61  float value_; // the latest reading from the sensor
62  bool new_data_; // Whether or not new data is ready to be returned
63  I2C *i2c_; // The i2c object used for communication
64  bool ready_to_ping_; // Whether the sensor is ready to make another measurement
65  uint8_t buffer_[2]; // for receiving data from the sensor
66  bool sensor_present_{false}; // Flag of whether we have received data from the sensor
67  bool sensor_initialized_{false}; // Whether the init function has been called yet
68 
69 public:
70  I2CSonar();
71  void init(I2C *_i2c);
72  bool present();
73  float read(); // Returns the most recent reading, converted to meters, or 0 if there is none
74  void update(); // Tries to either start a measurement, or read it from the sensor
75  // update will do nothing if it has done something in the last MB1242_UPDATE_WAIT_MILLIS ms
76  // Calling it more frequently won't break anything
77  inline bool is_initialized() { return sensor_initialized_; }
78 
79  // Callbacks. For internal use only, but public so the I2C peripheral can call them
80  void cb_start_read(uint8_t result); // callback after the measure command has been sent to the sensor
81  void cb_finished_read(uint8_t result); // callback after reading from the sensor has finished
82 };
83 
84 #endif
bool present()
Definition: mb1242.cpp:73
bool sensor_initialized_
Definition: mb1242.h:67
I2CSonar()
Definition: mb1242.cpp:48
void cb_finished_read(uint8_t result)
Definition: mb1242.cpp:131
uint8_t buffer_[2]
Definition: mb1242.h:65
uint32_t last_update_ms_
Definition: mb1242.h:59
bool new_data_
Definition: mb1242.h:62
bool is_initialized()
Definition: mb1242.h:77
void update()
Definition: mb1242.cpp:85
I2C * i2c_
Definition: mb1242.h:63
float value_
Definition: mb1242.h:61
uint32_t last_callback_ms_
Definition: mb1242.h:60
void init(I2C *_i2c)
Definition: mb1242.cpp:53
bool ready_to_ping_
Definition: mb1242.h:64
float read()
Definition: mb1242.cpp:103
Definition: i2c.h:39
void cb_start_read(uint8_t result)
Definition: mb1242.cpp:120
bool sensor_present_
Definition: mb1242.h:66


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:47