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
hackhd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger
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 
42 #include "generated/modules.h"
43 #include "generated/airframe.h"
44 #include "mcu_periph/gpio.h"
45 #include "mcu_periph/sys_time.h"
46 
48 #define HACKHD_PUSH gpio_clear
49 #define HACKHD_RELEASE gpio_set
50 
51 #ifndef HACKHD_GPIO
52 #error HACKHD: Please specify at least a HACKHD_GPIO (e.g. <define name="HACKHD_GPIO" value="GPIOC,GPIO5"/>)
53 #endif
54 
55 static inline uint32_t port_of_gpio(uint32_t port, uint16_t __attribute__((unused)) pin) { return port; }
56 static inline uint16_t pin_of_gpio(uint32_t __attribute__((unused)) port, uint16_t pin) { return pin; }
57 
59 #define HACKHD_POWER_DELAY 5.
60 
62 #define HACKHD_RECORD_DELAY 0.2
63 
67 #define HACKHD_LOG_DELAY 1000
68 
72 #define HACKHD_TIMER_OF_DELAY(_delay) ((uint32_t)(_delay * HACKHD_PERIODIC_FREQ))
73 
77 #ifndef HACKHD_AUTOSHOOT_DELAY
78 #define HACKHD_AUTOSHOOT_DELAY 4.0
79 #endif
80 #define HACKHD_AUTOSHOOT_TIMER_OF_DELAY(_delay) ((uint32_t)(_delay * HACKHD_AUTOSHOOT_FREQ))
81 
83 #if HACKHD_SYNC_SEND
84 
85 #include "mcu_periph/uart.h"
86 #include "messages.h"
88 #include "state.h"
89 #include "subsystems/gps.h"
90 
91 static inline void hackhd_send_shot_position(void)
92 {
93  // angles in decideg
94  int16_t phi = DegOfRad(stateGetNedToBodyEulers_f()->phi * 10.0f);
95  int16_t theta = DegOfRad(stateGetNedToBodyEulers_f()->theta * 10.0f);
96  int16_t psi = DegOfRad(stateGetNedToBodyEulers_f()->psi * 10.0f);
97  // course in decideg
98  int16_t course = DegOfRad(stateGetHorizontalSpeedDir_f()) * 10;
99  // ground speed in cm/s
101 
102  DOWNLINK_SEND_DC_SHOT(DefaultChannel, DefaultDevice,
103  &hackhd.photo_nr,
104  &stateGetPositionLla_i()->lat,
105  &stateGetPositionLla_i()->lon,
106  &stateGetPositionLla_i()->alt,
107  &gps.hmsl,
108  &phi,
109  &theta,
110  &psi,
111  &course,
112  &speed,
113  &gps.tow);
114 }
115 #endif
116 
117 #if HACKHD_LOG
118 #include "sdLog.h"
119 #include "subsystems/chibios-libopencm3/chibios_sdlog.h"
120 #include "state.h"
121 #include "subsystems/gps.h"
122 
123 static inline void hackhd_log_shot_position(void)
124 {
125  // For unknown reason, the first shot is not taken
126  // so we start logging at photo_nr = 1
127  if (pprzLogFile != -1 && hackhd.photo_nr > 0) {
128  struct FloatEulers att = *stateGetNedToBodyEulers_f();
129  struct EnuCoor_f pos = *stateGetPositionEnu_f();
130  uint32_t time = get_sys_time_msec();
131  sdLogWriteLog(pprzLogFile, "%d %d %d %d %d %d %d %u\n",
133  (int32_t)(DegOfRad(att.phi * 10.0f)),
134  (int32_t)(DegOfRad(att.theta * 10.0f)),
135  (int32_t)(DegOfRad(att.psi * 10.0f)),
136  (int32_t)(pos.x * 100.0f),
137  (int32_t)(pos.y * 100.0f),
138  (int32_t)(pos.z * 100.0f),
139  time);
140  }
141 }
142 #endif
143 
144 struct HackHD hackhd;
145 
146 void hackhd_init(void)
147 {
149  hackhd.timer = 0;
150  hackhd.photo_nr = 0;
151  hackhd.autoshoot = 0;
152  hackhd.log_delay = 0;
153 
154 #ifndef SITL
155  gpio_setup_output(HACKHD_GPIO);
156  // set gpio as open-drain, only possible on stm32f4
157  gpio_set_output_options(
158  port_of_gpio(HACKHD_GPIO),
159  GPIO_OTYPE_OD,
160  GPIO_OSPEED_25MHZ,
161  pin_of_gpio(HACKHD_GPIO));
162  HACKHD_RELEASE(HACKHD_GPIO);
163 #endif
164 }
165 
166 void hackhd_periodic(void)
167 {
168  if (hackhd.timer) {
169  hackhd.timer--;
170  } else {
171  HACKHD_RELEASE(HACKHD_GPIO);
172  }
173  // test log delay if set
174  if (hackhd.log_delay) {
175 #ifndef SITL
177 #endif
178 #if HACKHD_LOG
179  hackhd_log_shot_position();
180 #endif
181 #if HACKHD_SYNC_SEND
182  hackhd_send_shot_position();
183 #endif
184  // increment photo
185  hackhd.photo_nr++;
186  // unset log delay
187  hackhd.log_delay = 0;
188 #ifndef SITL
189  }
190 #endif
191  }
192 }
193 
194 /* Command the powering and recording */
196 {
197  hackhd.status = cmd;
198  switch (cmd) {
199  case HACKHD_POWER_ON:
200  case HACKHD_POWER_OFF:
202  HACKHD_PUSH(HACKHD_GPIO);
203  break;
204  case HACKHD_START_RECORD:
205  case HACKHD_STOP_RECORD:
207  HACKHD_PUSH(HACKHD_GPIO);
208  break;
209  case HACKHD_SHOOT:
212  HACKHD_PUSH(HACKHD_GPIO);
215  break;
216  default:
217  break;
218  }
219 }
220 
222 {
223 // at least wait a minimum time before two shoots
224  if (hackhd.autoshoot) {
225  hackhd.autoshoot--;
226  } else {
227  // test distance if needed
228  // or take picture if first of the sequence
229 #ifdef HACKHD_AUTOSHOOT_DIST
230  struct EnuCoor_f pos = *stateGetPositionEnu_f();
231  struct FloatVect2 d_pos;
232  d_pos.x = pos.x - hackhd.last_shot_pos.x;
233  d_pos.y = pos.y - hackhd.last_shot_pos.y;
234  if (VECT2_NORM2(d_pos) > (HACKHD_AUTOSHOOT_DIST * HACKHD_AUTOSHOOT_DIST)
236 #endif
237  // take a picture
239  // reset timer
241 #ifdef HACKHD_AUTOSHOOT_DIST
242  }
243 #endif
244  }
245 }
246 
248 {
249  // start taking a picture immediately
250  hackhd.autoshoot = 0;
252 }
253 
static uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.h:49
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void hackhd_periodic(void)
Definition: hackhd.c:166
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:912
float phi
in radians
hackhd_status
Definition: hackhd.h:48
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1114
int16_t photo_nr
Definition: hackhd.h:61
vector in East North Up coordinates Units: meters
void hackhd_init(void)
Definition: hackhd.c:146
Some architecture independent helper functions for GPIOs.
void hackhd_autoshoot(void)
Definition: hackhd.c:221
struct HackHD hackhd
send report
Definition: hackhd.c:144
float psi
in radians
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:702
uint32_t log_delay
Definition: hackhd.h:64
struct EnuCoor_f last_shot_pos
Definition: hackhd.h:63
euler angles
#define HACKHD_RECORD_DELAY
time in seconds to start/stop recording or take a picture
Definition: hackhd.c:62
float theta
in radians
int32_t hmsl
height above mean sea level in mm
Definition: gps.h:71
#define HACKHD_POWER_DELAY
time in seconds to press the button to power on/off
Definition: hackhd.c:59
float x
in meters
Architecture independent timing functions.
uint32_t tow
GPS time of week in ms.
Definition: gps.h:84
Device independent GPS code (interface)
void hackhd_command(enum hackhd_status cmd)
Definition: hackhd.c:195
enum hackhd_status status
Definition: hackhd.h:59
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
uint32_t timer
Definition: hackhd.h:60
#define VECT2_NORM2(_v)
Definition: pprz_algebra.h:117
static uint32_t port_of_gpio(uint32_t port, uint16_t pin)
Definition: hackhd.c:55
#define HACKHD_AUTOSHOOT_TIMER_OF_DELAY(_delay)
Definition: hackhd.c:80
#define HACKHD_LOG_DELAY
delay in milli-seconds before logging after a shot this has been estimated to 1s
Definition: hackhd.c:67
signed long int32_t
Definition: types.h:19
static float stateGetHorizontalSpeedDir_f(void)
Get dir of horizontal ground speed (float).
Definition: state.h:921
#define HACKHD_AUTOSHOOT_DELAY
autoshoot timer delay based on periodic freq from modules.h
Definition: hackhd.c:78
void hackhd_autoshoot_start(void)
Definition: hackhd.c:247
Digital video/photo recorder HackHD control.
API to get/set the generic vehicle states.
float z
in meters
uint32_t autoshoot
Definition: hackhd.h:62
#define HACKHD_TIMER_OF_DELAY(_delay)
get timer from delay based on periodic freq from modules.h
Definition: hackhd.c:72
#define HACKHD_PUSH
Trigger button is active low.
Definition: hackhd.c:48
static uint16_t pin_of_gpio(uint32_t port, uint16_t pin)
Definition: hackhd.c:56
Definition: hackhd.h:58
#define HACKHD_RELEASE
Definition: hackhd.c:49
float y
in meters
struct GpsState gps
global GPS state
Definition: gps.c:41
void gpio_setup_output(uint32_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_ardrone.c:102
static struct LlaCoor_i * stateGetPositionLla_i(void)
Get position in LLA coordinates (int).
Definition: state.h:666