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 // disable physics and collision of walker actor
47 simulator->SetActorSimulatePhysics(*walker, false);
48 simulator->SetActorCollisions(*walker, false);
49 }
50 }
51 }
52
54 auto simulator = GetEpisode().Lock();
55 if (simulator->IsNavigationServerSide()) {
56 auto walker = GetParent();
57 if (walker == nullptr) {
58 throw_exception(std::runtime_error(GetDisplayId() + ": not attached to walker"));
59 return;
60 }
61 if (!simulator->WalkerStopNavigation(walker->GetId())) {
62 log_warning("NAV: server failed to stop navigation for walker", walker->GetId());
63 }
64 return;
65 }
66
67 simulator->UnregisterAIController(*this);
68
69 // remove the walker from the Recast & Detour
70 auto walker = GetParent();
71 if (walker != nullptr) {
72 auto nav = simulator->GetNavigation();
73 if (nav != nullptr) {
74 nav->RemoveWalker(walker->GetId());
75 }
76 }
77 }
78
79 std::optional<geom::Location> WalkerAIController::GetRandomLocation() {
80 auto simulator = GetEpisode().Lock();
81 if (simulator->IsNavigationServerSide()) {
82 return simulator->GetRandomLocationFromNavigation();
83 }
84
85 auto nav = simulator->GetNavigation();
86 if (nav != nullptr) {
87 return nav->GetRandomLocation();
88 }
89 return {};
90 }
91
93 auto simulator = GetEpisode().Lock();
94 if (simulator->IsNavigationServerSide()) {
95 auto walker = GetParent();
96 if (walker != nullptr) {
97 if (!simulator->WalkerGoToLocation(walker->GetId(), destination)) {
98 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z);
99 }
100 } else {
101 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z, "(parent does not exist)");
102 }
103 return;
104 }
105
106 auto nav = simulator->GetNavigation();
107 if (nav != nullptr) {
108 auto walker = GetParent();
109 if (walker != nullptr) {
110 if (!nav->SetWalkerTarget(walker->GetId(), destination)) {
111 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z);
112 }
113 } else {
114 log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z, "(parent does not exist)");
115 }
116 }
117 }
118
119 void WalkerAIController::SetMaxSpeed(const float max_speed) {
120 auto simulator = GetEpisode().Lock();
121 if (simulator->IsNavigationServerSide()) {
122 auto walker = GetParent();
123 if (walker != nullptr) {
124 if (!simulator->WalkerSetMaxSpeed(walker->GetId(), max_speed)) {
125 log_warning("NAV: failed to set max speed");
126 }
127 } else {
128 log_warning("NAV: failed to set max speed (parent does not exist)");
129 }
130 return;
131 }
132
133 auto nav = simulator->GetNavigation();
134 if (nav != nullptr) {
135 auto walker = GetParent();
136 if (walker != nullptr) {
137 if (!nav->SetWalkerMaxSpeed(walker->GetId(), max_speed)) {
138 log_warning("NAV: failed to set max speed");
139 }
140 } else {
141 log_warning("NAV: failed to set max speed (parent does not exist)");
142 }
143 }
144 }
145
146} // namespace client
147} // namespace carla
Used to initialize Actor classes.
Represents an actor in the simulation.
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
static void log_warning(Args &&... args)
Definition Logging.h:96