Team2168 / 2018_Main_Robot

0 stars 0 forks source link

found error in matlab code #30

Closed NotInControl closed 6 years ago

NotInControl commented 6 years ago

@AidanSullivan03 I found an error in the original matlab code and fixed it. The new code is below:

the errors were before the for loop so make sure all of the code from the start of the s-curve function to the start of the for loop matches the below. I pasted the entire code for reference. Should fix your discontinuities and the weird stuff being plotted

``

    q0 = 0;
    q1 = 25;
    v0 = 0.5;
    v1 = 0.5;
    a0 = 0;
    a1 = 0;
    t0 = 0;
    % t1 = 2;

    v_max=12;
    a_max=10;
    j_max=30;
    v_min = -v_max;
    a_min = -a_max;
    j_min = -j_max;

    %% S-curves
    %Case 1 Where the max velocity traversed is equal to v_max
    %calculate the time ranges
    if (((v_max - v0) * j_max ) < a_max^2) %max accel is not reached

        Tj1 = sqrt((v_max - v0)/j_max);
        Ta = 2*Tj1;
    else
        Tj1 = a_max/j_max;
        Ta = Tj1 + ((v_max - v0)/a_max);
    end

    if (((v_max - v1) * j_max) < a_max^2) %min accel not reached (a_min = -a_max)
        Tj2 = sqrt((v_max -v1)/j_max);
        Td = 2*Tj2;
    else
        Tj2 = a_max/j_max;
        Td = Tj2 + ((v_max -v1)/a_max);
    end

    %duration of constant velocity
    Tv = (q1-q0)/v_max - (Ta/2)*(1+v0/v_max) - (Td/2)*(1+(v1/v_max));

    if (Tv<=0)
        Tj1 = a_max/j_max;
        Tj2 = a_max/j_max;
        Tj = a_max/j_max;
        Tv=0;

        delta = a_max^4/j_max^2 + 2*(v0^2+v1^2) + a_max*(4*(q1-q0)-2*(a_max/j_max)*(v0+v1));

        Ta = (a_max^2/j_max-2*v0+sqrt(delta))/(2*a_max);
        Td = (a_max^2/j_max-2*v1+sqrt(delta))/(2*a_max);

    end

    t1=Ta+Tv+Td;
    T=t1-t0;
    time = t0:1/100:t1;

    %compute actual max/min accel and vel
    a_lima = j_max*Tj1
    a_limd = -j_max*Tj2
    v_lim = v0 + (Ta - Tj1)*a_lima

    %Computation of Trajectory for q1 > q0

    for i=1:length(time)
        %Accel
        if (time(i) <= Tj1)
            pos(i) = q0 + v0*time(i) + j_max*time(i)^3/6;
            vel(i) = v0 + j_max*time(i)^2/2;
            acc(i) = j_max*time(i);
            jerk(i) = j_max;
        elseif (time(i) > Tj1 && time(i) <= Ta - Tj1) 
            pos(i) = q0 + v0*time(i) + (a_lima/6)*(3*time(i)^2 - 3*Tj1*time(i) + Tj1^2);
            vel(i) = v0 + a_lima*(time(i) - (Tj1/2));
            acc(i) = j_max*Tj1;
            jerk(i) = 0;
        elseif (time(i)>  Ta - Tj1 && time(i) <= Ta)
            pos(i) = q0 + (v_lim + v0)*Ta/2 - v_lim*(Ta-time(i)) - j_min*((Ta-time(i))^3/6);
            vel(i) = v_lim + j_min*((Ta-time(i))^2/2);
            acc(i) = -j_min*(Ta-time(i));
            jerk(i) = j_min;
        elseif (time(i) > Ta && time(i) <= Ta + Tv)
            pos(i) = q0 + (v_lim + v0)*(Ta/2)+v_lim*(time(i)-Ta);
            vel(i) = v_lim;
            acc(i) = 0;
            jerk(i) = 0;
        elseif (time(i) > T-Td && time(i) <= T-Td+Tj2)
            pos(i) = q1 - ((v_lim +v1)*(Td/2)) + v_lim*(time(i)-T+Td)-j_max*(((time(i)-T+Td)^3)/6);
            vel(i) = v_lim - j_max*((((time(i)-T+Td)^2))/2);
            acc(i) = -j_max*(time(i)-T+Td);
            jerk(i) = j_min;
        elseif (time(i) > T-Td+Tj2 && time(i) <= T-Tj2)    
            pos(i) = q1 - (v_lim+v1)*(Td/2)+v_lim*(time(i)-T+Td) + (a_limd/6)*(3*(time(i)-T+Td)^2 - 3*Tj2*(time(i)-T+Td) + Tj2^2);
            vel(i) = v_lim+a_limd*(time(i)-T+Td-Tj2/2);
            acc(i) = a_limd;
            jerk(i) = 0;
        elseif (time(i) > T-Tj2 && time(i) <=T)    
            pos(i) = q1-v1*(T-time(i))-j_max*((T-time(i))^3/6);
            vel(i) = v1+j_max*((T-time(i))^2/2);
            acc(i) = -j_max*(T-time(i));
            jerk(i) = j_max;        
        end
    end

