CARLA
WalkerAIController.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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 
11 
12 namespace carla {
13 namespace client {
14 
16  : Actor(std::move(init)) {}
17 
19  GetEpisode().Lock()->RegisterAIController(*this);
20 
21  // add the walker in the Recast & Detour
22  auto walker = GetParent();
23  if (walker != nullptr) {
24  auto nav = GetEpisode().Lock()->GetNavigation();
25  if (nav != nullptr) {
26  nav->AddWalker(walker->GetId(), walker->GetLocation());
27  // disable physics and collision of walker actor
28  GetEpisode().Lock()->SetActorSimulatePhysics(*walker, false);
29  GetEpisode().Lock()->SetActorCollisions(*walker, false);
30  }
31  }
32  }
33 
35  GetEpisode().Lock()->UnregisterAIController(*this);
36 
37  // remove the walker from the Recast & Detour
38  auto walker = GetParent();
39  if (walker != nullptr) {
40  auto nav = GetEpisode().Lock()->GetNavigation();
41  if (nav != nullptr) {
42  nav->RemoveWalker(walker->GetId());
43  }
44  }
45  }
46 
47  boost::optional<geom::Location> WalkerAIController::GetRandomLocation() {
48  auto nav = GetEpisode().Lock()->GetNavigation();
49  if (nav != nullptr) {
50  return nav->GetRandomLocation();
51  }
52  return {};
53  }
54 
56  auto nav = GetEpisode().Lock()->GetNavigation();
57  if (nav != nullptr) {
58  auto walker = GetParent();
59  if (walker != nullptr) {
60  if (!nav->SetWalkerTarget(walker->GetId(), destination)) {
61  log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z);
62  }
63  } else {
64  log_warning("NAV: Failed to set request to go to ", destination.x, destination.y, destination.z, "(parent does not exist)");
65  }
66  }
67  }
68 
69  void WalkerAIController::SetMaxSpeed(const float max_speed) {
70  auto nav = GetEpisode().Lock()->GetNavigation();
71  if (nav != nullptr) {
72  auto walker = GetParent();
73  if (walker != nullptr) {
74  if (!nav->SetWalkerMaxSpeed(walker->GetId(), max_speed)) {
75  log_warning("NAV: failed to set max speed");
76  }
77  } else {
78  log_warning("NAV: failed to set max speed (parent does not exist)");
79  }
80  }
81  }
82 
83 } // namespace client
84 } // namespace carla
WalkerAIController(ActorInitializer init)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
Used to initialize Actor classes.
void SetMaxSpeed(const float max_speed)
void GoToLocation(const carla::geom::Location &destination)
static void log_warning(Args &&... args)
Definition: Logging.h:96
SharedPtr< Actor > GetParent() const
Definition: ActorState.cpp:31
Represents an actor in the simulation.
Definition: client/Actor.h:18
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
boost::optional< geom::Location > GetRandomLocation()