How to constrain the time and how to derive the acceleration and position trajectories from given velocity trajectory?
Show older comments
Dear all,
I am trying to generate trajectories in matlab for a robot to pass through. It is 1-D. There are 2 questions:
1- Using the trapveltraj built-in function in matlab, how can I assign a constrained time to acheive the desired velocity/position trajectory? (I know EndTime Name-Value argument exists but looks like it sets the time duration between each sample?!).
2- second question is imagine if there is a given velocity profile, how can I derive the corresponding displacement/position and acceleration trajectories?
(using trapveltraj we usually define the waypoints to be passed and the number of samples)
Accepted Answer
More Answers (1)
Alireza
on 16 Dec 2024
1 Comment
You say: Max range=+-0.01 m, max velocity=5 m/s.
I assume, in my analysis and code below, that the robot starts moving in the opposite direction as soon as it reaches point b or point -b.
Function trapveltraj() will make a trajectory meeting the requirements, if we tell it two of the following four things:
- End Time — Duration of each segment between two waypoints
- Peak Velocity — Peak velocity for each segment
- Acceleration Time — Time spent in the acceleration and deceleration phases of each segment
- Peak Acceleration — Magnitude of the acceleration applied during the acceleration and deceleration phases of each segment
I think acceleration times (option A) and peak acceleration (option B) are the more appealing options. We will consider both options.
A. Specify acceleration times. To do this, we could just specify a time value in seconds. I think it would be more elegant to specify accFrac=the fraction of each segment spent accelerating. It must be that 0<=accFrac<=0.5, because the total time spent accelerating and decelerating must not exceed the segment duration. With a little algebra, you can verify that time duration of segment i is
where Ti=time duration and Di=distance covered in segment i.
We need a function, wayPointAccTime(), that returns the waypoints (i.e. position values) and acceleration times, given vmax and b and accFrac and Nc=desired number of cycles. It will also return the total elapsed time at each waypoint, which will be useful for plotting. Each cycle goes from position 0 to +b to -b to 0. If there is more than 1 cycle, the robot does not slow down as it goes from -b to +b. The waypoints and acceleration times from wayPointAccTime() will be inputs to trapveltraj().
function [wayPts,accTime,elapTime] = wayPointAccTime(b,vmax,Nc,accFrac)
% WAYPOINTACCTIME Compute waypoints and acceleraiton times for a 1-D trajectory.
% See @Alireza, "How to constrain the time...", Matlab Answers 2024-12-16.
% Alireza and W. Rose
% Inputs
% b = max + and - position of robot
% vmax = maximum velocity
% Nc = number of cycles
% accFrac= time fraction of each cycle spent accelerating (0<=accFrac<=0.5)
% Outputs
% wayPts=vector of 2*Nc+2 positions. wayPt(1)=wayPt(end)=0.
% accTime = vector of 2*Nc+1 times = duration of acceleration in each segment
% = duration of deceleration in each segment.
% elapTime=vector of 2*Nc+2 times=total elapsed time at each waypoint.
% Outputs wayPts and accTime are inputs to trapveltraj().
wayPts=b*(-1).^(1:2*Nc+2); % [-b,+b,-b,+b...], length 2*Nc+2
wayPts(1)=0; wayPts(end)=0; % fist and last elements=0
d=abs(diff(wayPts)); % distances between waypoints
T=d/(vmax*(1-accFrac)); % duration of each segment
accTime=T*accFrac; % acceleration durations for each segment
elapTime=cumsum([0,T]); % total elapsed time to each waypoint
end
Check that function wayPointAccTime() works as expected:
b=0.05; % max displacement [m]
vmax=10; % max veloc [m/s]
Nc=2; % number of cycles
accFrac=0.25; % time fraction of each segment spent accelerating
[wayPts,accTime,elapTime] = wayPointAccTime(b,vmax,Nc,accFrac);
% Display outputs from function wayPointAccTime()
fprintf('Way points: %.3f %.3f %.3f %.3f %.3f %.3f \n',wayPts)
fprintf('Elapsed time x 1000: %.2f %.2f %.2f %.2f %.2f %.2f \n',elapTime*1000)
fprintf('Accel. time x 1000: %.2f %.2f %.2f %.2f %.2f \n',accTime*1000)
The results above look reasonable.
Use trapveltraj() to make a trajectory. The inputs to trapveltraj() will include the waypoints and acceleration times.
numSamples=24*length(accTime)+1; % number of points=24*number of segements, +1
[q,qd,qdd,tSamples,pp] = ...
trapveltraj(wayPts,numSamples,AccelTime=accTime,PeakVelocity=vmax);
% Plot results
figure
subplot(211), plot(elapTime,wayPts,'bs',tSamples,q,'-r.')
ylabel('Position (m)'); grid on
legend('Way Points','Trajectory'); title('Position')
subplot(212), plot(tSamples,qd,'-r.')
ylabel('Velocity (m/s)'); xlabel('Time (s)'); grid on
legend('Trajectory'); title('Velocity')
The plot above shows that the functions are working as expected and they produce a reasonable trajectory.
B. Specify peak acceleration, accMax, instead of acceleration times. With this option, we will not specify acceleration fraction (accFrac). With this option, we do not need function wayPointAccTime(), because we don't need the acceleration time vector. You can show that accMax must satisfy
in order that the peak velocity, vMax, be reached at least momentarily in every segment. You can also show, with some algebra, that Ti, the fastest allowed time duration of segment i, is given by
where Di = distance covered in segment i. You can work out the details to specify a trajectory using vMax and accMax.
Categories
Find more on Trajectory Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



