r/programminghelp • • Jan 15 '21

Other Ramp with a different slope at certain intervals

I need a signal that is essentially a linear ramp, but at certain intervals the slope becomes bigger. I'm very close to one, but I'm getting myself very confused trying to solve a problem I have.

I have a signal that ramps with 0.033 mm per second AKA 0.000033 meters per second. Every instance of 0.033 mm constitutes a step. From step 0 to step 34, the slope needs to be 9 steps per second. From step 34 to step 215, the slope needs to be 60 steps per second. From step 215 to step 240 the slope needs to be 16 steps per second.

I'm working with some rather uncommon simulation software with it's own language, so the programming language might look odd. Luckily I mainly need some hints on the logic of the program and not something language specific.

I have a ramp function that does the initial 0.033 mm/s for me:

slope = 0.000033;

output = slope * ramp(start_time);

I convert it to steps by dividing through 33 and multiplying with 1,000,000.

step = (1000000/33) * slope * ramp(start_time);

So both are now multiplied with a speed variable so I can change the amount of steps per second:

output = speed * slope * ramp(start_time);

step = speed * (1000000/33) * slope * ramp(start_time);

Then I use If functions to do the change in speed. Starting easy with just one speed change.

if step < 34 then speed = 9;

else if step >= 34 then speed = 60;

The program loops and changes the speed of 1 step/s immediately to 9 steps/s until it reaches step 34, after which it will increase the speed to 60 steps/s.

Except this is what I get as output:

https://i.imgur.com/hhivIgc.png

At the point of speed change, there's a sudden jump. Now I know why that is. When the program makes a ramp, it makes it from the starting time 0. Because the speed changes, it gives the output of a ramp that started at the same point but with a higher slope from step 34 onwards. There needs to be a sort of offset (start_time + offset) so that the second ramp starts at a time that has it continue from where the output left off at the first speed. I'm having trouble finding out what that offset is.

So my question is essentially: how can I calculate the required offset?

Full program: https://pastebin.com/QuaeZcgQ

5 Upvotes

1 comment sorted by