News:

;) This forum is the property of Proton software developers

Main Menu

question for process engineers ref scaling of output data

Started by TimB, May 10, 2023, 02:25 PM

Previous topic - Next topic

TimB

Hi

I'm developing a product that needs to talk to the outside world. I decided on 0-5v output and then the customer can decide what to do with the signal via a din rail module. e.g. I would recommend a 0-5v to 4-20ma module.
These will drive say a chart recorder or get interpreted using a BMS thingy

Now my question is on scaling I have 4 channels and the user can pick what is being output from a few options

Values they represent are the following
-20 - 200 oc (normal range 110 - 150oC)
0 to 25% A(normal range 0 - 3.5%)
0 to 25% B(normal range 0 - 3.5%)
0 to 5mls A flow (normal range 0.2 - 0.5mls)
0 to 5mls B (normal range 0-2mls)
0.0 to 1 (normal range 0.95 - 1.0)
- 20 to +30oC (normal range -10 to 25oC)
0 to 6 Barg (normal range 1 to 5Barg)

Now my question is:-
How do I decide on the scaling? Do I just take the max scale (4095 in the 12bit DAC) and then take the max range value and / by the 4095 and out put that. Or is there some format you would expect and should I offer scaling eg x 2.

Thanks in advance





Yasin

Sorry for my poor english. I am using the following 2 procedures. Since I used it in many different transformations in the same program, I divided it into 2 main parts. The first is analog readout, the second is scaling. Analog magnitude can be 0-20mA, 4-20mA, 0-5V or many unlimited quantities. I hope it helps. The explanation in the sample procedures is in Turkish. If you find it difficult to understand, Google translator will help.




'-------------------------------------------------------------------------------
' Read_mA Prosedürü giriş parametrelerini matematiksel işlemlere tabi tutarak
' Seçilen analog kanaldaki büyüklüğü işler mA*1000(başka değişle µA cinsine dönüştürür)
' Word değişken olarak çıktı verir.
' Genel kullanım amacı 0-20mA veya 4-20mA okumaktır.
' Inputs    : pChannel > Analog büyüklük okunacak kanal numarası
'          : pVref    > Analog modülün referans voltajı milivolt cinsinden
'          : pRes    > Akım okuması yapılan direncin ohm cinsinden değeri
'          : pFilters > Akım okumasının kaç defa yapılacağını belirleyen değişken 0-9 arası değer alabilir.
' Output    : Result  > mA cinsinden çıktı
' Notes    : None    >
'
'Proc Read_mA(pChannel, as byte, pVref As Word,pImin As Word,pImax As Word,pRes As Word),Word
Proc Read_mA(pChannel As Byte, pVref As Word,pRes As Word,pFilter As Byte),SWord
'    Dim ANG_CHANNEL As Byte
    Dim ADC_Filter[5] As SWord
    Dim ADC_Filter_Count As Byte
    Dim TempANG As Float
    'Dim ADVAL As ADRES.Word
    'ADCON2.7 = 1   
    $if _device = _18F87K90
        $define ADC_Resolation 4095
    $elseif _device = _18f8722
        $define ADC_Resolation 1023
    $endif
    ADC_Filter[4] = 0
    For ADC_Filter_Count = 0 To pFilter
        ADC_Filter[ADC_Filter_Count] = ADIn pChannel
        ADC_Filter[4] = ADC_Filter[4] + ADC_Filter[ADC_Filter_Count]
    Next
   
    $if _device = _18F87K90
        ADC_Filter[4] = ADC_Filter[4] >> 2
    $endif
   
    TempANG = ADC_Filter[4] / ADC_Filter_Count
   
    TempANG = (TempANG * pVref * 1000) / ADC_Resolation
    Result = TempANG / pRes
    If Result < 0 Then Result = 0
EndProc





''-------------------------------------------------------------------------------
' Scala_Conversion Prosedürü giriş parametrelerini matematiksel işlemlere tabi tutarak
' Word değişken olarak çıktı verir.
' Genel kullanım amacı 0-20mA veya 4-20mA okunan verilerin yüzde yada birim cisinden skalandırılmasıdır.
' Inputs    : pVal    > Dönüştürülecek olan analog büyüklük
'          : pMin    > Analog büyüklüklüğü alt limiti
'          : pMax    > Analog büyüklüklüğü üst limiti
'          : pFull    > Maksimum skaladaki en büyük değer. Örneğin bir sensörün ölçebildiği 20mA tekabül eden maksimum değer
' Output    : Result  > Scala birimi cinsinden çıktı
' Notes    : None    >
'
Proc Scala_Conversion(pVal As Word,pMin As Word,pMax As Word,pFull As Word),Word
    Dim TempConvert As Dword
    If pMin < pMax Then
        If pVal < pMin Then pVal = pMin
        TempConvert = (pVal - pMin) * pFull
        Result = TempConvert  / (pMax - pMin)
    Else
        If pVal < pMax Then pVal = pMax
        TempConvert = (pMin - pVal) * pFull
        Result = TempConvert  / (pMin - pMax)
    EndIf
    If Result > pFull Then Result = pFull
