Shop OBEX P1 Docs P2 Docs Learn Events
Cecil W. Soileau wrote: How do I raise a variable, X to a Power of 1.197 in my Stamp2 Basic language — Parallax Forums

Cecil W. Soileau wrote: How do I raise a variable, X to a Power of 1.197 in my Stamp2 Basic language

I am trying to use a Sharp GP2Y0A21YK0F distance measuring ir emitter/irtransistor module with an ADC0831 converter, which reads the voltage coming out of the Sharp DME. the problem is that the voltage is not leaner function of the distance, but is instead an inverse function of the distance to the object being sensed. My problem then is how do I take the value read and raise it to a power of 1.197. The equation I need to solve is: Y = 27.639/X^1.197. So X the voltage read from the ADC0831 must be converted to a distance in millimeters, via a solution to this equation.

Comments

  • Can anyone help. I cannot find a Basic Stamp2 function that will raise a number to a power.
  • A lookup table will probably be easiest. Just linearly interpolate between points, and you'll get close enough.

    -Phil
  • Thanks Phil, I think that will work, but I was trying to add a bit of sophistication to my project. But I will try the lookup approach.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2017-03-29 23:43
    It seems you are crippling the resolution with 8-bits which would be bad enough if the sensor output was linear and full range but it is neither and neither is the full resolution of the A/D usable as normally quantization error affects at least the lsb. However your calculation doesn't seem to be correct as you can easily calculate that for X=1 that Y=27.639. If I have time I will look at the curve but with less than 8-bits a table is your best bet. I even tried y = (27.639/x)^1.197 too but normal evaluation is x^
    y = 27.639/(x^1.197)
    
    1   27.639
    2   12.055616
    3   7.420073
    4   5.258435
    5   4.025828
    6   3.236498
    7   2.691163
    8   2.293631
    9   1.992022
    10   1.755991
    11   1.566662
    12   1.4117
    13   1.282721
    14   1.173835
    15   1.080789
    16   1.000439
    17   0.930411
    18   0.868883
    19   0.814431
    20   0.765931
    21   0.72248
    22   0.683349
    23   0.647939
    24   0.615757
    25   0.586392
    26   0.559499
    27   0.534786
    28   0.512005
    29   0.490944
    30   0.47142
    31   0.453276
    32   0.436373
    33   0.420592
    34   0.405828
    35   0.391988
    36   0.37899
    37   0.366762
    38   0.35524
    39   0.344364
    40   0.334085
    41   0.324355
    42   0.315132
    43   0.30638
    44   0.298064
    45   0.290153
    46   0.282619
    47   0.275436
    48   0.268582
    49   0.262034
    50   0.255773
    51   0.249782
    52   0.244043
    53   0.238542
    54   0.233264
    55   0.228196
    56   0.223327
    57   0.218645
    58   0.214141
    59   0.209803
    60   0.205625
    61   0.201596
    62   0.19771
    63   0.19396
    64   0.190338
    65   0.186838
    66   0.183454
    67   0.180182
    68   0.177015
    69   0.173948
    70   0.170978
    71   0.168099
    72   0.165309
    73   0.162602
    74   0.159975
    75   0.157425
    76   0.154949
    77   0.152543
    78   0.150205
    79   0.147932
    80   0.145721
    81   0.143571
    82   0.141477
    83   0.13944
    84   0.137455
    85   0.135521
    86   0.133637
    87   0.131801
    88   0.13001
    89   0.128263
    90   0.126559
    91   0.124896
    92   0.123273
    93   0.121688
    94   0.12014
    95   0.118628
    96   0.11715
    97   0.115706
    98   0.114294
    99   0.112914
    100   0.111564
    101   0.110243
    102   0.10895
    103   0.107685
    104   0.106447
    105   0.105235
    106   0.104047
    107   0.102884
    108   0.101745
    109   0.100629
    110   0.099535
    111   0.098462
    112   0.097411
    113   0.09638
    114   0.095369
    115   0.094377
    116   0.093404
    117   0.092449
    118   0.091512
    119   0.090593
    120   0.08969
    121   0.088803
    122   0.087933
    123   0.087077
    124   0.086238
    125   0.085412
    126   0.084602
    127   0.083805
    128   0.083022
    129   0.082252
    130   0.081495
    131   0.080751
    132   0.080019
    133   0.0793
    134   0.078592
    135   0.077896
    136   0.07721
    137   0.076536
    138   0.075873
    139   0.07522
    140   0.074577
    141   0.073945
    142   0.073322
    143   0.072708
    144   0.072105
    145   0.07151
    146   0.070924
    147   0.070347
    148   0.069778
    149   0.069218
    150   0.068666
    151   0.068122
    152   0.067586
    153   0.067057
    154   0.066537
    155   0.066023
    156   0.065517
    157   0.065018
    158   0.064525
    159   0.06404
    160   0.063561
    161   0.063089
    162   0.062623
    163   0.062163
    164   0.06171
    165   0.061262
    166   0.060821
    167   0.060385
    168   0.059955
    169   0.059531
    170   0.059112
    171   0.058698
    172   0.05829
    173   0.057887
    174   0.057489
    175   0.057096
    176   0.056708
    177   0.056325
    178   0.055946
    179   0.055572
    180   0.055203
    181   0.054838
    182   0.054477
    183   0.054121
    184   0.053769
    185   0.053422
    186   0.053078
    187   0.052739
    188   0.052403
    189   0.052071
    190   0.051743
    191   0.051419
    192   0.051099
    193   0.050782
    194   0.050469
    195   0.050159
    196   0.049853
    197   0.04955
    198   0.049251
    199   0.048955
    200   0.048662
    201   0.048372
    202   0.048086
    203   0.047802
    204   0.047522
    205   0.047245
    206   0.04697
    207   0.046699
    208   0.04643
    209   0.046164
    210   0.045901
    211   0.045641
    212   0.045384
    213   0.045129
    214   0.044876
    215   0.044627
    216   0.044379
    217   0.044135
    218   0.043892
    219   0.043653
    220   0.043415
    221   0.04318
    222   0.042947
    223   0.042717
    224   0.042489
    225   0.042263
    226   0.042039
    227   0.041818
    228   0.041598
    229   0.041381
    230   0.041166
    231   0.040952
    232   0.040741
    233   0.040532
    234   0.040325
    235   0.040119
    236   0.039916
    237   0.039714
    238   0.039515
    239   0.039317
    240   0.039121
    241   0.038927
    242   0.038734
    243   0.038544
    244   0.038355
    245   0.038167
    246   0.037982
    247   0.037798
    248   0.037615
    249   0.037434
    250   0.037255
    251   0.037078
    252   0.036902
    253   0.036727
    254   0.036554
    255   0.036383
    
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2017-03-30 17:08
    +1 on table lookup. Additionally, I'd massage the data and the maths. First of all, you won't be measuring volts, you'll be measuring millivolts, actually a count that has 256 steps of 19.53 millivolts for the range of 0V-5V. Reframe the formula for counts.
    y = 27.639/(x^1.197)
    mm = 3072.34 / count ^ 1.197 ' where millimeters is a function of count from 0 to 256.
    No need to convert to millivolts.

    Next, make your table for only the range that you need, cut off the values that are too far distant. Those are the low values of count. For example, cut off the values below count 16, greater than 100mm distant.

    I'd also consider making the table as a function of 1/x instead of proportional to x directly. The graph of 1/x will be almost linear. Commensurately, the Stamp would have to compute 1/x in real time by computing 10000/x or 65535/x. At that point the table lookup is better, and even a quadratic approximation with r^2>0.99 is possible.
  • Thanks to everyone who tried to help. I got some really good suggestions, and will try each to see which is the best. Cecil
Sign In or Register to comment.