CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
client
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
7
#include "
carla/client/WalkerAIController.h
"
8
9
#include "
carla/Exception.h
"
10
#include "
carla/Logging.h
"
11
#include "
carla/client/detail/Simulator.h
"
12
#include "
carla/client/detail/WalkerNavigation.h
"
13
14
#include <stdexcept>
15
16
namespace
carla
{
17
namespace
client
{
18
19
WalkerAIController::WalkerAIController
(
ActorInitializer
init)
20
:
Actor
(
std
::move(init)) {}
21
22
void
WalkerAIController::Start
() {
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
52
void
WalkerAIController::Stop
() {
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
97
void
WalkerAIController::GoToLocation
(
const
carla::geom::Location
&destination) {
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
Exception.h
WalkerAIController.h
Logging.h
Simulator.h
WalkerNavigation.h
carla::client::ActorInitializer
Used to initialize Actor classes.
Definition
client/detail/ActorState.h:112
carla::client::Actor::Actor
Actor(ActorInitializer init)
Definition
client/Actor.h:25
carla::client::WalkerAIController::GetRandomLocation
std::optional< geom::Location > GetRandomLocation()
Definition
WalkerAIController.cpp:84
carla::client::WalkerAIController::SetMaxSpeed
void SetMaxSpeed(const float max_speed)
Definition
WalkerAIController.cpp:124
carla::client::WalkerAIController::GoToLocation
void GoToLocation(const carla::geom::Location &destination)
Definition
WalkerAIController.cpp:97
carla::client::WalkerAIController::Stop
void Stop()
Definition
WalkerAIController.cpp:52
carla::client::WalkerAIController::Start
void Start()
Definition
WalkerAIController.cpp:22
carla::client::WalkerAIController::WalkerAIController
WalkerAIController(ActorInitializer init)
Definition
WalkerAIController.cpp:19
carla::client::detail::ActorState::GetEpisode
EpisodeProxy & GetEpisode()
Definition
client/detail/ActorState.h:85
carla::client::detail::ActorState::GetParent
SharedPtr< Actor > GetParent() const
Definition
ActorState.cpp:31
carla::client::detail::ActorState::GetDisplayId
const std::string & GetDisplayId() const
Definition
client/detail/ActorState.h:35
carla::client::detail::EpisodeProxyImpl::Lock
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
Definition
EpisodeProxy.cpp:39
carla::client
Definition
BasicAgent.h:24
carla::nav
Definition
Navigation.cpp:22
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::throw_exception
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
std
Definition
CarlaRecorderCollision.h:31
carla::geom::Location
Definition
geom/Location.h:23
carla::geom::Vector3D::y
float y
Definition
geom/Vector3D.h:27
carla::geom::Vector3D::x
float x
Definition
geom/Vector3D.h:25
carla::geom::Vector3D::z
float z
Definition
geom/Vector3D.h:29
Generated by
1.18.0