EndProc

'-------------------------------------------------------------------------------


top204

I don't fully understand your question Tim, but the compiler has a library for scaling, named: "Scale.inc". It is in the users "includes" folder (C:\Users\User Name\PDS\Includes\), so it can just be added to a program's listing using the Include directive. It has scaling procedures for different variable types.

A demo of a scale procedure converting a 16-bit value into a smaller value is listed below:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a scale procedure to convert 16-bit values into smaller scaled 8-bit values
'
' Written for the Positron8 compiler by Les Johnson
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600
    Declare HRSOut1_Pin   = PORTC.6

    Include "Scale.inc"                             ' Load the scale library into the program
'
' Create some variables for the demo
'
    Dim wOrigValue   As Word                        ' Holds the original value
    Dim bScaledValue As Byte                        ' Holds the smaller scaled value
   
'--------------------------------------------------------------------
' The main program starts here
'
Main:  
    wOrigValue = 0                                          ' \ Create a loop
    Repeat                                                  ' /
        bScaledValue = wScale(wOrigValue, 0, 4095, 0, 100)  ' Scale the values to 0 to 100
        HRSOutLn "Orig ", Dec4 wOrigValue,                  ' \
                 " : Scaled is ", Dec3 bScaledValue         ' / Display the original and scaled values on a serial terminal
        wOrigValue = wOrigValue + 16                        ' Increment the original value
    Until wOrigValue > 4096                                 ' Loop until the maximum value is reached

The scaling procedures also scale small values into larger values.

The above program will transmit the text below on a serial terminal:

