blob: 0d14f6651319c879c89422a9983ebc10abe4c13c [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) {
28 double *val = spline->Point(alpha).data();
Alex Perry20762632019-01-21 17:48:02 -050029 res[0] = val[0];
30 res[1] = val[1];
31 }
32
Alex Perry0603b542019-01-25 20:29:51 -080033 void SplineDPoint(NSpline<6> *spline, double alpha, double *res) {
34 double *val = spline->DPoint(alpha).data();
Alex Perry20762632019-01-21 17:48:02 -050035 res[0] = val[0];
36 res[1] = val[1];
37 }
38
Alex Perry0603b542019-01-25 20:29:51 -080039 void SplineDDPoint(NSpline<6> *spline, double alpha, double *res) {
40 double *val = spline->DDPoint(alpha).data();
Alex Perry20762632019-01-21 17:48:02 -050041 res[0] = val[0];
42 res[1] = val[1];
43 }
44
Alex Perry0603b542019-01-25 20:29:51 -080045 void SplineDDDPoint(NSpline<6> *spline, double alpha, double *res) {
46 double *val = spline->DDDPoint(alpha).data();
Alex Perry20762632019-01-21 17:48:02 -050047 res[0] = val[0];
48 res[1] = val[1];
49 }
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) {
84 double *val = spline->XY(distance).data();
85 res[0] = val[0];
86 res[1] = val[1];
87 }
88
89 void DistanceSplineDXY(DistanceSpline *spline, double distance, double *res) {
90 double *val = spline->DXY(distance).data();
91 res[0] = val[0];
92 res[1] = val[1];
93 }
94
95 void DistanceSplineDDXY(DistanceSpline *spline, double distance,
96 double *res) {
97 double *val = spline->DDXY(distance).data();
98 res[0] = val[0];
99 res[1] = val[1];
100 }
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
152 double TrajectoryLength(Trajectory *t) { return t->length(); }
153
154 int TrajectoryGetPathLength(Trajectory *t) { return t->plan().size(); }
155
156 // This assumes that res is created in python to be getPathLength() long.
157 // Likely to SEGFAULT otherwise.
158 void Distances(Trajectory *t, double *res) {
159 const ::std::vector<double> &distances = t->Distances();
160 ::std::memcpy(res, distances.data(), sizeof(double) * distances.size());
161 }
162
163 double TrajectoryDistance(Trajectory *t, int index) {
164 return t->Distance(index);
165 }
166
167 // This assumes that res is created in python to be getPathLength() long.
168 // Likely to SEGFAULT otherwise.
169 void TrajectoryGetPlan(Trajectory *t, double *res) {
170 const ::std::vector<double> &velocities = t->plan();
171 ::std::memcpy(res, velocities.data(), sizeof(double) * velocities.size());
172 }
173
174 // Time in in nanoseconds.
175 ::std::vector<::Eigen::Matrix<double, 3, 1>> *TrajectoryGetPlanXVAPtr(
176 Trajectory *t, int dt) {
177 return new ::std::vector<::Eigen::Matrix<double, 3, 1>>(
178 t->PlanXVA(::std::chrono::nanoseconds(dt)));
179 }
180
181 void TrajectoryDeleteVector(
182 ::std::vector<::Eigen::Matrix<double, 3, 1>> *vec) {
183 delete vec;
184 }
185
186 int TrajectoryGetVectorLength(
187 ::std::vector<::Eigen::Matrix<double, 3, 1>> *vec) {
188 return vec->size();
189 }
190
191 void TrajectoryGetPlanXVA(::std::vector<::Eigen::Matrix<double, 3, 1>> *vec,
192 double *X, double *V, double *A) {
193 for (size_t i = 0; i < vec->size(); ++i) {
194 X[i] = (*vec)[0][0];
195 V[i] = (*vec)[0][1];
196 A[i] = (*vec)[0][2];
197 }
198 }
199
200 // Util
201 void SetUpLogging() {
202 ::aos::logging::Init();
203 ::aos::network::OverrideTeamNumber(971);
204 }
Alex Perry20762632019-01-21 17:48:02 -0500205}
206
207} // namespace drivetrain
208} // namespace control_loops
209} // namespace frc971