Position to speed


I have a serial node that reports the position (mm) of an physical axis twice per second.

For example: 141.27, 142.56, 145.89, 148,66...

Now I'd like to determine the axis's speed in millimeters per second.

What is the most efficient way to achieve this?

Thanks a lot.

Probably with Math :stuck_out_tongue: But don't ask me how, I can barely spel

That and I don't know if you are looking for RPM of the object or a linear speed of a point on the circumference of the object... In fact, I am unsure if there is a difference :thinking:

Thankfully there is a Google for that!

Or continue to wait for a math geek to reply... but at least I bumped your topic :slight_smile:

You can use the node context in a function node to save the previous sample and time in the context. Then you can get the time difference in milliseconds by subtracting the times and the distance move by subtracting the positions. Then the speed is distance/time. See the node red docs page on Writing Functions for more information on using the context.

Of course that isn't necessarily the most efficient way to do it. Why are you worried about efficiency when you are only doing it twice a second?

Thank you Colin.

I understand the concept but have no idea how to implement it.

Could you give me an example?

Start by reading the node-red docs on Writing Functions and Working with Context.

You can get the current time in millisecs using let now = new Date().getTime()

Since you're receiving the updates twice/second, the formula would be:
mm/s = (current_position - last_position) * 2

Using two numbers from your example...

(145.89 - 142.56) * 2 = 6.66 mm/s

In a function node (assuming you already know how to get the values into it):

let speed = (current_position - last_position) * 2;

I hope this helps.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.