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 /*
34  * Driver for the Maxbotix I2CXL-MaxSonar-EZ series sonar.
35  * This has been made for and tested with the MB1242 sonar module,
36  * but should work for the MB1202, MB1212, MB1222, and MB1232.
37  */
38 #ifndef I2C_SONAR_H
39 #define I2C_SONAR_H
40 
41 #include <cstdint>
42 
43 #include "i2c.h"
44 
45 // These 3 constants come from the spec sheet for the sonar
46 #define MB1242_DEFAULT_ADDRESS 112
47 #define MB1242_DEFAULT_REGISTER 0xFF
48 #define MB1242_PING_COMMAND 81
49 
50 // Uncomment this to return raw distances instead of calibrated distances
51 // #define MB1242_RAW
52 
53 #define MB1242_UPDATE_WAIT_MILLIS 50 // minimum time between calls of async_update that actually do something
54 // 50 ms is chosen because it will read only once per two calls to async_update, at max,
55 // and the spec sheet for the sonar recomends waiting 100 ms in between pings
56 
57 class I2CSonar
58 {
59 private:
60  uint32_t last_update_ms_; // The last time that async_update was called
61  uint32_t last_callback_ms_; // The last time the sensor responded
62  float value_; // the latest reading from the sensor
63  bool new_data_; // Whether or not new data is ready to be returned
64  I2C *i2c_; // The i2c object used for communication
65  bool ready_to_ping_; // Whether the sensor is ready to make another measurement
66  uint8_t buffer_[2]; // for recieving data from the sensor
67  bool sensor_present_; // Flag of whether we have received data from the sensor
68 
69 
70 public:
71  I2CSonar();
72  void init(I2C *_i2c);
73  bool present();
74  float read(); // Returns the most recent reading, converted to meters, or 0 if there is none
75  void update(); // Tries to either start a measurement, or read it from the sensor
76  // update will do nothing if it has done something in the last MB1242_UPDATE_WAIT_MILLIS ms
77  // Calling it more frequently won't break anything
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 
85 #endif
bool present()
Definition: mb1242.cpp:72
I2CSonar()
Definition: mb1242.cpp:48
void cb_finished_read(uint8_t result)
Definition: mb1242.cpp:128
uint8_t buffer_[2]
Definition: mb1242.h:66
uint32_t last_update_ms_
Definition: mb1242.h:60
bool new_data_
Definition: mb1242.h:63
void update()
Definition: mb1242.cpp:84
I2C * i2c_
Definition: mb1242.h:64
float value_
Definition: mb1242.h:62
uint32_t last_callback_ms_
Definition: mb1242.h:61
void init(I2C *_i2c)
Definition: mb1242.cpp:53
bool ready_to_ping_
Definition: mb1242.h:65
float read()
Definition: mb1242.cpp:100
Definition: i2c.h:40
void cb_start_read(uint8_t result)
Definition: mb1242.cpp:117
bool sensor_present_
Definition: mb1242.h:67


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:25