Autonomous Racing  1
f1tenth Project Group of Technical University Dortmund, Germany
chase_cam.cpp
Go to the documentation of this file.
1 #include "chase_cam.h"
2 
3 ignition::math::Pose3d convertPoseMessageToPose(const gazebo::msgs::Pose pose_message)
4 {
5  auto position = pose_message.position();
6  auto orientation = pose_message.orientation();
7 
8  return ignition::math::Pose3d(position.x(), position.y(), position.z(), orientation.w(), orientation.x(),
9  orientation.y(), orientation.z());
10 }
11 
13 {
14  this->m_gazebo_node = gazebo::transport::NodePtr(new gazebo::transport::Node());
15  this->m_gazebo_node->Init();
16 
17  this->m_camera_pose_publisher = this->m_gazebo_node->Advertise<gazebo::msgs::Pose>(TOPIC_CAMERA_POSE);
18  this->m_racer_pose_subscriber =
19  this->m_gazebo_node->Subscribe(TOPIC_GAZEBO_POSES, &ChaseCam::gazeboPosesCallback, this);
20 }
21 
22 void ChaseCam::gazeboPosesCallback(ConstPosesStampedPtr& message)
23 {
24  for (int i = 0; i < message->pose_size(); i++)
25  {
26  auto pose_message = message->pose(i);
27  if (pose_message.name() == CAR_NAME)
28  {
29  auto pose = convertPoseMessageToPose(pose_message);
30  this->updateCamera(pose);
31  return;
32  }
33  }
34 }
35 
36 void ChaseCam::updateCamera(ignition::math::Pose3d& car_pose)
37 {
38  auto car_rotation = ignition::math::Quaterniond(0, 0, car_pose.Rot().Yaw());
39 
40  this->m_last_position = car_pose.Pos() * 0.1 + this->m_last_position * 0.9;
41  this->m_last_rotation = ignition::math::Quaterniond::Slerp(0.08, this->m_last_rotation, car_rotation, true);
42 
43  auto camera_position = this->m_last_position + this->m_last_rotation * CAMERA_OFFSET;
44 
45  auto camera_pose = ignition::math::Pose3d(camera_position, this->m_last_rotation);
46  this->publishCameraPose(camera_pose);
47 }
48 
49 void ChaseCam::publishCameraPose(ignition::math::Pose3d& pose)
50 {
51  gazebo::msgs::Pose message;
52  gazebo::msgs::Set(&message, pose);
53  this->m_camera_pose_publisher->Publish(message);
54 }
55 
56 int main(int argc, char** argv)
57 {
58  gazebo::client::setup(argc, argv);
59  ChaseCam chase_cam;
60 
61  while (true)
62  {
63  gazebo::common::Time::MSleep(1000);
64  }
65 
66  gazebo::client::shutdown();
67  return EXIT_SUCCESS;
68 }
Chase camera Gazebo node that listens to racer pose messages and updates the Gazebo client camera pos...
Definition: chase_cam.h:17
constexpr const char * TOPIC_CAMERA_POSE
Definition: chase_cam.h:6
const ignition::math::Vector3d CAMERA_OFFSET(-1.7, 0, 0.5)
constexpr const char * TOPIC_GAZEBO_POSES
Definition: chase_cam.h:7
ignition::math::Pose3d convertPoseMessageToPose(const gazebo::msgs::Pose pose_message)
Definition: chase_cam.cpp:3
int main(int argc, char **argv)
Definition: chase_cam.cpp:56
const std::string CAR_NAME
Definition: chase_cam.h:8