Autonomous Racing  1
f1tenth Project Group of Technical University Dortmund, Germany
vesc_sim.cpp
Go to the documentation of this file.
1 #include "vesc_sim.h"
2 #include "car_config.h"
3 #include <cmath>
4 #include <geometry_msgs/TransformStamped.h>
5 #include <nav_msgs/Odometry.h>
6 
8 {
9  m_yaw = 0;
10  m_servo_data = 0.5;
11  m_state_speed = 0;
12  m_x_position = 0;
13  m_x_dot = 0;
14  m_y_position = 0;
15  m_y_dot = 0;
16  m_current_speed = 0;
17  m_current_steering_angle = 0;
18  m_current_angular_velocity = 0;
19  m_started = false;
20  m_frequency = 50.0;
21  m_last_stamp = ros::Time::now();
22  m_odom_frame = "odom";
23  m_base_frame = "base_link";
24 
25  this->m_odometry_publisher = this->m_node_handle.advertise<nav_msgs::Odometry>("odom", 10);
26 }
27 
29 {
30  if (!m_started)
31  {
32  m_timer = m_node_handle.createTimer(ros::Duration(1.0 / m_frequency), &VESCSimulator::timerCallback, this);
33  m_started = true;
34  }
35 }
36 
38 {
39  if (m_started)
40  {
41  m_timer.stop();
42  m_started = false;
43  }
44 }
45 
46 void VESCSimulator::timerCallback(const ros::TimerEvent& event)
47 {
48 
49  // convert to engineering units
50  m_current_speed = m_state_speed * car_config::ERPM_TO_SPEED / car_config::TRANSMISSION; // m/s
51  m_current_steering_angle =
53  m_current_angular_velocity = m_current_speed * std::tan(m_current_steering_angle) / car_config::WHEELBASE; // rad/s
54 
55  // calc elapsed time
56  ros::Time stamp_now = ros::Time::now();
57  ros::Duration dt = stamp_now - m_last_stamp;
58  m_last_stamp = stamp_now;
59 
60  // propagate odometry
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(); // meter
64  m_y_position += m_y_dot * dt.toSec(); // meter
65  m_yaw += m_current_angular_velocity * dt.toSec();
66 
67  // publish odometry message
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;
72 
73  // Position
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);
80 
81  // Position uncertainty
83  odom->pose.covariance[0] = 0.2;
84  odom->pose.covariance[7] = 0.2;
85  odom->pose.covariance[35] = 0.4;
86 
87  // Velocity ("in the coordinate frame given by the child_frame_id")
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;
91 
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;
100 
101  if (ros::ok())
102  {
103  m_odometry_publisher.publish(odom);
104  m_tf_publisher.sendTransform(tf);
105  }
106 }
constexpr double TRANSMISSION
Gear transmission inside the differential of the car. This is an estimate, the exact value is not kno...
Definition: car_config.h:83
void start()
Definition: vesc_sim.cpp:28
constexpr double WHEELBASE
The distance between the front and rear axes.
Definition: car_config.h:15
constexpr double STEERING_TO_SERVO_GAIN
Conversion factor from steering angle to servo input 1/radian.
Definition: car_config.h:73
constexpr double STEERING_TO_SERVO_OFFSET
Position of the servo for normal wheel position.
Definition: car_config.h:67
void stop()
Definition: vesc_sim.cpp:37
constexpr double ERPM_TO_SPEED
Conversion factor from electrical revolutions per minute to meters per second m/s * minute = m...
Definition: car_config.h:50