32 for (
int i = 1; i <= n; i++)
34 powers[i] = powers[i - 1] * x;
38 void sampleCubicSpline(
const std::vector<double>& coefficients,
double time,
double& position,
double& velocity,
45 position = t[0] * coefficients[0] + t[1] * coefficients[1] + t[2] * coefficients[2] + t[3] * coefficients[3];
46 velocity = t[0] * coefficients[1] + 2.0 * t[1] * coefficients[2] + 3.0 * t[2] * coefficients[3];
47 acceleration = 2.0 * t[0] * coefficients[2] + 6.0 * t[1] * coefficients[3];
51 std::vector<double>& coefficients)
53 coefficients.resize(4);
57 coefficients[0] = end_pos;
58 coefficients[1] = end_vel;
59 coefficients[2] = 0.0;
60 coefficients[3] = 0.0;
67 coefficients[0] = start_pos;
68 coefficients[1] = start_vel;
69 coefficients[2] = (-3.0 * start_pos + 3.0 * end_pos - 2.0 * start_vel * T[1] - end_vel * T[1]) / T[2];
70 coefficients[3] = (2.0 * start_pos - 2.0 * end_pos + start_vel * T[1] + end_vel * T[1]) / T[3];
75 void splineCoefficients(
int steps,
double *timearray,
double *encoderarray,
double *arr_p1,
double *arr_p2,
76 double *arr_p3,
double *arr_p4)
82 double deltatime[steps];
84 for (i = 0; i < steps; i++)
86 deltatime[i] = timearray[i + 1] - timearray[i];
87 b[i] = 1.0 / deltatime[i];
92 for (i = 0; i < (steps - 1); i++)
94 a[i] = (2 / deltatime[i]) + (2 / deltatime[i + 1]);
102 for (i = 0; i < steps; i++)
104 c[i] = (encoderarray[i + 1] - encoderarray[i]) / (deltatime[i] * deltatime[i]);
106 for (i = 0; i < (steps - 1); i++)
108 d[i + 1] = 3 * (c[i] + c[i + 1]);
112 double Alin[steps - 1][steps];
115 for (i = 0; i < (steps - 1); i++)
117 for (j = 0; j < steps; j++)
123 for (i = 0; i < (steps - 1); i++)
129 Alin[0][steps - 1] = d[1];
133 Alin[i][i - 1] = b[i];
135 Alin[i][i + 1] = b[i + 1];
136 Alin[i][steps - 1] = d[i + 1];
141 boost::numeric::ublas::matrix<double> ublas_A(steps - 1, steps - 1);
142 boost::numeric::ublas::matrix<double> ublas_b(steps - 1, 1);
143 for (i = 0; i < (steps - 1); i++)
145 for (j = 0; j < (steps - 1); j++)
147 ublas_A(i, j) = Alin[i][j];
149 ublas_b(i, 0) = Alin[i][steps - 1];
151 boost::numeric::ublas::permutation_matrix<unsigned int> piv(steps - 1);
152 lu_factorize(ublas_A, piv);
153 lu_substitute(ublas_A, piv, ublas_b);
156 double derivatives[steps + 1];
158 for (i = 0; i < (steps - 1); i++)
160 derivatives[i + 1] = ublas_b(i, 0);
162 derivatives[steps] = 0;
166 double a0, b0, c0, d0;
167 for (i = 0; i < steps; i++)
169 a0 = encoderarray[i];
170 b0 = encoderarray[i + 1] - a0;
171 c0 = b0 - deltatime[i] * derivatives[i];
172 d0 = deltatime[i] * (derivatives[i + 1] + derivatives[i]) - 2 * b0;
179 arr_p2[i] = arr_p2[i] / deltatime[i];
180 arr_p3[i] = arr_p3[i] / pow(deltatime[i], 2);
181 arr_p4[i] = arr_p4[i] / pow(deltatime[i], 3);
186 double& position,
double& velocity,
double& acceleration)
195 else if (time > duration)
void sampleCubicSpline(const std::vector< double > &coefficients, double time, double &position, double &velocity, double &acceleration)
Samples a cubic spline segment at a particular time.
void getCubicSplineCoefficients(double start_pos, double start_vel, double end_pos, double end_vel, double time, std::vector< double > &coefficients)
void splineCoefficients(int steps, double *timearray, double *encoderarray, double *arr_p1, double *arr_p2, double *arr_p3, double *arr_p4)
void generatePowers(int n, double x, double *powers)
FloatVector FloatVector * a
void sampleSplineWithTimeBounds(const std::vector< double > &coefficients, double duration, double time, double &position, double &velocity, double &acceleration)