-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalizer.cpp
More file actions
192 lines (145 loc) · 5.73 KB
/
localizer.cpp
File metadata and controls
192 lines (145 loc) · 5.73 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
/**
localizer.cpp
Purpose: implements a 2-dimensional histogram filter
for a robot living on a colored cyclical grid by
correctly implementing the "initialize_beliefs",
"sense", and "move" functions.
Programmer: Daniel Rines
Date Created: 2019.06.06
Date Revised: 2019.06.07
*/
#include "localizer.h"
#include "helpers.cpp"
#include <stdlib.h>
#include "debugging_helpers.cpp"
using namespace std;
/**
Initializes a grid of beliefs to a uniform distribution.
@param grid - a two dimensional grid map (vector of vectors
of chars) representing the robot's world. For example:
g g g
g r g
g g g
would be a 3x3 world where every cell is green except
for the center, which is red.
@return - a normalized two dimensional grid of floats. For
a 2x2 grid, for example, this would be:
0.25 0.25
0.25 0.25
*/
vector< vector <float> > initialize_beliefs(vector< vector <char> > grid) {
// initialize local variables for the grid variables
int height = grid.size();
int width = grid[0].size();
int area = height * width;
// ratio factor creating the uniform distribution
float belief_per_cell = 1.0 / area;
// newGrid with uniform ratio values populating all of the newGrid cells
vector < vector<float> > newGrid (height, vector<float>(width, belief_per_cell));
// returns the uniform newGrid structure
return newGrid;
}
/**
Implements robot motion by updating beliefs based on the
intended dx and dy of the robot.
For example, if a localized robot with the following beliefs
0.00 0.00 0.00
0.00 1.00 0.00
0.00 0.00 0.00
and dx and dy are both 1 and blurring is 0 (noiseless motion),
than after calling this function the returned beliefs would be
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00 1.00
@param dy - the intended change in y position of the robot
@param dx - the intended change in x position of the robot
@param beliefs - a two dimensional grid of floats representing
the robot's beliefs for each cell before sensing. For
example, a robot which has almost certainly localized
itself in a 2D world might have the following beliefs:
0.01 0.98
0.00 0.01
@param blurring - A number representing how noisy robot motion
is. If blurring = 0.0 then motion is noiseless.
@return - a normalized two dimensional grid of floats
representing the updated beliefs for the robot.
*/
vector< vector <float> > move(int dy, int dx,
vector < vector <float> > beliefs,
float blurring) {
// initialize local variables based on the beliefs matrix dimensions
int height = beliefs.size();
int width = beliefs[0].size();
// construct a newGrid that is the same size as the beliefs matrix
// and fully initialize it with zeros:
vector < vector <float> > newGrid (height, vector <float> (width, 0.0));
// create local variables for new position iterators (new_i, new_j)
int new_i = 0;
int new_j = 0;
// loop thru the beliefs matrix and shift cell values based on dy, dx
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// calculate the frame shift for newGrid
new_i = ((i + dy) % height + height) % height;
new_j = ((j + dx) % width + width) % width;
// store the value from beliefs into the correct newGrid pos.
newGrid[new_i][new_j] = beliefs[i][j];
}
}
// return the newGrid matrix + passed in blurring value
return blur(newGrid, blurring);
}
/**
Implements robot sensing by updating beliefs based on the
color of a sensor measurement
@param color - the color the robot has sensed at its location
@param grid - the current map of the world, stored as a grid
(vector of vectors of chars) where each char represents a
color. For example:
g g g
g r g
g g g
@param beliefs - a two dimensional grid of floats representing
the robot's beliefs for each cell before sensing. For
example, a robot which has almost certainly localized
itself in a 2D world might have the following beliefs:
0.01 0.98
0.00 0.01
@param p_hit - the RELATIVE probability that any "sense" is
correct. The ratio of p_hit / p_miss indicates how many
times MORE likely it is to have a correct "sense" than
an incorrect one.
@param p_miss - the RELATIVE probability that any "sense" is
incorrect. The ratio of p_hit / p_miss indicates how many
times MORE likely it is to have a correct "sense" than
an incorrect one.
@return - a normalized two dimensional grid of floats
representing the updated beliefs for the robot.
*/
vector< vector <float> > sense(char color,
vector< vector <char> > grid,
vector< vector <float> > beliefs,
float p_hit,
float p_miss)
{
// initialize local variables based on the beliefs matrix dimensions
int height = beliefs.size();
int width = beliefs[0].size();
// construct a newGrid that is the same size as the beliefs matrix
// and fully initialize it with zeros:
vector < vector <float> > newGrid (height, vector <float> (width, 0.0));
// loop thru the grid and beliefs matrices to establish the new beliefs
// construct some local variables for comparing the hit truth in sense
int hit = 0;
// start with the first row and loop through the cols:
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// check if we have a color match (hit)
hit = (color == grid[i][j]) ? true : false;
// assign the newGrid (beliefs) values
newGrid[i][j] = beliefs[i][j] * (hit * p_hit + (1 - hit) * p_miss);
}
}
// first normalize the newGrid values and return new beliefs
return normalize(newGrid);
}