Orig 0000 : Scaled is 000
Orig 0016 : Scaled is 000
Orig 0032 : Scaled is 000
Orig 0048 : Scaled is 001
Orig 0064 : Scaled is 001
Orig 0080 : Scaled is 001
Orig 0096 : Scaled is 002
Orig 0112 : Scaled is 002
Orig 0128 : Scaled is 003
Orig 0144 : Scaled is 003
Orig 0160 : Scaled is 003
Orig 0176 : Scaled is 004
Orig 0192 : Scaled is 004
Orig 0208 : Scaled is 005
Orig 0224 : Scaled is 005
Orig 0240 : Scaled is 005
Orig 0256 : Scaled is 006
Orig 0272 : Scaled is 006
Orig 0288 : Scaled is 007
Orig 0304 : Scaled is 007
Orig 0320 : Scaled is 007
Orig 0336 : Scaled is 008
Orig 0352 : Scaled is 008
Orig 0368 : Scaled is 008
Orig 0384 : Scaled is 009
Orig 0400 : Scaled is 009
Orig 0416 : Scaled is 010
Orig 0432 : Scaled is 010
Orig 0448 : Scaled is 010
Orig 0464 : Scaled is 011
Orig 0480 : Scaled is 011
Orig 0496 : Scaled is 012
Orig 0512 : Scaled is 012
Orig 0528 : Scaled is 012
Orig 0544 : Scaled is 013
Orig 0560 : Scaled is 013
Orig 0576 : Scaled is 014
Orig 0592 : Scaled is 014
Orig 0608 : Scaled is 014
Orig 0624 : Scaled is 015
Orig 0640 : Scaled is 015
Orig 0656 : Scaled is 016
Orig 0672 : Scaled is 016
Orig 0688 : Scaled is 016
Orig 0704 : Scaled is 017
Orig 0720 : Scaled is 017
Orig 0736 : Scaled is 017
Orig 0752 : Scaled is 018
Orig 0768 : Scaled is 018
Orig 0784 : Scaled is 019
Orig 0800 : Scaled is 019
Orig 0816 : Scaled is 019
Orig 0832 : Scaled is 020
Orig 0848 : Scaled is 020
Orig 0864 : Scaled is 021
Orig 0880 : Scaled is 021
Orig 0896 : Scaled is 021
Orig 0912 : Scaled is 022
Orig 0928 : Scaled is 022
Orig 0944 : Scaled is 023
Orig 0960 : Scaled is 023
Orig 0976 : Scaled is 023
Orig 0992 : Scaled is 024
Orig 1008 : Scaled is 024
Orig 1024 : Scaled is 025
Orig 1040 : Scaled is 025
Orig 1056 : Scaled is 025
Orig 1072 : Scaled is 026
Orig 1088 : Scaled is 026
Orig 1104 : Scaled is 026
Orig 1120 : Scaled is 027
Orig 1136 : Scaled is 027
Orig 1152 : Scaled is 028
Orig 1168 : Scaled is 028
Orig 1184 : Scaled is 028
Orig 1200 : Scaled is 029
Orig 1216 : Scaled is 029
Orig 1232 : Scaled is 030
Orig 1248 : Scaled is 030
Orig 1264 : Scaled is 030
Orig 1280 : Scaled is 031
Orig 1296 : Scaled is 031
Orig 1312 : Scaled is 032
Orig 1328 : Scaled is 032
Orig 1344 : Scaled is 032
Orig 1360 : Scaled is 033
Orig 1376 : Scaled is 033
Orig 1392 : Scaled is 033
Orig 1408 : Scaled is 034
Orig 1424 : Scaled is 034
Orig 1440 : Scaled is 035
Orig 1456 : Scaled is 035
Orig 1472 : Scaled is 035
Orig 1488 : Scaled is 036
Orig 1504 : Scaled is 036
Orig 1520 : Scaled is 037
Orig 1536 : Scaled is 037
Orig 1552 : Scaled is 037
Orig 1568 : Scaled is 038
Orig 1584 : Scaled is 038
Orig 1600 : Scaled is 039
Orig 1616 : Scaled is 039
Orig 1632 : Scaled is 039
Orig 1648 : Scaled is 040
Orig 1664 : Scaled is 040
Orig 1680 : Scaled is 041
Orig 1696 : Scaled is 041
Orig 1712 : Scaled is 041
Orig 1728 : Scaled is 042
Orig 1744 : Scaled is 042
Orig 1760 : Scaled is 042
Orig 1776 : Scaled is 043
Orig 1792 : Scaled is 043
Orig 1808 : Scaled is 044
Orig 1824 : Scaled is 044
Orig 1840 : Scaled is 044
Orig 1856 : Scaled is 045
Orig 1872 : Scaled is 045
Orig 1888 : Scaled is 046
Orig 1904 : Scaled is 046
Orig 1920 : Scaled is 046
Orig 1936 : Scaled is 047
Orig 1952 : Scaled is 047
Orig 1968 : Scaled is 048
Orig 1984 : Scaled is 048
Orig 2000 : Scaled is 048
Orig 2016 : Scaled is 049
Orig 2032 : Scaled is 049
Orig 2048 : Scaled is 050
Orig 2064 : Scaled is 050
Orig 2080 : Scaled is 050
Orig 2096 : Scaled is 051
Orig 2112 : Scaled is 051
Orig 2128 : Scaled is 051
Orig 2144 : Scaled is 052
Orig 2160 : Scaled is 052
Orig 2176 : Scaled is 053
Orig 2192 : Scaled is 053
Orig 2208 : Scaled is 053
Orig 2224 : Scaled is 054
Orig 2240 : Scaled is 054
Orig 2256 : Scaled is 055
Orig 2272 : Scaled is 055
Orig 2288 : Scaled is 055
Orig 2304 : Scaled is 056
Orig 2320 : Scaled is 056
Orig 2336 : Scaled is 057
Orig 2352 : Scaled is 057
Orig 2368 : Scaled is 057
Orig 2384 : Scaled is 058
Orig 2400 : Scaled is 058
Orig 2416 : Scaled is 058
Orig 2432 : Scaled is 059
Orig 2448 : Scaled is 059
Orig 2464 : Scaled is 060
Orig 2480 : Scaled is 060
Orig 2496 : Scaled is 060
Orig 2512 : Scaled is 061
Orig 2528 : Scaled is 061
Orig 2544 : Scaled is 062
Orig 2560 : Scaled is 062
Orig 2576 : Scaled is 062
Orig 2592 : Scaled is 063
Orig 2608 : Scaled is 063
Orig 2624 : Scaled is 064
Orig 2640 : Scaled is 064
Orig 2656 : Scaled is 064
Orig 2672 : Scaled is 065
Orig 2688 : Scaled is 065
Orig 2704 : Scaled is 066
Orig 2720 : Scaled is 066
Orig 2736 : Scaled is 066
Orig 2752 : Scaled is 067
Orig 2768 : Scaled is 067
Orig 2784 : Scaled is 067
Orig 2800 : Scaled is 068
Orig 2816 : Scaled is 068
Orig 2832 : Scaled is 069
Orig 2848 : Scaled is 069
Orig 2864 : Scaled is 069
Orig 2880 : Scaled is 070
Orig 2896 : Scaled is 070
Orig 2912 : Scaled is 071
Orig 2928 : Scaled is 071
Orig 2944 : Scaled is 071
Orig 2960 : Scaled is 072
Orig 2976 : Scaled is 072
Orig 2992 : Scaled is 073
Orig 3008 : Scaled is 073
Orig 3024 : Scaled is 073
Orig 3040 : Scaled is 074
Orig 3056 : Scaled is 074
Orig 3072 : Scaled is 075
Orig 3088 : Scaled is 075
Orig 3104 : Scaled is 075
Orig 3120 : Scaled is 076
Orig 3136 : Scaled is 076
Orig 3152 : Scaled is 076
Orig 3168 : Scaled is 077
Orig 3184 : Scaled is 077
Orig 3200 : Scaled is 078
Orig 3216 : Scaled is 078
Orig 3232 : Scaled is 078
Orig 3248 : Scaled is 079
Orig 3264 : Scaled is 079
Orig 3280 : Scaled is 080
Orig 3296 : Scaled is 080
Orig 3312 : Scaled is 080
Orig 3328 : Scaled is 081
Orig 3344 : Scaled is 081
Orig 3360 : Scaled is 082
Orig 3376 : Scaled is 082
Orig 3392 : Scaled is 082
Orig 3408 : Scaled is 083
Orig 3424 : Scaled is 083
Orig 3440 : Scaled is 084
Orig 3456 : Scaled is 084
Orig 3472 : Scaled is 084
Orig 3488 : Scaled is 085
Orig 3504 : Scaled is 085
Orig 3520 : Scaled is 085
Orig 3536 : Scaled is 086
Orig 3552 : Scaled is 086
Orig 3568 : Scaled is 087
Orig 3584 : Scaled is 087
Orig 3600 : Scaled is 087
Orig 3616 : Scaled is 088
Orig 3632 : Scaled is 088
Orig 3648 : Scaled is 089
Orig 3664 : Scaled is 089
Orig 3680 : Scaled is 089
Orig 3696 : Scaled is 090
Orig 3712 : Scaled is 090
Orig 3728 : Scaled is 091
Orig 3744 : Scaled is 091
Orig 3760 : Scaled is 091
Orig 3776 : Scaled is 092
Orig 3792 : Scaled is 092
Orig 3808 : Scaled is 092
Orig 3824 : Scaled is 093
Orig 3840 : Scaled is 093
Orig 3856 : Scaled is 094
Orig 3872 : Scaled is 094
Orig 3888 : Scaled is 094
Orig 3904 : Scaled is 095
Orig 3920 : Scaled is 095
Orig 3936 : Scaled is 096
Orig 3952 : Scaled is 096
Orig 3968 : Scaled is 096
Orig 3984 : Scaled is 097
Orig 4000 : Scaled is 097
Orig 4016 : Scaled is 098
Orig 4032 : Scaled is 098
Orig 4048 : Scaled is 098
Orig 4064 : Scaled is 099
Orig 4080 : Scaled is 099
Orig 4096 : Scaled is 100

