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 }
71 
73 {
74  if (sensor_present_ && millis() > last_callback_ms_ + 500)
75  {
76  sensor_present_ = false;
77  }
78  return sensor_present_;
79 }
80 
81 // Tries to either start a measurement, or read it from the sensor
82 // Does nothing if it has done something in the last UPDATE_WAIT_MILLIS ms
83 // Feel free to call it more often, though.
85 {
86  uint64_t now=millis();
88  {
89  last_update_ms_ = now;
90  if (ready_to_ping_)
92  else
94  }
95 }
96 
97 //Returns the most recent reading
98 //It is during this method that the reading is converted to meters, as well
99 //If there has not yet been a successful reading, returns 0
101 {
102  if (new_data_)
103  {
104  uint16_t centimeters = buffer_[0] << 8 | buffer_[1];//Convert to a single number
105 #ifdef MB1242_RAW
106  value=(float)centimeters * 0.01;
107 #else
108  //Calibration from BreezySTM32 by Simon D. Levy
109  value_=(1.071 * static_cast<float>(centimeters) + 3.103) / 100.0;
110 #endif
111  new_data_=false;
112  }
113  return value_;
114 }
115 
116 //callback after the measure command has been sent to the sensor
117 void I2CSonar::cb_start_read(uint8_t result)
118 {
119  if (result == I2C::RESULT_SUCCESS)
120  {
121  sensor_present_ = true;
123  ready_to_ping_ = false;
124  }
125 }
126 
127 //callback after reading from the sensor has finished
128 void I2CSonar::cb_finished_read(uint8_t result)
129 {
130  if (result == I2C::RESULT_SUCCESS)
131  {
132  sensor_present_ = true;
133  new_data_ = true;
135  ready_to_ping_ = true;
136  }
137 }
bool present()
Definition: mb1242.cpp:72
I2CSonar * sonarPtr
Definition: mb1242.cpp:34
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:47
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
#define MB1242_UPDATE_WAIT_MILLIS
Definition: mb1242.h:53
bool new_data_
Definition: mb1242.h:63
void update()
Definition: mb1242.cpp:84
void _I2C_Sonar_finished_read_cb(uint8_t result)
Definition: mb1242.cpp:42
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
int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking=false)
Definition: i2c.cpp:298
float read()
Definition: mb1242.cpp:100
#define MB1242_PING_COMMAND
Definition: mb1242.h:48
Definition: i2c.h:40
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:179
void cb_start_read(uint8_t result)
Definition: mb1242.cpp:117
#define MB1242_DEFAULT_ADDRESS
Definition: mb1242.h:46
bool sensor_present_
Definition: mb1242.h:67


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Oct 24 2019 03:17:19