Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "subsystems/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 
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
144  (uint32_t)((lidar_lite.trans.buf[0] << 8) | lidar_lite.trans.buf[1]));
145  lidar_lite.distance = ((float)lidar_lite.distance_raw)/100.0;
146 
147  // compensate AGL measurement for body rotation
149  float phi = stateGetNedToBodyEulers_f()->phi;
150  float theta = stateGetNedToBodyEulers_f()->theta;
151  float gain = (float)fabs( (double) (cosf(phi) * cosf(theta)));
153  }
154 
155  // send message (if requested)
156  if (lidar_lite.update_agl) {
157  AbiSendMsgAGL(AGL_LIDAR_LITE_ID, lidar_lite.distance);
158  }
159 
160  // increment status
162  break;
163  default:
164  break;
165  }
166 }
167 
172 {
173  uint8_t trans = lidar_lite.trans.status;
175  DOWNLINK_SEND_LIDAR(DefaultChannel, DefaultDevice,
177  &status,
178  &trans);
179 }
#define LIDAR_LITE_READ_ADDR
Definition: lidar_lite.c:50
status
Definition: anemotaxis.c:10
float phi
in radians
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
transaction successfully finished by I2C driver
Definition: i2c.h:57
bool update_agl
Definition: lidar_lite.h:53
Main include for ABI (AirBorneInterface).
bool compensate_rotation
Definition: lidar_lite.h:54
float distance
Definition: lidar_lite.h:51
void lidar_lite_downlink(void)
Downlink message for debug.
Definition: lidar_lite.c:171
driver for the Lidar-Lite i2c lidar
static int32_t update_median_filter_i(struct MedianFilterInt *filter, int32_t new_data)
Definition: median_filter.h:68
struct LidarLite lidar_lite
Definition: lidar_lite.c:45
float theta
in radians
struct i2c_transaction trans
Definition: lidar_lite.h:48
transaction set to done by user level
Definition: i2c.h:59
unsigned long uint32_t
Definition: types.h:18
#define LIDAR_LITE_REG_VAL
Definition: lidar_lite.c:49
transaction failed
Definition: i2c.h:58
uint32_t distance_raw
Definition: lidar_lite.h:50
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:258
uint8_t addr
Definition: lidar_lite.h:49
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
enum LidarLiteStatus status
Definition: lidar_lite.h:52
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
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
static void init_median_filter_i(struct MedianFilterInt *filter, uint8_t size)
Definition: median_filter.h:39
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:268
transaction is currently ongoing
Definition: i2c.h:56
void lidar_lite_init(void)
Initialization function.
Definition: lidar_lite.c:56
#define LIDAR_LITE_REG_ADDR
Definition: lidar_lite.c:48
struct MedianFilterInt lidar_lite_filter
Definition: lidar_lite.c:46
#define AGL_LIDAR_LITE_ID
transaction is pending in queue
Definition: i2c.h:55