Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
alt_srf08.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Pascal Brisset, Antoine Drouin
3  * Copyright (C) 2002 Chris efstathiou hendrix@otenet.gr
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
30 #include "mcu_periph/i2c.h"
31 #include "alt_srf08.h"
32 #include "mcu_periph/uart.h"
33 #include "pprzlink/messages.h"
35 #include "led.h"
36 
37 
38 #ifndef SRF08_I2C_DEV
39 #define SRF08_I2C_DEV i2c0
40 #endif
41 
42 /* Global Variables */
46 
47 
48 /*###########################################################################*/
49 
50 void srf08_init(void)
51 {
52  srf08_received = false;
53  srf08_got = false;
54 
55  srf_trans.buf[0] = 0x00;
56  srf_trans.buf[1] = 0x51;
58 
63 
64  return;
65 }
66 /*###########################################################################*/
67 
69 {
70  LED_ON(2);
74 }
75 
77 void srf08_receive(void)
78 {
79  LED_OFF(2);
81  srf08_received = true;
83 }
84 
86 void srf08_read(void)
87 {
88  srf08_got = true;
90 }
91 
93 void srf08_copy(void)
94 {
95  srf08_range = srf_trans.buf[0] << 8 | srf_trans.buf[1];
96 }
97 
98 void srf08_ping()
99 {
101  while (srf_trans.status != I2CTransSuccess); /* blocking */
102 
103  srf08_receive();
104 }
105 /*###########################################################################*/
106 
108 {
109  uint8_t cnt;
110 
111  union i2c_union {
112  uint32_t rx_word;
113  uint8_t rx_byte[2];
114  } i2c;
115 
116 
117  srf_trans.buf[0] = srf08_register;
118 
119  /* get high byte msb first */
120  if (srf08_register >= 2) {
121  cnt = 2;
122  } else {
123  cnt = 1;
124  }
125 
127 
128  /* get high byte msb first */
129  if (srf08_register >= 2) {
130  i2c.rx_byte[1] = srf_trans.buf[1];
131  }
132 
133  /* get low byte msb first */
134  i2c.rx_byte[0] = srf_trans.buf[0];
135 
136  return (i2c.rx_word);
137 }
138 
139 void srf08_event(void)
140 {
141  float f = 0;
142  uint8_t i = 0;
143 
146  if (srf08_received) {
147  srf08_received = false;
148  srf08_read();
149  } else if (srf08_got) {
150  srf08_got = false;
151  srf08_copy();
152  DOWNLINK_SEND_RANGEFINDER(DefaultChannel, DefaultDevice, &srf08_range, &f, &f, &f, &f, &f, &i);
153  }
154  }
155 }
void srf08_event(void)
Definition: alt_srf08.c:139
void srf08_initiate_ranging(void)
Definition: alt_srf08.c:68
bool srf08_got
Definition: alt_srf08.c:43
#define SRF08_I2C_DEV
Definition: alt_srf08.c:39
void srf08_receive(void)
Ask the value to the device.
Definition: alt_srf08.c:77
void srf08_ping()
Definition: alt_srf08.c:98
uint16_t srf08_range
Definition: alt_srf08.c:45
void srf08_copy(void)
Copy the I2C buffer.
Definition: alt_srf08.c:93
bool srf08_received
Definition: alt_srf08.c:43
struct i2c_transaction srf_trans
Definition: alt_srf08.c:44
void srf08_init(void)
Definition: alt_srf08.c:50
uint32_t srf08_read_register(uint8_t srf08_register)
Definition: alt_srf08.c:107
void srf08_read(void)
Read values on the bus.
Definition: alt_srf08.c:86
Basic library for SRF08 telemeter.
#define SRF08_COMMAND
Definition: alt_srf08.h:72
#define SRF08_ECHO_1
Definition: alt_srf08.h:75
#define SRF08_CENTIMETERS
Definition: alt_srf08.h:68
#define SRF08_UNIT_0
Definition: alt_srf08.h:39
#define SRF08_SET_GAIN
Definition: alt_srf08.h:73
#define SRF08_MIN_GAIN
Definition: alt_srf08.h:62
#define LED_ON(i)
Definition: led_hw.h:51
#define LED_OFF(i)
Definition: led_hw.h:52
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
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:344
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
I2C transaction structure.
Definition: i2c.h:93
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
arch independent LED (Light Emitting Diodes) API
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
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
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup)
Definition: wedgebug.c:204