-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState_trooper.cpp
More file actions
145 lines (130 loc) · 4.96 KB
/
Copy pathState_trooper.cpp
File metadata and controls
145 lines (130 loc) · 4.96 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
#include "State_trooper.h"
#include <iostream>
void State_trooper::goToDestination(const std::string& warehouseName) {
pending_goto = true;
pending_goto_name = warehouseName;
}
void State_trooper::update() {
if (pending_position) {
beginPositionMode(pending_pos_speed);
arrived = false;
setState("Moving to position");
pending_position = false;
}
if (pending_course) {
beginCourseMode(pending_course_speed);
arrived = false;
setState("Moving to " + std::to_string(angle));
pending_course = false;
}
if (pending_goto) {
const auto& warehouses = Model::getInstance().getWarehouses();
for (const auto& wh : warehouses) {
if (wh->getName() == pending_goto_name) {
destination = wh->get_location();
angle = to_degrees(std::atan2(destination->x - _location->x,
destination->y - _location->y));
speed = 90.0;
en_route = true;
arrived = false;
mode = 2;
currentDestinationName = pending_goto_name;
visitedWarehouses.insert(pending_goto_name);
setState("Moving to " + pending_goto_name);
pending_goto = false;
break;
}
}
if (pending_goto) {
std::cerr << "Warehouse \"" << pending_goto_name << "\" not found.\n";
pending_goto = false;
}
}
if (!en_route || speed == 0.0 || state == "Stopped") {
this->stop();
return;
}
stepAlongHeading(speed / 100.0);
if (mode == 0 && destination) {
// Move to position, then stop
double remaining = Vehicle::computeDistance(_location->x, _location->y,
destination->x, destination->y);
if (remaining < 1 && !arrived) {
_location->x = destination->x;
_location->y = destination->y;
setState("Stopped");
speed = 0.0;
en_route = false;
arrived = true;
} else {
setState("Moving to position");
}
} else if (mode == 1) {
// Course mode: move forever
setState("Moving to " + std::to_string(angle));
} else if (mode == 2 && destination) {
// Patrol/warehouse mode
double remaining = Vehicle::computeDistance(_location->x, _location->y,
destination->x, destination->y);
if (remaining < 1 && !arrived) {
_location->x = destination->x;
_location->y = destination->y;
setState("Arrived at " + currentDestinationName);
en_route = false;
arrived = true;
// Start patrol to next warehouse (unless all visited)
startPatrol();
}
}
}
void State_trooper::broadcast_current_state() {
std::cout << "State_trooper " << getName()
<< " at (" << std::fixed << std::setprecision(2)
<< _location->x << ", " << _location->y << "), ";
if (mode == 2 && en_route) {
// Scheduled route or manual move in progress
std::cout << "Heading to " << currentDestinationName << ",speed: " << speed << " km/h" << std::endl;
}
else if (mode == 1 && en_route) {
std::cout << "Moving on course " << std::fmod(angle, 360.0) << " deg, speed: " << speed << " km/h" << std::endl;
}
else if (mode == 0 && en_route) {
std::cout << "Moving to position (" << destination->x << ", " << destination->y << "), speed: " << speed << " km/h" << std::endl;
}
else {
std::cout << this->state << "." << std::endl;
}
}
void State_trooper::startPatrol() {
const auto& warehouses = Model::getInstance().getWarehouses();
std::string nextName;
std::shared_ptr<Point> nextLocation = nullptr;
double minDist = 1e9;
for (const auto& wh : warehouses) {
if (visitedWarehouses.count(wh->getName()) == 0) {
double dist = Vehicle::computeDistance(_location->x, _location->y,
wh->get_location()->x, wh->get_location()->y);
if ((dist < minDist) || (dist == minDist && wh->getName() < nextName)) {
minDist = dist;
nextName = wh->getName();
nextLocation = wh->get_location();
}
}
}
if (nextLocation) {
destination = nextLocation;
angle = to_degrees(std::atan2(destination->x - _location->x,
destination->y - _location->y));
speed = 90.0;
en_route = true;
arrived = false;
currentDestinationName = nextName;
visitedWarehouses.insert(nextName);
setState("Heading to " + nextName);
} else {
// All warehouses visited
destination = nullptr;
speed = 0.0;
setState("Stopped");
}
}