Who's a math wizard

PeteOlsen

Member
Join Date
Jan 2007
Location
Lenexa, KS
Posts
4
NOT a PLC so no scaling is built in.

Analog to Digital Joystick, range 0 to 512(neutral) to 1023

Have a deadband set from 412 to 612

I need a scaling conversion that will give me approximately

-5 to 0(neutral) to + 5..

So considering this
-1 would have to be less than 412
-5 would be near 0

+1 would have to be greater than 612
+ 5 would be near 1023.

Since this is in a micro controller, I tried to do several if statements, but was still too fast for my desired output wich is a PWM on a servo.
 
Try the following:

IF x >= 612 then
y = ((x - 612) / 411) * 5
ELSE IF x<=412 then
y = ((412 - x) / 412) * -5
ELSE
y = 0
END IF


I also attached an Excel document with the formula. This is what I used to test the formula.

View attachment Scaling.xls
 
REM pos = Joystick position
If pos < 412 then pos = 412
If pos > 612 then pos = 612
pos = pos - 412 !set value at 0-200
REM Subtracts 412 giving 0-200 scale
x = pos / 20 ! scale 0-10
x=x-5 !shift scale to -5 to +5

Use the WAIT statement to add delays for your PWM.

Edit: gmferg posted while I was typing. His code will execute faster (compiles smaller) and probably work. Mine is untested.
 
Last edited:
How's your PWM being created? :S Do you have a DAC output to a chip?
If not, then why the -5 to 5 conversion?

Please, do not take this question the wrong way, I'm just curious why you'd want those specific values instead of -100 to 100. :\
One reason is if you want accuracy with -5 to 5 you'll have to use floating point variables whilst with -100 to 100 you could use integer types.

A lot of microcontrollers will allow you to divide the PWM frequency by hardware to some pretty low values. Unless it is not being used for timers and the microcontroller has one or more to use.

Don't get me wrong, I started with microcontrollers and moved to PLCs, so it just seems weird to me. And maybe I can learn something from you! :)
 

Similar Topics

I have an expression in a structured text routine of a Logix controller that looks more or less like the following: ResultInteger := Integer1 *...
Replies
13
Views
386
This application has a motor with encoder feedback that drives a linear actuator that moves in/out, and is at roughly 45 degs from horiz. As the...
Replies
19
Views
1,362
Hi all. First time programming a machine of this type. A center driven unwind feeding to a center driven rewind. No dancers or load cells, just...
Replies
37
Views
4,884
Hello, Been a while since I posted , Still sucking air been a interesting 3.5 years the dreaded health with old age scenario. Back at it and had a...
Replies
11
Views
2,057
Hello: Some time ago I wrote some basic code for a GuardLogix, in which I needed to add and subtract tags of LINT data type. When tried to use the...
Replies
13
Views
3,744
Back
Top Bottom