Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
no_rear_wss_ekf.cpp
Go to the documentation of this file.
2
20
22 this->imu_data_ = imu_data;
23 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"), "1 - State: %f %f %f", this->_state_(0),
24 this->_state_(1), this->_state_(2));
25 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "1 - Covariance: \n"
26 << this->_covariance_);
27 if (!this->imu_data_received_) {
28 this->imu_data_received_ = true;
29 } else {
30 // this->predict(this->_state_, this->_covariance_, this->_process_noise_matrix_,
31 // this->_last_update_, this->imu_data_);
32 }
33 this->_last_update_ = rclcpp::Clock().now();
34 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"), "2 - State: %f %f %f", this->_state_(0),
35 this->_state_(1), this->_state_(2));
36 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "2 - Covariance: \n"
37 << this->_covariance_);
38
39 this->correct_imu(this->_state_, this->_covariance_, this->imu_data_);
40 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"), "3 - State: %f %f %f", this->_state_(0),
41 this->_state_(1), this->_state_(2));
42 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "3 - Covariance: \n"
43 << this->_covariance_);
44}
45
47 this->wss_data_ = wss_data;
48 this->wss_data_received_ = true;
50 this->correct_wheels(this->_state_, this->_covariance_, this->wss_data_, this->motor_rpm_,
51 this->steering_angle_);
52 this->wss_data_received_ = false;
53 this->steering_angle_received_ = false;
54 this->motor_rpm_received_ = false;
55 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"), "3 - State: %f %f %f", this->_state_(0),
56 this->_state_(1), this->_state_(2));
57 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "3 - Covariance: \n"
58 << this->_covariance_);
59 }
60}
61
62void NoRearWSSEKF::motor_rpm_callback(double motor_rpm) {
63 this->motor_rpm_ = motor_rpm;
64 this->motor_rpm_received_ = true;
66 this->correct_wheels(this->_state_, this->_covariance_, this->wss_data_, this->motor_rpm_,
67 this->steering_angle_);
68 this->wss_data_received_ = false;
69 this->steering_angle_received_ = false;
70 this->motor_rpm_received_ = false;
71 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"), "3 - State: %f %f %f", this->_state_(0),
72 this->_state_(1), this->_state_(2));
73 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "3 - Covariance: \n"
74 << this->_covariance_);
75 }
76}
77
78void NoRearWSSEKF::steering_callback(double steering_angle) {
79 this->steering_angle_ = steering_angle;
80 this->steering_angle_received_ = true;
81 if (this->wss_data_received_ && this->motor_rpm_received_) {
82 this->correct_wheels(this->_state_, this->_covariance_, this->wss_data_, this->motor_rpm_,
83 this->steering_angle_);
84 this->wss_data_received_ = false;
85 this->steering_angle_received_ = false;
86 this->motor_rpm_received_ = false;
87 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"), "3 - State: %f %f %f", this->_state_(0),
88 this->_state_(1), this->_state_(2));
89 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "3 - Covariance: \n"
90 << this->_covariance_);
91 }
92}
93
96 velocities.velocity_x = this->_state_(0);
97 velocities.velocity_y = this->_state_(1);
98 velocities.rotational_velocity = this->_state_(2);
99 velocities.timestamp_ = rclcpp::Clock().now();
100 velocities.velocity_x_noise_ = this->_covariance_(0, 0);
101 velocities.velocity_y_noise_ = this->_covariance_(1, 1);
102 velocities.rotational_velocity_noise_ = this->_covariance_(2, 2);
103 return velocities;
104}
105
106void NoRearWSSEKF::predict(Eigen::Vector3d& state, Eigen::Matrix3d& covariance,
107 const Eigen::Matrix3d& process_noise_matrix,
108 const rclcpp::Time last_update,
110 rclcpp::Time current_time_point =
111 rclcpp::Clock().now(); // TODO: change calculation to use message timestamps
112 double dt = (current_time_point - last_update).seconds();
113 Eigen::Vector3d accelerations(imu_data.acceleration_x, imu_data.acceleration_y, 0.0);
114
115 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"),
116 "predict - Process noise matrix: \n"
117 << process_noise_matrix);
118
119 Eigen::Matrix3d jacobian = this->process_model->get_jacobian_velocities(state, accelerations, dt);
120 covariance = jacobian * covariance * jacobian.transpose() + process_noise_matrix;
121 state = this->process_model->get_next_velocities(state, accelerations, dt);
122 this->_has_made_prediction_ = true;
123}
124
125void NoRearWSSEKF::correct_wheels(Eigen::Vector3d& state, Eigen::Matrix3d& covariance,
127 double motor_rpm, double steering_angle) {
128 Eigen::VectorXd predicted_observations = this->observation_model_->expected_observations(state);
129 Eigen::VectorXd used_predicted_observations(4);
130 used_predicted_observations << predicted_observations(0), predicted_observations(1),
131 predicted_observations(4), predicted_observations(5); // Skip rear wss
132 Eigen::VectorXd observations = Eigen::VectorXd::Zero(4);
133 observations << wss_data.fl_rpm, wss_data.fr_rpm, steering_angle, motor_rpm;
134 Eigen::VectorXd y = observations - used_predicted_observations;
135 Eigen::MatrixXd jacobian = this->observation_model_->expected_observations_jacobian(state);
136 Eigen::MatrixXd used_jacobian = Eigen::MatrixXd::Zero(4, 3);
137 used_jacobian.block(0, 0, 2, 3) = jacobian.block(0, 0, 2, 3); // Skip rear wss
138 used_jacobian.block(2, 0, 2, 3) = jacobian.block(4, 0, 2, 3); // Skip rear wss
139 double steering_to_vy = used_jacobian(2, 1);
140 used_jacobian.col(1).setZero();
141 used_jacobian(2, 1) = steering_to_vy;
142 Eigen::MatrixXd kalman_gain = covariance * used_jacobian.transpose() *
143 (used_jacobian * covariance * used_jacobian.transpose() +
145 .inverse();
146
147 // DEBUG PRINTS
148 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"),
149 "correct_wheels - Predicted observations: %f %f %f %f",
150 used_predicted_observations(0), used_predicted_observations(1),
151 used_predicted_observations(2), used_predicted_observations(3));
152 RCLCPP_DEBUG(rclcpp::get_logger("velocity_estimation"),
153 "correct_wheels - Observations: %f %f %f %f %f %f", observations(0), observations(1),
154 observations(2), observations(3), observations(4), observations(5));
155 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_wheels - Covariance: \n"
156 << covariance);
157 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_wheels - y: \n" << y);
158 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_wheels - Jacobian: \n"
159 << used_jacobian);
160 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_wheels - Kalman gain: \n"
161 << kalman_gain);
162 double prev_omega = state(2);
163 state += kalman_gain * y;
164 state(2) = prev_omega;
165 if (this->_has_made_prediction_)
166 covariance = (Eigen::Matrix3d::Identity() - kalman_gain * used_jacobian) * covariance;
167}
168
169void NoRearWSSEKF::correct_imu(Eigen::Vector3d& state, Eigen::Matrix3d& covariance,
171 Eigen::VectorXd y = Eigen::VectorXd(1);
172 y(0) = imu_data.rotational_velocity - state(2);
173 Eigen::MatrixXd jacobian = Eigen::MatrixXd(1, 3);
174 jacobian(0, 0) = 0;
175 jacobian(0, 1) = 0;
176 jacobian(0, 2) = 1;
177 Eigen::MatrixXd kalman_gain =
178 covariance * jacobian.transpose() *
179 (jacobian * covariance * jacobian.transpose() + this->_imu_measurement_noise_matrix_)
180 .inverse();
181
182 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_imu - Covariance: \n"
183 << covariance);
184 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_imu - y: \n" << y);
185 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_imu - Jacobian: \n"
186 << jacobian);
187 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("velocity_estimation"), "correct_imu - Kalman gain: \n"
188 << kalman_gain);
189 state += kalman_gain * y;
190 if (this->_has_made_prediction_)
191 covariance = (Eigen::Matrix3d::Identity() - kalman_gain * jacobian) * covariance;
192}
NoRearWSSEKF(const VEParameters &params)
bool steering_angle_received_
common_lib::structures::Velocities get_velocities() override
std::shared_ptr< VEObservationModel > observation_model_
Eigen::Matrix3d _covariance_
rclcpp::Time _last_update_
common_lib::sensor_data::ImuData imu_data_
void predict(Eigen::Vector3d &state, Eigen::Matrix3d &covariance, const Eigen::Matrix3d &process_noise_matrix, const rclcpp::Time last_update, common_lib::sensor_data::ImuData &imu_data)
Predict velocities at the next index based on IMU measurements and current state.
std::shared_ptr< BaseVelocityProcessModel > process_model
void correct_wheels(Eigen::Vector3d &state, Eigen::Matrix3d &covariance, common_lib::sensor_data::WheelEncoderData &wss_data, double motor_rpm, double steering_angle)
Correct the state estimate based on wheel speed sensor, resolver, and steering data.
void imu_callback(const common_lib::sensor_data::ImuData &imu_data) override
Callback function for the IMU data that should be called by adapters when new IMU data is available.
Eigen::MatrixXd _wheels_measurement_noise_matrix_
void correct_imu(Eigen::Vector3d &state, Eigen::Matrix3d &covariance, common_lib::sensor_data::ImuData &imu_data)
void motor_rpm_callback(double motor_rpm) override
Callback function for the motor RPM data that should be called by adapters when new motor RPM data is...
Eigen::Vector3d _state_
common_lib::sensor_data::WheelEncoderData wss_data_
Eigen::MatrixXd _imu_measurement_noise_matrix_
Eigen::Matrix3d _process_noise_matrix_
void wss_callback(const common_lib::sensor_data::WheelEncoderData &wss_data) override
Callback function for the wheel speed sensor data that should be called by adapters when new wheel sp...
common_lib::car_parameters::CarParameters car_parameters_
void steering_callback(double steering_angle) override
Callback function for the steering angle data that should be called by adapters when new steering ang...
const std::map< std::string, std::function< std::shared_ptr< BaseVelocityProcessModel >()>, std::less<> > vel_process_models_map
Definition map.hpp:15
const std::map< std::string, std::function< std::shared_ptr< VEObservationModel >(const common_lib::car_parameters::CarParameters &)>, std::less<> > ve_observation_models_map
Definition map.hpp:18
double wheel_speed_noise_
std::string _ve_observation_model_name_
double motor_rpm_noise_
double imu_acceleration_noise_
double imu_rotational_noise_
std::string _process_model_name_
double steering_angle_noise_
common_lib::car_parameters::CarParameters car_parameters_