Autonomous Racing  1
f1tenth Project Group of Technical University Dortmund, Germany
vesc_sim_driver.cpp
Go to the documentation of this file.
1 #include "vesc_sim_driver.h"
2 #include "car_config.h"
3 #include <cmath>
4 
6 {
7  this->m_servo_position_subscriber =
8  this->m_node_handle.subscribe<std_msgs::Float64>(COMMAND_POSITION, 1,
10 
11  this->m_motor_speed_subscriber =
12  this->m_node_handle.subscribe<std_msgs::Float64>(COMMAND_THROTTLE, 1, &VESCSimulationDriver::motorSpeedCallback,
13  this);
14 
15  this->m_motor_brake_subscriber =
16  this->m_node_handle.subscribe<std_msgs::Float64>(COMMAND_BRAKE, 1, &VESCSimulationDriver::motorBrakeCallback,
17  this);
18 
19  this->m_left_rear_wheel_velocity_publisher =
20  this->m_node_handle.advertise<std_msgs::Float64>(simulation::WHEEL_LEFT_BACK_VELOCITY, 10);
21  this->m_right_rear_wheel_velocity_publisher =
22  this->m_node_handle.advertise<std_msgs::Float64>(simulation::WHEEL_RIGHT_BACK_VELOCITY, 10);
23  this->m_left_front_wheel_velocity_publisher =
24  this->m_node_handle.advertise<std_msgs::Float64>(simulation::WHEEL_LEFT_FRONT_VELOCITY, 10);
25  this->m_right_front_wheel_velocity_publisher =
26  this->m_node_handle.advertise<std_msgs::Float64>(simulation::WHEEL_RIGHT_FRONT_VELOCITY, 10);
27 
28  this->m_left_steering_position_publisher =
29  this->m_node_handle.advertise<std_msgs::Float64>(simulation::LEFT_STEERING_POSITION, 10);
30  this->m_right_steering_position_publisher =
31  this->m_node_handle.advertise<std_msgs::Float64>(simulation::RIGHT_STEERING_POSITION, 10);
32 
33  m_VESC_simulator.start();
34 }
35 
36 void VESCSimulationDriver::motorSpeedCallback(const std_msgs::Float64::ConstPtr& throttle_message)
37 {
38  std_msgs::Float64 throttle;
39  throttle.data = throttle_message->data * car_config::ERPM_TO_RAD_PER_SEC / car_config::TRANSMISSION;
40 
41  m_VESC_simulator.setSpeed(throttle_message->data);
42 
43  this->m_left_rear_wheel_velocity_publisher.publish(throttle);
44  this->m_right_rear_wheel_velocity_publisher.publish(throttle);
45  this->m_left_front_wheel_velocity_publisher.publish(throttle);
46  this->m_right_front_wheel_velocity_publisher.publish(throttle);
47 }
48 
49 void VESCSimulationDriver::motorBrakeCallback(const std_msgs::Float64::ConstPtr& motor_brake)
50 {
51  if (motor_brake->data != 0)
52  {
53  std_msgs::Float64 throttle;
54  throttle.data = 0;
55 
56  m_VESC_simulator.setSpeed(0);
57 
58  this->m_left_rear_wheel_velocity_publisher.publish(throttle);
59  this->m_right_rear_wheel_velocity_publisher.publish(throttle);
60  this->m_left_front_wheel_velocity_publisher.publish(throttle);
61  this->m_right_front_wheel_velocity_publisher.publish(throttle);
62  }
63 }
64 
65 void VESCSimulationDriver::servoPositionCallback(const std_msgs::Float64::ConstPtr& servo_position)
66 {
67  double angle = (servo_position->data - car_config::STEERING_TO_SERVO_OFFSET) / car_config::STEERING_TO_SERVO_GAIN;
68  AckermannSteeringAngles angles = calculateSteeringAngles(angle);
69 
70  m_VESC_simulator.setServoAngle(servo_position->data);
71 
72  std_msgs::Float64 left_wheel;
73  left_wheel.data = angles.left_wheel_angle;
74 
75  std_msgs::Float64 right_wheel;
76  right_wheel.data = angles.right_wheel_angle;
77 
78  this->m_left_steering_position_publisher.publish(left_wheel);
79  this->m_right_steering_position_publisher.publish(right_wheel);
80 }
81 
82 VESCSimulationDriver::AckermannSteeringAngles VESCSimulationDriver::calculateSteeringAngles(const double& angle)
83 {
84  AckermannSteeringAngles angles;
85  double radius = std::tan(angle + M_PI / 2) * car_config::WHEELBASE;
86  angles.left_wheel_angle = -std::atan(car_config::WHEELBASE / (radius + car_config::REAR_WHEEL_DISTANCE / 2));
87  angles.right_wheel_angle = -std::atan(car_config::WHEELBASE / (radius - car_config::REAR_WHEEL_DISTANCE / 2));
88  return angles;
89 }
VESCSimulationDriver()
Constructor that creates subscribers and publishers.
constexpr const char * WHEEL_RIGHT_FRONT_VELOCITY
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 motorSpeedCallback(const std_msgs::Float64::ConstPtr &throttle_message)
Callback for ROS Subscriber.
constexpr double ERPM_TO_RAD_PER_SEC
Conversion factor from electrical revolutions per minute to radian to seconds radian/s * minute = ra...
Definition: car_config.h:91
void motorBrakeCallback(const std_msgs::Float64::ConstPtr &motor_brake)
Callback for ROS Subscriber.
constexpr double REAR_WHEEL_DISTANCE
Definition: car_config.h:23
void start()
Definition: vesc_sim.cpp:28
void setSpeed(const double &data)
Definition: vesc_sim.h:15
constexpr const char * WHEEL_LEFT_BACK_VELOCITY
constexpr const char * WHEEL_LEFT_FRONT_VELOCITY
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 const char * COMMAND_BRAKE
constexpr const char * COMMAND_THROTTLE
constexpr double STEERING_TO_SERVO_OFFSET
Position of the servo for normal wheel position.
Definition: car_config.h:67
void setServoAngle(const double &data)
Definition: vesc_sim.h:11
constexpr const char * COMMAND_POSITION
constexpr const char * WHEEL_RIGHT_BACK_VELOCITY
constexpr const char * RIGHT_STEERING_POSITION
void servoPositionCallback(const std_msgs::Float64::ConstPtr &servo_position)
Callback for ROS Subscriber.
constexpr const char * LEFT_STEERING_POSITION