Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
Localizer.cpp
Go to the documentation of this file.
2 
3 #include <control/control.hpp>
5 
6 using namespace galaxis::control;
7 
9  m_positionHistory(200) {}
10 
11 void Localizer::init(rclcpp::Time& timestamp, double orientationRad) {
12  m_positionHistory.add({ timestamp, galaxis::lina::Vector2f(0.f, 0.f), 0, orientationRad });
13 }
14 
16  return m_positionHistory.is_empty();
17 }
18 
19 void Localizer::add_position_timepoint(rclcpp::Time& timestamp, galaxis::lina::Vector2f positionMillimeter, double speedMillimeterPerSec, double orientationRad) {
20  PositionTimepoint newPositionPoint;
21  newPositionPoint.timestamp = timestamp;
22  newPositionPoint.speed = speedMillimeterPerSec;
23  newPositionPoint.position = positionMillimeter;
24  newPositionPoint.orientation = orientationRad;
25 
26  m_positionHistory.add(newPositionPoint);
27 }
28 
29 // TODO: Test this function! (not used in SimulationMode)
30 void Localizer::add_state(rclcpp::Time& timestamp, double speedMillimeterPerSec, double orientationRad) {
32  log_warning("[LOCALIZER] Position History Empty!");
33  this->add_position_timepoint(timestamp, galaxis::lina::Vector2f(0.f,0.f), speedMillimeterPerSec, orientationRad);
34  return;
35  }
36 
37  orientationRad = std::fmod(orientationRad, 2*MATH_PI_DOUBLE);
38  // assert(orientationRad <= 2 * MATH_PI_DOUBLE + 0.1);
39 
40  PositionTimepoint newPositionPoint;
41  newPositionPoint.timestamp = timestamp;
42  newPositionPoint.orientation = orientationRad;
43  newPositionPoint.speed = speedMillimeterPerSec;
44 
45  PositionTimepoint lastPositionPoint = m_positionHistory.get_last();
46 
47  // calc driven distance within time interval
48  rclcpp::Duration timeIntervalNanoseconds = timestamp - lastPositionPoint.timestamp;
49  double speed = (lastPositionPoint.speed + speedMillimeterPerSec) / 2.f;
50  double drivenDistanceMillimeter = timeIntervalNanoseconds.nanoseconds() * speed / 1000000000.f; // based on speed at last point // TODO: CHECK
51 
52  // based on single-track model
53  std::shared_ptr<CarPose> lastPose = std::make_shared<CarPose>();
54  lastPose->orientation = lastPositionPoint.orientation;
55 
56  double orientationDiff = orientationRad - lastPositionPoint.orientation;
57 
58  if(orientationDiff > MATH_PI_DOUBLE) { // if currentOrientation is lower than 0 deg (something around 2*PI) and lastOrientation greater than 0 deg
59  orientationDiff -= 2 * MATH_PI_DOUBLE;
60  } else if(orientationDiff < -MATH_PI_DOUBLE) { // analog the other way round
61  orientationDiff += 2 * MATH_PI_DOUBLE;
62  }
63 
64  // orientationDiff treated as turn angle
65  // lastPose->position = galaxis::lina::Vector2f(m_positionHistory.get_last().position[1], m_positionHistory.get_last().position[0]); // in mm
66  // std::shared_ptr<CarPose> newPoseSPtr = galaxis::control::get_new_pose(lastPose, galaxis::controllerSettings::carWheelbase * 1000, drivenDistanceMillimeter, orientationDiff);
67  // newPositionPoint.position = galaxis::lina::Vector2f(newPoseSPtr->position[1], newPoseSPtr->position[0]);
68  // newPositionPoint.orientation = orientationRad; // should be approx. equal to newPoseSPtr->orientation // TODO: check (look above) ***
69 
70  // new calculation
71  Eigen::Rotation2Df lastOrientationRotMat(-lastPositionPoint.orientation); // counter-clock wise rotation
72  galaxis::lina::Vector2f drivenDistanceVector = lastOrientationRotMat * galaxis::lina::Vector2f(0.f, drivenDistanceMillimeter);
73  Eigen::Rotation2Df orientationDiffRotMat(-orientationDiff);
74  drivenDistanceVector = orientationDiffRotMat * drivenDistanceVector;
75 
76  newPositionPoint.position = lastPositionPoint.position + drivenDistanceVector;
77 
78 
79  log_debug("[LOCALIZER] Position: ", newPositionPoint.position[0], ",", newPositionPoint.position[1], "; Orientation: ", orientationRad);
80 
81  // append to buffer
82  m_positionHistory.add(newPositionPoint);
83 }
84 
85 std::shared_ptr<PositionTimepoint> Localizer::get_latest_position_timepoint() {
86  return std::make_shared<PositionTimepoint>(m_positionHistory.get_last());
87 }
88 
89 std::shared_ptr<PositionTimepoint> Localizer::get_position_timepoint_by_time(rclcpp::Time& timestamp) {
90  return std::make_shared<PositionTimepoint>(m_positionHistory.get_last_by_time(timestamp));
91 }
Localizer::get_position_timepoint_by_time
std::shared_ptr< PositionTimepoint > get_position_timepoint_by_time(rclcpp::Time &timestamp)
Definition: Localizer.cpp:89
Localizer::get_latest_position_timepoint
std::shared_ptr< PositionTimepoint > get_latest_position_timepoint()
Definition: Localizer.cpp:85
PositionTimepoint::orientation
double orientation
Definition: Localizer.hpp:23
PositionTimepoint::position
galaxis::lina::Vector2f position
Definition: Localizer.hpp:21
galaxis::utils::log_warning
void log_warning(const Args &... args)
Definition: logger.hpp:41
PositionTimepoint::speed
double speed
Definition: Localizer.hpp:22
PositionTimepointBuffer::get_last
PositionTimepoint get_last()
Definition: Localizer.hpp:76
Localizer::m_positionHistory
PositionTimepointBuffer m_positionHistory
Definition: Localizer.hpp:106
control.hpp
Localizer::add_position_timepoint
void add_position_timepoint(rclcpp::Time &timestamp, galaxis::lina::Vector2f positionMillimeter, double speedMillimeterPerSec, double orientationRad)
Definition: Localizer.cpp:19
Localizer::Localizer
Localizer()
Definition: Localizer.cpp:8
galaxis::lina::Vector2f
Eigen::Vector2f Vector2f
Definition: linatypes.hpp:14
galaxis::control
Definition: control.hpp:24
MATH_PI_DOUBLE
constexpr double MATH_PI_DOUBLE
Definition: AutoPilot.hpp:22
PositionTimepointBuffer::get_last_by_time
PositionTimepoint get_last_by_time(rclcpp::Time &timestamp)
Definition: Localizer.hpp:48
Localizer::add_state
void add_state(rclcpp::Time &timestamp, double speedMillimeterPerSec, double orientationRad)
Definition: Localizer.cpp:30
PositionTimepoint::timestamp
rclcpp::Time timestamp
Definition: Localizer.hpp:20
PositionTimepointBuffer::add
void add(PositionTimepoint const &element)
Definition: Localizer.hpp:37
controllerSettings.hpp
PositionTimepointBuffer::is_empty
bool is_empty()
Definition: Localizer.hpp:84
Localizer::init
void init(rclcpp::Time &timestamp, double orientationRad)
Definition: Localizer.cpp:11
Localizer::is_empty
bool is_empty()
Definition: Localizer.cpp:15
galaxis::utils::log_debug
void log_debug(const Args &... args)
Definition: logger.hpp:62
PositionTimepoint
Definition: Localizer.hpp:19
Localizer.hpp