2023-Robot
Robot code for 2023 FRC season by Argos, FRC team #1756
Loading...
Searching...
No Matches
convert_path.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <frc/trajectory/TrapezoidProfile.h>
8
9#include <algorithm>
10#include <numeric>
11#include <vector>
12
14#include "types.h"
15
16namespace path_planning {
17
18 [[nodiscard]] units::degree_t CalculateCuspAngle(const ArmPath& path, const size_t segmentIndex);
19
20 [[nodiscard]] VelocityComponents DecomposeVelocity(const ArmMPPathPoint& pathPoint, const ArmPathPoint& armVector);
21
22 [[nodiscard]] CompositeMPPath GenerateCompositeMPPath(ArmMPPath generalPath,
23 const BashGuardMPPath& bashGuardPath,
24 const ArmPathPoint& shoulderFulcrum,
25 LifterSubsystem* lifter);
26 [[nodiscard]] BashGuardMPPath GenerateProfiledBashGuard(const BashGuardPoint& startPoint,
27 const BashGuardPoint& endPoint,
28 const PathDynamicsConstraints& constraints,
29 units::millisecond_t resolution = 20_ms);
30 [[nodiscard]] ArmMPPath GenerateProfiledPath(const ArmPath& initialPath,
31 const PathDynamicsConstraints& constraints,
32 const Polygon& avoidancePolygon,
33 units::millisecond_t resolution = 20_ms);
34
35 [[nodiscard]] std::vector<units::inch_t> GenerateSegmentLengths(const path_planning::ArmPath& path);
36
37 template <typename SegmentIt, typename PathIt>
38 [[nodiscard]] std::vector<frc::TrapezoidProfile<units::inch>> GenerateSegmentProfiles(
39 SegmentIt segmentLengthsBegin,
40 SegmentIt segmentLengthsEnd,
41 PathIt pathBegin,
42 PathIt pathEnd,
43 const PathDynamicsConstraints& constraints) {
44 auto totalPathLength = std::accumulate(
45 segmentLengthsBegin, segmentLengthsEnd, 0_in, [](units::inch_t sum, const units::inch_t& segmentLength) {
46 return sum + segmentLength;
47 });
48
49 // Transition velocities cannot exceed max total velocity over path
50 auto minPathTime = units::math::sqrt(2 * totalPathLength / constraints.maxAcceleration);
51 auto maxVel = units::math::min(constraints.maxVelocity, minPathTime / 2 * constraints.maxAcceleration);
52
53 std::vector<frc::TrapezoidProfile<units::inch>> segmentProfiles;
54 segmentProfiles.reserve(std::distance(segmentLengthsBegin, segmentLengthsEnd));
55 auto lastVelocity = 0_ips;
56 path_planning::ArmPath path{pathBegin, pathEnd};
57 size_t segmentIndex = 0;
58 while (segmentLengthsBegin != segmentLengthsEnd) {
59 auto cuspAngle = CalculateCuspAngle(path, segmentIndex);
60 auto transitionSpeed = std::min(
61 maxVel, constraints.maxVelocity * std::max(0.25, ((-units::math::cos(cuspAngle) + 1) / 2).to<double>()));
62 segmentProfiles.emplace_back(
63 frc::TrapezoidProfile<units::inch>::Constraints{constraints.maxVelocity, constraints.maxAcceleration},
64 frc::TrapezoidProfile<units::inch>::State{*segmentLengthsBegin, transitionSpeed},
65 frc::TrapezoidProfile<units::inch>::State{0_in, lastVelocity});
66 lastVelocity = segmentProfiles.back().Calculate(segmentProfiles.back().TotalTime()).velocity;
67 ++segmentLengthsBegin;
68 ++segmentIndex;
69 }
70
71 return segmentProfiles;
72 }
73
74 [[nodiscard]] std::vector<frc::TrapezoidProfile<units::inch>> GenerateContinuousSegmentProfiles(
75 const std::vector<units::inch_t>& segmentLengths,
76 const path_planning::ArmPath& path,
77 const PathDynamicsConstraints& constraints);
78
79 template <typename PositionType, typename VelocityType>
80 [[nodiscard]] std::vector<GenericMPPathPoint<PositionType, VelocityType>> PadProfile(
81 const std::vector<GenericMPPathPoint<PositionType, VelocityType>>& profile,
82 units::millisecond_t paddingTime,
83 bool padFront) {
84 std::vector<GenericMPPathPoint<PositionType, VelocityType>> paddedProfile;
85 unsigned paddingElements = std::ceil((paddingTime / profile.front().time).template to<double>());
86 paddedProfile.reserve(profile.size() + paddingElements);
87 if (padFront) {
88 paddedProfile.insert(paddedProfile.end(), paddingElements, profile.front());
89 paddedProfile.insert(paddedProfile.end(), profile.begin(), profile.end());
90 } else {
91 paddedProfile.insert(paddedProfile.end(), profile.begin(), profile.end());
92 paddedProfile.insert(paddedProfile.end(), paddingElements, profile.back());
93 }
94 return paddedProfile;
95 }
96
97} // namespace path_planning
Definition lifter_subsystem.h:26
Definition convert_path.h:16
std::vector< ArmPathPoint > Polygon
Definition types.h:64
std::vector< frc::TrapezoidProfile< units::inch > > GenerateContinuousSegmentProfiles(const std::vector< units::inch_t > &segmentLengths, const path_planning::ArmPath &path, const PathDynamicsConstraints &constraints)
Definition convert_path.cpp:255
std::vector< BashGuardMPPathPoint > BashGuardMPPath
Definition types.h:77
units::degree_t CalculateCuspAngle(const ArmPath &path, const size_t segmentIndex)
Definition convert_path.cpp:20
units::inch_t BashGuardPoint
Definition types.h:62
std::vector< ArmPathPoint > ArmPath
Definition types.h:65
BashGuardMPPath GenerateProfiledBashGuard(const BashGuardPoint &startPoint, const BashGuardPoint &endPoint, const PathDynamicsConstraints &constraints, units::millisecond_t resolution=20_ms)
Definition convert_path.cpp:126
CompositeMPPath GenerateCompositeMPPath(ArmMPPath generalPath, const BashGuardMPPath &bashGuardPath, const ArmPathPoint &shoulderFulcrum, LifterSubsystem *lifter)
Definition convert_path.cpp:52
VelocityComponents DecomposeVelocity(const ArmMPPathPoint &pathPoint, const ArmPathPoint &armVector)
Definition convert_path.cpp:33
std::vector< GenericMPPathPoint< PositionType, VelocityType > > PadProfile(const std::vector< GenericMPPathPoint< PositionType, VelocityType > > &profile, units::millisecond_t paddingTime, bool padFront)
Definition convert_path.h:80
std::vector< units::inch_t > GenerateSegmentLengths(const path_planning::ArmPath &path)
Definition convert_path.cpp:242
std::vector< ArmMPPathPoint > ArmMPPath
Definition types.h:74
ArmMPPath GenerateProfiledPath(const ArmPath &initialPath, const PathDynamicsConstraints &constraints, const Polygon &avoidancePolygon, units::millisecond_t resolution=20_ms)
Definition convert_path.cpp:164
std::vector< frc::TrapezoidProfile< units::inch > > GenerateSegmentProfiles(SegmentIt segmentLengthsBegin, SegmentIt segmentLengthsEnd, PathIt pathBegin, PathIt pathEnd, const PathDynamicsConstraints &constraints)
Definition convert_path.h:38
Definition types.h:19
Definition types.h:79
units::velocity::inches_per_second_t maxVelocity
Definition types.h:87
units::acceleration::inches_per_second_squared_t maxAcceleration
Definition types.h:88