Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
orange_avoider.c
Go to the documentation of this file.
1/*
2 * Copyright (C) Roland Meertens
3 *
4 * This file is part of paparazzi
5 *
6 */
22#include "generated/airframe.h"
23#include "state.h"
24#include "modules/core/abi.h"
25#include <time.h>
26#include <stdio.h>
27
28#include "generated/flight_plan.h"
29
30#define ORANGE_AVOIDER_VERBOSE TRUE
31
32#define PRINT(string,...) fprintf(stderr, "[orange_avoider->%s()] " string,__FUNCTION__ , ##__VA_ARGS__)
33#if ORANGE_AVOIDER_VERBOSE
34#define VERBOSE_PRINT PRINT
35#else
36#define VERBOSE_PRINT(...)
37#endif
38
44
51
52// define settings
53float oa_color_count_frac = 0.18f;
54
55// define and initialise global variables
57int32_t color_count = 0; // orange color count from color filter for obstacle detection
58int16_t obstacle_free_confidence = 0; // a measure of how certain we are that the way ahead is safe.
59float heading_increment = 5.f; // heading angle increment [deg]
60float maxDistance = 2.25; // max waypoint displacement [m]
61
62const int16_t max_trajectory_confidence = 5; // number of consecutive negative object detections to be sure we are obstacle free
63
64/*
65 * This next section defines an ABI messaging event (http://wiki.paparazziuav.org/wiki/ABI), necessary
66 * any time data calculated in another module needs to be accessed. Including the file where this external
67 * data is defined is not enough, since modules are executed parallel to each other, at different frequencies,
68 * in different threads. The ABI event is triggered every time new data is sent out, and as such the function
69 * defined in this file does not need to be explicitly called, only bound in the init function
70 */
71#ifndef ORANGE_AVOIDER_VISUAL_DETECTION_ID
72#define ORANGE_AVOIDER_VISUAL_DETECTION_ID ABI_BROADCAST
73#endif
78 int32_t quality, int16_t __attribute__((unused)) extra)
79{
80 color_count = quality;
81}
82
83/*
84 * Initialisation function, setting the colour filter, random seed and heading_increment
85 */
87{
88 // Initialise random values
89 srand(time(NULL));
91
92 // bind our colorfilter callbacks to receive the color filter outputs
94}
95
96/*
97 * Function that checks it is safe to move forwards, and then moves a waypoint forward or changes the heading
98 */
100{
101 // only evaluate our state machine if we are flying
102 if(!autopilot_in_flight()){
103 return;
104 }
105
106 // compute current color thresholds
108
109 VERBOSE_PRINT("Color_count: %d threshold: %d state: %d \n", color_count, color_count_threshold, navigation_state);
110
111 // update our safe confidence using color threshold
114 } else {
115 obstacle_free_confidence -= 2; // be more cautious with positive obstacle detections
116 }
117
118 // bound obstacle_free_confidence
120
122
123 switch (navigation_state){
124 case SAFE:
125 // Move waypoint forward
129 } else if (obstacle_free_confidence == 0){
131 } else {
133 }
134
135 break;
136 case OBSTACLE_FOUND:
137 // stop
140
141 // randomly select new search direction
143
145
146 break;
149
150 // make sure we have a couple of good readings before declaring the way safe
151 if (obstacle_free_confidence >= 2){
153 }
154 break;
155 case OUT_OF_BOUNDS:
158
160 // add offset to head back into arena
162
163 // reset safe counter
165
166 // ensure direction is safe before continuing
168 }
169 break;
170 default:
171 break;
172 }
173 return;
174}
175
176/*
177 * Increases the NAV heading. Assumes heading is an INT32_ANGLE. It is bound in this function.
178 */
180{
182
183 // normalize heading to [-pi, pi]
185
186 // set heading, declared in firmwares/rotorcraft/navigation.h
188
189 VERBOSE_PRINT("Increasing heading to %f\n", DegOfRad(new_heading));
190 return false;
191}
192
193/*
194 * Calculates coordinates of distance forward and sets waypoint 'waypoint' to those coordinates
195 */
203
204/*
205 * Calculates coordinates of a distance of 'distanceMeters' forward w.r.t. current position and heading
206 */
208{
210
211 // Now determine where to place the waypoint you want to go to
214 VERBOSE_PRINT("Calculated %f m forward position. x: %f y: %f based on pos(%f, %f) and heading(%f)\n", distanceMeters,
217 return false;
218}
219
220/*
221 * Sets waypoint 'waypoint' to the coordinates of 'new_coor'
222 */
224{
225 VERBOSE_PRINT("Moving waypoint %d to x:%f y:%f\n", waypoint, POS_FLOAT_OF_BFP(new_coor->x),
228 return false;
229}
230
231/*
232 * Sets the variable 'heading_increment' randomly positive/negative
233 */
235{
236 // Randomly choose CW or CCW avoiding direction
237 if (rand() % 2 == 0) {
238 heading_increment = 5.f;
239 VERBOSE_PRINT("Set avoidance increment to: %f\n", heading_increment);
240 } else {
241 heading_increment = -5.f;
242 VERBOSE_PRINT("Set avoidance increment to: %f\n", heading_increment);
243 }
244 return false;
245}
246
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition abi_common.h:67
bool autopilot_in_flight(void)
get in_flight flag
Definition autopilot.c:340
#define WaypointX(_wp)
Definition common_nav.h:45
#define WaypointY(_wp)
Definition common_nav.h:46
float psi
in radians
#define FLOAT_ANGLE_NORMALIZE(_a)
#define POS_FLOAT_OF_BFP(_ai)
#define POS_BFP_OF_REAL(_af)
int32_t y
North.
int32_t x
East.
vector in East North Up coordinates
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1306
static struct EnuCoor_i * stateGetPositionEnu_i(void)
Get position in local ENU coordinates (int).
Definition state.h:803
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition state.h:848
float new_heading
uint16_t foo
Definition main_demo5.c:58
float moveDistance
navigation_state_t
#define front_camera
Definition mt9f002_nps.c:3
void waypoint_move_xy_i(uint8_t wp_id, int32_t x, int32_t y)
Definition waypoints.c:214
void waypoint_move_here_2d(uint8_t wp_id)
Definition waypoints.c:310
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(...)
static uint8_t chooseRandomIncrementAvoidance(void)
static uint8_t moveWaypointForward(uint8_t waypoint, float distanceMeters)
static abi_event color_detection_ev
float heading_increment
static uint8_t moveWaypoint(uint8_t waypoint, struct EnuCoor_i *new_coor)
void orange_avoider_periodic(void)
static uint8_t calculateForwards(struct EnuCoor_i *new_coor, float distanceMeters)
int16_t obstacle_free_confidence
float oa_color_count_frac
const int16_t max_trajectory_confidence
static uint8_t increase_nav_heading(float incrementDegrees)
int32_t color_count
@ OUT_OF_BOUNDS
@ SAFE
@ SEARCH_FOR_SAFE_HEADING
@ OBSTACLE_FOUND
#define ORANGE_AVOIDER_VISUAL_DETECTION_ID
void orange_avoider_init(void)
float maxDistance
struct RotorcraftNavigation nav
Definition navigation.c:51
Rotorcraft navigation functions.
float heading
heading setpoint (in radians)
Definition navigation.h:133
API to get/set the generic vehicle states.
int int32_t
Typedef defining 32 bit int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
navigation_state
Camera focal length, in pixels (i.e. distance between camera.
Definition wedgebug.c:209
float heading
Definition wedgebug.c:258