Shop OBEX P1 Docs P2 Docs Learn Events
Math and variables — Parallax Forums

Math and variables

ghost13ghost13 Posts: 133
edited 2007-05-30 10:30 in BASIC Stamp
Hi,

I have the Basic Stamp 1. I'm trying to use exponents and powers. How do I do this?

Also, what's wrong with the following? I took it right out of the serial LCD manual.
SEROUT 3, 84, [noparse][[/noparse]22, 12, 17]



And what's wrong with this line?
baseRead = oddSign


oddSign is created like this:
SYMBOL oddSign = BIT0


baseRead is created like this:
SYMBOL baseRead = W0



Do I have to do
LET baseRead = oddSign



And in addition, this line says it expects a variable:
IF (oddSign-baseRead)>=3235 THEN HIGH PIN7



Am I creating variables incorrectly? I just want a simple integer. This is how I tried to create my variable:
SYMBOL  reading  = W0



THANKS!!!

Post Edited (ghost13) : 5/26/2007 9:33:17 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-26 21:40
    Sometimes it's a little confusing that the Basic acceptable for the BS1 isn't the same as that for the BS2. The BS1 was first and is much more limited in resources than any of the BS2 models and the Basic reflects that.

    1) The 84 is the Baud value for a BS2 and sets 9600 Baud, no parity, true. The BS1 can't handle 9600 Baud. The highest speed is 2400.
    At 2400 Baud, the statement would be: SEROUT 3,T2400,(22, 12, 17)
    Note that parentheses are used with the BS1 SEROUT statement.

    2/3) The LET is optional. Whether the statement is valid or not depends on the definition of baseRead and oddSign.

    4) The "fancy" syntax with the statement following the THEN is only allowed for the BS2 and only if you've specified PBASIC 2.5
    using the PBASIC directive. Use IF <condition> THEN <address>. For the BS1, <condition> is more restricted than for the BS2.
    The PBASIC manual has the syntax in it as well as examples for the BS1 and BS2.

    5) The BS1 has 6 - 16bit predefined variables (W0-W5) which can also be accessed as 12 - 8bit variables (B0-B11). The SYMBOL
    statement allows aliases to be defined for these predefined names. In your case, you could use "SYMBOL baseRead = W0".

    6) None of the Stamps really support powers and exponents although the BS2 models can do integer square roots and there have been
    some routines written for the BS2 for natural logs and exponents (see: www.emesystems.com).

    Post Edited (Mike Green) : 5/26/2007 9:46:49 PM GMT
  • ghost13ghost13 Posts: 133
    edited 2007-05-26 21:50
    Hmm... I fixed all the questions except the ones about math.

    First, how do I divide by something like 0.009? I'm trying to convert the ADC reading to pressure. Then, how do I use exponents/powers?

    Thanks!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-26 21:56
    All of the Stamps only do 16-bit integer arithmetic. Have a look at the "math" section of www.emesystems.com. It goes into issues of fixed point arithmetic and scaling which is what you need to do. For example, if you have a voltage between 0 and 15, you'd keep all your data in millivolts rather than volts (3V would be 3000). To divide a voltage by 0.009 to get an integer value, you'd divide millivolts by 9 to get the integer. The website gives lots of examples.
  • ghost13ghost13 Posts: 133
    edited 2007-05-26 22:09
    So if I multiply everything by 1000, and then do the math, and then divide by 1000, that might work?

    What about needing to use exponents?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-26 22:12
    Read the stuff on the website. He does a much better job of explaining it with examples than I do.

    What about needing to use exponents? I don't know ... They're not all they're cracked up to be.

    Really, why do you think you need them? (You might, but I don't know what you're trying to do).
  • ghost13ghost13 Posts: 133
    edited 2007-05-26 22:17
    I'm trying to convert pressure to feet. The excell formatted equation is:
    ((1-POWER((pressure/101.325),0.190263))*(288.15/0.00198122))
    



    I looked at the website and I got dividing by 0.009 to work [noparse]:)[/noparse]

    Now I just need exponents [noparse]:([/noparse]
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-26 22:24
    You might be able to do table lookup, but you're really very very limited in what you can do with a BS1 because of the size of the EEPROM (256 bytes) and the amount of variable space available. Look at the EEPROM, READ, and WRITE statements in the PBasic manual.
  • ghost13ghost13 Posts: 133
    edited 2007-05-26 23:03
    I think i'll just output the ADC values (I'm displaying this stuff on a 2x16 serial LCD). Then, I'll make a Java program to convert it.

    Thanks!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-26 23:05
    That sounds like a good idea. The BS1 is useful as a controller, but it just doesn't have "the stuff" for any significant calculations.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-27 07:36
    That is a pressure->altitude conversion, right? If you graph the function in Excel, you will find that it does not change very fast with altitude, especially at low altitudes. Up to 10000 feet, a quadratic approximation might be a good enough fit and over a narrower range even a linear approximation would do. Even the BS1 can do linear.

    The attached graph has altitude in feet from 0 to 10000 on the left y axis, and pressure in mb on the right y axis, both as functions of the millivolt output of an MXP4115 analog sensor. Even though it has an exponent, the exponent is small and the graph is not strongly curved.

    So the message is, Excel is your friend to simplify the problem enough for the BS1, and if that is not possible, ageed, offload the task to the computer that can handle it easily.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
    324 x 259 - 5K
  • ghost13ghost13 Posts: 133
    edited 2007-05-27 09:43
    How accurate would a quadratic system be? This program relies on accuracy, so that might not work.

    Regardless, however, I'm out of memory :-(

    Am I not programming well? Or is the BS1 just too limited. The code is attached.

    Thanks!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-27 13:51
    The BS1 is very very limited in program space (256 bytes). Your program looks about right for a BS1 program. You could drastically shorten your display messages. There are about 75 bytes of messages in your program. That's almost 1/3 of your memory space.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-27 17:33
    Also, "accuracy" and the "pressure-altitude formula" should not be uttered in the same breath. The formula you have above is the one that applies when the sea level pressure and temperature are at their standard values (101325 Pascal and 288.15 Kelvin). The next level requires that the full formula be used that includes the correct sea level pressure and temperature. Furthermore, there are assumptions behind the model, which assumes a constant lapse rate of -6.5 Kelvin per kilometer. That is not necessarily a good assumption, especially in conditions of unsettled weather or situations in uneven terrain, near mountains say.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ghost13ghost13 Posts: 133
    edited 2007-05-27 18:40
    This device is to be used in rockets as an altimeter. Most model rocket altimeters are barometric based, so I think it will be accurate enough.
  • SumanSuman Posts: 19
    edited 2007-05-28 08:12
    Hello. For all these mathematical functions you can use uMFPU math co processor. I have been working with this chip and the results are quite encouraging. The BS2 has 16 bit operation while floatng data require atleast 32bit. This chip provides with it and also a number of registers.

    Also the support for the chip is quite good.

    Suman Sadhu
  • MightorMightor Posts: 338
    edited 2007-05-30 10:30
    Could you tell me where you sourced your uM-FPU from? I can't seem to locate a reseller of them in the Netherlands.

    Thanks,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
Sign In or Register to comment.