Click here to Skip to main content
15,899,314 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: Algorithm try and figure this out in c# Pin
Richard Deeming16-Feb-17 2:02
mveRichard Deeming16-Feb-17 2:02 
AnswerRe: Algorithm try and figure this out in c# Pin
Patrice T18-Feb-17 14:59
mvePatrice T18-Feb-17 14:59 
QuestionProof by mathematical induction Pin
Member 129868787-Feb-17 0:18
Member 129868787-Feb-17 0:18 
AnswerRe: Proof by mathematical induction Pin
Pete O'Hanlon7-Feb-17 0:42
mvePete O'Hanlon7-Feb-17 0:42 
SuggestionRe: Proof by mathematical induction Pin
Richard MacCutchan7-Feb-17 1:46
mveRichard MacCutchan7-Feb-17 1:46 
AnswerRe: Proof by mathematical induction Pin
Chris Losinger7-Feb-17 8:46
professionalChris Losinger7-Feb-17 8:46 
AnswerRe: Proof by mathematical induction Pin
Patrice T8-Feb-17 6:27
mvePatrice T8-Feb-17 6:27 
QuestionTrapezoid shaped movement (updated with the newest code and status) - SOLVED Pin
Joan M5-Feb-17 22:12
professionalJoan M5-Feb-17 22:12 
Hello all,

I do need to send a motor from position A to B accelerating and decelerating accordingly to the defined allowed acceleration.

Constant acceleration.
Different starting speeds.
Different starting positions.
Different ending positions.
0 end speed.

I've tried to make my own which works perfectly when the values allow the motor to reach the maximum speed "plateau" but it is not working well when is not possible to achieve this and v0 (initial speed) and vf (end speed) are different.

Previous notes:
vmax = maximum allowed speed for the movement.
x0 = initial position.
xf = destination position.
v0 = initial speed.
vf = end speed (final).
ta = acceleration time from v0 to vmax.
da = distance to accelerate from v0 to vmax.
tf = braking time from vmax to vf.
df = braking distance from vmax to vf.
dist = total distance I want to move the motor xf-x0

The code I've used is:
VB
LimitIterations = 1000
    ' Search the amount of time and distance we use to accelerate from v0 to vmax.
    ta = Abs((vmax - v0) / a)
    da = (v0 * ta) + (0.5 * a * ta * ta)
    ' Search the amount of time and distance we use to brake from vmax to vf.
    tf = Abs((vmax - vf) / a)
    df = (vf * tf) + (0.5 * a * tf * tf)
   
    ' If da and df are bigger than the distance to run...
    If (da + df > dist + 0.000000001) Then
      ' WRONG APPROACH STARTS HERE --->
      ' adapt the acceleration and braking distances to the distance we must run.
      DistTotal = xf - x0 - da - df
      RelAccDec = da / (da + df)
      dist_corr_acc = DistTotal * RelAccDec
      da = da + dist_corr_acc
      dist_corr_dec = DistTotal * (1 - RelAccDec)
      df = df + dist_corr_dec
      ' <---- WRONG APPROACH ENDS HERE

      ' **** See explanations under the code about the WRONG APPROACH.

      ' This is something that you

      ' Now that we know the correct distances it should be easy to find the needed time and therefore the reached vmax after the acceleration stage.
      taux = Sqr(da / (0.5 * a))  ' Normal formula that doesn't work when v0 > 0
      taux = (Sqr((2 * a * da) + (v0 * v0)) - v0) / a  ' Rare formula that nobody understands...
      vmax = v0 + (a * taux)  ' Easy way to calculate the vmax.

      ' Ugliest code in the world... this works, not always but mostly, but of course it is not the way to go...
      Do
        ' refresh the values (time, distance...) to find the maximum allowed speed for that movement vmax...
        ta = Abs((vmax - v0) / a)
        da = (v0 * ta) + (0.5 * a * ta * ta)
        tf = Abs((vmax - vf) / a)
        df = (vf * tf) + (0.5 * a * tf * tf)
        If (da + df > dist + 0.000000001) Then
          vmax = vmax - 1   ' Decrease the vmax till it works... 
          iterations = iterations + 1
          If (iterations > LimitIterations) Then
            ErrorID = 1
            Exit Do
          End If
        Else
          Exit Do
        End If
      Loop
    End If

WRONG APPROACH EXPLANATIONS
The idea of that code was to find the maximum speed that can be reached given the specified conditions.

