28 #include <sys/fcntl.h>
40 #ifndef AVOIDANCES_DISTANCES_HOR_COUNT
41 #define AVOIDANCES_DISTANCES_HOR_COUNT 36
44 #ifndef AVOIDANCE_AMOUNT_OF_BOARDS
45 #define AVOIDANCE_AMOUNT_OF_BOARDS 6
48 #ifndef AVOIDANCE_HEIGHT_IN_MEASUREMENT_VALUES
49 #define AVOIDANCE_HEIGHT_IN_MEASUREMENT_VALUES 6
52 #ifndef AVOIDANCE_WIDTH_IN_MEASUREMENT_VALUES
53 #define AVOIDANCE_WIDTH_IN_MEASUREMENT_VALUES 6
168 for (
int i1 = 0; i1 < size_matrix_local[0]; i1++) {
169 for (
int i3 = 0; i3 < size_matrix_local[2]; i3++) {
170 anglesMeasurements[i1 * size_matrix_local[0] + i3] = centersensorRad[i1] - 0.5 * fieldOfViewRad[0] + fieldOfViewRad[0] /
171 size_matrix_local[2] / 2 + (fieldOfViewRad[0] / size_matrix_local[2]) * i3;
178 if (stereocam_data.fresh) {
179 printf(
"Stereo fresh: %i \n", stereocam_data.len);
181 float distancesMeters[stereocam_data.len];
182 float anglesMeasurements[stereocam_data.matrix_width];
187 printf(
"%3d,", stereocam_data.data[i_print1 * (stereocam_data.len / AVOIDANCE_AMOUNT_OF_BOARDS) + i_print2]);
196 stereocam_disparity_to_meters(stereocam_data.data, distancesMeters, stereocam_data.len);
226 stereocam_data.fresh = 0;
227 if (stereocam_data.len > 50) {
242 for (
int i_m = 0; i_m < size_matrix_local[0]; i_m++) {
243 for (
int i_m3 = 0; i_m3 < size_matrix_local[2]; i_m3++) {
244 distances_hor_local[i_m * size_matrix_local[2] + i_m3] = 10000;
245 distances_hor_local_new[i_m * size_matrix_local[2] + i_m3] = 10000;
246 for (
int i_m2 = 0; i_m2 < 4; i_m2++) {
247 if (distancesMeters[i_m * size_matrix_local[1] + i_m2 * size_matrix_local[0]*size_matrix_local[2] + i_m3] <
248 distances_hor_local[i_m * size_matrix_local[2] + i_m3]) {
249 distances_hor_local_old[i_m * size_matrix_local[2] + i_m3] = distances_hor_local_new[i_m * size_matrix_local[2] + i_m3];
250 distances_hor_local_new[i_m * size_matrix_local[2] + i_m3] = distancesMeters[i_m * size_matrix_local[1] + i_m2 *
251 size_matrix_local[0] * size_matrix_local[2] + i_m3];
253 distances_hor_local[i_m * size_matrix_local[2] + i_m3] = (distances_hor_local_old[i_m * size_matrix_local[2] + i_m3] +
254 distances_hor_local_new[i_m * size_matrix_local[2] + i_m3]) / 2;
268 float Xest_old[size_matrix[0]*size_matrix[1]*size_matrix[2]];
269 float Ppred_new[size_matrix[0]*size_matrix[1]*size_matrix[2]];
270 float Pest_old[size_matrix[0]*size_matrix[1]*size_matrix[2]];
271 float K_gain[size_matrix[0]*size_matrix[1]*size_matrix[2]];
274 for (
int i_k = 0; i_k < (size_matrix[0]*size_matrix[1]*size_matrix[2]); i_k++) {
279 Xpred_new[i_k] =
A_kal * Xest_old[i_k];
288 Xest_new[i_k] = Xpred_new[i_k] + K_gain[i_k] * (float)(stereocam_data.data[i_k] -
H_kal * Xpred_new[i_k]);
298 float A_butter = -0.7265;
299 float B_butter[2] = {0.1367, 0.1367};
302 for (
int i_k = 0; i_k < (size_matrix[0] * size_matrix[1] * size_matrix[2]); i_k++) {
310 butter[i_k] = B_butter[0] * (float)stereocam_data.data[i_k] + B_butter[1] * (
float)
READimageBuffer_old[i_k] - A_butter *
312 butter_old[i_k] = butter[i_k];
315 for (
int ifill = 0; ifill < (size_matrix[0] * size_matrix[1] * size_matrix[2]); ifill++) {
326 float s_psi = sin(psi);
327 float c_psi = cos(psi);
342 target.
x = init_target.
x + dx_ref_NED;
343 target.
y = init_target.
y + dy_ref_NED;
357 void pingpong_euler(
float *distances_hor_local,
float *horizontalAnglesMeasurements,
int horizontalAmountOfMeasurements,
358 float attitude_reference_pitch,
float attitude_reference_roll,
float dist_treshold_local)
362 float sumPitch = 0.0;
365 float oa_pitch_angle[horizontalAmountOfMeasurements];
366 float oa_roll_angle[horizontalAmountOfMeasurements];
368 for (
int horizontal_index = 0; horizontal_index < horizontalAmountOfMeasurements; horizontal_index++) {
371 if (distances_hor_local[horizontal_index] < dist_treshold_local) {
373 oa_pitch_angle[horizontal_index] = cos(horizontalAnglesMeasurements[horizontal_index]) * attitude_reference_pitch;
374 oa_roll_angle[horizontal_index] = -sin(horizontalAnglesMeasurements[horizontal_index]) * attitude_reference_roll;
375 sumPitch += oa_pitch_angle[horizontal_index];
376 sumRoll += oa_roll_angle[horizontal_index];
381 if (sumPitch > attitude_reference_pitch) {
383 }
else if (sumPitch < -attitude_reference_pitch) {
390 if (sumRoll > attitude_reference_roll) {
392 }
else if (sumRoll < -attitude_reference_roll) {
393 ref_roll = -attitude_reference_roll;
410 float potential_obst = 0;
411 float potential_obst_temp = 0;
412 float potential_obst_integrated = 0;
417 float obst_count = 0.0;
424 int8_t min_disparity = 40;
449 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
451 angle_hor = angle_hor +
stereo_fow[0] / size_matrix[2];
453 potential_obst_temp = 0.0;
456 for (
int i2 = 0; i2 < 4; i2++) {
458 if (stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3] > min_disparity) {
459 Distance_est = ((
baseline * (float)
focal / (
float)stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0] *
460 size_matrix[2] + i3] - 18.0)) / 1000;
463 angle_hor_abs = -1 * angle_hor;
465 angle_hor_abs = angle_hor;
467 potential_obst_temp = potential_obst_temp -
K_obst * (angle_hor) * exp(-
c3_oa * angle_hor_abs) * exp(
468 -
c4_oa * Distance_est);
469 potential_obst_integrated = potential_obst_integrated +
K_obst *
c3_oa * (fabs(angle_hor) + 1) / (
c3_oa *
c3_oa) * exp(
470 -c3_oa * fabs(angle_hor)) * exp(-
c4_oa * Distance_est);
483 if (obst_count != 0) {
484 potential_obst = potential_obst + potential_obst_temp / ((float)obst_count);
497 if (speed_pot <= 0) {
508 float OF_Result_Vy = 0;
509 float OF_Result_Vx = 0;
516 float potential_obst = 0;
517 float potential_obst_integrated = 0;
522 float current_heading;
528 int8_t min_disparity = 40;
535 total_vel = pow((OF_Result_Vy * OF_Result_Vy + OF_Result_Vx * OF_Result_Vx), 0.5);
537 if (total_vel >
vmin) {
538 fp_angle = atan2(OF_Result_Vx, OF_Result_Vy);
550 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
551 angle_hor = angle_hor +
stereo_fow[0] / size_matrix[2];
554 for (
int i2 = 0; i2 < size_matrix[1]; i2++) {
556 if (stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3] > min_disparity) {
557 Distance_est = ((
baseline * (float)
focal / (
float)stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0] *
558 size_matrix[2] + i3] - 18.0)) / 1000;
562 diff_angle = fp_angle - angle_hor;
564 potential_obst = potential_obst +
K_obst * (diff_angle) * exp(-
c3_oa * fabs(diff_angle)) * exp(
565 -
c4_oa * Distance_est);
566 potential_obst_integrated = potential_obst_integrated + K_obst *
c3_oa * (fabs(diff_angle) + 1) / (
c3_oa *
c3_oa) * exp(
567 -c3_oa * fabs(diff_angle)) * exp(-
c4_oa * Distance_est);
579 if (speed_pot <= 0) {
584 new_heading = fp_angle + alpha_fil *
r_dot_new;
595 float A_butter = -0.8541;
596 float B_butter[2] = {0.0730 , 0.0730 };
600 float escape_angle = 0;
621 int8_t min_disparity = 45;
658 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
659 angle_hor = angle_hor +
stereo_fow[0] / size_matrix[2];
662 for (
int i2 = 0; i2 < 4; i2++) {
663 angle_ver = angle_ver -
stereo_fow[1] / size_matrix[1];
672 if (stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3] > min_disparity) {
673 Distance_est = (
baseline * (float)
focal / ((
float)stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0] *
674 size_matrix[2] + i3]) - 18.0) / 1000;
677 2) * cos(angle_hor) * cos(angle_ver);
679 2) * sin(angle_hor) * cos(angle_ver);
682 printf(
"rep.x %f index %d %d %d disp: %d cv: %f angle_hor: %f angle_ver: %f \n",
Repulsionforce_Kan.
x, i1, i2, i3,
683 stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3], Cv, angle_hor, angle_ver);
713 filter_repforce_old.
x;
715 filter_repforce_old.
y;
717 filter_repforce_old.
z;
747 if (y_goal_frame >= 0) {
748 escape_angle = 0.5 * M_PI;
749 }
else if (y_goal_frame < 0) {
750 escape_angle = -0.5 * M_PI;
769 printf(
"ref_pitch: %f ref_roll: %f disp_count: %d",
ref_pitch,
ref_roll, disp_count);
783 float A_butter = -0.8541;
784 float B_butter[2] = {0.0730 , 0.0730 };
805 int8_t min_disparity = 45;
806 int8_t number_of_buffer = 20;
807 float bias = 2 * M_PI;
811 float diff_available_heading[size_matrix[0] * size_matrix[2]];
812 float new_heading_diff = 1000;
828 for (
int i1 = 0; i1 < size_matrix[0]; i1++) {
832 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
833 angle_hor = angle_hor +
stereo_fow[0] / size_matrix[2];
836 for (
int i2 = 0; i2 < 4; i2++) {
837 angle_ver = angle_ver -
stereo_fow[1] / size_matrix[1];
847 if (stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3] > min_disparity) {
848 Distance_est = (
baseline * (float)
focal / ((
float)stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0] *
849 size_matrix[2] + i3]) - 18.0) / 1000;
852 2) * cos(angle_hor) * cos(angle_ver);
854 2) * sin(angle_hor) * cos(angle_ver);
870 filter_repforce_old.
x;
872 filter_repforce_old.
y;
874 filter_repforce_old.
z;
902 V_total = sqrt(pow(Total_Kan.
x, 2) + pow(Total_Kan.
y, 2));
912 printf(
"V_total: %f", V_total);
922 for (
int i1 = 0; i1 < size_matrix[0]; i1++) {
924 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
925 angle_hor = angle_hor +
stereo_fow[0] / size_matrix[2];
926 available_heading[i] = angle_hor;
935 for (
int i1 = 0; i1 < size_matrix[0]; i1++) {
936 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
937 for (
int i2 = 0; i2 < 4; i2++) {
938 if (stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3] > min_disparity) {
941 diff_available_heading[i] = 100;
942 for (
int i_diff = -number_of_buffer; i_diff <= number_of_buffer + 1; i_diff++) {
943 i_buffer = i + i_diff;
947 i_buffer = i_buffer + size_matrix[0] * size_matrix[2];
950 if (i_buffer > size_matrix[0]*size_matrix[2]) {
951 i_buffer = i_buffer - size_matrix[0] * size_matrix[2];
954 diff_available_heading[i_buffer] = 100;
964 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
965 if (diff_available_heading[i100] <= 0 && (fabs(diff_available_heading[i100]) < M_PI - 5.0 / 360.0 * 2.0 * M_PI)) {
966 diff_available_heading[i100] = diff_available_heading[i100] - bias;
970 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
971 if (diff_available_heading[i100] > 0 && (fabs(diff_available_heading[i100]) < M_PI - 5.0 / 360.0 * 2.0 * M_PI)) {
972 diff_available_heading[i100] = diff_available_heading[i100] + bias;
977 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
978 if (fabs(diff_available_heading[i100]) < fabs(new_heading_diff)) {
979 new_heading_diff = diff_available_heading[i100];
980 new_heading = available_heading[i100];
984 if (
obstacle_flag == 0 && fabs(new_heading_diff) > (2 * M_PI / 36.0)) {
986 if (new_heading_diff > 0) {
989 }
else if (new_heading_diff <= 0) {
995 if (fabs(new_heading_diff) <= (2 * M_PI / 36.0)) {
1004 if (fabs(new_heading_diff) >= 0.5 * M_PI) {
1017 for (
int i100 = 0; i100 < (size_matrix[0] * size_matrix[2]); i100++) {
1018 printf(
"%i diff_available_heading: %f available_heading: %f \n", i100, diff_available_heading[i100],
1019 available_heading[i100]);
1036 int8_t min_disparity = 45;
1037 int8_t number_of_buffer = 20;
1038 float bias = 2 * M_PI;
1042 float angle_hor = 0;
1044 float diff_available_heading[size_matrix[0]*size_matrix[2]];
1045 float new_heading_diff = 1000;
1050 float distance_heading = 0;
1055 for (
int i1 = 0; i1 < size_matrix[0]; i1++) {
1057 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
1058 angle_hor = angle_hor +
stereo_fow[0] / size_matrix[2];
1059 available_heading[i] = angle_hor;
1069 for (
int i1 = 0; i1 < size_matrix[0]; i1++) {
1070 for (
int i3 = 0; i3 < size_matrix[2]; i3++) {
1071 for (
int i2 = 0; i2 < 4; i2++) {
1072 if (stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0]*size_matrix[2] + i3] > min_disparity) {
1073 distance_est = (
baseline * (float)
focal / ((
float)stereocam_data.data[i1 * size_matrix[1] + i2 * size_matrix[0] *
1074 size_matrix[2] + i3]) - 18.0) / 1000;
1076 if (distance_est < distance_heading) {
1077 distance_heading = distance_est;
1080 diff_available_heading[i] = 100;
1081 for (
int i_diff = -number_of_buffer; i_diff <= number_of_buffer + 1; i_diff++) {
1082 i_buffer = i + i_diff;
1086 i_buffer = i_buffer + size_matrix[0] * size_matrix[2];
1089 if (i_buffer > size_matrix[0]*size_matrix[2]) {
1090 i_buffer = i_buffer - size_matrix[0] * size_matrix[2];
1093 diff_available_heading[i_buffer] = 100;
1104 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
1105 if (diff_available_heading[i100] <= 0 && (fabs(diff_available_heading[i100]) < M_PI - 5.0 / 360.0 * 2.0 * M_PI)) {
1106 diff_available_heading[i100] = diff_available_heading[i100] - bias;
1110 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
1111 if (diff_available_heading[i100] > 0 && (fabs(diff_available_heading[i100]) < M_PI - 5.0 / 360.0 * 2.0 * M_PI)) {
1112 diff_available_heading[i100] = diff_available_heading[i100] + bias;
1118 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
1119 if (fabs(diff_available_heading[i100]) < fabs(new_heading_diff)) {
1120 new_heading_diff = diff_available_heading[i100];
1121 new_heading = available_heading[i100];
1127 }
else if (
obstacle_flag == 0 && fabs(new_heading_diff) > (2 * M_PI / 36.0)) {
1129 if (new_heading_diff > 0) {
1132 }
else if (new_heading_diff <= 0) {
1139 if (fabs(new_heading_diff) >= 0.5 * M_PI) {
1143 if (fabs(new_heading_diff) <= (2 * M_PI / 36.0)) {
1163 for (
int i100 = 0; i100 < (size_matrix[0]*size_matrix[2]); i100++) {
1164 printf(
"%i diff_available_heading: %f available_heading: %f \n", i100, diff_available_heading[i100],
1165 available_heading[i100]);
#define FLOAT_ANGLE_NORMALIZE(_a)
#define VECT3_ADD(_a, _b)
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
float distances_hor[AVOIDANCES_DISTANCES_HOR_COUNT]
void pingpong_euler(float *distances_hor_local, float *horizontalAnglesMeasurements, int horizontalAmountOfMeasurements, float attitude_reference_pitch, float attitude_reference_roll, float dist_treshold_local)
Periodic telemetry system header (includes downlink utility and generated code).
#define VECT3_COPY(_a, _b)
float butter_old[AVOIDANCE_AMOUNT_OF_BOARDS *AVOIDANCE_HEIGHT_IN_MEASUREMENT_VALUES *AVOIDANCE_WIDTH_IN_MEASUREMENT_VALUES]
struct FloatVect2 init_target
void CN_potential_velocity(void)
float Xest_new[AVOIDANCE_AMOUNT_OF_BOARDS *AVOIDANCE_HEIGHT_IN_MEASUREMENT_VALUES *AVOIDANCE_WIDTH_IN_MEASUREMENT_VALUES]
void CN_potential_heading(void)
#define AVOIDANCES_DISTANCES_HOR_COUNT
uint8_t * READimageBuffer_old
void CN_escape_velocity(void)
void matrix_2_pingpong(float *distancesMeters, uint16_t *size_matrix_local, float *distances_hor_local)
struct FloatVect3 filter_repforce_old
struct FloatVect2 pos_diff
Guidance for the obstacle avoidance methods.
vector in North East Down coordinates Units: meters
Paparazzi floating point algebra.
void CN_calculate_target(void)
int8_t repulsionforce_filter_flag
struct NedCoor_f current_pos
interface to the TU Delft serial stereocam
void CN_matrix_butterworth(void)
#define AVOIDANCE_HEIGHT_IN_MEASUREMENT_VALUES
Obstacle avoidance methods.
Paparazzi fixed point math for geodetic calculations.
void CN_matrix_Kalman_filter(void)
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
void setAnglesMeasurements(float *anglesMeasurements, float *centersensorRad, float *fieldOfViewRad, uint16_t *size_matrix_local)
struct FloatVect3 Repulsionforce_Used
#define VECT3_SMUL(_vo, _vi, _s)
struct FloatVect3 Attractforce_goal_send
#define AVOIDANCE_WIDTH_IN_MEASUREMENT_VALUES
API to get/set the generic vehicle states.
#define AVOIDANCE_AMOUNT_OF_BOARDS
struct FloatVect3 Repulsionforce_Kan
float Pest_new[AVOIDANCE_AMOUNT_OF_BOARDS *AVOIDANCE_HEIGHT_IN_MEASUREMENT_VALUES *AVOIDANCE_WIDTH_IN_MEASUREMENT_VALUES]
void CN_vector_velocity(void)
struct FloatVect3 Repulsionforce_Kan_old
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
struct FloatVect3 Attractforce_goal
struct FloatVect3 Repulsionforce_Kan_send
void CN_vector_escape_velocity(void)