Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
inspection_ros.cpp
Go to the documentation of this file.
2
4#include "message_filters/message_traits.h"
5#include "message_filters/sync_policies/approximate_time.h"
6#include "message_filters/time_synchronizer.h"
7
9 _inspection_object_.turning_period_ = declare_parameter<double>("turning_period");
10 _inspection_object_.finish_time_ = declare_parameter<double>("finish_time");
11 _inspection_object_.wheel_radius_ = declare_parameter<double>("wheel_radius");
12 _inspection_object_.max_angle_ = declare_parameter<double>("max_angle"); // 22.5 degrees in rad
13 _inspection_object_.start_and_stop_ = declare_parameter<bool>("start_and_stop");
14 declare_parameter<double>("ebs_test_ideal_velocity");
15 declare_parameter<double>("ebs_test_gain");
16 declare_parameter<double>("inspection_ideal_velocity");
17 declare_parameter<double>("inspection_gain");
18
19 // creates publisher that should yield throttle/acceleration/...
21 this->create_publisher<custom_interfaces::msg::ControlCommand>("/control/command", 10);
22
23 // creates client for the end of mission signal
24 _finish_client_ = this->create_client<std_srvs::srv::Trigger>("/as_srv/mission_finished");
25
26 // creates client for the emergency signal
27 _emergency_client_ = this->create_client<std_srvs::srv::Trigger>("/as_srv/emergency");
28
29 // get mission
31 this->create_subscription<custom_interfaces::msg::OperationalStatus>(
32 "/vehicle/operational_status", 10,
33 std::bind(&InspectionMission::mission_decider, this, std::placeholders::_1));
34
35 _timer_ = this->create_wall_timer(
36 std::chrono::milliseconds(10),
37 std::bind(&InspectionMission::inspection_script, this)); // Timer for end of mission
38
39 _motor_rpm_subscription_ = this->create_subscription<custom_interfaces::msg::WheelRPM>(
40 "/vehicle/motor_rpm", 10,
41 std::bind(&InspectionMission::update_rpms_callback, this, std::placeholders::_1));
42
43 RCLCPP_INFO(this->get_logger(), "Inspection node has been started.");
44}
45
46bool finished = false;
47
49 custom_interfaces::msg::OperationalStatus::SharedPtr mission_signal) {
50 std::string mission_string =
51 common_lib::competition_logic::get_mission_string(mission_signal->as_mission);
52 // RCLCPP_DEBUG(this->get_logger(), "Mission received: %s", mission_string.c_str());
53
54 try {
55 if (mission_signal->as_mission == common_lib::competition_logic::Mission::INSPECTION) {
56 _inspection_object_.ideal_velocity_ = get_parameter("inspection_ideal_velocity").as_double();
57 _inspection_object_.gain_ = get_parameter("inspection_gain").as_double();
58 this->_mission_ = common_lib::competition_logic::Mission::INSPECTION;
59 // } else if (mission_signal->as_mission == common_lib::competition_logic::Mission::EBS_TEST)
60 // {
61 // _inspection_object_.ideal_velocity_ =
62 // get_parameter("ebs_test_ideal_velocity").as_double(); _inspection_object_.gain_ =
63 // get_parameter("ebs_test_gain").as_double(); this->_mission_ =
64 // common_lib::competition_logic::Mission::EBS_TEST;
65 } else {
66 RCLCPP_WARN(this->get_logger(), "Invalid mission for inspection: %s", mission_string.c_str());
67 return;
68 }
69 } catch (const rclcpp::exceptions::ParameterNotDeclaredException& exception) {
70 RCLCPP_ERROR(this->get_logger(), "Parameter not declared: %s", exception.what());
71 return;
72 }
73
74 if (mission_signal->go_signal != _go_ && mission_signal->go_signal) {
75 RCLCPP_INFO(this->get_logger(), "Starting timer");
76 _initial_time_ = this->_clock_.now();
79 this->_mission_end_timer_started_ = false;
80 this->_car_stopped_ = false;
81 }
82 _go_ = mission_signal->go_signal;
83}
84
85void InspectionMission::update_rpms_callback(const custom_interfaces::msg::WheelRPM& motor_rpm) {
86 RCLCPP_DEBUG(this->get_logger(), "Motor RPM: %f", motor_rpm.rr_rpm);
87 _motor_rpm_ = motor_rpm.rl_rpm / 4.0;
88}
89
91 if (!_go_ || _mission_ == common_lib::competition_logic::Mission::NONE) {
92 return;
93 }
94 RCLCPP_DEBUG(this->get_logger(), "Executing Inspection Script.");
95
96 if (this->_car_stopped_) {
97 // if the mission is over, publish the finish signal
98 RCLCPP_DEBUG(this->get_logger(), "Mission is over. Stopping the car.");
99 if (!this->_mission_end_timer_started_) {
100 this->_mission_end_timer_ = this->create_wall_timer(
101 std::chrono::milliseconds(500), std::bind(&InspectionMission::end_of_mission, this));
102 this->_mission_end_timer_started_ = true;
103 }
104 return;
105 }
106
107 // initialization
108 auto current_time = this->_clock_.now();
109 auto elapsed_time = (current_time - _initial_time_).seconds();
110 double average_rpm = _motor_rpm_ / 4;
111 RCLCPP_DEBUG(this->get_logger(), "average_rpm %f", average_rpm);
112 double current_velocity = _inspection_object_.rpm_to_velocity(average_rpm);
113 RCLCPP_DEBUG(this->get_logger(), "current_velocity %f", current_velocity);
114
115 // calculate steering
116 double calculated_steering = _mission_ == common_lib::competition_logic::Mission::INSPECTION
118 : 0;
119
120 // if the time is over, the car should be stopped
121 bool steering_straight = calculated_steering < 0.1 && calculated_steering > -0.1;
122 if (elapsed_time >= (_inspection_object_.finish_time_)) {
124 if (steering_straight) {
126 }
127 }
128
129 // calculate throttle and convert to control command
130 double calculated_throttle = _inspection_object_.calculate_throttle(current_velocity);
131
132 if (elapsed_time >= _inspection_object_.finish_time_ &&
133 std::abs(current_velocity) <= WHEELS_STOPPED_THRESHOLD) {
134 calculated_throttle = 0.0;
135 }
136
137 // if (elapsed_time >= _inspection_object_.finish_time_){
139 //}
140
141 if (elapsed_time >= _inspection_object_.finish_time_ && steering_straight) {
142 calculated_steering = 0.0;
143 }
144
145
146 if (elapsed_time >= _inspection_object_.finish_time_ &&
147 std::abs(current_velocity) <= WHEELS_STOPPED_THRESHOLD && steering_straight) {
148 this->_car_stopped_ = true;
149 }
150
151 // publish suitable message
152 double scaled_throttle = _inspection_object_.throttle_to_adequate_range(calculated_throttle);
153 publish_controls(scaled_throttle, calculated_steering);
154 RCLCPP_DEBUG(this->get_logger(),
155 "Publishing control command. Steering: %lf; Torque after conversion - before: %lf - "
156 "%lf; Elapsed Time: %lf",
157 calculated_steering, scaled_throttle, calculated_throttle, elapsed_time);
158
159 // update ideal velocity if necessary
161}
162
163void InspectionMission::publish_controls(double throttle, double steering) const {
164 custom_interfaces::msg::ControlCommand control_command;
165 control_command.throttle_rl = control_command.throttle_rr = throttle;
166 control_command.steering = steering;
167 _control_command_publisher_->publish(control_command);
168}
169
170// --------------------- END OF MISSION LOGIC -------------------------
171
173 RCLCPP_INFO(this->get_logger(), "Sending ending signal...");
174 if (this->_mission_ == common_lib::competition_logic::Mission::INSPECTION) {
175 this->_finish_client_->async_send_request(
176 std::make_shared<std_srvs::srv::Trigger::Request>(),
177 std::bind(&InspectionMission::handle_end_of_mission_response, this, std::placeholders::_1));
178 //} else if (this->_mission_ == common_lib::competition_logic::Mission::EBS_TEST) {
179 // this->_emergency_client_->async_send_request(
180 // std::make_shared<std_srvs::srv::Trigger::Request>(),
181 // std::bind(&InspectionMission::handle_end_of_mission_response, this,
182 // std::placeholders::_1));
183 } else {
184 RCLCPP_ERROR(
185 this->get_logger(), "Invalid mission at mission end: %s",
187 }
188}
189
191 rclcpp::Client<std_srvs::srv::Trigger>::SharedFuture future) const {
192 auto result = future.get();
193 if (result->success) {
194 this->_mission_end_timer_->cancel();
195 RCLCPP_INFO(this->get_logger(), "Mission has been finished!");
196 } else {
197 RCLCPP_ERROR(this->get_logger(), "Mission could not be finished!");
198 }
199}
void redefine_goal_velocity(double current_velocity)
used when in oscilation mode to redefine the ideal velocity
double calculate_throttle(double current_velocity) const
calculate torque output according to velocity
bool stop_oscilating_
Used to stop the oscilation near zero and avoid violent wheel movements.
double throttle_to_adequate_range(double throttle) const
convert the throttle to a range between -1 and 1
double rpm_to_velocity(double rpm) const
convert rpm [rotations/minute] to velocity [m/s]
double calculate_steering(double time) const
calculate the steering angle according to time
rclcpp::Subscription< custom_interfaces::msg::OperationalStatus >::SharedPtr _mission_signal_subscription_
rclcpp::Clock _clock_
Timer for main callback.
void publish_controls(double throttle, double steering) const
Publishes the control command.
void inspection_script()
Function to publish control command while time is less than the defined time limit.
rclcpp::Publisher< custom_interfaces::msg::ControlCommand >::SharedPtr _control_command_publisher_
void mission_decider(custom_interfaces::msg::OperationalStatus::SharedPtr mission_signal)
recieves GoSignal and stores the mission that should be ran
rclcpp::Subscription< custom_interfaces::msg::WheelRPM >::SharedPtr _motor_rpm_subscription_
rclcpp::TimerBase::SharedPtr _mission_end_timer_
InspectionMission()
Contruct a new Inspection Mission with constants defined in file.
InspectionFunctions _inspection_object_
Ellapsed time in seconds.
void update_rpms_callback(const custom_interfaces::msg::WheelRPM &motor_rpm)
Function to update the current rpms of the wheels.
void end_of_mission()
Rotations per minute of the motor.
rclcpp::Client< std_srvs::srv::Trigger >::SharedPtr _emergency_client_
rclcpp::Client< std_srvs::srv::Trigger >::SharedPtr _finish_client_
double _motor_rpm_
Mission to be executed;.
rclcpp::TimerBase::SharedPtr _timer_
void handle_end_of_mission_response(rclcpp::Client< std_srvs::srv::Trigger >::SharedFuture future) const
Function to end communication of end of mission.
common_lib::competition_logic::Mission _mission_
Flag to start the mission.
bool _mission_end_timer_started_
Timer for repetition of end of mission signal.
rclcpp::Time _initial_time_
constexpr double WHEELS_STOPPED_THRESHOLD
bool finished
std::string get_mission_string(int mission)
const std::map< Mission, std::string > MISSION_STRING_MAP