CARLA
Loading...
Searching...
No Matches
WalkerAIController.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8
9#include "carla/Exception.h"
10#include "carla/Logging.h"
13
14#include <stdexcept>
15
16namespace carla {
17namespace client {
18
21
23 auto simulator = GetEpisode().Lock();
24 if (simulator->IsNavigationServerSide()) {
25 // Server-side navigation: the walker keeps physics and collisions on;
26 // the server's crowd controller drives its CharacterMovement directly.
27 auto walker = GetParent();
28 if (walker == nullptr) {
29 throw_exception(std::runtime_error(GetDisplayId() + ": not attached to walker"));
30 return;
31 }
32 if (!simulator->WalkerStartNavigation(walker->GetId())) {
33 log_warning("NAV: server failed to start navigation for walker", walker->GetId());
34 }
35 return;
36 }
37
38 simulator->RegisterAIController(*this);
39
40 // add the walker in the Recast & Detour
41 auto walker = GetParent();
42 if (walker != nullptr) {
43 auto nav = simulator->GetNavigation();
44 if (nav != nullptr) {
45 nav->AddWalker(walker->GetId(), walker->GetLocation());
46 // The crowd teleports the walker; keep its collision on or sensors miss it (#10068).
47 simulator->SetActorSimulatePhysics(*walker, false);
48 }
49 }
50 }
51
53 auto simulator = GetEpisode().Lock();
54 if (simulator->IsNavigationServerSide()) {
55 auto walker = GetParent();
56 if (walker == nullptr) {
57 throw_exception(std::runtime_error(GetDisplayId() + ": not attached to walker"));
58 return;
59 }
60 if (!simulator->WalkerStopNavigation(walker->GetId())) {
61 log_warning("NAV: server failed to stop navigation for walker", walker->GetId());
62 }
63 return;
64 }
65
66 simulator->UnregisterAIController(*this);
67
68 // remove the walker from the Recast & Detour
69 auto walker = GetParent();
70 if (walker != nullptr) {
71 auto nav = simulator->GetNavigation();
72 if (nav != nullptr) {
73 nav->RemoveWalker(walker->GetId());
74 // Restore movement so a stopped walker obeys apply_control() again.
75 try {
76 simulator->SetActorSimulatePhysics(*walker, true);
77 } catch (const std::exception &e) {
78 log_warning("NAV: could not restore movement of walker", walker->GetId(), ":", e.what());
79 }
80 }
81 }
82 }
83
84 std::optional<geom::Location> WalkerAIController::GetRandomLocation() {
85 auto simulator = GetEpisode().Lock();
86 if (simulator->IsNavigationServerSide()) {
87 return simulator->GetRandomLocationFromNavigation();
88 }
89
90 auto nav = simulator->GetNavigation();
91 if (nav != nullptr) {
92 return nav->GetRandomLocation();
93 }
94 return {};
95 }
96
98 auto simulator = GetEpisode().Lock();
99 if (simulator->IsNavigationServerSide()) {
100 auto walker = GetParent();
101 if (walker != nullptr) {
102 if (!simulator->WalkerGoToLocation(walker->GetId(), destination)) {
103 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z);
104 }
105 } else {
106 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z, "(parent does not exist)");
107 }
108 return;
109 }
110
111 auto nav = simulator->GetNavigation();
112 if (nav != nullptr) {
113 auto walker = GetParent();
114 if (walker != nullptr) {
115 if (!nav->SetWalkerTarget(walker->GetId(), destination)) {
116 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z);
117 }
118 } else {
119 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z, "(parent does not exist)");
120 }
121 }
122 }
123
124 void WalkerAIController::SetMaxSpeed(const float max_speed) {
125 auto simulator = GetEpisode().Lock();
126 if (simulator->IsNavigationServerSide()) {
127 auto walker = GetParent();
128 if (walker != nullptr) {
129 if (!simulator->WalkerSetMaxSpeed(walker->GetId(), max_speed)) {
130 log_warning("NAV: failed to set max speed");
131 }
132 } else {
133 log_warning("NAV: failed to set max speed (parent does not exist)");
134 }
135 return;
136 }
137
138 auto nav = simulator->GetNavigation();
139 if (nav != nullptr) {
140 auto walker = GetParent();
141 if (walker != nullptr) {
142 if (!nav->SetWalkerMaxSpeed(walker->GetId(), max_speed)) {
143 log_warning("NAV: failed to set max speed");
144 }
145 } else {
146 log_warning("NAV: failed to set max speed (parent does not exist)");
147 }
148 }
149 }
150
151} // namespace client
152} // namespace carla
Used to initialize Actor classes.
Actor(ActorInitializer init)
std::optional< geom::Location > GetRandomLocation()
void SetMaxSpeed(const float max_speed)
void GoToLocation(const carla::geom::Location &destination)
WalkerAIController(ActorInitializer init)
SharedPtr< Actor > GetParent() const
const std::string & GetDisplayId() const
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
This file contains definitions of common data structures used in traffic manager.
void throw_exception(const ExceptionT &e)
Templated on the static type so the thrown object keeps its dynamic type: taking a const std::excepti...
Definition Exception.h:44