advanced smoother E2

He all.
In the past there was a code for the Expression1. It called Adv Smoother.
Code E1:



n@Advanced_Smoother
i@Target Speed Acceleration
o@Value Active

# Activate when target has been set
~Target -> Active = 1;

# Main computation loop
first() | clk() ->
# Precomputation of inputs
    !Acceleration -> Acceleration = Speed * 50 * 2;
    AccLength = Speed^2 / Acceleration / 2
    AccRate = min(Speed, Acceleration / 50)
# Precomputation of variables
    Distance = Target - Value
    Direction = Distance >= 0 ? 1 : -1
# Calculate ideal speed modifier
    IdealRate = Speed * Direction
    abs(Distance) <= AccLength ->
        IdealRate *= sqrt(abs(Distance - Rate / 50) / AccLength);
# Calculate final speed for iteration
    Rate += clamp(IdealRate - Rate, -Acceleration / 50, Acceleration / 50)
    Value += Rate / 50
# Check if value has reached target
    Active = abs(Rate) > AccRate
                | abs(Distance) > AccRate / 50
    !Active -> Rate = 0, Value = Target;;

# Schedule the main loop if active
Active -> interval(20);


Because E1 no longer exists i want to make it E2
Well…
Code E2:



@name Advanced Smoother
@inputs Target Speed Acceleration
@outputs Value Active 
@persist  AccRate AccLength Distance Direction Rate IdealRate

# Activate when target has been set
Active=(Target ? 1 : 0)

# Main computation loop
if(first() | clk()){
# Precomputation of inputs
    if(!Acceleration){
        Acceleration = Speed * 50 * 2
        AccLength = Speed^2 / Acceleration / 2
        AccRate = min(Speed, Acceleration / 50)
    }
    
    # Precomputation of variables
    Distance = Target - Value
    Direction = (Distance >= 0 ? 1 : -1)
    
    # Calculate ideal speed modifier
    IdealRate = Speed * Direction
    if(abs(Distance) <= AccLength){
        IdealRate *= sqrt(abs(Distance - Rate / 50) / AccLength)
    }
        
    # Calculate final speed for iteration
    Rate += clamp(IdealRate - Rate, -Acceleration / 50, Acceleration / 50)
    Value += Rate / 50
    
    # Check if value has reached target
    Active = ((abs(Rate) > AccRate)|(abs(Distance) > AccRate / 50))
    if(!Active){
        Rate = 0
        Value = Target
    }
}
    
# Schedule the main loop if active
if(Active==1){interval(20)}


Well the code worked so far.
But the only thing is that the hydrolics don’t stop!
if the target is 50 then it swings between 40 and 60.
It don’t stop.
if the target is 0 its the same.!

Can someone help me?

Bump - Need serius help here!

Change


Activate = (Target ? 1 : 0)

to


if (~Target) {Activate = 1}

Doesn’t work :frowning:

Still dont stop.

[editline]Edit[/editline]
WireMod.com

Hello, came across your post.

I was trying to do the same thing as you, this is how I got it to work



@name Advanced Smoother
@inputs Target Speed Acceleration
@outputs Value Active
@persist AccLength AccRate Distance Direction IdealRate Rate

if(~Target){interval(20)}

    # Precomputation of inputs
if(Acceleration == 0){
    Acceleration = Speed * 50 * 2
}
AccLength = Speed^2 / Acceleration / 2
AccRate = min(Speed, Acceleration / 50)
    
    # Precomputation of variables
Distance = Target - Value
Direction = (Distance >= 0 ? 1 : -1)
    
    # Calculate ideal speed modifier
IdealRate = Speed * Direction
if(abs(Distance) <= AccLength){
    IdealRate *= sqrt(abs(Distance - Rate / 50) / AccLength)
}
        
    # Calculate final speed for iteration
Rate += clamp(IdealRate - Rate, -Acceleration / 50, Acceleration / 50)
Value += Rate / 50
    
    # Check if value has reached target
Active = ((abs(Rate) > AccRate)|(abs(Distance) > (AccRate / 50)))
if(!Active){
    Rate = 0
    Value = Target
}

    # Schedule the main loop if active
if(Active==1){interval(20)}



The Acceleration Input is optional, and will become Speed * 100 if not set (like the original code)

The main problem was if the Acceleration WAS set, AccLength AccRate would not be calculated, messing up end of the code. I fixed this by taking them out of the if(Acceleration == 0) loop. You can see in the original code, they were not in the loop either.

Guess you should have gone the the lua coding category/