4 #include <geometry_msgs/TransformStamped.h> 5 #include <nav_msgs/Odometry.h> 17 m_current_steering_angle = 0;
18 m_current_angular_velocity = 0;
21 m_last_stamp = ros::Time::now();
22 m_odom_frame =
"odom";
23 m_base_frame =
"base_link";
25 this->m_odometry_publisher = this->m_node_handle.advertise<nav_msgs::Odometry>(
"odom", 10);
32 m_timer = m_node_handle.createTimer(ros::Duration(1.0 / m_frequency), &VESCSimulator::timerCallback,
this);
46 void VESCSimulator::timerCallback(
const ros::TimerEvent& event)
51 m_current_steering_angle =
53 m_current_angular_velocity = m_current_speed * std::tan(m_current_steering_angle) /
car_config::WHEELBASE;
56 ros::Time stamp_now = ros::Time::now();
57 ros::Duration dt = stamp_now - m_last_stamp;
58 m_last_stamp = stamp_now;
61 m_x_dot = m_current_speed * std::cos(m_yaw);
62 m_y_dot = m_current_speed * std::sin(m_yaw);
63 m_x_position += m_x_dot * dt.toSec();
64 m_y_position += m_y_dot * dt.toSec();
65 m_yaw += m_current_angular_velocity * dt.toSec();
68 nav_msgs::Odometry::Ptr odom(
new nav_msgs::Odometry);
69 odom->header.frame_id = m_odom_frame;
70 odom->header.stamp = stamp_now;
71 odom->child_frame_id = m_base_frame;
74 odom->pose.pose.position.x = m_x_position;
75 odom->pose.pose.position.y = m_y_position;
76 odom->pose.pose.orientation.x = 0.0;
77 odom->pose.pose.orientation.y = 0.0;
78 odom->pose.pose.orientation.z = std::sin(m_yaw / 2.0);
79 odom->pose.pose.orientation.w = std::cos(m_yaw / 2.0);
83 odom->pose.covariance[0] = 0.2;
84 odom->pose.covariance[7] = 0.2;
85 odom->pose.covariance[35] = 0.4;
88 odom->twist.twist.linear.x = m_current_speed;
89 odom->twist.twist.linear.y = 0.0;
90 odom->twist.twist.angular.z = m_current_angular_velocity;
92 geometry_msgs::TransformStamped tf;
93 tf.header.frame_id = m_odom_frame;
94 tf.child_frame_id = m_base_frame;
95 tf.header.stamp = stamp_now;
96 tf.transform.translation.x = m_x_position;
97 tf.transform.translation.y = m_y_position;
98 tf.transform.translation.z = 0.0;
99 tf.transform.rotation = odom->pose.pose.orientation;
103 m_odometry_publisher.publish(odom);
104 m_tf_publisher.sendTransform(tf);
constexpr double TRANSMISSION
Gear transmission inside the differential of the car. This is an estimate, the exact value is not kno...
constexpr double WHEELBASE
The distance between the front and rear axes.
constexpr double STEERING_TO_SERVO_GAIN
Conversion factor from steering angle to servo input 1/radian.
constexpr double STEERING_TO_SERVO_OFFSET
Position of the servo for normal wheel position.
constexpr double ERPM_TO_SPEED
Conversion factor from electrical revolutions per minute to meters per second m/s * minute = m...