Robot speed calculation

T

Tim Daim

Since 3 days I'm fighting with a percentage calculation.

I have a combo box that shows values from "10%" - "200%".
The device I'm going to control (an automated machine) accepts values of
let's say Speed Min 35 and Speed Max 249.
I am also given a default speed which could be 85.
Yes, the speed really has no value (like operations/ second or something
like that), it's only some made up value!
My boss wants me to give the user a choice to select the robot's speed.
So I am offering the user a combo box with values from 10% to 200%
percent, and 100% should be the robot's default speed (=85). I wanted to
use a slider but my boss didn't want it.

I tried to make a sensefull calculation to which speed values some
percentage values might point, but I haven't found the correct formula yet.
The one I have is really faulty. Can somebody help? I am totally desperate.

Thank you!
Tim

Private Function PercentToSpeed(ByVal uPercent As Integer, ByVal
uMinSpeed As Long, ByVal uMaxSpeed As Long, ByVal uDefaultSpeed As Long,
ByVal uMinPercent As Long, ByVal uMaxPercent As Long) As Long

Dim lMiddle& '/100%
lMiddle = uDefault

Dim lAvail&

If uPercent = 100 Then
PercentToSpeed= lMiddle
ElseIf uPercent < 100 Then
lAvail = lMiddle - uMin
PercentToSpeed = uMin + ((uPercent * lAvail) \ 100)
ElseIf uPercent > 100 Then
lAvail = uMax - lMiddle
PercentToSpeed= lMiddle + ((uPercent * lAvail) \ 100) \ 2 '/* 2
because our max percent is 200%.
End If

End Function
 
F

Family Tree Mike

Tim Daim said:
Since 3 days I'm fighting with a percentage calculation.

I have a combo box that shows values from "10%" - "200%".
The device I'm going to control (an automated machine) accepts values of
let's say Speed Min 35 and Speed Max 249.
I am also given a default speed which could be 85.
Yes, the speed really has no value (like operations/ second or something
like that), it's only some made up value!
My boss wants me to give the user a choice to select the robot's speed.
So I am offering the user a combo box with values from 10% to 200%
percent, and 100% should be the robot's default speed (=85). I wanted to
use a slider but my boss didn't want it.

I tried to make a sensefull calculation to which speed values some
percentage values might point, but I haven't found the correct formula
yet.
The one I have is really faulty. Can somebody help? I am totally
desperate.

Thank you!
Tim

Private Function PercentToSpeed(ByVal uPercent As Integer, ByVal uMinSpeed
As Long, ByVal uMaxSpeed As Long, ByVal uDefaultSpeed As Long, ByVal
uMinPercent As Long, ByVal uMaxPercent As Long) As Long

Dim lMiddle& '/100%
lMiddle = uDefault

Dim lAvail&

If uPercent = 100 Then
PercentToSpeed= lMiddle
ElseIf uPercent < 100 Then
lAvail = lMiddle - uMin
PercentToSpeed = uMin + ((uPercent * lAvail) \ 100)
ElseIf uPercent > 100 Then
lAvail = uMax - lMiddle
PercentToSpeed= lMiddle + ((uPercent * lAvail) \ 100) \ 2 '/* 2
because our max percent is 200%.
End If

End Function


I would use this function. I think you should change it to accept the
percentage for the default speed however.


Private Function PercentToSpeed _
(ByVal Percent As Integer, ByVal MinSpeed As Long, _
ByVal MaxSpeed As Long, ByVal AvgSpeed As Long, _
ByVal MinPercent As Integer, ByVal MaxPercent As Integer) As Integer

If (Percent <= 100) Then
Dim frac As Double = CDbl(Percent - MinPercent) / CDbl(100 -
MinPercent)
Dim spd As Double = CDbl(AvgSpeed - MinSpeed) * frac + MinSpeed
Return CInt(spd)
Else
Dim frac As Double = CDbl(Percent - 100) / CDbl(MaxPercent - 100)
Dim spd As Double = CDbl(MaxSpeed - AvgSpeed) * frac + AvgSpeed
Return CInt(spd)
End If

