Shop OBEX P1 Docs P2 Docs Learn Events
Math Question — Parallax Forums

Math Question

ajwardajward Posts: 1,130
edited 2012-01-31 03:30 in Propeller 1
Hi All...

I've been tinkering with the H48C accelerometer using PST and Beau's H48C object to display the results on an LCD. It's been a learning experience! After dealing with a weak battery and a reversed connector, the accelerometer is returning a z-axis value varying +/-455.
According to the notes on the accelerometer spin file, I can divide that axis value by 455 to obtain the g-value which should yield +/- 1g. Do that and I get a value of "1". Where I'm stuck is... I'd like to display the value out to 2 or 3 decimal places and I cannot remember how to do that. I know I've seen the code to display decimals 'somewhere', but I can't find it. Can some kind soul point me in the right direction?

Now, after a few pints of midnight oil, a couple of sodas and (maybe) a bit of harsh language :innocent: I'm calling it a night. Thanks for any guidance!!!!!!!!

Amanda

Comments

  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2012-01-29 04:49
    Amanda;

    I assume you are using integer math.
    And you need to divide the value by 455.

    Just multiply the value something like 10000 first.
    z * 10000 / 455 = g

    Duane J
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-01-29 07:51
    ...
    Just multiply the value something like 10000 first.
    z * 10000 / 455 = g

    ...

    Duane has you off to a good start. For the sake of an example, let's say g = 1234567.
    So now you divide g by, let's say, 100. 1234567/100 = 12345. (The 67 gets dropped because you're doing this in integer math.)
    Now multiply that result by 100. New result = 1234500.
    So if you do this: (1234567 - 1234500), you will get 67, which could be saved in a temporary variable as your decimal fraction.
    Then when you display or record your final number, you display 12345, then display a decimal (.) then display your decimal fraction, so now the displayed value looks like 12345.67.

    At least that's one way to do it while sticking with integers.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-29 09:43
    Amanda,

    To add to what the other Duane and ElectricAye have said, I add this method to some of the serial objects to make it easier to display the decimal point where I want it.
    PUB DecPoint(value, denominator) 
      if value < 0
        tx("-")
        -value
          
      if value > denominator
        result := value / denominator
        dec(result)
        value //= denominator     
      else    
        tx("0")
      tx(".")  
      repeat while denominator > 1
        denominator /= 10
        if value > denominator
          result := value / denominator
          dec(result)
          value //= denominator
        else
          tx("0")
          
    
    

    In the example Duane gave, you'd use "10000" in the denominator argument. This code uses the "tx" method that several serial objects have. If you add it to PST, change "tx" to "char". Or "out" if using "tv_terminal".

    I've seen the technique of using a larger multiples of values referred to as "pseudoreal" in some of Parallax's literature.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-01-29 09:51
    Oh yes! Stupid me. What Duane Degn's code achieves that I left out in my example is the situation in which the decimal fraction is less than 10, or less than 100, or less than 1000 or however many decimal places you want to take it out to. In that case, as Degn has demonstrated, you would need to "pad" your decimal fractions with zeros so the digits show up correctly after the decimal point.
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-01-29 10:11
    Hmmm ... why would you need these additional calculations? If g = 1234567 then you don't need to store 67 somewhere else. It's already there! You only need a conversion function which places the decimal at the right place. I could bet something like this already exists!


    PS: oh ... ok ... outdated ... should refresh tabs opened a while back before posting ;o)
  • ajwardajward Posts: 1,130
    edited 2012-01-29 22:37
    Excellent! Thank you all so much!

    The project behind all this is building a 3-axis vibration sensor. I really want to say "seismometer", but being based in the 2nd story of a wood frame apartment building, I'm afraid actual seismic readings are going to be pretty messy. And, I'm afraid the property management would take a dim view of digging down to bedrock in the basement.
    Still... living in the San Francisco area, I should catch an occasional twitch or jiggle of one of our local faults (or one of the many trucks trundling down our street).

    Not attempting to build a precision tool here... just another "Gee, I wonder if that would work?" idea! One of the many "shiny things" that hinder my completion of previous "shiny things"! :smile:

    Thanks again for all your responses! Quite helpful.

    Amanda
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-01-30 07:41
    ajward wrote: »
    ....

    The project behind all this is building a 3-axis... "seismometer"....

    Sounds like a fun project. But, as you pointed out, you will probably pick up all sorts of vibrations in your building. Depending on how sensitive you make it, you could easily detect foot steps in your building, nearby traffic, or perhaps even your own heartbeat if you're sitting next to the thing. But considering how frequent small earthquakes are in your area, you could probably take it out to a park, away from traffic, place it on a piece of bedrock and sun yourself while you take some readings. Don't use rubber feet, though. I would suggest pointy metal feet in a tripod arrangement that can kinda "bite" the rock a little.

    Keep us posted on how it goes.
  • ajwardajward Posts: 1,130
    edited 2012-01-30 15:15
    I finally got my accelerometer displaying decent values. ***Thanks to Duane Degn for the DecPoint add-in to the serial object. Sweet!***

    http://bit.ly/AlNjmY

    In the LCD you can see the g-values... On the first row X-Axis then Y-Axis. On the second row Z-Axis. A lot to cram into my 2-line LCD. I think I'm going to splurge for a 4x20 LCD soon.

    At this point, I'm using 3 decimal places and that gives a stable display. One issue I need to look at is calibrating each axis so I get very close to 0g when the accelerometer "isn't moving".

    3-AxisAccelerometer.spin.zip
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-01-30 23:08
    I like the display of -0.000 / (+)0.000 ... ;o)

    "...calibrating each axis so I get very close to 0g..." - setting z-Axis to 0? It shows you the gravity-vector.
  • ajwardajward Posts: 1,130
    edited 2012-01-31 03:30
    @MagiO2

    The -0.000 is showing my checking account balance! :-)

    Actually, I was fiddling with the display format. The x-axis was returning a very small negative value that didn't appear in three decimal places, thus the minus zero. After rejiggering things, my x-axis is showing ~ -0.0210. The values also vary depending on where and how the breadboard is sitting on my desk.

    "- setting z-Axis to 0? It shows you the gravity-vector." It does! However my idea is to detect movement of the accelerometer. In the grand scheme, the z-axis reading 1.xxxx isn't terribly important, but aesthetically if the chip is sitting on my desk behaving itself, I'd like it to show zero... at least till "The Big One" hits. ;)

    Right now, It's displaying to 4 decimal places and updating 5 times per second. The circuit is next to my keyboard and as I type this, I can see small changes in the z-axis.

    Anyhow... time for sleep. This project seems to have become an oh-dark-thirty thing. Yawn!

    Nite All...

    Amanda
Sign In or Register to comment.