-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrobot.cpp
More file actions
115 lines (97 loc) · 3.01 KB
/
robot.cpp
File metadata and controls
115 lines (97 loc) · 3.01 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
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
#include "robot.h"
Robot::Robot(double _x, double _y, double _orientation, int _world_size, std::vector<std::vector<double>> _landmarks)
{
x = _x;
y = _y;
orientation = _orientation;
world_size = _world_size;
landmarks = _landmarks;
srand (time(NULL));
generator.seed(rand()%100);
}
std::vector<double> Robot::get_pose()
{
std::vector<double> curr_pos = {x, y, orientation};
return curr_pos;
}
void Robot::set_pose(double new_x, double new_y, double new_orientation)
{
Robot::check_in_world_bounds(new_x, new_y, new_orientation);
x = new_x;
y = new_y;
orientation = new_orientation;
}
void Robot::set_noise(double _f_noise, double _t_noise, double _s_noise)
{
f_noise = _f_noise;
t_noise = _t_noise;
s_noise = _s_noise;
}
void Robot::move(double turn, double forward)
{
// Add turn noise and turn the robot
std::normal_distribution<double> t_dis(0.0, t_noise);
double t_noise_value = t_dis(generator);
orientation = orientation + turn + t_noise_value;
orientation = fmod(orientation,(2 * PI));
// Add move noise and move the robot
std::normal_distribution<double> f_dis(0.0, f_noise);
double f_noise_value = f_dis(generator);
x = x + cos(orientation) * (forward + f_noise_value);
y = y + sin(orientation) * (forward + f_noise_value);
x = fmod(x, world_size);
y = fmod(y, world_size);
}
std::vector<double> Robot::sense_landmarks(bool add_s_noise)
{
std::vector<double> distances;
for(auto const &landmark: landmarks)
{
double distance;
distance = std::sqrt(std::pow(x-landmark[0], 2) + std::pow(y-landmark[1], 2));
if(add_s_noise)
{
// adding sensor noise
std::normal_distribution<double> s_dis(0.0, s_noise);
double s_noise_value = s_dis(generator);
distance += s_noise_value;
}
distances.push_back(distance);
}
return distances;
}
void Robot::print_curr_pose()
{
std::cout << "The X position is " << x << std::endl;
std::cout << "The Y position is " << y << std::endl;
std::cout << "The Orientation is " << orientation << std::endl;
}
void Robot::print_curr_dis_to_landmarks()
{
std::vector<double> landmark_dis = Robot::sense_landmarks();
std::cout << "The landmark distances.." << std::endl;
for(auto distance: landmark_dis)
{
std::cout << distance << std::endl;
}
}
bool Robot::check_in_world_bounds(double new_x, double new_y, double new_orientation)
{
if(new_x < 0 || new_x >= world_size)
{
throw std::invalid_argument("The X value is out of bound of the world defined!!");
}
else if(new_y < 0 || new_y >= world_size)
{
throw std::invalid_argument("The Y value is out of bound of the world defined!!");
}
else if(new_orientation < 0 || new_orientation >= 2*PI)
{
throw std::invalid_argument("The orientation should be between 0 to 2*pi");
}
return true;
}