Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
pprz_circfit_float.c
Go to the documentation of this file.
1/*
2 * This file is part of paparazzi
3 *
4 * paparazzi is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * paparazzi is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with paparazzi; see the file COPYING. If not, see
16 * <http://www.gnu.org/licenses/>.
17 */
18
20#include <math.h>
21
22#ifndef PPRZ_CIRCFIT_EPSILON
23#define PPRZ_CIRCFIT_EPSILON 1e-3f
24#endif
25
26#ifndef PPRZ_CIRCFIT_ITER_MAX
27#define PPRZ_CIRCFIT_ITER_MAX 250
28#endif
29
30#ifndef PPRZ_CIRCFIT_NORM_CUTOFF
31#define PPRZ_CIRCFIT_NORM_CUTOFF 1e-6f
32#endif
33
34enum CircFitStatus_t pprz_circfit_wei_float(struct circle_t *c, const float *x, const float *y, uint16_t n, struct circle_t *g) {
35
36 // Check if initial guess is provided
37 if (g != NULL) {
38 c->r = g->r;
39 c->x = g->x;
40 c->y = g->y;
41 } else {
42 c->x = 0.0f;
43 c->y = 0.0f;
44 c->r = 1.0f;
45 }
46
47 float norm[n];
48 float x_prev = 0;
49 float y_prev = 0;
50 float r_prev = -1;
52
54
55 float sum_norm = 0.0f;
56 x_prev = c->x;
57 y_prev = c->y;
58 r_prev = c->r;
59
60 // Prepare ||x - c||
61 for (int i = 0; i < n; i++) {
62 norm[i] = sqrtf((x[i] - x_prev) * (x[i] - x_prev) + (y[i] - y_prev) * (y[i] - y_prev));
63 sum_norm += norm[i];
64 }
65
66 c->r = sum_norm / n;
67
68 for (int i = 0; i < n; i++) {
70 {
71 return CIRC_FIT_NORM_ERROR; // Norm error, too small distance
72 }
73 c->x += x[i] + c->r * (x_prev - x[i]) / norm[i];
74 c->y += y[i] + c->r * (y_prev - y[i]) / norm[i];
75 }
76
77 c->x /= n;
78 c->y /= n;
79
80 iteration++;
81
83 return CIRC_FIT_ITERATION_LIMIT; // Reached iteration limit
84 }
85
86 }
87
88 return CIRC_FIT_OK; // Circle fit successful
89
90}
uint16_t foo
Definition main_demo5.c:58
static float g
#define PPRZ_CIRCFIT_ITER_MAX
enum CircFitStatus_t pprz_circfit_wei_float(struct circle_t *c, const float *x, const float *y, uint16_t n, struct circle_t *g)
#define PPRZ_CIRCFIT_EPSILON
#define PPRZ_CIRCFIT_NORM_CUTOFF
CircFitStatus_t
@ CIRC_FIT_NORM_ERROR
@ CIRC_FIT_OK
@ CIRC_FIT_ITERATION_LIMIT
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.