TrajoptLib
Library for generating time-optimal trajectories for FRC robots.
Loading...
Searching...
No Matches
differential_trajectory_generator.hpp
1// Copyright (c) TrajoptLib contributors
2
3#pragma once
4
5#include <stdint.h>
6
7#include <cstddef>
8#include <expected>
9#include <utility>
10#include <vector>
11
12#include <sleipnir/autodiff/variable.hpp>
13#include <sleipnir/optimization/problem.hpp>
14#include <sleipnir/optimization/solver/exit_status.hpp>
15
16#include "trajopt/path/path.hpp"
17#include "trajopt/path/path_builder.hpp"
18#include "trajopt/util/symbol_exports.hpp"
19
20namespace trajopt {
21
23struct TRAJOPT_DLLEXPORT DifferentialDrivetrain {
25 double mass;
26
28 double moi;
29
32
35
38
40 double wheel_cof;
41
43 double trackwidth;
44};
45
47struct TRAJOPT_DLLEXPORT DifferentialSolution {
49 std::vector<double> dt;
50
52 std::vector<double> x;
53
55 std::vector<double> y;
56
58 std::vector<double> heading;
59
61 std::vector<double> vl;
62
64 std::vector<double> vr;
65
68 std::vector<double> angular_velocity;
69
71 std::vector<double> al;
72
74 std::vector<double> ar;
75
78 std::vector<double> angular_acceleration;
79
81 std::vector<double> Fl;
82
84 std::vector<double> Fr;
85};
86
88class TRAJOPT_DLLEXPORT DifferentialTrajectorySample {
89 public:
91 double timestamp = 0.0;
92
94 double x = 0.0;
95
97 double y = 0.0;
98
100 double heading = 0.0;
101
103 double velocity_l = 0.0;
104
106 double velocity_r = 0.0;
107
109 double angular_velocity = 0.0;
110
112 double acceleration_l = 0.0;
113
115 double acceleration_r = 0.0;
116
119
121 double force_l = 0.0;
122
124 double force_r = 0.0;
125
126 DifferentialTrajectorySample() = default;
127
159};
160
162class TRAJOPT_DLLEXPORT DifferentialTrajectory {
163 public:
165 std::vector<DifferentialTrajectorySample> samples;
166
167 DifferentialTrajectory() = default;
168
173 std::vector<DifferentialTrajectorySample> samples)
174 : samples{std::move(samples)} {}
175
180 double ts = 0.0;
181 for (size_t sample = 0; sample < solution.x.size(); ++sample) {
182 samples.emplace_back(
183 ts, solution.x[sample], solution.y[sample], solution.heading[sample],
184 solution.vl[sample], solution.vr[sample],
185 solution.angular_velocity[sample], solution.al[sample],
186 solution.ar[sample], solution.angular_acceleration[sample],
187 solution.Fl[sample], solution.Fr[sample]);
188 ts += solution.dt[sample];
189 }
190 }
191};
192
194using DifferentialPath = Path<DifferentialDrivetrain, DifferentialSolution>;
195
199using DifferentialPathBuilder =
200 PathBuilder<DifferentialDrivetrain, DifferentialSolution>;
201
204class TRAJOPT_DLLEXPORT DifferentialTrajectoryGenerator {
205 public:
210 explicit DifferentialTrajectoryGenerator(DifferentialPathBuilder path_builder,
211 int64_t handle = 0);
212
220 std::expected<DifferentialSolution, slp::ExitStatus> generate(
221 bool diagnostics = false);
222
223 private:
225 DifferentialPath path;
226
228 std::vector<slp::Variable<double>> x;
229 std::vector<slp::Variable<double>> y;
230 std::vector<slp::Variable<double>> θ;
231 std::vector<slp::Variable<double>> vl;
232 std::vector<slp::Variable<double>> vr;
233 std::vector<slp::Variable<double>> al;
234 std::vector<slp::Variable<double>> ar;
235
237 std::vector<slp::Variable<double>> Fl;
238 std::vector<slp::Variable<double>> Fr;
239
241 std::vector<slp::Variable<double>> dts;
242
244 std::vector<size_t> Ns;
245
246 slp::Problem<double> problem;
247
248 void apply_initial_guess(const DifferentialSolution& solution);
249
250 DifferentialSolution construct_differential_solution();
251};
252
253} // namespace trajopt
DifferentialTrajectoryGenerator(DifferentialPathBuilder path_builder, int64_t handle=0)
std::expected< DifferentialSolution, slp::ExitStatus > generate(bool diagnostics=false)
double velocity_r
The right wheel's velocity.
Definition differential_trajectory_generator.hpp:106
double x
The x coordinate.
Definition differential_trajectory_generator.hpp:94
double force_l
The left wheel's force.
Definition differential_trajectory_generator.hpp:121
double y
The y coordinate.
Definition differential_trajectory_generator.hpp:97
double force_r
The right wheel's force.
Definition differential_trajectory_generator.hpp:124
double heading
The heading.
Definition differential_trajectory_generator.hpp:100
double angular_acceleration
The chassis angular acceleration.
Definition differential_trajectory_generator.hpp:118
double velocity_l
The left wheel's velocity.
Definition differential_trajectory_generator.hpp:103
double acceleration_l
The left wheel's acceleration.
Definition differential_trajectory_generator.hpp:112
double timestamp
The timestamp.
Definition differential_trajectory_generator.hpp:91
DifferentialTrajectorySample(double timestamp, double x, double y, double heading, double velocity_l, double velocity_r, double angular_velocity, double acceleration_l, double acceleration_r, double angular_acceleration, double force_l, double force_r)
Definition differential_trajectory_generator.hpp:141
double angular_velocity
The chassis angular velocity.
Definition differential_trajectory_generator.hpp:109
double acceleration_r
The right wheel's acceleration.
Definition differential_trajectory_generator.hpp:115
DifferentialTrajectory(std::vector< DifferentialTrajectorySample > samples)
Definition differential_trajectory_generator.hpp:172
DifferentialTrajectory(const DifferentialSolution &solution)
Definition differential_trajectory_generator.hpp:179
std::vector< DifferentialTrajectorySample > samples
The samples that make up the trajectory.
Definition differential_trajectory_generator.hpp:165
A differential drivetrain physical model.
Definition differential_trajectory_generator.hpp:23
double moi
The moment of inertia of the robot about the origin (kg−m²).
Definition differential_trajectory_generator.hpp:28
double mass
The mass of the robot (kg).
Definition differential_trajectory_generator.hpp:25
double trackwidth
Distance between the two driverails (m).
Definition differential_trajectory_generator.hpp:43
double wheel_max_torque
Maximum torque applied to the wheels (N−m).
Definition differential_trajectory_generator.hpp:37
double wheel_radius
Radius of the wheels (m).
Definition differential_trajectory_generator.hpp:31
double wheel_max_angular_velocity
Maximum angular velocity of the wheels (rad/s).
Definition differential_trajectory_generator.hpp:34
double wheel_cof
The Coefficient of Friction (CoF) of the wheels.
Definition differential_trajectory_generator.hpp:40
The holonomic trajectory optimization solution.
Definition differential_trajectory_generator.hpp:47
std::vector< double > vl
The left velocities.
Definition differential_trajectory_generator.hpp:61
std::vector< double > y
Y positions.
Definition differential_trajectory_generator.hpp:55
std::vector< double > Fr
The force of the right driverail wheels.
Definition differential_trajectory_generator.hpp:84
std::vector< double > vr
The right velocities.
Definition differential_trajectory_generator.hpp:64
std::vector< double > ar
The right acceleration.
Definition differential_trajectory_generator.hpp:74
std::vector< double > angular_velocity
Definition differential_trajectory_generator.hpp:68
std::vector< double > heading
Heading.
Definition differential_trajectory_generator.hpp:58
std::vector< double > dt
Times between samples.
Definition differential_trajectory_generator.hpp:49
std::vector< double > al
The left accelerations.
Definition differential_trajectory_generator.hpp:71
std::vector< double > Fl
The force of the left driverail wheels.
Definition differential_trajectory_generator.hpp:81
std::vector< double > angular_acceleration
Definition differential_trajectory_generator.hpp:78
std::vector< double > x
X positions.
Definition differential_trajectory_generator.hpp:52