blob: 5f5b02e203a30d991b863cce45e1d5f759b2ade9 [file] [log] [blame]
Alex Perrya60da442019-01-21 19:00:27 -05001#include <vector>
Alex Perry0603b542019-01-25 20:29:51 -08002#include <string>
Alex Perry20762632019-01-21 17:48:02 -05003
4#include "Eigen/Dense"
5
Alex Perry0603b542019-01-25 20:29:51 -08006#include "aos/logging/implementations.h"
7#include "aos/network/team_number.h"
Alex Perrya60da442019-01-21 19:00:27 -05008#include "frc971/control_loops/drivetrain/distance_spline.h"
Alex Perry20762632019-01-21 17:48:02 -05009#include "frc971/control_loops/drivetrain/spline.h"
Alex Perry0603b542019-01-25 20:29:51 -080010#include "frc971/control_loops/drivetrain/trajectory.h"
11#include "y2019/control_loops/drivetrain/drivetrain_base.h"
Alex Perry20762632019-01-21 17:48:02 -050012
13namespace frc971 {
14namespace control_loops {
15namespace drivetrain {
16
17extern "C" {
Alex Perry0603b542019-01-25 20:29:51 -080018 // Based on spline.h
19 NSpline<6> *NewSpline(double x[6], double y[6]) {
Alex Perry20762632019-01-21 17:48:02 -050020 return new NSpline<6>((::Eigen::Matrix<double, 2, 6>() << x[0], x[1], x[2],
21 x[3], x[4], x[5], y[0], y[1], y[2], y[3], y[4],
22 y[5]).finished());
23 }
24
Alex Perry0603b542019-01-25 20:29:51 -080025 void deleteSpline(NSpline<6> *spline) { delete spline; }
Alex Perry20762632019-01-21 17:48:02 -050026
Alex Perry0603b542019-01-25 20:29:51 -080027 void SplinePoint(NSpline<6> *spline, double alpha, double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080028 const Eigen::Vector2d xy = spline->Point(alpha);
29 res[0] = xy.x();
30 res[1] = xy.y();
Alex Perry20762632019-01-21 17:48:02 -050031 }
32
Alex Perry0603b542019-01-25 20:29:51 -080033 void SplineDPoint(NSpline<6> *spline, double alpha, double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080034 const Eigen::Vector2d dxy = spline->DPoint(alpha);
35 res[0] = dxy.x();
36 res[1] = dxy.y();
Alex Perry20762632019-01-21 17:48:02 -050037 }
38
Alex Perry0603b542019-01-25 20:29:51 -080039 void SplineDDPoint(NSpline<6> *spline, double alpha, double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080040 const Eigen::Vector2d ddxy = spline->DDPoint(alpha);
41 res[0] = ddxy.x();
42 res[1] = ddxy.y();
Alex Perry20762632019-01-21 17:48:02 -050043 }
44
Alex Perry0603b542019-01-25 20:29:51 -080045 void SplineDDDPoint(NSpline<6> *spline, double alpha, double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080046 const Eigen::Vector2d dddxy = spline->DDDPoint(alpha);
47 res[0] = dddxy.x();
48 res[1] = dddxy.y();
Alex Perry20762632019-01-21 17:48:02 -050049 }
50
Alex Perry0603b542019-01-25 20:29:51 -080051 double SplineTheta(NSpline<6> *spline, double alpha) {
Alex Perry20762632019-01-21 17:48:02 -050052 return spline->Theta(alpha);
53 }
54
Alex Perry0603b542019-01-25 20:29:51 -080055 double SplineDTheta(NSpline<6> *spline, double alpha) {
Alex Perry20762632019-01-21 17:48:02 -050056 return spline->DTheta(alpha);
57 }
58
Alex Perry0603b542019-01-25 20:29:51 -080059 double SplineDDTheta(NSpline<6> *spline, double alpha) {
Alex Perry20762632019-01-21 17:48:02 -050060 return spline->DDTheta(alpha);
61 }
62
Alex Perry0603b542019-01-25 20:29:51 -080063 void SplineControlPoints(NSpline<6> *spline, double *x, double *y) {
Alex Perry20762632019-01-21 17:48:02 -050064 auto points = spline->control_points();
65 // Deal with incorrectly strided matrix.
66 for (int i = 0; i < 6; ++i) {
67 x[i] = points(0, i);
68 y[i] = points(1, i);
69 }
70 }
Alex Perrya60da442019-01-21 19:00:27 -050071
Alex Perry0603b542019-01-25 20:29:51 -080072 // Based on distance_spline.h
73 DistanceSpline *NewDistanceSpline(Spline **splines, int count) {
Alex Perrya60da442019-01-21 19:00:27 -050074 ::std::vector<Spline> splines_;
75 for (int i = 0; i < count; ++i) {
76 splines_.push_back(*splines[i]);
77 }
78 return new DistanceSpline(::std::vector<Spline>(splines_));
79 }
80
Alex Perry0603b542019-01-25 20:29:51 -080081 void deleteDistanceSpline(DistanceSpline *spline) { delete spline; }
Alex Perrya60da442019-01-21 19:00:27 -050082
83 void DistanceSplineXY(DistanceSpline *spline, double distance, double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080084 const Eigen::Vector2d xy = spline->XY(distance);
85 res[0] = xy.x();
86 res[1] = xy.y();
Alex Perrya60da442019-01-21 19:00:27 -050087 }
88
89 void DistanceSplineDXY(DistanceSpline *spline, double distance, double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080090 const Eigen::Vector2d dxy = spline->DXY(distance);
91 res[0] = dxy.x();
92 res[1] = dxy.y();
Alex Perrya60da442019-01-21 19:00:27 -050093 }
94
95 void DistanceSplineDDXY(DistanceSpline *spline, double distance,
96 double *res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080097 const Eigen::Vector2d ddxy = spline->DDXY(distance);
98 res[0] = ddxy.x();
99 res[1] = ddxy.y();
Alex Perrya60da442019-01-21 19:00:27 -0500100 }
101
102 double DistanceSplineTheta(DistanceSpline *spline, double distance) {
103 return spline->Theta(distance);
104 }
105
106 double DistanceSplineDTheta(DistanceSpline *spline, double distance) {
107 return spline->DTheta(distance);
108 }
109
110 double DistanceSplineDThetaDt(DistanceSpline *spline, double distance,
111 double velocity) {
112 return spline->DThetaDt(distance, velocity);
113 }
114
115 double DistanceSplineDDTheta(DistanceSpline *spline, double distance) {
116 return spline->DDTheta(distance);
117 }
118
119 double DistanceSplineLength(DistanceSpline *spline) {
120 return spline->length();
121 }
Alex Perry0603b542019-01-25 20:29:51 -0800122
123 // Based on trajectory.h
124 Trajectory *NewTrajectory(DistanceSpline *spline, double vmax,
125 int num_distance) {
126 return new Trajectory(
127 spline, ::y2019::control_loops::drivetrain::GetDrivetrainConfig(), vmax,
128 num_distance);
129 }
130
131 void deleteTrajectory(Trajectory *t) { delete t; }
132
133 void TrajectorySetLongitudalAcceleration(Trajectory *t, double accel) {
134 t->set_longitudal_acceleration(accel);
135 }
136
137 void TrajectorySetLateralAcceleration(Trajectory *t, double accel) {
138 t->set_lateral_acceleration(accel);
139 }
140
141 void TrajectorySetVoltageLimit(Trajectory *t, double limit) {
142 t->set_voltage_limit(limit);
143 }
144
145 void TrajectoryLimitVelocity(Trajectory *t, double start, double end,
146 double max) {
147 t->LimitVelocity(start, end, max);
148 }
149
150 void TrajectoryPlan(Trajectory *t) { t->Plan(); }
151
Alex Perry50baefc2019-01-27 13:26:29 -0800152 void TrajectoryVoltage(Trajectory *t, double distance, double* res) {
James Kuszmaul3ae42262019-11-08 12:33:41 -0800153 const Eigen::Vector2d ff_voltage = t->FFVoltage(distance);
154 res[0] = ff_voltage.x();
155 res[1] = ff_voltage.y();
Alex Perry50baefc2019-01-27 13:26:29 -0800156 }
157
Alex Perry0603b542019-01-25 20:29:51 -0800158 double TrajectoryLength(Trajectory *t) { return t->length(); }
159
160 int TrajectoryGetPathLength(Trajectory *t) { return t->plan().size(); }
161
162 // This assumes that res is created in python to be getPathLength() long.
163 // Likely to SEGFAULT otherwise.
164 void Distances(Trajectory *t, double *res) {
165 const ::std::vector<double> &distances = t->Distances();
166 ::std::memcpy(res, distances.data(), sizeof(double) * distances.size());
167 }
168
169 double TrajectoryDistance(Trajectory *t, int index) {
170 return t->Distance(index);
171 }
172
173 // This assumes that res is created in python to be getPathLength() long.
174 // Likely to SEGFAULT otherwise.
175 void TrajectoryGetPlan(Trajectory *t, double *res) {
176 const ::std::vector<double> &velocities = t->plan();
177 ::std::memcpy(res, velocities.data(), sizeof(double) * velocities.size());
178 }
179
180 // Time in in nanoseconds.
181 ::std::vector<::Eigen::Matrix<double, 3, 1>> *TrajectoryGetPlanXVAPtr(
182 Trajectory *t, int dt) {
183 return new ::std::vector<::Eigen::Matrix<double, 3, 1>>(
184 t->PlanXVA(::std::chrono::nanoseconds(dt)));
185 }
186
187 void TrajectoryDeleteVector(
188 ::std::vector<::Eigen::Matrix<double, 3, 1>> *vec) {
189 delete vec;
190 }
191
192 int TrajectoryGetVectorLength(
193 ::std::vector<::Eigen::Matrix<double, 3, 1>> *vec) {
194 return vec->size();
195 }
196
197 void TrajectoryGetPlanXVA(::std::vector<::Eigen::Matrix<double, 3, 1>> *vec,
198 double *X, double *V, double *A) {
199 for (size_t i = 0; i < vec->size(); ++i) {
Alex Perry4b9c5312019-03-02 20:59:47 -0800200 X[i] = (*vec)[i][0];
201 V[i] = (*vec)[i][1];
202 A[i] = (*vec)[i][2];
Alex Perry0603b542019-01-25 20:29:51 -0800203 }
204 }
205
206 // Util
207 void SetUpLogging() {
208 ::aos::logging::Init();
209 ::aos::network::OverrideTeamNumber(971);
210 }
Alex Perry20762632019-01-21 17:48:02 -0500211}
212
213} // namespace drivetrain
214} // namespace control_loops
215} // namespace frc971