Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
cloud_sim.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
27 #include "modules/nav/common_nav.h"
28 #include "generated/flight_plan.h"
29 #include "generated/airframe.h"
30 #include "modules/core/abi.h"
31 #include "state.h"
32 
33 // default radius in WP mode
34 #ifndef CLOUD_SIM_RADIUS
35 #define CLOUD_SIM_RADIUS 150.f
36 #endif
37 
38 // default mode
39 #ifndef CLOUD_SIM_MODE
40 #define CLOUD_SIM_MODE CLOUD_SIM_WP
41 #endif
42 
43 // use CLOUD waypoint if any by default, 0 (none) otherwise
44 #if (defined WP_CLOUD) && !(defined CLOUD_SIM_WP_ID)
45 #define CLOUD_SIM_WP_ID WP_CLOUD
46 #elif !(defined CLOUD_SIM_WP_ID)
47 #define CLOUD_SIM_WP_ID 0
48 #endif
50 
51 #if (defined CLOUD_SIM_WP_POLYGON)
52 static uint8_t cloud_sim_polygon[] = CLOUD_SIM_WP_POLYGON;
53 #ifndef CLOUD_SIM_WPS_NB
54 #pragma error "CLOUD_SIM: please define CLOUD_SIM_WPS_NB for custom polygon"
55 #endif
56 #elif (defined SECTOR_CLOUD)
57 static uint8_t cloud_sim_polygon[] = SECTOR_CLOUD;
58 #define CLOUD_SIM_WPS_NB SECTOR_CLOUD_NB
59 #else
60 #define CLOUD_SIM_WPS_NB 1
62 #endif
63 
64 #ifndef CLOUD_SIM_SPEED_X
65 #define CLOUD_SIM_SPEED_X 0.f
66 #endif
67 
68 #ifndef CLOUD_SIM_SPEED_Y
69 #define CLOUD_SIM_SPEED_Y 0.f
70 #endif
71 
72 struct CloudSim cloud_sim;
73 
74 /*********************
75  * Utility functions *
76  *********************/
77 
78 static float distance_to_wp(struct EnuCoor_f * pos, uint8_t id)
79 {
80  if (id > 0 && id < nb_waypoint) {
81  struct FloatVect2 diff = { pos->x - waypoints[id].x, pos->y - waypoints[id].y };
82  return float_vect2_norm(&diff);
83  } else {
84  return -1.f; // invalid WP id
85  }
86 }
87 
88 /**********************
89  * External functions *
90  **********************/
91 
92 void cloud_sim_init(void)
93 {
94  cloud_sim.reset = false;
99 }
100 
103 {
104  uint32_t stamp = get_sys_time_usec();
105  struct EnuCoor_f * pos = stateGetPositionEnu_f();
106  uint8_t inside = 0; // 1: inside, 0: outside
107 
108  switch (cloud_sim.mode) {
109  case CLOUD_SIM_WP:
110  // Test the distance to the reference waypoint
112  inside = 0;
113  } else {
114  inside = 1;
115  }
116  AbiSendMsgPAYLOAD_DATA(CLOUD_SENSOR_ID, stamp, 1 /* CLOUD_BORDER */, 1, &inside);
117  break;
118 #ifdef SECTOR_CLOUD
119  case CLOUD_SIM_POLYGON:
120  inside = (uint8_t) InsideCloud(pos->x, pos->y);
121  AbiSendMsgPAYLOAD_DATA(CLOUD_SENSOR_ID, stamp, 1 /* CLOUD_BORDER */, 1, &inside);
122  break;
123 #endif
124  default:
125  break;
126  }
127 }
128 
130 void cloud_sim_move(void)
131 {
132  if (cloud_sim.mode == CLOUD_SIM_WP) {
133  if (cloud_sim_circle_id > 0 && cloud_sim_circle_id < NB_WAYPOINT) {
134  struct point wp = waypoints[cloud_sim_circle_id];
135  wp.x += cloud_sim.speed.x; // assuming dt = 1.
136  wp.y += cloud_sim.speed.y; // assuming dt = 1.
139  }
140  } else if (cloud_sim.mode == CLOUD_SIM_POLYGON) {
141  for (int i = 0; i < CLOUD_SIM_WPS_NB; i++) {
142  if (cloud_sim_polygon[i] > 0 && cloud_sim_polygon[i] < NB_WAYPOINT) {
143  struct point wp = waypoints[cloud_sim_polygon[i]];
144  wp.x += cloud_sim.speed.x; // assuming dt = 1.
145  wp.y += cloud_sim.speed.y; // assuming dt = 1.
148  }
149  }
150  }
151 }
152 
155 {
156  if (reset) {
157  struct point wps[NB_WAYPOINT] = WAYPOINTS_UTM;
158  // reset WP
159  if (cloud_sim_circle_id > 0 && cloud_sim_circle_id < NB_WAYPOINT) {
162  }
163  for (int i = 0; i < CLOUD_SIM_WPS_NB; i++) {
164  if (cloud_sim_polygon[i] > 0 && cloud_sim_polygon[i] < NB_WAYPOINT) {
167  }
168  }
169  }
170  cloud_sim.reset = false;
171 }
172 
Main include for ABI (AirBorneInterface).
#define CLOUD_SENSOR_ID
static uint8_t reset[3]
Definition: baro_MS5534A.c:83
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
struct CloudSim cloud_sim
Definition: cloud_sim.c:72
void cloud_sim_detect(void)
periodic call for border detection
Definition: cloud_sim.c:102
#define CLOUD_SIM_SPEED_Y
Definition: cloud_sim.c:69
static uint8_t cloud_sim_circle_id
Definition: cloud_sim.c:49
#define CLOUD_SIM_MODE
Definition: cloud_sim.c:40
#define CLOUD_SIM_WPS_NB
Definition: cloud_sim.c:60
void cloud_sim_move(void)
periodic call for moving waypoints
Definition: cloud_sim.c:130
void cloud_sim_reset(bool reset)
reset handler
Definition: cloud_sim.c:154
void cloud_sim_init(void)
Definition: cloud_sim.c:92
#define CLOUD_SIM_WP_ID
Definition: cloud_sim.c:47
static uint8_t cloud_sim_polygon[CLOUD_SIM_WPS_NB]
Definition: cloud_sim.c:61
#define CLOUD_SIM_RADIUS
Definition: cloud_sim.c:35
static float distance_to_wp(struct EnuCoor_f *pos, uint8_t id)
Definition: cloud_sim.c:78
#define CLOUD_SIM_SPEED_X
Definition: cloud_sim.c:65
struct FloatVect2 speed
Definition: cloud_sim.h:38
#define CLOUD_SIM_WP
Definition: cloud_sim.h:32
#define CLOUD_SIM_POLYGON
Definition: cloud_sim.h:33
uint8_t mode
Definition: cloud_sim.h:37
float radius
radius in WP mode
Definition: cloud_sim.h:39
bool reset
Definition: cloud_sim.h:36
void nav_send_waypoint(uint8_t wp_id)
Send a waypoint throught default telemetry channel.
Definition: common_nav.c:210
void nav_move_waypoint_point(uint8_t wp_id, struct point *p)
Move a waypoint from point structure (local frame).
Definition: common_nav.c:202
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:39
const uint8_t nb_waypoint
Definition: common_nav.c:38
float y
Definition: common_nav.h:41
float x
Definition: common_nav.h:40
static float float_vect2_norm(struct FloatVect2 *v)
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
float y
in meters
float x
in meters
vector in East North Up coordinates Units: meters
API to get/set the generic vehicle states.
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