Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
stereocam.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Kirk Scheper
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 
30 #include "mcu_periph/uart.h"
33 #ifndef SEND_STEREO
34 #define SEND_STEREO TRUE
35 #endif
36 
37 // define coms link for stereocam
38 #define STEREO_PORT (&((UART_LINK).device))
40 #define StereoGetch() STEREO_PORT ->get_byte(STEREO_PORT->periph)
41 
42 // pervasive local variables
44 
45 
49 
50 #ifndef STEREO_BUF_SIZE
51 #define STEREO_BUF_SIZE 1024 // size of circular buffer
52 #endif
53 uint8_t ser_read_buf[STEREO_BUF_SIZE]; // circular buffer for incoming data
54 uint16_t insert_loc, extract_loc, msg_start; // place holders for buffer read and write
55 uint8_t msg_buf[STEREO_BUF_SIZE]; // define local data
56 uint8array stereocam_data = {.len = 0, .data = msg_buf, .fresh = 0, .matrix_width = 0, .matrix_height = 0}; // buffer used to contain image without line endings
57 
58 #define BASELINE_STEREO_MM 60.0
59 #define BRANDSPUNTSAFSTAND_STEREO 118.0*6
60 
61 extern void stereocam_disparity_to_meters(uint8_t *disparity, float *distancesMeters, int lengthArray)
62 {
63 
64  int indexArray = 0;
65  for (indexArray = 0; indexArray < lengthArray; indexArray++) {
66  if (disparity[indexArray] != 0) {
67  distancesMeters[indexArray] = ((BASELINE_STEREO_MM * BRANDSPUNTSAFSTAND_STEREO / (float)disparity[indexArray] - 18.0)) /
68  1000;
69  // printf("%i, distanceMeters: %f \n",indexArray,distancesMeters[indexArray]);
70  } else {
71  distancesMeters[indexArray] = 1000;
72  }
73  }
74 }
75 
76 extern void stereocam_start(void)
77 {
78  // initialize local variables
79  msgProperties = (MsgProperties) {0, 0, 0};
80 
81  insert_loc = 0;
82  extract_loc = 0;
83  msg_start = 0;
84 
85  //sys_time_init();
86  freq_counter = 0;
87  frequency = 0;
89 
90  stereocam_data.fresh = 0;
91 }
92 
93 extern void stereocam_stop(void)
94 {
95 }
96 
97 extern void stereocam_periodic(void)
98 {
99  // read all data from the stereo com link, check that don't overtake extract
100  while (linkdev->char_available(linkdev->periph) && stereoprot_add(insert_loc, 1, STEREO_BUF_SIZE) != extract_loc) {
102  &stereocam_data.fresh, &stereocam_data.len, &stereocam_data.matrix_width, &stereocam_data.matrix_height)) {
103  freq_counter++;
104  if ((sys_time.nb_tick - previous_time) > sys_time.ticks_per_sec) { // 1s has past
106  freq_counter = 0;
108  }
109 #if SEND_STEREO
110  if (stereocam_data.len > 100) {
111  DOWNLINK_SEND_STEREO_IMG(DefaultChannel, DefaultDevice, &frequency, &(stereocam_data.len), 100, stereocam_data.data);
112 
113  } else {
114  DOWNLINK_SEND_STEREO_IMG(DefaultChannel, DefaultDevice, &frequency, &(stereocam_data.len), stereocam_data.len,
115  stereocam_data.data);
116 
117  }
118 #endif
119  }
120  }
121 }
unsigned short uint16_t
Definition: types.h:16
uint8_t matrix_height
Definition: stereocam.h:37
struct MsgProperties MsgProperties
uint32_t previous_time
Definition: stereocam.c:48
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
uint16_t extract_loc
Definition: stereocam.c:54
Periodic telemetry system header (includes downlink utility and generated code).
uint8_t fresh
Definition: stereocam.h:35
void stereocam_start(void)
Definition: stereocam.c:76
uint16_t freq_counter
Definition: stereocam.c:46
uint8_t frequency
Definition: stereocam.c:47
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:71
standard protocol for TUDelft stereocamera data transfer
uint32_t ticks_per_sec
sys_time ticks per second (SYS_TIME_FREQUENCY)
Definition: sys_time.h:75
uint16_t insert_loc
Definition: stereocam.c:54
#define STEREO_BUF_SIZE
Definition: stereocam.c:51
uint16_t msg_start
Definition: stereocam.c:54
interface to the TU Delft serial stereocam
unsigned long uint32_t
Definition: types.h:18
void stereocam_stop(void)
Definition: stereocam.c:93
uint8array stereocam_data
Definition: stereocam.c:56
MsgProperties msgProperties
Definition: stereocam.c:43
void stereocam_disparity_to_meters(uint8_t *disparity, float *distancesMeters, int lengthArray)
Definition: stereocam.c:61
uint8_t len
Definition: stereocam.h:33
unsigned char uint8_t
Definition: types.h:14
#define StereoGetch()
Definition: stereocam.c:40
#define STEREO_PORT
Definition: stereocam.c:38
#define BRANDSPUNTSAFSTAND_STEREO
Definition: stereocam.c:59
uint16_t stereoprot_add(uint16_t counter, uint16_t i, uint16_t buffer_size)
Increment circular buffer counter by i.
uint8_t handleStereoPackage(uint8_t newByte, uint16_t buffer_size, uint16_t *insert_loc, uint16_t *extract_loc, uint16_t *msg_start, uint8_t *msg_buf, uint8_t *ser_read_buf, uint8_t *stereocam_datadata_new, uint8_t *stereocam_datalen, uint8_t *stereocam_data_matrix_width, uint8_t *stereocam_data_matrix_height)
Get all available data from stereo com link and decode any complete messages.
struct link_device * linkdev
Definition: stereocam.c:39
uint8_t matrix_width
Definition: stereocam.h:36
uint8_t ser_read_buf[STEREO_BUF_SIZE]
Definition: stereocam.c:53
uint8_t msg_buf[STEREO_BUF_SIZE]
Definition: stereocam.c:55
#define BASELINE_STEREO_MM
Definition: stereocam.c:58
uint8_t * data
Definition: stereocam.h:34
void stereocam_periodic(void)
Definition: stereocam.c:97