``

davidm2556 commented 6 years ago

Although the fix last night fixed discontinuites, it does not work with values that involve accelerating above max velocity(basically distances that are below 1) . Is there a fix for this @NotInControl ?

NotInControl commented 6 years ago

What are the vmax, amax, and jmax numbers?

If you make amax larger then vmax that should work but you need to also increase jmax to give amax room. Jmax is jerk which is the derivative of acceleration. So you can't increase amax and keep jmax small.

Get Outlook for Androidhttps://aka.ms/ghei36


From: davidm2556 notifications@github.com Sent: Tuesday, February 27, 2018 8:41:03 AM To: Team2168/2018_Main_Robot Cc: Kevin; Mention Subject: Re: [Team2168/2018_Main_Robot] found error in matlab code (#30)

Although the fix last night fixed discontinuites, it does not work with values that involve accelerating above max velocity. Is there a fix for this @NotInControlhttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotincontrol&data=02%7C01%7C%7Cd6d08baeb00345fbba8508d57de7bf8c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636553356652507403&sdata=9Rhsff1AQbh6cK%2BB4XMp3hm%2BvspmYFwyBNqRFEOJtIw%3D&reserved=0 ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTeam2168%2F2018_Main_Robot%2Fissues%2F30%23issuecomment-368878272&data=02%7C01%7C%7Cd6d08baeb00345fbba8508d57de7bf8c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636553356652507403&sdata=boB0RtYcio26K5lCqJDUWZYCYJtz2DkyJpTvEvGl7KA%3D&reserved=0, or mute the threadhttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABO1tzIWeZThwOEPmHtcRj0oHRZ7lCbCks5tZAXvgaJpZM4SSwX0&data=02%7C01%7C%7Cd6d08baeb00345fbba8508d57de7bf8c%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636553356652507403&sdata=hf1M%2F7QZ8OZ0V1Nh8kgHtN0AN5TxYZbDwSqInkTCdZE%3D&reserved=0.

davidm2556 commented 6 years ago

So aMax and jMax have to change in reference to the distance? The issue im referring to was pointed out by @AidanSullivan03, but maybe it more so dependent on values instead of the algorithm

NotInControl commented 6 years ago

No in relation to each other. The algorithm works when Amax/jmax is a ratio greater rhan some number.... if that relationship is not true which is usually not possible in practice, a path cant be generated. Increase jmax and rerun your test, the path where amax is greater than vmax should work

Get Outlook for Androidhttps://aka.ms/ghei36


From: davidm2556 notifications@github.com Sent: Tuesday, February 27, 2018 10:38:41 AM To: Team2168/2018_Main_Robot Cc: Kevin; Mention Subject: Re: [Team2168/2018_Main_Robot] found error in matlab code (#30)

So aMax and jMax have to change in reference to the distance?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FTeam2168%2F2018_Main_Robot%2Fissues%2F30%23issuecomment-368920084&data=02%7C01%7C%7C0651dfedabdd4009127708d57df82e78%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636553427232679475&sdata=mx7bVS0e14Zc8e%2Fh5brric%2F5K109ov7SbrzmCB%2BvI1U%3D&reserved=0, or mute the threadhttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABO1t1Y1lP4uMDrJzoJWyDoxx-bK60RHks5tZCF_gaJpZM4SSwX0&data=02%7C01%7C%7C0651dfedabdd4009127708d57df82e78%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636553427232679475&sdata=svGznC1qvA48nzo1lsaIYQrxpxxOkO0gwNAuIJeBcTQ%3D&reserved=0.