Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
ramp_2d.hpp
Go to the documentation of this file.
1#ifndef FANG_ROBOTICS_MCB_TRAP_ALGORITHMS_RAMP_2D_HPP
2#define FANG_ROBOTICS_MCB_TRAP_ALGORITHMS_RAMP_2D_HPP
6namespace trap::algorithms
7{
8 template<typename ValueUnit, typename TimeUnit>
9 class Ramp2D
10 {
11 public:
13
17 Ramp2D(const Vector2D& initialValue, double rampSpeed)
18 : m_xRamp{initialValue.x, rampSpeed},
19 m_yRamp{initialValue.y, rampSpeed},
20 m_speed{rampSpeed}
21 {
22 }
23
24 void setTarget(const Vector2D& target)
25 {
26 m_xRamp.setTarget(target.x);
27 m_yRamp.setTarget(target.y);
28 //If the target is already there, the delta is zero, leading to
29 //division by ZERO. This guards against that possibility
30 if(!isTargetReached())
31 {
32 //We want the ramps to arrive at roughly the same time
33 setRampSpeeds();
34 }
35 }
36
38 {
39 return Vector2D{m_xRamp.getTarget(), m_yRamp.getTarget()};
40 }
41
46 void setSpeed(double speed)
47 {
48 m_speed = speed;
49 }
50
52 {
53 return Vector2D{m_xRamp.getValue(), m_yRamp.getValue()};
54 }
55
56 bool isTargetReached() const
57 {
58 return m_xRamp.isTargetReached() && m_yRamp.isTargetReached();
59 }
60
61 void update()
62 {
63 m_xRamp.update();
64 m_yRamp.update();
65 }
66
67 private:
72 void setRampSpeeds()
73 {
74 //We want the x and y ramps to arrive at roughly the same time
75 const Vector2D delta{getTarget() - getValue()};
76 const fang::math::AbstractVector2D direction{delta.getUnitVector()};
77 fang::math::AbstractVector2D speeds{direction * m_speed};
78
79 m_xRamp.setSpeed(speeds.x);
80 m_yRamp.setSpeed(speeds.y);
81 }
82 Ramp<ValueUnit, TimeUnit> m_xRamp;
83 Ramp<ValueUnit, TimeUnit> m_yRamp;
84 double m_speed;
85 };
86
87}
88#endif
Definition ramp_2d.hpp:10
Vector2D getValue() const
Definition ramp_2d.hpp:51
Ramp2D(const Vector2D &initialValue, double rampSpeed)
Definition ramp_2d.hpp:17
void update()
Definition ramp_2d.hpp:61
void setTarget(const Vector2D &target)
Definition ramp_2d.hpp:24
bool isTargetReached() const
Definition ramp_2d.hpp:56
void setSpeed(double speed)
Definition ramp_2d.hpp:46
fang::math::Vector2D< ValueUnit > Vector2D
Definition ramp_2d.hpp:12
Vector2D getTarget() const
Definition ramp_2d.hpp:37
Definition dimensional_smooth_pid.hpp:10
Definition vector_2d.hpp:8
Vector2D< double > getUnitVector() const
Definition vector_2d.hpp:47
Unit x
Definition vector_2d.hpp:11
Unit y
Definition vector_2d.hpp:12