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.
2
6
8 : Node("invictasim_node"), params_(params), sim_time_(0.0) {
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
26 // Create simulation timer
27 auto timer_period = std::chrono::microseconds(
28 static_cast<int>((params_.timestep / params_.simulation_speed) * 1000000.0));
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
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
47
48 // Update vehicle dynamics
50
51 // Publish test message
52 auto msg = std_msgs::msg::Float64();
53 msg.data = sim_time_;
54 test_pub_->publish(msg);
55}
InvictaSimNode(const InvictaSimParameters &params)
Constructor.
std::chrono::steady_clock::time_point next_loop_time_
void init()
Initialize the simulator node.
InvictaSimParameters params_
std::shared_ptr< VehicleModel > vehicle_model_
rclcpp::TimerBase::SharedPtr simulation_timer_
void simulation_step()
Perform a single simulation step.
rclcpp::Publisher< std_msgs::msg::Float64 >::SharedPtr test_pub_
std::string get_config_yaml_path(const std::string &package_name, const std::string &dir, const std::string &filename)
std::string vehicle_model_config_path