-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
dc.c
289 lines (251 loc) · 7.84 KB
/
dc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright (C) 2010-2014 The Paparazzi Team
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/** @file modules/digital_cam/dc.c
* Standard Digital Camera Control Interface.
*
* -Standard IO
* -I2C Control
*
* Usage: (from the flight plan, the settings or any airborne code):
* - dc_send_command( )
* - set the appropriate autoshoot mode (off/time/distance/trigger)
* - use the module periodic function to set the autorepeat interval
* - define SENSOR_SYNC_SEND to get the DC_SHOT_MESSAGE on every SHOOT command
*/
#include "dc.h"
// for waypoints, include correct header until we have unified API
#ifdef AP
#include "subsystems/navigation/common_nav.h"
#else
#include "firmwares/rotorcraft/navigation.h"
#endif
/** default quartersec perioid = 0.5s */
#ifndef DC_AUTOSHOOT_QUARTERSEC_PERIOD
#define DC_AUTOSHOOT_QUARTERSEC_PERIOD 2
#endif
/** default distance interval for distance mode: 50m */
#ifndef DC_AUTOSHOOT_DISTANCE_INTERVAL
#define DC_AUTOSHOOT_DISTANCE_INTERVAL 50
#endif
/** default distance interval for survey mode: 50m */
#ifndef DC_AUTOSHOOT_SURVEY_INTERVAL
#define DC_AUTOSHOOT_SURVEY_INTERVAL 50
#endif
// Variables with boot defaults
dc_autoshoot_type dc_autoshoot = DC_AUTOSHOOT_STOP;
uint16_t dc_gps_count = 0;
uint8_t dc_cam_tracing = 1;
float dc_cam_angle = 0;
float dc_circle_interval = 0;
float dc_circle_start_angle = 0;
float dc_circle_last_block = 0;
float dc_circle_max_blocks = 0;
float dc_survey_interval = DC_AUTOSHOOT_SURVEY_INTERVAL;
float dc_gps_next_dist = 0;
float dc_gps_x = 0;
float dc_gps_y = 0;
static struct FloatVect2 last_shot_pos = {0.0, 0.0};
float dc_distance_interval;
uint8_t dc_autoshoot_quartersec_period;
/** by default send DC_SHOT message when photo was taken */
#ifndef DC_SHOT_SYNC_SEND
#define DC_SHOT_SYNC_SEND 1
#endif
#if DC_SHOT_SYNC_SEND
uint16_t dc_photo_nr = 0;
#include "mcu_periph/uart.h"
#include "messages.h"
#include "subsystems/datalink/downlink.h"
#include "state.h"
#include "subsystems/gps.h"
void dc_send_shot_position(void)
{
int16_t phi = DegOfRad(stateGetNedToBodyEulers_f()->phi*10.0f);
int16_t theta = DegOfRad(stateGetNedToBodyEulers_f()->theta*10.0f);
// course in decideg
int16_t course = DegOfRad(*stateGetHorizontalSpeedDir_f()) * 10;
// ground speed in cm/s
uint16_t speed = (*stateGetHorizontalSpeedNorm_f()) * 10;
int16_t photo_nr = -1;
if (dc_photo_nr < DC_IMAGE_BUFFER) {
dc_photo_nr++;
photo_nr = dc_photo_nr;
}
DOWNLINK_SEND_DC_SHOT(DefaultChannel, DefaultDevice,
&photo_nr,
&stateGetPositionLla_i()->lat,
&stateGetPositionLla_i()->lon,
&stateGetPositionLla_i()->alt,
&phi,
&theta,
&course,
&speed,
&gps.tow);
}
#else
void dc_send_shot_position(void)
{
}
#endif /* DC_SHOT_SYNC_SEND */
void dc_init(void)
{
dc_autoshoot = DC_AUTOSHOOT_STOP;
dc_autoshoot_quartersec_period = DC_AUTOSHOOT_QUARTERSEC_PERIOD;
dc_distance_interval = DC_AUTOSHOOT_DISTANCE_INTERVAL;
}
uint8_t dc_info(void) {
#ifdef DOWNLINK_SEND_DC_INFO
float course = DegOfRad(stateGetNedToBodyEulers_f()->psi);
int16_t mode = dc_autoshoot;
DOWNLINK_SEND_DC_INFO(DefaultChannel, DefaultDevice,
&mode,
&stateGetPositionLla_i()->lat,
&stateGetPositionLla_i()->lon,
&stateGetPositionLla_i()->alt,
&course,
&dc_photo_nr,
&dc_survey_interval,
&dc_gps_next_dist,
&dc_gps_x,
&dc_gps_y,
&dc_circle_start_angle,
&dc_circle_interval,
&dc_circle_last_block,
&dc_gps_count,
&dc_autoshoot_quartersec_period);
#endif
return 0;
}
/* shoot on distance */
uint8_t dc_distance(float interval) {
dc_autoshoot = DC_AUTOSHOOT_DISTANCE;
dc_gps_count = 0;
dc_distance_interval = interval;
last_shot_pos.x = 0;
last_shot_pos.y = 0;
dc_info();
return 0;
}
/* shoot on circle */
uint8_t dc_circle(float interval, float start) {
dc_autoshoot = DC_AUTOSHOOT_CIRCLE;
dc_gps_count = 0;
dc_circle_interval = fmodf(fmaxf(interval, 1.), 360.);
if(start == DC_IGNORE) {
start = DegOfRad(stateGetNedToBodyEulers_f()->psi);
}
dc_circle_start_angle = fmodf(start, 360.);
if (start < 0.)
start += 360.;
//dc_circle_last_block = floorf(dc_circle_start_angle/dc_circle_interval);
dc_circle_last_block = 0;
dc_circle_max_blocks = floorf(360./dc_circle_interval);
dc_info();
return 0;
}
/* shoot on survey */
uint8_t dc_survey(float interval, float x, float y) {
dc_autoshoot = DC_AUTOSHOOT_SURVEY;
dc_gps_count = 0;
dc_survey_interval = interval;
if (x == DC_IGNORE && y == DC_IGNORE) {
dc_gps_x = stateGetPositionEnu_f()->x;
dc_gps_y = stateGetPositionEnu_f()->y;
} else if (y == DC_IGNORE) {
uint8_t wp = (uint8_t)x;
dc_gps_x = WaypointX(wp);
dc_gps_y = WaypointY(wp);
} else {
dc_gps_x = x;
dc_gps_y = y;
}
dc_gps_next_dist = 0;
dc_info();
return 0;
}
uint8_t dc_stop(void) {
dc_autoshoot = DC_AUTOSHOOT_STOP;
dc_info();
return 0;
}
static float dim_mod(float a, float b, float m) {
if (a < b) {
float tmp = a;
a = b;
b = tmp;
}
return fminf(a-b, b+m-a);
}
void dc_periodic_4Hz(void)
{
static uint8_t dc_shutter_timer = 0;
switch (dc_autoshoot) {
case DC_AUTOSHOOT_PERIODIC:
if (dc_shutter_timer) {
dc_shutter_timer--;
} else {
dc_shutter_timer = dc_autoshoot_quartersec_period;
dc_send_command(DC_SHOOT);
}
break;
case DC_AUTOSHOOT_DISTANCE:
{
struct FloatVect2 cur_pos;
cur_pos.x = stateGetPositionEnu_f()->x;
cur_pos.y = stateGetPositionEnu_f()->y;
struct FloatVect2 delta_pos;
VECT2_DIFF(delta_pos, cur_pos, last_shot_pos);
float dist_to_last_shot = float_vect2_norm(&delta_pos);
if (dist_to_last_shot > dc_distance_interval) {
dc_gps_count++;
dc_send_command(DC_SHOOT);
VECT2_COPY(last_shot_pos, cur_pos);
}
}
break;
case DC_AUTOSHOOT_CIRCLE:
{
float course = DegOfRad(stateGetNedToBodyEulers_f()->psi) - dc_circle_start_angle;
if (course < 0.)
course += 360.;
float current_block = floorf(course/dc_circle_interval);
if (dim_mod(current_block, dc_circle_last_block, dc_circle_max_blocks) == 1) {
dc_gps_count++;
dc_circle_last_block = current_block;
dc_send_command(DC_SHOOT);
}
}
break;
case DC_AUTOSHOOT_SURVEY:
{
float dist_x = dc_gps_x - stateGetPositionEnu_f()->x;
float dist_y = dc_gps_y - stateGetPositionEnu_f()->y;
if (dist_x*dist_x + dist_y*dist_y >= dc_gps_next_dist*dc_gps_next_dist) {
dc_gps_next_dist += dc_survey_interval;
dc_gps_count++;
dc_send_command(DC_SHOOT);
}
}
break;
default:
dc_autoshoot = DC_AUTOSHOOT_STOP;
}
}