This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
/
coav_gcs.cc
172 lines (145 loc) · 4.97 KB
/
coav_gcs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include <gazebo/gazebo.hh>
#include <gazebo/gazebo_client.hh>
#include <gazebo/msgs/msgs.hh>
#include <gazebo/transport/transport.hh>
#include <getopt.h>
#include <iostream>
#include <memory>
#include <thread>
#include "avoidance/QuadCopterShiftAvoidance.hh"
#include "vehicles/GazeboQuadCopter.hh"
#include "vehicles/MavQuadCopter.hh"
#include "sensors/GazeboRealSenseCamera.hh"
#include "detection/DepthImagePolarHistDetector.hh"
#include "common/common.hh"
enum device_type_ { GAZEBO, PHYSICAL, OTHER };
static device_type_ vehicle_type = OTHER;
static device_type_ depth_camera_type = OTHER;
static void print_usage();
static device_type_ parse_device_type(const char *optarg);
// Definitions
bool running = true;
static void print_usage()
{
std::cout << "Usage: coav [options]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --depth-camera GAZEBO|PHYSICAL Depth Camera Type"
<< std::endl;
std::cout << " --vehicle GAZEBO|PHYSICAL Vehicle Type"
<< std::endl;
std::cout << " --help Display Help"
<< std::endl;
}
static device_type_ parse_device_type(const char *optarg)
{
device_type_ ret_val = OTHER;
if (optarg) {
std::string optarg_str(optarg);
std::transform(optarg_str.begin(), optarg_str.end(), optarg_str.begin(),
::tolower);
if (optarg_str == std::string("gazebo")) {
ret_val = GAZEBO;
}
if (optarg_str == std::string("physical")) {
ret_val = PHYSICAL;
}
}
return ret_val;
}
int main(int argc, char **argv)
{
// Parse command line arguments
bool args_need_help = false;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"depth-camera", required_argument, 0, 'c'},
{"vehicle", required_argument, 0, 'v'},
{0, 0, 0, 0}};
while (true) {
int option_index = 0;
int c = getopt_long(argc, argv, "hc:v:d", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'c':
depth_camera_type = parse_device_type(optarg);
if (depth_camera_type == OTHER) {
std::cout << "err: invalid depth camera type." << std::endl;
args_need_help = true;
}
break;
case 'v':
vehicle_type = parse_device_type(optarg);
if (vehicle_type == OTHER) {
std::cout << "err: invalid vehicle type." << std::endl;
args_need_help = true;
}
break;
case 'h':
default:
print_usage();
return 0;
}
}
if (args_need_help) {
print_usage();
return 0;
}
// Check if camera and vehicle types were given
if (depth_camera_type == OTHER || vehicle_type == OTHER) {
std::cout << "Please provide valid device types" << std::endl;
return 0;
}
// TODO: Implement PHYSICAL device support
if (depth_camera_type == PHYSICAL || vehicle_type == PHYSICAL) {
std::cout << "Physical devices are not supported yet" << std::endl;
return 0;
}
std::shared_ptr<DepthCamera> depth_camera;
std::shared_ptr<MavQuadCopter> vehicle;
std::shared_ptr<DepthImagePolarHistDetector> obstacle_detector;
// Initialize Sensors
switch (depth_camera_type) {
case OTHER:
case PHYSICAL:
case GAZEBO:
depth_camera = std::make_shared<GazeboRealSenseCamera>();
std::cout << "[coav] GazeboRealSenseCamera instantiated" << std::endl;
obstacle_detector = std::make_shared<DepthImagePolarHistDetector>(
depth_camera, 180.0 * (atan(depth_camera->get_fov_tan())) / M_PI);
std::cout << "[coav] ObstacleDetector instantiated" << std::endl;
}
// Initialize Avoidance Strategy
auto avoidance = std::make_shared<QuadCopterShiftAvoidance>();
// Initialize Vehicle
switch (depth_camera_type) {
case OTHER:
case PHYSICAL:
case GAZEBO:
vehicle = std::make_shared<MavQuadCopter>();
std::cout << "[coav] Vehicle instantiated" << std::endl;
}
while (running) {
// Sense
std::vector<Obstacle> obstacles;
std::vector<double> hist = obstacle_detector->detect();
// Avoid
avoidance->avoid(hist, vehicle);
}
}