TimB


HI,

The question is not about how to scale. I can figure that out. Its directed to people who have to design and instal kit in factories or labs.

They get presented with a new bit of kit and have to integrate it into there BSM system or connect it to say a chart recorder. 

Its about what they would expect from an output like my kit.

gtvpic

Hi, in my personal experience on this, I normally leave 5% free range on both ends of the output. For example, if the internal measurement is 0-6Bar and my analog output is 12Bit. 0-4095 I perform the calculations so that 0 bits = -0.3Bar and 4095=6.3 Bar so that I readjust the values by software and have a margin of error in the output electronics.
This Adjustment Range often solves resistance and OP errors in real life. ;D  ;D

TimB

Quote from: gtvpic on May 11, 2023, 07:50 AMHi, in my personal experience on this, I normally leave 5% free range on both ends of the output. For example, if the internal measurement is 0-6Bar and my analog output is 12Bit. 0-4095 I perform the calculations so that 0 bits = -0.3Bar and 4095=6.3 Bar so that I readjust the values by software and have a margin of error in the output electronics.
This Adjustment Range often solves resistance and OP errors in real life. ;D  ;D

Fantastic advice. Much appreciated.

Yasin

My method is to calibrate the conversion with actual measurements instead of using constant numbers to eliminate resistance and reference voltage errors. It will be clearer if the "Read_mA()" procedure I shared above is examined. When no external reference source is used for the ADC reference voltage. Normally the highest value in analog measurement is equal to vdd. And we process it as 5V or 3.3V. Actually, we make mistakes. Because in reality our 5V regulator is not entirely 5V. Sometimes 4.98V, sometimes 5.12v.. Another mistake we make is that we take voltage divider resistors into account exactly. But in reality they have error like 1%, 5%, 10%. A resistor that we use as 10k ohms may be 10.234k ohms. Here I do not use constant values in the conversion to eliminate these errors. Instead, I use a variable registered to the eprom. I save this as calibration value to eprom. I calibrate the measuring device I produce with DMM before going to the field.

TimB


Hi Yasin

So you have added the ability to calibrate the output in the device?

Yasin