Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
orange_avoider_guided.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Kirk Scheper <kirkscheper@gmail.com>
3  *
4  * This file is part of paparazzi
5  *
6  */
30 #include "generated/airframe.h"
31 #include "state.h"
32 #include "modules/core/abi.h"
33 #include <stdio.h>
34 #include <time.h>
35 
36 #define ORANGE_AVOIDER_VERBOSE TRUE
37 
38 #define PRINT(string,...) fprintf(stderr, "[orange_avoider_guided->%s()] " string,__FUNCTION__ , ##__VA_ARGS__)
39 #if ORANGE_AVOIDER_VERBOSE
40 #define VERBOSE_PRINT PRINT
41 #else
42 #define VERBOSE_PRINT(...)
43 #endif
44 
46 
53 };
54 
55 // define settings
56 float oag_color_count_frac = 0.18f; // obstacle detection threshold as a fraction of total of image
57 float oag_floor_count_frac = 0.05f; // floor detection threshold as a fraction of total of image
58 float oag_max_speed = 0.5f; // max flight speed [m/s]
59 float oag_heading_rate = RadOfDeg(20.f); // heading change setpoint for avoidance [rad/s]
60 
61 // define and initialise global variables
62 enum navigation_state_t navigation_state = SEARCH_FOR_SAFE_HEADING; // current state in state machine
63 int32_t color_count = 0; // orange color count from color filter for obstacle detection
64 int32_t floor_count = 0; // green color count from color filter for floor detection
65 int32_t floor_centroid = 0; // floor detector centroid in y direction (along the horizon)
66 float avoidance_heading_direction = 0; // heading change direction for avoidance [rad/s]
67 int16_t obstacle_free_confidence = 0; // a measure of how certain we are that the way ahead if safe.
68 
69 const int16_t max_trajectory_confidence = 5; // number of consecutive negative object detections to be sure we are obstacle free
70 
71 // This call back will be used to receive the color count from the orange detector
72 #ifndef ORANGE_AVOIDER_VISUAL_DETECTION_ID
73 #error This module requires two color filters, as such you have to define ORANGE_AVOIDER_VISUAL_DETECTION_ID to the orange filter
74 #error Please define ORANGE_AVOIDER_VISUAL_DETECTION_ID to be COLOR_OBJECT_DETECTION1_ID or COLOR_OBJECT_DETECTION2_ID in your airframe
75 #endif
77 static void color_detection_cb(uint8_t __attribute__((unused)) sender_id,
78  int16_t __attribute__((unused)) pixel_x, int16_t __attribute__((unused)) pixel_y,
79  int16_t __attribute__((unused)) pixel_width, int16_t __attribute__((unused)) pixel_height,
80  int32_t quality, int16_t __attribute__((unused)) extra)
81 {
82  color_count = quality;
83 }
84 
85 #ifndef FLOOR_VISUAL_DETECTION_ID
86 #error This module requires two color filters, as such you have to define FLOOR_VISUAL_DETECTION_ID to the orange filter
87 #error Please define FLOOR_VISUAL_DETECTION_ID to be COLOR_OBJECT_DETECTION1_ID or COLOR_OBJECT_DETECTION2_ID in your airframe
88 #endif
90 static void floor_detection_cb(uint8_t __attribute__((unused)) sender_id,
91  int16_t __attribute__((unused)) pixel_x, int16_t pixel_y,
92  int16_t __attribute__((unused)) pixel_width, int16_t __attribute__((unused)) pixel_height,
93  int32_t quality, int16_t __attribute__((unused)) extra)
94 {
95  floor_count = quality;
96  floor_centroid = pixel_y;
97 }
98 
99 /*
100  * Initialisation function
101  */
103 {
104  // Initialise random values
105  srand(time(NULL));
107 
108  // bind our colorfilter callbacks to receive the color filter outputs
110  AbiBindMsgVISUAL_DETECTION(FLOOR_VISUAL_DETECTION_ID, &floor_detection_ev, floor_detection_cb);
111 }
112 
113 /*
114  * Function that checks it is safe to move forwards, and then sets a forward velocity setpoint or changes the heading
115  */
117 {
118  // Only run the mudule if we are in the correct flight mode
122  return;
123  }
124 
125  // compute current color thresholds
126  int32_t color_count_threshold = oag_color_count_frac * front_camera.output_size.w * front_camera.output_size.h;
127  int32_t floor_count_threshold = oag_floor_count_frac * front_camera.output_size.w * front_camera.output_size.h;
128  float floor_centroid_frac = floor_centroid / (float)front_camera.output_size.h / 2.f;
129 
130  VERBOSE_PRINT("Color_count: %d threshold: %d state: %d \n", color_count, color_count_threshold, navigation_state);
131  VERBOSE_PRINT("Floor count: %d, threshold: %d\n", floor_count, floor_count_threshold);
132  VERBOSE_PRINT("Floor centroid: %f\n", floor_centroid_frac);
133 
134  // update our safe confidence using color threshold
135  if(color_count < color_count_threshold){
137  } else {
138  obstacle_free_confidence -= 2; // be more cautious with positive obstacle detections
139  }
140 
141  // bound obstacle_free_confidence
143 
144  float speed_sp = fminf(oag_max_speed, 0.2f * obstacle_free_confidence);
145 
146  switch (navigation_state){
147  case SAFE:
148  if (floor_count < floor_count_threshold || fabsf(floor_centroid_frac) > 0.12){
150  } else if (obstacle_free_confidence == 0){
152  } else {
154  }
155 
156  break;
157  case OBSTACLE_FOUND:
158  // stop
160 
161  // randomly select new search direction
163 
165 
166  break;
169 
170  // make sure we have a couple of good readings before declaring the way safe
171  if (obstacle_free_confidence >= 2){
174  }
175  break;
176  case OUT_OF_BOUNDS:
177  // stop
179 
180  // start turn back into arena
182 
184 
185  break;
186  case REENTER_ARENA:
187  // force floor center to opposite side of turn to head back into arena
188  if (floor_count >= floor_count_threshold && avoidance_heading_direction * floor_centroid_frac >= 0.f){
189  // return to heading mode
191 
192  // reset safe counter
194 
195  // ensure direction is safe before continuing
197  }
198  break;
199  default:
200  break;
201  }
202  return;
203 }
204 
205 /*
206  * Sets the variable 'incrementForAvoidance' randomly positive/negative
207  */
209 {
210  // Randomly choose CW or CCW avoiding direction
211  if (rand() % 2 == 0) {
213  VERBOSE_PRINT("Set avoidance increment to: %f\n", avoidance_heading_direction * oag_heading_rate);
214  } else {
216  VERBOSE_PRINT("Set avoidance increment to: %f\n", avoidance_heading_direction * oag_heading_rate);
217  }
218  return false;
219 }
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1306
struct FloatVect3 speed_sp
Definition: guidance_indi.c:73
navigation_state_t
Definition: mav_exercise.c:37
#define ORANGE_AVOIDER_VISUAL_DETECTION_ID
Definition: mav_exercise.c:56
#define front_camera
Definition: mt9f002_nps.c:3
static void color_detection_cb(uint8_t sender_id, int16_t pixel_x, int16_t pixel_y, int16_t pixel_width, int16_t pixel_height, int32_t quality, int16_t extra)
#define VERBOSE_PRINT(...)
uint8_t chooseRandomIncrementAvoidance(void)
static abi_event color_detection_ev
int32_t floor_centroid
static void floor_detection_cb(uint8_t sender_id, int16_t pixel_x, int16_t pixel_y, int16_t pixel_width, int16_t pixel_height, int32_t quality, int16_t extra)
void orange_avoider_guided_periodic(void)
float oag_floor_count_frac
int32_t floor_count
float avoidance_heading_direction
static abi_event floor_detection_ev
int16_t obstacle_free_confidence
float oag_color_count_frac
const int16_t max_trajectory_confidence
int32_t color_count
float oag_max_speed
@ REENTER_ARENA
@ OUT_OF_BOUNDS
@ SEARCH_FOR_SAFE_HEADING
@ OBSTACLE_FOUND
float oag_heading_rate
void orange_avoider_guided_init(void)
void guidance_h_set_heading(float heading)
Set heading setpoint.
Definition: guidance_h.c:465
struct HorizontalGuidance guidance_h
Definition: guidance_h.c:45
void guidance_h_set_body_vel(float vx, float vy)
Set body relative horizontal velocity setpoint.
Definition: guidance_h.c:472
void guidance_h_set_heading_rate(float rate)
Set heading rate setpoint.
Definition: guidance_h.c:508
Horizontal guidance for rotorcrafts.
#define GUIDANCE_H_MODE_GUIDED
Definition: guidance_h.h:59
API to get/set the generic vehicle states.
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
navigation_state
Camera focal length, in pixels (i.e. distance between camera.
Definition: wedgebug.c:209