mb1242.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson amd 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 #include "mb1242.h"
33 
35 
36 void _I2C_Sonar_start_read_cb(uint8_t result)
37 {
38  (void)result;
39  sonarPtr->cb_start_read(result);
40 }
41 
42 void _I2C_Sonar_finished_read_cb(uint8_t result)
43 {
44  (void)result;
45  sonarPtr->cb_finished_read(result);
46 }
47 
49 {
50  sonarPtr = this;
51 }
52 
53 void I2CSonar::init(I2C* _i2c)
54 {
55  i2c_ = _i2c;
56  new_data_ = false;
57  value_ = 0;
59  ready_to_ping_ = true;
61  {
62  sensor_present_ = true;
64  }
65  else
66  {
67  sensor_present_ = false;
69  }
70  sensor_initialized_ = true;
71 }
72 
74 {
75  if (sensor_present_ && millis() > last_callback_ms_ + 500)
76  {
77  sensor_present_ = false;
78  }
79  return sensor_present_;
80 }
81 
82 // Tries to either start a measurement, or read it from the sensor
83 // Does nothing if it has done something in the last UPDATE_WAIT_MILLIS ms
84 // Feel free to call it more often, though.
86 {
88  return;
89  uint64_t now = millis();
91  {
92  last_update_ms_ = now;
93  if (ready_to_ping_)
95  else
97  }
98 }
99 
100 // Returns the most recent reading
101 // It is during this method that the reading is converted to meters, as well
102 // If there has not yet been a successful reading, returns 0
104 {
105  if (new_data_)
106  {
107  uint16_t centimeters = buffer_[0] << 8 | buffer_[1]; // Convert to a single number
108 #ifdef MB1242_RAW
109  value = (float)centimeters * 0.01;
110 #else
111  // Calibration from BreezySTM32 by Simon D. Levy
112  value_ = (1.071 * static_cast<float>(centimeters) + 3.103) / 100.0;
113 #endif
114  new_data_ = false;
115  }
116  return value_;
117 }
118 
119 // callback after the measure command has been sent to the sensor
120 void I2CSonar::cb_start_read(uint8_t result)
121 {
122  if (result == I2C::RESULT_SUCCESS)
123  {
124  sensor_present_ = true;
126  ready_to_ping_ = false;
127  }
128 }
129 
130 // callback after reading from the sensor has finished
131 void I2CSonar::cb_finished_read(uint8_t result)
132 {
133  if (result == I2C::RESULT_SUCCESS)
134  {
135  sensor_present_ = true;
136  new_data_ = true;
138  ready_to_ping_ = true;
139  }
140 }
bool present()
Definition: mb1242.cpp:73
I2CSonar * sonarPtr
Definition: mb1242.cpp:34
bool sensor_initialized_
Definition: mb1242.h:67
I2CSonar()
Definition: mb1242.cpp:48
void _I2C_Sonar_start_read_cb(uint8_t result)
Definition: mb1242.cpp:36
volatile uint32_t millis(void)
Definition: system.c:50
#define MB1242_DEFAULT_REGISTER
Definition: mb1242.h:46
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
#define MB1242_UPDATE_WAIT_MILLIS
Definition: mb1242.h:52
bool new_data_
Definition: mb1242.h:62
void update()
Definition: mb1242.cpp:85
void _I2C_Sonar_finished_read_cb(uint8_t result)
Definition: mb1242.cpp:42
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
int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking=false)
Definition: i2c.cpp:297
float read()
Definition: mb1242.cpp:103
#define MB1242_PING_COMMAND
Definition: mb1242.h:47
Definition: i2c.h:39
int8_t read(uint8_t addr, uint8_t reg, uint8_t num_bytes, uint8_t *data, void(*callback)(uint8_t)=nullptr, bool blocking=false)
Definition: i2c.cpp:177
void cb_start_read(uint8_t result)
Definition: mb1242.cpp:120
#define MB1242_DEFAULT_ADDRESS
Definition: mb1242.h:45
bool sensor_present_
Definition: mb1242.h:66


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Sat May 9 2020 03:16:53