Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
catia.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include <errno.h>
5 
6 #include "serial.h"
7 #include "chdk_pipe.h"
8 #include "protocol.h"
9 
10 #define MAX_FILENAME 512
11 #define MAX_PROCESSING_THREADS 8
12 #define MAX_IMAGE_BUFFERS 25
13 #define IMAGE_SIZE 70
14 // Search&Rescue Onboard Detection Application
15 #define SODA "/root/develop/allthings_obc2014/src/soda/soda"
16 
17 static void *handle_msg_shoot(void *ptr);
18 static inline void send_msg_image_buffer(void);
19 static inline void send_msg_status(void);
20 
23 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
24 
25 int main(int argc, char *argv[])
26 {
27  pthread_t shooting_threads[MAX_PROCESSING_THREADS];
28  char c;
29  int i;
30 
31  // Initialization
32  printf("CATIA:\tStarting Camera Application Triggering Image Analysis\n");
34  int ret = serial_init("/dev/ttySAC0");
35  if (ret < 0) {
36  printf("CATIA:\tfailed to open /dev/ttySAC0\n");
37  return -1;
38  }
39  pthread_mutex_init(&mut, NULL);
40  socket_init(1);
41 
42  // Initial settings
43  is_shooting = 0;
45  image_idx = 0;
46  image_count = 0;
47  shooting_idx = 0;
48  shooting_count = 0;
50 
51  // MAIN loop
52  while (1) {
53 
54  // Read the serial
55  if (read(fd, &c, 1) > 0) {
57  } else if (errno != 11) {
58  printf("CATIA:\nSerial error: %d\n" , errno);
59  }
60 
61  // Parse serial commands
63  // Process Only Once
65 
66  // Shoot an image if not busy
68  // Parse the shoot message
69  union dc_shot_union *shoot = (union dc_shot_union *) malloc(sizeof(union dc_shot_union));
70  for (i = 0; i < MORA_SHOOT_MSG_SIZE; i++) {
71  shoot->bin[i] = mora_protocol.payload[i];
72  }
73  printf("CATIA:\tSHOT %d,%d\n", shoot->data.nr, shoot->data.phi);
74 
75  pthread_create(&shooting_threads[(shooting_idx++ % MAX_PROCESSING_THREADS)], NULL, handle_msg_shoot, (void *)shoot);
77  }
78 
79  // Fill the image buffer (happens busy because needs fd anyway)
82  }
83  }
84 
85  // Read the socket
88 
90  image_count++;
91  }
92  }
93 
94  }
95 
96  // Close
97  close(fd);
99 
100  printf("CATIA:\tShutdown\n");
101  return 0;
102 }
103 
104 static void *handle_msg_shoot(void *ptr)
105 {
106  char filename[MAX_FILENAME], soda_call[512];
107  union dc_shot_union *shoot = (union dc_shot_union *) ptr;
108 
109  // Test if can shoot
110  pthread_mutex_lock(&mut);
111  if (is_shooting) {
112  pthread_mutex_unlock(&mut);
113  printf("CATIA-%d:\tShooting: too fast\n", shoot->data.nr);
114 
115  free(shoot);
116  return NULL;
117  }
118 
119  is_shooting = 1;
120  shooting_count++;
122  pthread_mutex_unlock(&mut);
123 
124  printf("CATIA-%d:\tShooting: start\n", shoot->data.nr);
125  chdk_pipe_shoot(filename);
126  printf("CATIA-%d:\tShooting: got image %s\n", shoot->data.nr, filename);
127 
128  pthread_mutex_lock(&mut);
129  is_shooting = 0;
130  pthread_mutex_unlock(&mut);
131 
132  //Parse the image
133  sprintf(soda_call, "%s %s %d %d %d %d %d %d %d %d %d %d", SODA, filename,
134  shoot->data.nr, shoot->data.lat, shoot->data.lon, shoot->data.alt, shoot->data.phi, shoot->data.theta, shoot->data.psi,
135  shoot->data.vground, shoot->data.course, shoot->data.groundalt);
136  printf("CATIA-%d:\tCalling '%s'\n", shoot->data.nr, soda_call);
137  short int ret = system(soda_call);
138  printf("CATIA-%d:\tShooting: soda return %d of image %s\n", shoot->data.nr, ret, filename);
139 
140  pthread_mutex_lock(&mut);
142  pthread_mutex_unlock(&mut);
143 
144  free(shoot);
145 }
146 
147 static inline void send_msg_image_buffer(void)
148 {
149  int i;
150 
151  // Check if image is available
152  if (image_count > 0) {
153  printf("CATIA:\thandle_msg_buffer: Send %d\n", image_idx);
154  // Send the image
156  image_count--;
157 
159  for (i = 0; i < IMAGE_SIZE; i++) {
161  }
162  MoraTrailer();
163  }
164 }
165 
166 static inline void send_msg_status(void)
167 {
168  int i;
169  struct mora_status_struct status_msg;
170  char *buffer = (char *) &status_msg;
171 
172  pthread_mutex_lock(&mut);
173  status_msg.cpu = 0;
174  status_msg.threads = shooting_thread_count;
175  status_msg.shots = shooting_count;
176  status_msg.extra = 0;
177  pthread_mutex_unlock(&mut);
178 
180  for (i = 0; i < MORA_STATUS_MSG_SIZE; i++) {
181  MoraPutUint8(buffer[i]);
182  }
183  MoraTrailer();
184 }
c
VIC slots used for the LPC2148 define name e g gps UART1_VIC_SLOT e g modem SPI1_VIC_SLOT SPI1 in mcu_periph spi_arch c or spi_slave_hs_arch c(and some others not using the SPI peripheral yet..) I2C0_VIC_SLOT 8 mcu_periph/i2c_arch.c I2C1_VIC_SLOT 9 mcu_periph/i2c_arch.c USB_VIC_SLOT 10 usb
socket_init
void socket_init(int is_server)
Definition: socket.c:19
MORA_SHOOT
#define MORA_SHOOT
Definition: protocol.h:47
MoraHeader
#define MoraHeader(msg_id, payload_len)
Definition: protocol.h:109
protocol.h
MORA_PAYLOAD
#define MORA_PAYLOAD
Definition: protocol.h:74
mora_transport::msg_id
uint8_t msg_id
Definition: protocol.h:134
MORA_STATUS
#define MORA_STATUS
Definition: protocol.h:80
MORA_STATUS_MSG_SIZE
#define MORA_STATUS_MSG_SIZE
Definition: protocol.h:81
chdk_pipe_deinit
void chdk_pipe_deinit(void)
Deinitialize CHDK pipe.
Definition: chdk_pipe.c:79
is_shooting
static volatile int is_shooting
Definition: catia.c:21
MAX_IMAGE_BUFFERS
#define MAX_IMAGE_BUFFERS
Definition: catia.c:12
MORA_PAYLOAD_MSG_SIZE
#define MORA_PAYLOAD_MSG_SIZE
Definition: protocol.h:75
image_idx
static volatile int image_idx
Definition: catia.c:21
chdk_pipe_shoot
void chdk_pipe_shoot(char *filename)
Shoot an image.
Definition: chdk_pipe.c:92
MORA_BUFFER_EMPTY
#define MORA_BUFFER_EMPTY
Definition: protocol.h:70
mora_transport::payload
uint8_t payload[256]
Definition: protocol.h:132
MORA_SHOOT_MSG_SIZE
#define MORA_SHOOT_MSG_SIZE
Definition: protocol.h:48
serial_init
int serial_init(char *port_name)
Definition: serial.c:28
MoraTrailer
#define MoraTrailer()
Definition: protocol.h:117
IMAGE_SIZE
#define IMAGE_SIZE
Definition: catia.c:13
mora_protocol
struct mora_transport mora_protocol
Definition: protocol.c:18
send_msg_status
static void send_msg_status(void)
Definition: catia.c:166
mut
static pthread_mutex_t mut
Definition: catia.c:23
dc_shot_union::i
int32_t i[10]
Definition: protocol.h:67
MoraPutUint8
#define MoraPutUint8(_byte)
Definition: protocol.h:103
chdk_pipe.h
dc_shot_union::data
struct dc_shot_union::@288 data
parse_mora
void parse_mora(struct mora_transport *t, uint8_t c)
Definition: protocol.c:20
shooting_count
static volatile int shooting_count
Definition: catia.c:21
MAX_FILENAME
#define MAX_FILENAME
Definition: catia.c:10
mora_transport::msg_received
bool msg_received
Definition: protocol.h:135
serial.h
MAX_PROCESSING_THREADS
#define MAX_PROCESSING_THREADS
Definition: catia.c:11
fd
int fd
Definition: serial.c:26
shooting_idx
static volatile int shooting_idx
Definition: catia.c:21
image_buffer
static char image_buffer[MAX_IMAGE_BUFFERS][IMAGE_SIZE]
Definition: catia.c:22
shooting_thread_count
static volatile int shooting_thread_count
Definition: catia.c:21
main
int main(int argc, char *argv[])
Definition: catia.c:25
dc_shot_union::bin
uint8_t bin[MORA_SHOOT_MSG_SIZE]
Definition: protocol.h:66
dc_shot_union
Definition: protocol.h:53
socket_recv
int socket_recv(char *buffer, int len)
Definition: socket.c:41
SODA
#define SODA
Definition: catia.c:15
mora_transport::status
uint8_t status
Definition: protocol.h:138
handle_msg_shoot
static void * handle_msg_shoot(void *ptr)
Definition: catia.c:104
image_count
static volatile int image_count
Definition: catia.c:21
chdk_pipe_init
void chdk_pipe_init(void)
Initialize the CHDK pipe.
Definition: chdk_pipe.c:50
send_msg_image_buffer
static void send_msg_image_buffer(void)
Definition: catia.c:147