24 MCLDistanceSensor(
leftDistance, Point(-6.4, -0.5), LEFT),
40 void StartMCL(
double x_,
double y_,
double theta_) {
46 uniform_real_distribution<double> start_dist_x(-start_std_pos[0], start_std_pos[0]);
47 uniform_real_distribution<double> start_dist_y(-start_std_pos[1], start_std_pos[1]);
48 uniform_real_distribution<double> start_dist_theta(-start_std_pos[2], start_std_pos[2]);
50 for (
int i = 0; i < num_particles; ++i) {
53 Particles[i].theta = lemlib::radToDeg(theta_) + start_dist_theta(
Random);
65 uint32_t start_time = pros::millis();
69 normal_distribution<double> dist_pos(0, std::abs(distance_step * 0.25));
71 lemlib::Pose odomPose =
chassis.getPose(
true);
72 const double theta_ = odomPose.theta;
73 const double rotated_theta = M_PI_2 - theta_;
74 const float cos_theta = cosf(rotated_theta);
75 const float sin_theta = sinf(rotated_theta);
81 p.step = Point(cos_theta, sin_theta);
82 p.x += (distance_step * cos_theta) + dist_pos(
Random);
83 p.y += (distance_step * sin_theta) + dist_pos(
Random);
84 p.x = std::clamp(p.x, -
field_.HalfSize,
field_.HalfSize);
85 p.y = std::clamp(p.y, -
field_.HalfSize,
field_.HalfSize);
94 if (sensor.measurement > -1) {
104 const double predicted =
field_.get_sensor_distance(P, sensor);
109 Deviation = (predicted - sensor.measurement);
118 const double Weight = inv_num_particles;
119 for (
auto &p :
Particles) p.weight = Weight;
122 for (
auto &p :
Particles) p.weight *= inv;
126 for (
int i = 1; i < num_particles; ++i) {
130 uniform_real_distribution<double> dist(0, inv_num_particles);
133 for (
int i = 0; i < num_particles; ++i) {
134 const double sample_point = i * (inv_num_particles) + dist(
Random);
135 auto it = lower_bound(
CDF.begin(),
CDF.end(), sample_point);
136 int index = std::distance(
CDF.begin(), it);
137 if(index >= num_particles) index = num_particles - 1;
142 double new_x = 0.0, new_y = 0.0, total_weight = 0.0;
144 new_x += p.x * p.weight;
145 new_y += p.y * p.weight;
146 total_weight += p.weight;
149 if (total_weight > 0) {
150 new_x /= total_weight;
151 new_y /= total_weight;
159 double dist_error = std::hypot(odomPose.x -
X, odomPose.y -
Y);
161 cout <<
" time: (" << pros::millis() - start_time <<
") " << std::endl;