When the da + df are bigger than the total distance I'm doing a proportional reduction which approximates the vmax to it's desired/needed value.

The problem here I think is that part: I don't have to make a proportional reduction, I do need to find the position in which the acceleration movement and the deceleration movement are coincident once I will have this position then I should check the speed any of the movements has in that position. And that one should be vmax.

Now the issue is doing that...

I could use 0 as the starting position for the acceleration and xf as the starting position for the deceleration.
Both accelerations are the same (different signs).

I've seen examples of that when movements are not accelerated (the typical problem with two trains that leave the station) but nothing when the movements have constant acceleration.

Any hint, example, resource, formula that could solve it...

The solution
OK, at the end I've done it by algebra:
C++
// STAGE A
vp = v0 + (a0 * t0)  // Acceleration stage
vp = v1 + (a1 * t1)  // Brake stage
v0+(a0*t0) = v1+(a1*t1)  // vp = vp
t1=((v0-v1)+(a0*t0))/a1  // Get the t1 in terms of t0 (braking time in terms of accelerating time).  ==> Note v0 is the starting speed and v1 is the ending speed.

// STAGE B
d0 = (v00 * t0) + (0.5 * a0 * t0 * t0) // Distance while accelerating.
d1 = (v01 * t1) + (0.5 * a1 * t1 * t1) // Distance while braking.
D = (v00 * t0) + (0.5 * a0 * t0 * t0) + (v01 * t1) + (0.5 * a1 * t1 * t1)  // Total distance is the sum of both equations.

// At this point is only a matter of sorting the values to get the shape of the quadratic equation Ax^2 + Bx + C = 0

And solve the quadratic equation to get the positive value (as we are speaking of time).


So.. this previous code substitutes the wrong approach method. Cool | :cool:

Thank you all,

modified 12-Feb-17 9:01am.

AnswerRe: Trapezoid interpolation Pin
Ralf Meier6-Feb-17 0:23
mveRalf Meier6-Feb-17 0:23 
GeneralRe: Trapezoid interpolation Pin
Joan M6-Feb-17 21:47
professionalJoan M6-Feb-17 21:47 
AnswerRe: Trapezoid interpolation Pin
Ralf Meier7-Feb-17 0:00
mveRalf Meier7-Feb-17 0:00 
GeneralRe: Trapezoid interpolation Pin
Joan M7-Feb-17 0:54
professionalJoan M7-Feb-17 0:54 
GeneralRe: Trapezoid interpolation Pin
Ralf Meier7-Feb-17 1:25
mveRalf Meier7-Feb-17 1:25 
GeneralRe: Trapezoid interpolation Pin
Joan M7-Feb-17 3:10
professionalJoan M7-Feb-17 3:10 
GeneralRe: Trapezoid interpolation Pin
Ralf Meier7-Feb-17 6:57
mveRalf Meier7-Feb-17 6:57 
GeneralRe: Trapezoid interpolation Pin
Joan M7-Feb-17 10:13
professionalJoan M7-Feb-17 10:13 
GeneralRe: Trapezoid interpolation Pin
Ralf Meier7-Feb-17 20:21
mveRalf Meier7-Feb-17 20:21 
GeneralRe: Trapezoid interpolation Pin
Joan M7-Feb-17 20:45
professionalJoan M7-Feb-17 20:45 
GeneralRe: Trapezoid interpolation Pin
Ralf Meier7-Feb-17 21:34
mveRalf Meier7-Feb-17 21:34 
GeneralRe: Trapezoid interpolation Pin
Joan M7-Feb-17 21:46
professionalJoan M7-Feb-17 21:46 
AnswerRe: Trapezoid interpolation Pin
Ralf Meier8-Feb-17 0:33
mveRalf Meier8-Feb-17 0:33 
GeneralRe: Trapezoid interpolation Pin
Joan M8-Feb-17 1:07
professionalJoan M8-Feb-17 1:07 
GeneralRe: Trapezoid interpolation Pin
Ralf Meier8-Feb-17 2:01
mveRalf Meier8-Feb-17 2:01 
GeneralRe: Trapezoid interpolation Pin
Joan M8-Feb-17 8:59
professionalJoan M8-Feb-17 8:59 
GeneralRe: Trapezoid interpolation Pin
Ralf Meier8-Feb-17 10:05
mveRalf Meier8-Feb-17 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.