Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
search_map.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 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
28
29#define SEARCH_MAP_DEBUG 0
30
31#if (defined SITL) && SEARCH_MAP_DEBUG
32#define DEBUG_PRINT printf
33#else
34#define DEBUG_PRINT(...) {}
35#endif
36
37
38void search_map_init(struct search_map_t *map, struct NedCoor_f pos, float res)
39{
40 map->res = res;
41 map->center = pos;
42 for (int i = 0; i < SEARCH_MAP_SIZE; i++) {
43 for (int j = 0; j < SEARCH_MAP_SIZE; j++) {
44 map->grid[i][j] = 0.f;
45 }
46 }
47 DEBUG_PRINT("map init at %f %f | res %f\n", pos.x, pos.y, res);
48}
49
50void search_map_init_from_wp(struct search_map_t *map, uint8_t wp_id, float res)
51{
52 struct EnuCoor_f enu = *waypoint_get_enu_f(wp_id);
53 struct NedCoor_f ned;
55 search_map_init(map, ned, res);
56}
57
59{
60 for (int i = 0; i < SEARCH_MAP_SIZE; i++) {
61 for (int j = 0; j < SEARCH_MAP_SIZE; j++) {
62 map->grid[i][j] = 0.f;
63 }
64 }
65}
66
67bool search_map_update(struct search_map_t *map, float data, struct NedCoor_f pos)
68{
69 int x = (int)((pos.x - map->center.x) / map->res + SEARCH_MAP_SIZE / 2.f + 0.5f);
70 int y = (int)((pos.y - map->center.y) / map->res + SEARCH_MAP_SIZE / 2.f + 0.5f);
71 DEBUG_PRINT("update %f | %f %f | %d %d\n", data, pos.x, pos.y, x, y);
73 return false; // out of map
74 }
75 map->grid[x][y] = data; // TODO filter with current data in cell ?
76 return true;
77}
78
79float search_map_get_barycenter(struct search_map_t *map, float threshold, struct NedCoor_f *pos)
80{
81 float bx = 0.f;
82 float by = 0.f;
83 float quality = 0.f;
84 int nb_qual = 0;
85 float sum = 0.f;
86 const int offset = (int)(SEARCH_MAP_SIZE / 2.f + 0.5f);
87 float max = 0.f;
88#if SEARCH_MAP_DEBUG
89 float mx = 0.f;
90 float my = 0.f;
91#endif
92 for (int i = 0; i < SEARCH_MAP_SIZE; i++) {
93 for (int j = 0; j < SEARCH_MAP_SIZE; j++) {
94 if (map->grid[i][j] > max) {
95 max = map->grid[i][j];
96#if SEARCH_MAP_DEBUG
97 mx = (float)(i - offset) * map->res;
98 my = (float)(j - offset) * map->res;
99#endif
100 }
101 }
102 }
103 if (max < 1e-5) {
104 *pos = map->center;
105 DEBUG_PRINT("nothing in map\n");
106 return 0.f; // nothing in the map
107 }
108 DEBUG_PRINT("map:\n");
109 Bound(threshold, 0.f, 1.f);
110 for (int i = 0; i < SEARCH_MAP_SIZE; i++) {
111 for (int j = 0; j < SEARCH_MAP_SIZE; j++) {
112 float val = map->grid[i][j] / max; // normalized value
113 DEBUG_PRINT("\t%.1f,", val);
114 if (val >= threshold) {
115 bx += val * (float)(i - offset);
116 by += val * (float)(j - offset);
117 sum += val;
118 quality += map->grid[i][j];
119 nb_qual++;
120 }
121 }
122 DEBUG_PRINT("\n");
123 }
124 pos->x = map->center.x + (bx * map->res / sum);
125 pos->y = map->center.y + (by * map->res / sum);
126 pos->z = map->center.z;
127 quality /= (float)nb_qual;
128
129 DEBUG_PRINT(" -> offset %d, sum %f, res %f, bx %f, by %f, cx %f, cy %f, mx %f, my %f\n", offset, sum, map->res, bx, by, map->center.x, map->center.y, mx, my);
130 DEBUG_PRINT(" -> pos %.4f %.4f, quality=%.2f\n", pos->x, pos->y, quality);
131
132 return quality;
133}
134
135void search_map_update_wp(struct search_map_t *map, float threshold, uint8_t wp_id)
136{
137 struct NedCoor_f pos;
138 if (search_map_get_barycenter(map, threshold, &pos) > 0.f) {
139 struct EnuCoor_f enu;
140 ENU_OF_TO_NED(enu, pos);
141 waypoint_set_enu(wp_id, &enu);
142 }
143}
144
#define ENU_OF_TO_NED(_po, _pi)
uint16_t foo
Definition main_demo5.c:58
struct EnuCoor_f * waypoint_get_enu_f(uint8_t wp_id)
Get ENU coordinates (float)
Definition waypoints.c:392
void waypoint_set_enu(uint8_t wp_id, struct EnuCoor_f *enu)
Set local ENU waypoint coordinates.
Definition waypoints.c:182
float z
in meters
float x
in meters
float y
in meters
vector in East North Up coordinates Units: meters
vector in North East Down coordinates Units: meters
void search_map_init_from_wp(struct search_map_t *map, uint8_t wp_id, float res)
init map from waypoint
Definition search_map.c:50
void search_map_reset(struct search_map_t *map)
reset map
Definition search_map.c:58
void search_map_update_wp(struct search_map_t *map, float threshold, uint8_t wp_id)
update waypoint position from map
Definition search_map.c:135
float search_map_get_barycenter(struct search_map_t *map, float threshold, struct NedCoor_f *pos)
get weighted center of the data, expected to be the search position
Definition search_map.c:79
#define DEBUG_PRINT(...)
Definition search_map.c:34
void search_map_init(struct search_map_t *map, struct NedCoor_f pos, float res)
init map
Definition search_map.c:38
bool search_map_update(struct search_map_t *map, float data, struct NedCoor_f pos)
update map with new data
Definition search_map.c:67
Library for generic map to store and search from sensor data.
#define SEARCH_MAP_SIZE
Definition search_map.h:33
float res
map resolution in m/cell
Definition search_map.h:41
struct NedCoor_f center
center of the map
Definition search_map.h:40
float grid[SEARCH_MAP_SIZE][SEARCH_MAP_SIZE]
data
Definition search_map.h:39
map for detection
Definition search_map.h:38
uint16_t val[TCOUPLE_NB]
static const float offset[]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.