-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyplanner.cpp
More file actions
196 lines (171 loc) · 4 KB
/
myplanner.cpp
File metadata and controls
196 lines (171 loc) · 4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "myplanner.h"
#include "vehicle/diromni.h"
using std::cout;
using std::endl;
MyPlanner::MyPlanner()
{
}
MyPlanner::MyPlanner(string paramfile, string logpath)
{
_param_file = paramfile;
_log_path = logpath;
}
vector<float> MyPlanner::action()
{
update_belief();
vector<float> a = get_action();
print_action(a);
_uav.move(a);
return a;
}
void MyPlanner::print_obs(double o)
{
planner_log << "obs = " << o << endl << endl;
}
void MyPlanner::print_action(vector<float> &a)
{
planner_log << "COMMAND: (north,east,yaw) = " << a[0] << "," << a[1] << "," << a[2] << endl << endl;
}
/**
*
* Returns -1 (true) if failure, 0 otherwise.
*/
int MyPlanner::start_log()
{
planner_log.open(_log_path + "/planner_log.txt");
if (!planner_log.is_open())
return -1;
return 0;
}
/**
* Uses the sensor type of the planner's vehicle to determine
* what constitutes an observation.
*
*/
double MyPlanner::get_obs()
{
double o = 0.0;
int stype = _uav.sensor->type();
if (stype == 0)
o = _bearing_max;
else if (stype == 1)
o = 0.0; //TODO: let's add the other sensor...
else
planner_log << "ERROR: SENSOR TYPE UNRECOGNIZED" << endl;
return o;
}
/**
* Reads in a configuration file and creates the vehicle and filter
* Should fail gracefully
*/
string MyPlanner::read_config(string paramfile)
{
ifstream param_stream;
string line;
param_stream.open(paramfile);
if (!param_stream.is_open())
{
planner_log << "FAILURE TO OPEN PLANNER CONFIGURATION FILE." <<endl;
return "error";
}
/* get the path */
int num_chars = paramfile.find_last_of("/") + 1;
string path = paramfile.substr(0, num_chars);
/* read each line */
bool err_flag;
while( !getline(param_stream, line).eof() )
{
err_flag = read_param_line(line, path);
if (err_flag)
{
param_stream.close();
return "error";
}
}
/* close the stream and return the path of configuration files */
param_stream.close();
return path;
}
/**
* Returns error if the line has some error in it
*/
int MyPlanner::read_param_line(string line, string path)
{
if (line.substr(0,1) == "#") // line is a comment
return 0;
if (line.find("search_size") != string::npos)
return read_search_size_line(line);
if (line.find("sensor") != string::npos)
return read_sensor_line(line, path);
if (line.find("filter") != string::npos)
return read_filter_line(line);
// it's ok if we don't recognize the line
return 0;
}
int MyPlanner::read_search_size_line(string line)
{
int ind = line.find_last_of(",")+1;
_search_size = stod(line.substr(ind, line.length()-ind));
_uav.set_limit(_search_size);
_uav.set_xy();
_uav.set_max_step(10.0); // TODO: do this correctly
return 0;
}
int MyPlanner::read_sensor_line(string line, string path)
{
string sub;
int e_flag = 0;
stringstream ss(line);
getline(ss, sub, ',');
getline(ss, sub, ','); // done twice to get second element
if (sub == "bo")
{
getline(ss, sub, ',');
double sensor_noise = stod(sub);
_uav.sensor = new BearingOnly(sensor_noise);
getline(ss, sub, ',');
if (stoi(sub) == 1)
{
DF *f = static_cast<DF *>(filter);
f->set_obs_probs(_uav.sensor);
}
return 0;
}
if (sub == "do")
{
DirOmni *domni = new DirOmni();
e_flag += domni->set_means(path + "norm_means.csv");
e_flag += domni->set_stds(path + "norm_means.csv");
_uav.sensor = domni;
// TODO: check that filter is a discrete filter
DF* f = static_cast<DF*>(filter);
f->bin_offset = -20;
return e_flag;
}
planner_log << "Failed to recognize requested sensor." << endl;
return -1;
}
int MyPlanner::read_filter_line(string line)
{
string sub;
int e_flag = 0;
stringstream ss(line);
getline(ss, sub, ',');
getline(ss, sub, ','); // done twice to get second element
if (sub == "df")
{
getline(ss, sub, ',');
int filter_info = stoi(sub);
filter = new DF(_search_size, filter_info);
return 0;
}
planner_log << "Failed to recognize requested filter." << endl;
return -1;
}
void MyPlanner::update_belief()
{
double o = get_obs();
print_obs(o);
filter->update(_uav, o);
filter->print_belief(planner_log);
}