Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
lidar_lite.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Michal Podhradsky <michal.podhradsky@aggiemail.usu.edu>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
35 #include "modules/core/abi.h"
36 #include "filters/median_filter.h"
37 
38 // State interface for rotation compensation
39 #include "state.h"
40 
41 // Messages
42 #include "pprzlink/messages.h"
44 
45 struct LidarLite lidar_lite;
47 
48 #define LIDAR_LITE_REG_ADDR 0x00
49 #define LIDAR_LITE_REG_VAL 0x04
50 #define LIDAR_LITE_READ_ADDR 0x8F
51 
52 
56 void lidar_lite_init(void)
57 {
59  lidar_lite.addr = LIDAR_LITE_I2C_ADDR;
61  lidar_lite.update_agl = USE_LIDAR_LITE_AGL;
62  lidar_lite.compensate_rotation = LIDAR_LITE_COMPENSATE_ROTATION;
63 
64  lidar_lite.distance = 0;
66 
67  init_median_filter_i(&lidar_lite_filter, LIDAR_LITE_MEDIAN_LENGTH);
68 }
69 
76 void lidar_lite_event(void)
77 {
78  switch (lidar_lite.trans.status) {
79  case I2CTransPending:
80  // wait and do nothing
81  break;
82  case I2CTransRunning:
83  // wait and do nothing
84  break;
85  case I2CTransSuccess:
86  // set to done
88  break;
89  case I2CTransFailed:
90  // set to done
92  break;
93  case I2CTransDone:
94  // do nothing
95  break;
96  default:
97  break;
98  }
99 }
100 
107 {
108  switch (lidar_lite.status) {
111  // ask for i2c frame
112  lidar_lite.trans.buf[0] = LIDAR_LITE_REG_ADDR; // sets register pointer to (0x00)
113  lidar_lite.trans.buf[1] = LIDAR_LITE_REG_VAL; // take acquisition & correlation processing with DC correction
114  if (i2c_transmit(&LIDAR_LITE_I2C_DEV, &lidar_lite.trans, lidar_lite.addr, 2)){
115  // transaction OK, increment status
117  }
118  }
119  break;
120  case LIDAR_LITE_REQ_READ:
122  lidar_lite.trans.buf[0] = LIDAR_LITE_READ_ADDR; // sets register pointer to results register
123  if (i2c_transmit(&LIDAR_LITE_I2C_DEV, &lidar_lite.trans, lidar_lite.addr, 1)){
124  // transaction OK, increment status
126  }
127  }
128  break;
131  // clear buffer
132  lidar_lite.trans.buf[0] = 0;
133  lidar_lite.trans.buf[1] = 0;
134  if (i2c_receive(&LIDAR_LITE_I2C_DEV, &lidar_lite.trans, lidar_lite.addr, 2)){
135  // transaction OK, increment status
137  }
138  }
139  break;
140  case LIDAR_LITE_PARSE: {
141  // filter data
142  uint32_t now_ts = get_sys_time_usec();
145  (uint32_t)((lidar_lite.trans.buf[0] << 8) | lidar_lite.trans.buf[1]));
146  lidar_lite.distance = ((float)lidar_lite.distance_raw)/100.0;
147 
148  // compensate AGL measurement for body rotation
150  float phi = stateGetNedToBodyEulers_f()->phi;
151  float theta = stateGetNedToBodyEulers_f()->theta;
152  float gain = (float)fabs( (double) (cosf(phi) * cosf(theta)));
154  }
155 
156  // send message (if requested)
157  if (lidar_lite.update_agl) {
158  AbiSendMsgAGL(AGL_LIDAR_LITE_ID, now_ts, lidar_lite.distance);
159  }
160 
161  // increment status
163  break;
164  }
165  default:
166  break;
167  }
168 }
169 
174 {
175  uint8_t trans = lidar_lite.trans.status;
177  DOWNLINK_SEND_LIDAR(DefaultChannel, DefaultDevice,
179  &status,
180  &trans);
181 }
Main include for ABI (AirBorneInterface).
#define AGL_LIDAR_LITE_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition: i2c.c:334
@ I2CTransRunning
transaction is currently ongoing
Definition: i2c.h:56
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
@ I2CTransPending
transaction is pending in queue
Definition: i2c.h:55
float phi
in radians
float theta
in radians
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1306
#define LIDAR_LITE_REG_ADDR
Definition: lidar_lite.c:48
struct MedianFilterInt lidar_lite_filter
Definition: lidar_lite.c:46
void lidar_lite_downlink(void)
Downlink message for debug.
Definition: lidar_lite.c:173
void lidar_lite_init(void)
Initialization function.
Definition: lidar_lite.c:56
void lidar_lite_event(void)
Lidar event function Basically just check the progress of the transation to prevent overruns during h...
Definition: lidar_lite.c:76
#define LIDAR_LITE_REG_VAL
Definition: lidar_lite.c:49
void lidar_lite_periodic(void)
Poll lidar for data for altitude hold 50Hz-100Hz should be enough, in theory can go faster (see the d...
Definition: lidar_lite.c:106
#define LIDAR_LITE_READ_ADDR
Definition: lidar_lite.c:50
struct LidarLite lidar_lite
Definition: lidar_lite.c:45
driver for the Lidar-Lite i2c lidar
uint32_t distance_raw
Definition: lidar_lite.h:50
@ LIDAR_LITE_REQ_READ
Definition: lidar_lite.h:41
@ LIDAR_LITE_READ_DISTANCE
Definition: lidar_lite.h:42
@ LIDAR_LITE_PARSE
Definition: lidar_lite.h:43
@ LIDAR_LITE_INIT_RANGING
Definition: lidar_lite.h:40
float distance
Definition: lidar_lite.h:51
uint8_t addr
Definition: lidar_lite.h:49
bool compensate_rotation
Definition: lidar_lite.h:54
bool update_agl
Definition: lidar_lite.h:53
struct i2c_transaction trans
Definition: lidar_lite.h:48
enum LidarLiteStatus status
Definition: lidar_lite.h:52
static void init_median_filter_i(struct MedianFilterInt *filter, uint8_t size)
Definition: median_filter.h:39
static int32_t update_median_filter_i(struct MedianFilterInt *filter, int32_t new_data)
Definition: median_filter.h:68
uint8_t status
API to get/set the generic vehicle states.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98