Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/trackjoint/single_joint_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ class SingleJointGenerator
ErrorCodeEnum predictTimeToReach();

/** \brief Calculate vel/accel/jerk from position */
void calculateDerivatives();
void calculateDerivativesFromPosition();

/** \brief Calculate accel/jerk from velocity */
void calculateDerivativesFromVelocity();

const size_t kNumWaypointsThreshold, kMaxNumWaypointsFullTrajectory;

Expand Down
29 changes: 17 additions & 12 deletions src/single_joint_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ErrorCodeEnum SingleJointGenerator::generateTrajectory()
waypoints_.positions = interpolate(nominal_times_);

waypoints_.elapsed_times.setLinSpaced(waypoints_.positions.size(), 0., desired_duration_);
calculateDerivatives();
calculateDerivativesFromPosition();

ErrorCodeEnum error_code = positionVectorLimitLookAhead(&index_last_successful_);
if (error_code)
Expand All @@ -76,7 +76,7 @@ ErrorCodeEnum SingleJointGenerator::extendTrajectoryDuration()
size_t expected_num_waypoints = 1 + desired_duration_ / timestep_;
waypoints_.elapsed_times.setLinSpaced(expected_num_waypoints, 0., desired_duration_);
waypoints_.positions = interpolate(waypoints_.elapsed_times);
calculateDerivatives();
calculateDerivativesFromPosition();
ErrorCodeEnum error_code = forwardLimitCompensation(&index_last_successful_);
return error_code;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ inline ErrorCodeEnum SingleJointGenerator::forwardLimitCompensation(size_t* inde
0.5 * waypoints_.jerks(index) * timestep_ * timestep_;

// Re-calculate derivatives from the updated velocity vector
calculateDerivatives();
calculateDerivativesFromVelocity();

delta_v = 0.5 * delta_j * timestep_ * timestep_;

Expand Down Expand Up @@ -205,7 +205,7 @@ inline ErrorCodeEnum SingleJointGenerator::forwardLimitCompensation(size_t* inde
waypoints_.accelerations(index - 1) * timestep_ +
0.5 * waypoints_.jerks(index) * timestep_ * timestep_;
// Re-calculate derivatives from the updated velocity vector
calculateDerivatives();
calculateDerivativesFromVelocity();
}
else
{
Expand Down Expand Up @@ -247,7 +247,7 @@ inline ErrorCodeEnum SingleJointGenerator::forwardLimitCompensation(size_t* inde
delta_v = std::copysign(velocity_limit, waypoints_.velocities(index)) - waypoints_.velocities(index);
waypoints_.velocities(index) = std::copysign(velocity_limit, waypoints_.velocities(index));
// Re-calculate derivatives from the updated velocity vector
calculateDerivatives();
calculateDerivativesFromVelocity();

// Try adjusting the velocity in previous timesteps to compensate for this limit.
// Try to account for position error, too.
Expand Down Expand Up @@ -393,8 +393,7 @@ inline bool SingleJointGenerator::backwardLimitCompensation(size_t limited_index
inline ErrorCodeEnum SingleJointGenerator::predictTimeToReach()
{
// Take a trajectory that could not reach the desired position in time.
// Try increasing the duration until it is interpolated without violating
// limits.
// Try increasing the duration until it is interpolated without violating limits.
// This gives a new duration estimate.

ErrorCodeEnum error_code = ErrorCodeEnum::kNoError;
Expand Down Expand Up @@ -431,7 +430,7 @@ inline ErrorCodeEnum SingleJointGenerator::predictTimeToReach()
// Try to create the trajectory again, with the new duration
////////////////////////////////////////////////////////////
waypoints_.positions = interpolate(waypoints_.elapsed_times);
calculateDerivatives();
calculateDerivativesFromPosition();
positionVectorLimitLookAhead(&index_last_successful_);
}
}
Expand Down Expand Up @@ -509,17 +508,23 @@ inline ErrorCodeEnum SingleJointGenerator::positionVectorLimitLookAhead(size_t*
return error_code;
}

inline void SingleJointGenerator::calculateDerivatives()
inline void SingleJointGenerator::calculateDerivativesFromPosition()
{
// From position vector, approximate velocity and acceleration.
// velocity = (difference between adjacent position elements) / delta_t
// acceleration = (difference between adjacent velocity elements) / delta_t
// From position vector, approximate vel/accel/jerk.
waypoints_.velocities = DiscreteDifferentiation(waypoints_.positions, timestep_, current_joint_state_.velocity);
waypoints_.accelerations =
DiscreteDifferentiation(waypoints_.velocities, timestep_, current_joint_state_.acceleration);
waypoints_.jerks = DiscreteDifferentiation(waypoints_.accelerations, timestep_, 0);
}

inline void SingleJointGenerator::calculateDerivativesFromVelocity()
{
// From velocity vector, approximate accel/jerk.
waypoints_.accelerations =
DiscreteDifferentiation(waypoints_.velocities, timestep_, current_joint_state_.acceleration);
waypoints_.jerks = DiscreteDifferentiation(waypoints_.accelerations, timestep_, 0);
}

void SingleJointGenerator::updateTrajectoryDuration(double new_trajectory_duration)
{
// The trajectory will be forced to have this duration (or fail) because
Expand Down
4 changes: 2 additions & 2 deletions test/trajectory_generation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ TEST_F(TrajectoryGenerationTest, TimestepDidNotMatch)
EXPECT_NEAR(output_trajectories_[0].elapsed_times[1] - output_trajectories_[0].elapsed_times[0], timestep_,
timestep_tolerance);
}

/*
TEST_F(TrajectoryGenerationTest, CustomerStreaming)
{
// A customer-requested streaming test.
Expand Down Expand Up @@ -929,7 +929,7 @@ TEST_F(TrajectoryGenerationTest, CustomerStreaming)

// If the test gets here, it passed.
}

*/
TEST_F(TrajectoryGenerationTest, StreamingTooFewTimesteps)
{
// An error should be thrown if streaming mode is enabled with a desired duration < kMinNumTimesteps
Expand Down