Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
noise_generator.hpp
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 #ifndef NAV2_MPPI_CONTROLLER__TOOLS__NOISE_GENERATOR_HPP_
16 #define NAV2_MPPI_CONTROLLER__TOOLS__NOISE_GENERATOR_HPP_
17 
18 #include <Eigen/Dense>
19 
20 #include <string>
21 #include <memory>
22 #include <thread>
23 #include <mutex>
24 #include <condition_variable>
25 #include <random>
26 
27 #include "nav2_ros_common/lifecycle_node.hpp"
28 #include "nav2_mppi_controller/models/optimizer_settings.hpp"
29 #include "nav2_mppi_controller/tools/parameters_handler.hpp"
30 #include "nav2_mppi_controller/models/control_sequence.hpp"
31 #include "nav2_mppi_controller/models/state.hpp"
32 
33 namespace mppi
34 {
35 
41 {
42 public:
46  NoiseGenerator() = default;
47 
55  void initialize(
57  bool is_holonomic, const std::string & name, ParametersHandler * param_handler);
58 
62  void shutdown();
63 
68  void generateNextNoises();
69 
74  void setNoisedControls(models::State & state, const models::ControlSequence & control_sequence);
75 
81  void reset(mppi::models::OptimizerSettings & settings, bool is_holonomic);
82 
83 protected:
87  void noiseThread();
88 
97 
98  Eigen::ArrayXXf noises_vx_;
99  Eigen::ArrayXXf noises_vy_;
100  Eigen::ArrayXXf noises_wz_;
101 
102  std::default_random_engine generator_;
103  std::normal_distribution<float> ndistribution_vx_;
104  std::normal_distribution<float> ndistribution_wz_;
105  std::normal_distribution<float> ndistribution_vy_;
106 
108  bool is_holonomic_;
109 
110  std::thread noise_thread_;
111  std::condition_variable noise_cond_;
112  std::mutex noise_lock_;
113  bool active_{false}, ready_{false}, regenerate_noises_{false};
114 };
115 
116 } // namespace mppi
117 
118 #endif // NAV2_MPPI_CONTROLLER__TOOLS__NOISE_GENERATOR_HPP_
Generates noise trajectories from optimal trajectory.
NoiseGenerator()=default
Constructor for mppi::NoiseGenerator.
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 parameter changes.
A control sequence over time (e.g. trajectory)
Settings for the optimizer to use.
State information: velocities, controls, poses, speed.
Definition: state.hpp:32