Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
noise_generator.cpp
1 // Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nav2_mppi_controller/tools/noise_generator.hpp"
16 
17 #include <memory>
18 #include <mutex>
19 #include <xtensor/xmath.hpp>
20 #include <xtensor/xrandom.hpp>
21 #include <xtensor/xnoalias.hpp>
22 
23 namespace mppi
24 {
25 
27  mppi::models::OptimizerSettings & settings, bool is_holonomic,
28  const std::string & name, ParametersHandler * param_handler)
29 {
30  settings_ = settings;
31  is_holonomic_ = is_holonomic;
32  active_ = true;
33 
34  auto getParam = param_handler->getParamGetter(name);
35  getParam(regenerate_noises_, "regenerate_noises", false);
36 
37  if (regenerate_noises_) {
38  noise_thread_ = std::thread(std::bind(&NoiseGenerator::noiseThread, this));
39  } else {
41  }
42 }
43 
45 {
46  active_ = false;
47  ready_ = true;
48  noise_cond_.notify_all();
49  if (noise_thread_.joinable()) {
50  noise_thread_.join();
51  }
52 }
53 
55 {
56  // Trigger the thread to run in parallel to this iteration
57  // to generate the next iteration's noises (if applicable).
58  {
59  std::unique_lock<std::mutex> guard(noise_lock_);
60  ready_ = true;
61  }
62  noise_cond_.notify_all();
63 }
64 
66  models::State & state,
67  const models::ControlSequence & control_sequence)
68 {
69  std::unique_lock<std::mutex> guard(noise_lock_);
70 
71  xt::noalias(state.cvx) = control_sequence.vx + noises_vx_;
72  xt::noalias(state.cvy) = control_sequence.vy + noises_vy_;
73  xt::noalias(state.cwz) = control_sequence.wz + noises_wz_;
74 }
75 
76 void NoiseGenerator::reset(mppi::models::OptimizerSettings & settings, bool is_holonomic)
77 {
78  settings_ = settings;
79  is_holonomic_ = is_holonomic;
80 
81  // Recompute the noises on reset, initialization, and fallback
82  {
83  std::unique_lock<std::mutex> guard(noise_lock_);
84  xt::noalias(noises_vx_) = xt::zeros<float>({settings_.batch_size, settings_.time_steps});
85  xt::noalias(noises_vy_) = xt::zeros<float>({settings_.batch_size, settings_.time_steps});
86  xt::noalias(noises_wz_) = xt::zeros<float>({settings_.batch_size, settings_.time_steps});
87  ready_ = true;
88  }
89 
90  if (regenerate_noises_) {
91  noise_cond_.notify_all();
92  } else {
94  }
95 }
96 
98 {
99  do {
100  std::unique_lock<std::mutex> guard(noise_lock_);
101  noise_cond_.wait(guard, [this]() {return ready_;});
102  ready_ = false;
104  } while (active_);
105 }
106 
108 {
109  auto & s = settings_;
110 
111  xt::noalias(noises_vx_) = xt::random::randn<float>(
112  {s.batch_size, s.time_steps}, 0.0f,
113  s.sampling_std.vx);
114  xt::noalias(noises_wz_) = xt::random::randn<float>(
115  {s.batch_size, s.time_steps}, 0.0f,
116  s.sampling_std.wz);
117  if (is_holonomic_) {
118  xt::noalias(noises_vy_) = xt::random::randn<float>(
119  {s.batch_size, s.time_steps}, 0.0f,
120  s.sampling_std.vy);
121  }
122 }
123 
124 } // namespace mppi
void reset(mppi::models::OptimizerSettings &settings, bool is_holonomic)
Reset noise generator with settings and model types.
void initialize(mppi::models::OptimizerSettings &settings, bool is_holonomic, const std::string &name, ParametersHandler *param_handler)
Initialize noise generator with settings and model types.
void generateNoisedControls()
Generate random controls by gaussian noise with mean in control_sequence_.
void noiseThread()
Thread to execute noise generation process.
void setNoisedControls(models::State &state, const models::ControlSequence &control_sequence)
set noised control_sequence to state controls
void shutdown()
Shutdown noise generator thread.
void generateNextNoises()
Signal to the noise thread the controller is ready to generate a new noised control for the next iter...
Handles getting parameters and dynamic parmaeter changes.
auto getParamGetter(const std::string &ns)
Get an object to retreive parameters.
A control sequence over time (e.g. trajectory)
Settings for the optimizer to use.
State information: velocities, controls, poses, speed.
Definition: state.hpp:36