Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
kinematics.cpp
1 // Copyright (c) 2022 Samsung R&D Institute Russia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nav2_collision_monitor/kinematics.hpp"
16 
17 #include <cmath>
18 
19 namespace nav2_collision_monitor
20 {
21 
22 void transformPoints(const Pose & pose, std::vector<Point> & points)
23 {
24  const double cos_theta = std::cos(pose.theta);
25  const double sin_theta = std::sin(pose.theta);
26 
27  for (Point & point : points) {
28  // p = R*p' + pose
29  // p' = Rt * (p - pose)
30  // where:
31  // p - point coordinates in initial frame
32  // p' - point coordinates in a new frame
33  // R - rotation matrix =
34  // [cos_theta -sin_theta]
35  // [sin_theta cos_theta]
36  // Rt - transposed (inverted) rotation matrix
37  const double mul_x = point.x - pose.x;
38  const double mul_y = point.y - pose.y;
39  point.x = mul_x * cos_theta + mul_y * sin_theta;
40  point.y = -mul_x * sin_theta + mul_y * cos_theta;
41  }
42 }
43 
44 void projectState(const double & dt, Pose & pose, Velocity & velocity)
45 {
46  const double theta = velocity.tw * dt;
47  const double cos_theta = std::cos(theta);
48  const double sin_theta = std::sin(theta);
49 
50  // p' = p + vel*dt
51  // where:
52  // p - initial pose
53  // p' - projected pose
54  pose.x = pose.x + velocity.x * dt;
55  pose.y = pose.y + velocity.y * dt;
56  // Rotate the pose on theta
57  pose.theta = pose.theta + theta;
58 
59  // vel' = R*vel
60  // where:
61  // vel - initial velocity
62  // R - rotation matrix
63  // vel' - rotated velocity
64  const double velocity_upd_x = velocity.x * cos_theta - velocity.y * sin_theta;
65  const double velocity_upd_y = velocity.x * sin_theta + velocity.y * cos_theta;
66  velocity.x = velocity_upd_x;
67  velocity.y = velocity_upd_y;
68 }
69 
70 } // namespace nav2_collision_monitor