59 time = boost::posix_time::microsec_clock::local_time();
60 last_time = boost::posix_time::microsec_clock::local_time();
75 traj[0].start_time = boost::posix_time::microsec_clock::local_time();
76 traj[0].duration = boost::posix_time::microseconds(0);
92 this->
pid.
getGains(PParameter, IParameter, DParameter, IClippingMax, IClippingMin);
100 this->
pid.
setGains(PParameter, IParameter, DParameter, IClippingMax, IClippingMin);
107 if (input_traj.
segments.size() == 0)
108 throw std::runtime_error(
"Invalid trajectory");
110 boost::posix_time::ptime time_now = boost::posix_time::microsec_clock::local_time();
119 if (!prev_traj_ptr) {
120 throw std::runtime_error(
"The current trajectory can never be null");
128 int first_useful = -1;
129 while (first_useful + 1 < (
int) prev_traj.size() && prev_traj[first_useful + 1].start_time <= time_now) {
134 int last_useful = -1;
135 while (last_useful + 1 < (
int) prev_traj.size() && prev_traj[last_useful + 1].start_time < time_now) {
139 if (last_useful < first_useful)
140 first_useful = last_useful;
143 for (
int i = std::max(first_useful, 0); i <= last_useful; ++i) {
144 new_traj.push_back(prev_traj[i]);
149 if (new_traj.size() == 0)
150 new_traj.push_back(prev_traj[prev_traj.size() - 1]);
155 Segment &last = new_traj[new_traj.size() - 1];
156 double prev_positions;
157 double prev_velocities;
158 double prev_accelerations;
160 LOG(
debug) <<
"Initial conditions for new set of splines:";
163 (double)last.
start_time.time_of_day().total_microseconds()/1000.0/1000.0,
164 prev_positions, prev_velocities, prev_accelerations);
174 double accelerations;
178 std::vector<boost::posix_time::time_duration> durations;
179 durations.push_back(input_traj.
segments[0].time_from_start);
181 for (
size_t i = 1; i < input_traj.
segments.size(); ++i)
182 durations.push_back(input_traj.
segments[i].time_from_start - input_traj.
segments[i - 1].time_from_start);
184 for (
unsigned int i = 0; i < input_traj.
segments.size(); ++i) {
191 positions = input_traj.
segments[i].positions.value();
192 velocities = input_traj.
segments[i].velocities.value();
193 accelerations = input_traj.
segments[i].accelerations.value();
200 prev_positions, prev_velocities, prev_accelerations,
201 positions, velocities, accelerations,
202 (
double)durations[i].total_microseconds()/1000.0/1000.0,
231 new_traj.push_back(seg);
235 prev_positions = positions;
236 prev_velocities = velocities;
237 prev_accelerations = accelerations;
243 throw std::runtime_error(
"The new trajectory was null!");
248 LOG(
debug) <<
"The new trajectory has " << new_traj.size() <<
" segments";
257 traj[0].start_time = boost::posix_time::microsec_clock::local_time();
258 traj[0].duration = boost::posix_time::microseconds(0);
261 LOG(
trace) <<
"Trajectory has been canceled";
270 time = boost::posix_time::microsec_clock::local_time();
288 while (seg + 1 < (
int) traj.size() && traj[seg + 1].start_time <
time) {
293 if (traj.size() == 0)
294 LOG(
error) <<
"No segments in the trajectory";
296 LOG(
error) <<
"No earlier segments.";
299 if(seg == (
int) traj.size()-1 && (traj[seg].start_time + traj[seg].duration) <
time){
300 LOG(
trace) <<
"trajectory finished.";
351 coefficients.resize(6);
354 coefficients[0] = end_pos;
355 coefficients[1] = end_vel;
356 coefficients[2] = 0.5 * end_acc;
357 coefficients[3] = 0.0;
358 coefficients[4] = 0.0;
359 coefficients[5] = 0.0;
364 coefficients[0] = start_pos;
365 coefficients[1] = start_vel;
366 coefficients[2] = 0.5 * start_acc;
367 coefficients[3] = (-20.0 * start_pos + 20.0 * end_pos - 3.0 * start_acc * T[2] + end_acc * T[2] -
368 12.0 * start_vel * T[1] - 8.0 * end_vel * T[1]) / (2.0 * T[3]);
369 coefficients[4] = (30.0 * start_pos - 30.0 * end_pos + 3.0 * start_acc * T[2] - 2.0 * end_acc * T[2] +
370 16.0 * start_vel * T[1] + 14.0 * end_vel * T[1]) / (2.0 * T[4]);
371 coefficients[5] = (-12.0 * start_pos + 12.0 * end_pos - start_acc * T[2] + end_acc * T[2] -
372 6.0 * start_vel * T[1] - 6.0 * end_vel * T[1]) / (2.0 * T[5]);
383 position = t[0] * coefficients[0] +
384 t[1] * coefficients[1] +
385 t[2] * coefficients[2] +
386 t[3] * coefficients[3] +
387 t[4] * coefficients[4] +
388 t[5] * coefficients[5];
390 velocity = t[0] * coefficients[1] +
391 2.0 * t[1] * coefficients[2] +
392 3.0 * t[2] * coefficients[3] +
393 4.0 * t[3] * coefficients[4] +
394 5.0 * t[4] * coefficients[5];
396 acceleration = 2.0 * t[0] * coefficients[2] +
397 6.0 * t[1] * coefficients[3] +
398 12.0 * t[2] * coefficients[4] +
399 20.0 * t[3] * coefficients[5];
405 coefficients.resize(4);
408 coefficients[0] = end_pos;
409 coefficients[1] = end_vel;
410 coefficients[2] = 0.0;
411 coefficients[3] = 0.0;
416 coefficients[0] = start_pos;
417 coefficients[1] = start_vel;
418 coefficients[2] = (-3.0 * start_pos + 3.0 * end_pos - 2.0 * start_vel * T[1] - end_vel * T[1]) / T[2];
419 coefficients[3] = (2.0 * start_pos - 2.0 * end_pos + start_vel * T[1] + end_vel * T[1]) / T[3];
427 for (
int i = 1; i <= n; i++) {
428 powers[i] = powers[i - 1] * x;
440 }
else if (time > duration) {
447 position, velocity, acceleration);
DataObjectLockFree< boost::shared_ptr< const SpecifiedTrajectory > > current_trajectory_box_
boost::posix_time::ptime time
std::vector< TrajectorySegment > segments
void getCubicSplineCoefficients(const double start_pos, const double start_vel, const double end_pos, const double end_vel, const double time, std::vector< double > &coefficients)
boost::posix_time::ptime last_time
quantity< angular_velocity > angularVelocity
JointTrajectoryController()
boost::posix_time::ptime start_time
boost::posix_time::ptime start_time
virtual ~JointTrajectoryController()
Set-point velocity of the joint.
Output part from the EtherCat message of the youBot EtherCat slaves.
void setGains(double P, double I, double D, double i_max, double i_min)
Set PID gains for the controller.
virtual DataType Get() const
std::vector< Segment > SpecifiedTrajectory
bool updateTrajectoryController(const SlaveMessageInput &actual, SlaveMessageOutput &velocity)
bool isTrajectoryControllerActive()
void setConfigurationParameter(const double PParameter, const double IParameter, const double DParameter, const double IClippingMax, const double IClippingMin)
std::vector< double > coef
Joint Trajectory representation.
double updatePid(double p_error, boost::posix_time::time_duration dt)
Update the Pid loop with nonuniform time step size.
void getLastTargetVelocity(JointVelocitySetpoint &velocity)
void sampleQuinticSpline(const std::vector< double > &coefficients, const double time, double &position, double &velocity, double &acceleration)
Joint parameter exception.
virtual void Set(const DataType &push)
Joint trajectory segment.
void getGains(double &p, double &i, double &d, double &i_max, double &i_min)
Get PID gains for the controller.
void getConfigurationParameter(double &PParameter, double &IParameter, double &DParameter, double &IClippingMax, double &IClippingMin)
double time_till_seg_start
void setTrajectory(const JointTrajectory &input_traj)
void generatePowers(const int n, const double x, double *powers)
void getQuinticSplineCoefficients(const double start_pos, const double start_vel, const double start_acc, const double end_pos, const double end_vel, const double end_acc, const double time, std::vector< double > &coefficients)
double targetAcceleration
void initPid(double P, double I, double D, double I1, double I2)
Initialize PID-gains and integral term limits:[iMax:iMin]-[I1:I2].
boost::posix_time::time_duration duration
void cancelCurrentTrajectory()
void sampleSplineWithTimeBounds(const std::vector< double > &coefficients, const double duration, const double time, double &position, double &velocity, double &acceleration)
quantity< plane_angle > angle
Set-point angle / position of the joint.
void getLastTargetPosition(JointAngleSetpoint &position)