Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
invictasim_node.cpp
Go to the documentation of this file.
1
#include "
node/invictasim_node.hpp
"
2
3
#include "
common_lib/config_load/config_load.hpp
"
4
#include "
config/config_loader.hpp
"
5
#include "
vehicle_model/bicycle_model.hpp
"
6
7
InvictaSimNode::InvictaSimNode
(
const
InvictaSimParameters
& params)
8
: Node(
"invictasim_node"
), params_(params), sim_time_(0.0) {
9
std::string
vehicle_model_config_path
=
common_lib::config_load::get_config_yaml_path
(
10
"invictasim"
,
"invictasim/vehicle_models"
,
params_
.
vehicle_model
);
11
// Create vehicle model based on config
12
if
(
params_
.
vehicle_model
==
"bicycle_model"
) {
13
vehicle_model_
= std::make_shared<BicycleModel>(
vehicle_model_config_path
);
14
}
else
{
15
RCLCPP_ERROR(this->get_logger(),
"Unknown vehicle model: %s"
,
params_
.
vehicle_model
.c_str());
16
throw
std::runtime_error(
"Unknown vehicle model"
);
17
}
18
19
// Publishers, subscribers, etc. initialized here
20
test_pub_
= this->create_publisher<std_msgs::msg::Float64>(
"/invictasim/test_topic"
, 10);
21
22
init
();
23
}
24
25
void
InvictaSimNode::init
() {
26
// Create simulation timer
27
auto
timer_period = std::chrono::microseconds(
28
static_cast<
int
>
((
params_
.
timestep
/
params_
.
simulation_speed
) * 1000000.0));
29
simulation_timer_
=
30
this->create_wall_timer(timer_period, std::bind(&
InvictaSimNode::simulation_step
,
this
));
31
32
next_loop_time_
= std::chrono::steady_clock::now();
33
}
34
35
void
InvictaSimNode::simulation_step
() {
36
// Synchronize to real-time: wait until it's time for the next step
37
auto
now = std::chrono::steady_clock::now();
38
if
(now <
next_loop_time_
) {
39
std::this_thread::sleep_until(
next_loop_time_
);
40
}
41
next_loop_time_
= std::chrono::steady_clock::now() +
42
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
43
std::chrono::duration<double>(
params_
.
timestep
/
params_
.
simulation_speed
));
44
45
// Advance simulation time
46
sim_time_
+=
params_
.
timestep
;
47
48
// Update vehicle dynamics
49
vehicle_model_
->step(
params_
.
timestep
);
50
51
// Publish test message
52
auto
msg = std_msgs::msg::Float64();
53
msg.data =
sim_time_
;
54
test_pub_
->publish(msg);
55
}
InvictaSimNode::InvictaSimNode
InvictaSimNode(const InvictaSimParameters ¶ms)
Constructor.
Definition
invictasim_node.cpp:7
InvictaSimNode::next_loop_time_
std::chrono::steady_clock::time_point next_loop_time_
Definition
invictasim_node.hpp:32
InvictaSimNode::init
void init()
Initialize the simulator node.
Definition
invictasim_node.cpp:25
InvictaSimNode::params_
InvictaSimParameters params_
Definition
invictasim_node.hpp:25
InvictaSimNode::sim_time_
double sim_time_
Definition
invictasim_node.hpp:31
InvictaSimNode::vehicle_model_
std::shared_ptr< VehicleModel > vehicle_model_
Definition
invictasim_node.hpp:28
InvictaSimNode::simulation_timer_
rclcpp::TimerBase::SharedPtr simulation_timer_
Definition
invictasim_node.hpp:33
InvictaSimNode::simulation_step
void simulation_step()
Perform a single simulation step.
Definition
invictasim_node.cpp:35
InvictaSimNode::test_pub_
rclcpp::Publisher< std_msgs::msg::Float64 >::SharedPtr test_pub_
Definition
invictasim_node.hpp:36
config_load.hpp
config_loader.hpp
bicycle_model.hpp
invictasim_node.hpp
common_lib::config_load::get_config_yaml_path
std::string get_config_yaml_path(const std::string &package_name, const std::string &dir, const std::string &filename)
Definition
config_load.cpp:4
vehicle_model_config_path
std::string vehicle_model_config_path
Definition
pacsim_main.cpp:114
InvictaSimParameters
Definition
config_parameters.hpp:5
InvictaSimParameters::vehicle_model
std::string vehicle_model
Definition
config_parameters.hpp:13
InvictaSimParameters::simulation_speed
double simulation_speed
Definition
config_parameters.hpp:9
InvictaSimParameters::timestep
double timestep
Definition
config_parameters.hpp:12
src
invictasim
src
node
invictasim_node.cpp
Generated by
1.9.8