Rescaling of values in PBP.


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Location
    Stockholm
    Posts
    80

    Default Rescaling of values in PBP.

    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 ?
    Last edited by Glenn; - 27th December 2008 at 18:28.

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    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.
    All progress began with an idea

  3. #3
    Join Date
    Sep 2008
    Location
    Stockholm
    Posts
    80


    Did you find this post helpful? Yes | No

    Default

    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.
    Last edited by Glenn; - 27th December 2008 at 18:56.

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Talking PBP and Math ...

    Quote Originally Posted by aratti View Post
    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
    Last edited by Acetronics2; - 27th December 2008 at 20:08.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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.
    Code:
    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
    Code:
    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
    DT

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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?

  7. #7
    Join Date
    Sep 2008
    Location
    Stockholm
    Posts
    80


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Glenn; - 27th December 2008 at 23:01.

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    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 for the math first.
    <br>
    DT

Similar Threads

  1. PBP Book
    By Bruce in forum Off Topic
    Replies: 83
    Last Post: - 4th October 2021, 12:55
  2. PBP, ASM and LST files
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th January 2010, 13:43
  3. Funny PULSIN values: what is going on???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th April 2008, 08:02
  4. Compiler differences between PBP 2.33 & 2.46
    By nikopolis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd May 2006, 19:01
  5. Newby- PBP wont compile for 18F (MPLAB)
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th December 2005, 23:30

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts