drv_mb1242.c
Go to the documentation of this file.
1 /*
2  drv_mb1242.c : driver for MaxBotix MB1242 sonar
3 
4  Copyright (C) 2016 Simon D. Levy
5 
6  This file is part of BreezySTM32.
7 
8  BreezySTM32 is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BreezySTM32 is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with BreezySTM32. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <breezystm32.h>
23 
24 #include <stdbool.h>
25 #include <stdint.h>
26 
27 #include "drv_i2c.h"
28 
29 #define MB1242_DEFAULT_ADDRESS 0x70
30 
31 static const uint8_t START_MEASUREMENT_CMD = 0x51;
32 
33 static bool sensor_present_;
34 static uint32_t last_update_time_ms_;
35 static uint32_t next_update_time_ms_;
36 static uint8_t buf_[2];
37 static uint32_t distance_cm_;
38 static float distance_;
39 static bool new_data_ = false;
40 static uint8_t cmd_;
41 typedef enum
42 {
47 typedef enum
48 {
53 
54 static void cb(uint8_t result);
55 static void convert();
56 
58 {
62  sensor_present_ = true;
63  else
64  sensor_present_ = false;
66  return sensor_present_;
67 }
68 
70 {
72  sensor_present_ = false;
73  return sensor_present_;
74 }
75 
77 {
78  uint32_t now_ms = millis();
79  if (now_ms > next_update_time_ms_)
80  {
81  next_update_time_ms_ = now_ms +25;
82  switch (state_)
83  {
84  case START_MEAS:
88  break;
89  case READ_DATA:
92  break;
93  }
94  }
95  if (new_data_)
96  convert();
97 }
98 
100 {
101  return distance_;
102 }
103 
104 
105 void cb(uint8_t result)
106 {
107  if (result != I2C_JOB_ERROR)
108  {
109  sensor_present_ = true;
111 
112  switch (callback_type_)
113  {
114  case START_MEAS_CB:
115  state_ = READ_DATA;
116  break;
117  case READ_DATA_CB:
118  state_ = START_MEAS;
119  new_data_ = true;
120  break;
121  }
122  }
123 }
124 
125 void convert()
126 {
127  distance_cm_ = (buf_[0] << 8) + buf_[1];
128  if (distance_cm_ > 850)
129  {
130  distance_cm_ = 0;
131  }
132  distance_ = (1.071 * (float)distance_cm_ + 3.103)/100.0;
133 }
134 
static uint8_t cmd_
Definition: drv_mb1242.c:40
static bool new_data_
Definition: drv_mb1242.c:39
static callack_type_t callback_type_
Definition: drv_mb1242.c:52
static uint8_t buf_[2]
Definition: drv_mb1242.c:36
volatile uint32_t millis(void)
Definition: system.c:50
Definition: drv_i2c.h:32
void mb1242_async_update()
Definition: drv_mb1242.c:76
static void cb(uint8_t result)
Definition: drv_mb1242.c:105
bool mb1242_init()
Definition: drv_mb1242.c:57
bool mb1242_present()
Definition: drv_mb1242.c:69
static float distance_
Definition: drv_mb1242.c:38
bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data)
Definition: drv_i2c.c:152
sonar_state_t
Definition: drv_mb1242.c:41
static uint32_t next_update_time_ms_
Definition: drv_mb1242.c:35
void i2c_queue_job(i2cJobType_t type, uint8_t addr_, uint8_t reg_, uint8_t *data, uint8_t length, volatile uint8_t *status_, void(*CB)(uint8_t))
Definition: drv_i2c.c:579
static uint32_t last_update_time_ms_
Definition: drv_mb1242.c:34
#define MB1242_DEFAULT_ADDRESS
Definition: drv_mb1242.c:29
static bool sensor_present_
Definition: drv_mb1242.c:33
Definition: drv_i2c.h:31
#define NULL
Definition: usbd_def.h:50
static const uint8_t START_MEASUREMENT_CMD
Definition: drv_mb1242.c:31
static void convert()
Definition: drv_mb1242.c:125
float mb1242_async_read()
Definition: drv_mb1242.c:99
static uint32_t distance_cm_
Definition: drv_mb1242.c:37
static sonar_state_t state_
Definition: drv_mb1242.c:46
callack_type_t
Definition: drv_mb1242.c:47


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