Return 0

End Function
 
T

Tim Daim

Amazing,

thank you!

Can you tell me how you did it?

For example me: At first I thought it was easy, so I just scribbled a
few formulas.
Then - after failing - I thought I was more the visual type of guy and
made some sketches but didn't succeed either.

Did you "see it" in front of your eyes (if yes, what did you see?) or
how did you do it, please?
Sounds funny but it's a serious question because I never found a good
entry point to maths.

Regards,
Tim
 
F

Family Tree Mike

Tim Daim said:
Amazing,

thank you!

Can you tell me how you did it?

For example me: At first I thought it was easy, so I just scribbled a few
formulas.
Then - after failing - I thought I was more the visual type of guy and
made some sketches but didn't succeed either.

Did you "see it" in front of your eyes (if yes, what did you see?) or how
did you do it, please?
Sounds funny but it's a serious question because I never found a good
entry point to maths.

Regards,
Tim


My background is mathematics. This is simply a straightline interpolation
between two points, but the graph is broken into two sections. The first
section is below the 100% while the second section is above the 100%.

In both sections you need to mind that the speed values change as fast as
the percentage values do. The fraction of the way between the two
percentages is the same as the fraction of the way between the two speeds.

Another way to look at it would be:

10% x 100%
---- ----- ----
35 y 85

(That may not look good depending on fonts).

You are supplying the value for x to the function. You need to solve for y
where all three fractions are equal. This is just the first half of the
curve though.

Hopefully this shows you how to do this case, but of course it could be
extended to a multi-segment graph.
 
J

James Hahn

You may have confused yourself by thinking it was a percentage problem. In
fact, the figures you are using as percentages are just as arbitrary as the
raw speed numbers. They are not percentages of anything.

Your problem is simply one of fitting a curve to three points. In your
example the three points are (10,35) (100,85) (200,249). A second degree
polynomial (Ax^2+Bx+C) is adequate (but you may wish to confirm that with
your boss). Wikipedia has an excellent article about the process under the
heading Curve Fitting. You can visualise the process by graphing these
three points and drawing a smooth curve through them. Then read off the
speed value for any selected percentage figure.

Using your figures the system of equations is
A* 100 + B * 10 + C * 1 = 35
A*10000 + B* 100 + C * 1 = 85
A*40000 + B* 200 + C * 1 = 249

You can find code for solving a set of simultaneous equations at
http://www.freevbcode.com/ShowCode.asp?ID=3756
(and many other places). It's VB6 code, but the conversion is easy.

Replace the array initilisation loops with :
a(1,1) = PerMin*PerMin : a(1,2)=PerMin : a(1,3)=1 : bo(1)=SpdMin
a(2,1) = PerAvg*PerAvg : a(2,2)=PerAvg : a(2,3)=1 : bo(2)=SpdAvg
a(3,1)=PerMax*PerMax : a(3,2)=PerMax : a(3,3)=1 : bo(3)=SpdMax

The rest of the code is unchanged.

Once you have calculated A B and C, simply solve the equation Ax^2 + Bx + C
for the selected x (combo box) value whenever it changes. For your example
the ABC values are 0.00571, -0.07228 and 35.1520.
 
T

Tim Daim

Thank you!
Tim
My background is mathematics. This is simply a straightline
interpolation between two points, but the graph is broken into two
sections. The first section is below the 100% while the second section
is above the 100%.

In both sections you need to mind that the speed values change as fast
as the percentage values do. The fraction of the way between the two
percentages is the same as the fraction of the way between the two speeds.

Another way to look at it would be:

10% x 100%
---- ----- ----
35 y 85

(That may not look good depending on fonts).

You are supplying the value for x to the function. You need to solve
for y where all three fractions are equal. This is just the first half
of the curve though.

Hopefully this shows you how to do this case, but of course it could be
extended to a multi-segment graph.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top