clear; // Clear all variables
Distance=2.5; // Distance to move
Time=0.3; // Time to move the distance
Vel=1.5*Distance/Time // Calculate and print the constant velocity
Acc=4.5*Distance/Time^2 // Calculate and print the acceleration rate
t0=0; // Initial time
t1=Time/3; // Time when acceleration ends and constant velocities begins
t2=Time*2/3; // Time when constant velocity ends and deceleration begins
t3=Time; // Time when deceleration ends and the actuator is stopped
tn=Time+0.1; // Time when plot ends
N=(tn/0.01)+1; // Number of data point spaced 0.01 second apart
t01=t1-t0; // Acceleration time
t12=t2-t1; // Constant velocity time
t23=t3-t2; // Deceleration time
x0=0; // Initial position
v0=0; // Initial velocity
a0=Acc; // Initial acceleratoin
x1=x0+v0*t01+0.5*Acc*t01^2; // Position after accelerating
v1=v0+a0*t01; // Velocity after accelerating
a1=0; // Acceleration is 0 at constant velocity
x2=x1+v1*t12; // Position after constant velocity and before decelerating
v2=v1; // Velocity after constant velocity and before decelerating
a2=-Acc; // Acceleration is negative while decelerating
x3=Distance; // Final position
v3=0; // Final velocity
tv=linspace(t0,tn,N); // Time vector, N time points spaced 0.01 seconds apart
// Calculate the Motion Profile
for n=1:N;
t = tv(n); // get the time for this period
if t0<=t & t<t1 then // Accelerating
t=t-t0;
pos(n) = x0+v0*t+0.5*a0*t^2;
vel(n) = v0+a0*t;
elseif t1<=t & t<t2 then // Constant Velocity
t=t-t1;
pos(n) = x1+v1*t;
vel(n) = v1;
elseif t2<=t & t<t3 then // Decelerating
t=t-t2;
pos(n)= x2+v2*t+0.5*a2*t^2;
vel(n)= v2+a2*t;
elseif t3<=t then // Stopped when done
t=t-t3;
pos(n)=x3;
vel(n)=0;
end
end
// Plot the Motion Profile
clf(); // Clear or reset the current graphics figure
subplot(2,1,1); // Position plot
plot2d(tv,pos);
xtitle('Position Profile','Time In Seconds','Position');
// legend("Position");
subplot(2,1,2); // Velocity plot
plot2d(tv,vel);
xtitle('Velocity Profile','Time In Seconds','Velocity');
// legend("Velocity");