drv_sen13680.c
Go to the documentation of this file.
1 /*
2  drv_sen13680.c : driver for Sparkfun SEN13680 LIDAR-Lite v2
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 // See https://github.com/PulsedLight3D for Arduino code
23 // and more documentation on this sensor
24 
25 #include <breezystm32.h>
26 
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 #include "drv_i2c.h"
31 
32 #define SEN13680_DEFAULT_ADDRESS 0x62
33 
34 static float distance;
35 
36 
37 
39 {
40  // check for device SEN13680_DEFAULT_ADDRESS and set
41  // to read fast and noisy if it's there
42  bool success = true;
43  if (i2cWrite(SEN13680_DEFAULT_ADDRESS, 0x00, 0x00))
44  {
45  // Set the time between measurements (0x45). 0x04 means 250 Hz
46  success &= i2cWrite(SEN13680_DEFAULT_ADDRESS, 0x45, 0x04);
47  delay(10);
48  // Set the mode pin to default setting
49  success &= i2cWrite(SEN13680_DEFAULT_ADDRESS, 0x04, 0x21);
50  delay(10);
51  // Set the number of measurements to be taken (continuous)
52  success &= i2cWrite(SEN13680_DEFAULT_ADDRESS, 0x11, 0xFF);
53  delay(10);
54  // Initiate Reading
55  success &= i2cWrite(SEN13680_DEFAULT_ADDRESS, 0x00, 0x04);
56  delay(10);
57  }
58  else
59  {
60  success = false;
61  }
62  if (!success)
63  {
64  distance = -1;
65  }
66  return success;
67 }
68 
69 
71 {
72  // get current time
73  uint64_t now_us = micros();
74  static uint64_t last_update_time_us = 0;
75 
76  // populate new measurement at 100hz
77  if (now_us > last_update_time_us + 10000)
78  {
79  // save current time
80  last_update_time_us = now_us;
81  uint8_t read_buffer[2] = {100,100};
82 
83  // Request and read a lidar measurement
84  // Lidar Lite is stupid, and needs a stop signal before the second start signal
85  // in an I2C read. So, I'm writing the address to nowhere, stopping, starting again
86  // and then reading from nowhere
88  i2cRead(SEN13680_DEFAULT_ADDRESS, 0xFF, 2, read_buffer);
89 
90  distance = (float)((int16_t)(read_buffer[0] << 8) + read_buffer[1])/100.0;
91  }
92 }
93 
95 {
96  return distance;
97 }
#define SEN13680_DEFAULT_ADDRESS
Definition: drv_sen13680.c:32
bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t *buf)
Definition: drv_i2c.c:157
void sen13680_update()
Definition: drv_sen13680.c:70
float sen13680_read()
Definition: drv_sen13680.c:94
bool sen13680_init()
Definition: drv_sen13680.c:38
volatile uint64_t micros(void)
Definition: system.c:44
static float distance
Definition: drv_sen13680.c:34
bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data)
Definition: drv_i2c.c:152
void delay(uint32_t ms)
Definition: system.c:98


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