PDA

View Full Version : Rescaling of values in PBP.



Glenn
- 27th December 2008, 17:44
I am working on a project where I first do a calibrating sequence where I get the max and min values of an in-signal, typically it can be (during my tests) that min is 165 and max is 245.

Now I want to use this signal to a PWM output, therefore I want to make a scale so that (in the test example) 165 is 0, and 245 is 254.

How do I do this ? I guess there is some mathematical way, if it was fixed values I might have considered a lookup table, even if its a bit of an overkill, but now the values can be different from time to time.

..I can also say that I don't need much of a precision in the conversation.

I did a similar thing before, but then the in-signal was resistive, so I could use the scale parameter of the pot command, very easy. ..Now its not, so now I don't know how to solve it.

Please help ? :)

aratti
- 27th December 2008, 18:39
Tray (Value * 0.313)+165

a) (0*0.313)+165 = 165
b) (255*0.313)+165 = 244.8

Since you don't need high accuracy it should work.

Al.

Glenn
- 27th December 2008, 18:52
Thanks! I'll do some tests and see how it works :)

One thing I didnt mention was that its very important that I really get 254 as the max value so the highest insignal really give 100% dutycycle on the PWM, but thats easy to fix with some extra code, that put it on 254 on anything higher than 250 or something.

Acetronics2
- 27th December 2008, 19:56
Tray (Value * 0.313)+165

a) (0*0.313)+165 = 165
b) (255*0.313)+165 = 244.8

Since you don't need high accuracy it should work.

Al.

Hi,

With PbP ... I'd rather try :

Duty = ( Value * 5 /16 ) + 165



But it's just me ...

might better be PWM = (3.175 * Value ) - 523.875

OR PWM = ( 127 /40 * Value ) - ( 4191 / 8 )

PWM = (( 127 * value / 5 ) - 4191) >> 3

...

Alain

Darrel Taylor
- 27th December 2008, 21:20
I think you guys have it backwards.

You're turning a 0 into 165, he wants 165 to be 0.
And it should be adjustable according to each devices calibration numbers. Constants in the formula can't do that.

Here's a simple Y=mX + b type slope intercept.
It's limited to byte values and Positive slopes, but it should do the job.

DEFINE HSER_BAUD 9600 'Hser baud rate
DEFINE HSER_CLROERR 1 'Hser clear overflow automatically
DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 24h 'Hser transmit status init


Value VAR BYTE
Result VAR BYTE
TempB VAR BYTE

CAL_Low CON 165 ; These are the measurements from calibration
CAL_High CON 245

Result_Low CON 0 ; These are what the final range should be
Result_High CON 254

FOR Value = CAL_Low to CAL_High
GOSUB Scale
HSEROUT [DEC Value," = ",DEC Result,13,10]
NEXT Value

STOP

Scale:
TempB = (Value MAX CAL_Low) MIN CAL_High ; Limit Value to CAL range
Result = ((TempB-CAL_Low)*(Result_High-Result_Low))/(CAL_High-CAL_Low)
RETURN

Which gives these results

Value = Result
165 = 0
166 = 3
167 = 6
168 = 9
169 = 12
170 = 15
171 = 19
172 = 22
173 = 25
174 = 28
175 = 31
176 = 34
177 = 38
178 = 41
179 = 44
180 = 47
181 = 50
182 = 53
183 = 57
184 = 60
185 = 63
186 = 66
187 = 69
188 = 73
189 = 76
190 = 79
191 = 82
192 = 85
193 = 88
194 = 92
195 = 95
196 = 98
197 = 101
198 = 104
199 = 107
200 = 111
201 = 114
202 = 117
203 = 120
204 = 123
205 = 127
206 = 130
207 = 133
208 = 136
209 = 139
210 = 142
211 = 146
212 = 149
213 = 152
214 = 155
215 = 158
216 = 161
217 = 165
218 = 168
219 = 171
220 = 174
221 = 177
222 = 180
223 = 184
224 = 187
225 = 190
226 = 193
227 = 196
228 = 200
229 = 203
230 = 206
231 = 209
232 = 212
233 = 215
234 = 219
235 = 222
236 = 225
237 = 228
238 = 231
239 = 234
240 = 238
241 = 241
242 = 244
243 = 247
244 = 250
245 = 254

Melanie
- 27th December 2008, 22:48
Oh craps, everyone's beaten me to it... my offering...

OutputByte=(InputByte-165)*318/100

This is calculated from...

245-165=80 (the original span)

254/80=3.175 (new span divided by old span)

3.175 is rounded up to 3.18 and then we multiply it by 100 so as to keep everything an integer (which is why we finally do a divide by 100 to bring us back to reality).

You may want to preceed the formula with...

If InputByte<165 then InputByte=165
If InputByte>245 then InputByte=245

OMG Darrel... How much have you had to drink to think of that one?

Glenn
- 27th December 2008, 22:59
I guess that this was more complicated than I first guessed :)

..Currently I'm building a better test-rig so I can do some serious testing, right now I guess I just put a LCD in it and display the values.

The 165 and 245 was just typical values, I guess that the low value can be around 135-170 and the high one maybe 235-254. Maybe the span can be even larger.

The idea is to have a calibrating sequence first when the pic is started, where I determine the max and min value for this time, and then use this until the next start, where its calibrated again.'

Darrels solution was very neat btw :)

Darrel Taylor
- 27th December 2008, 23:24
OMG Darrel... How much have you had to drink to think of that one?

Ha! Apparently not enough Eggnog yet. It worked on the first compile. :)
Of course, I did an Ugly spreadsheet (http://spreadsheets.google.com/ccc?key=pthtFkHw5uiDQVCSAC-2pfg) for the math first.
<br>