Shop OBEX P1 Docs P2 Docs Learn Events
Tracy Allen, alms for the math impaired... — Parallax Forums

Tracy Allen, alms for the math impaired...

ArchiverArchiver Posts: 46,084
edited 2002-06-30 14:55 in General Discussion
Tracy and All,

While the code as you wrote it works great, (Thanks!) I don't understand it.
Which makes it impossible for me to change! What I need to do is be able to
change the input parameter projectile length.

What I have going is this:

I wrote a basic BUTTON routine. The default projectile length is 5mm,
established at startup. When the button is pressed, it increments the
projectile length in 5mm increments. Going over 50 resets the value to 5mm.
What I want to be able to do is take the value from here and do the math to
display the m/s correctly based on varying projectile lengths.

Thanks for the input. I am really math impaired, I'm sorry to bother you
over what is doubtless a simple matter. I pored over the math stuff in the
manual for some time to no avail.

In case, here is the button routine:

n9600 con $40f0 'These are the declarations for the LCD
I con 254 'prefix value for LCD
CLR con 1 'LCD clear command
line2 con 192 'Address for 1st char of second line

Pause 1000 ' To let LCD wake up


button1 var byte
currentLength var byte
mod con 5 ' each button press adds this to currentLength
currentLength=5 ' Sets projectile length default
GOSUB ShowLength ' Displays default setting


loop:

BUTTON 1,0,255,250,Button1,0,NoPress
currentLength = currentLength + mod
IF currentLength > 50 then defValue
GOSUB ShowLength


nopress:

goto loop

defValue:
currentLength = 5
gosub ShowLength
goto Loop

ShowLength:

serout 0,n9600,[noparse][[/noparse]I,CLR]
pause 1
serout 0,n9600,[noparse][[/noparse]"Current Length:"]
pause 1
serout 0,n9600,[noparse][[/noparse]I,line2]
pause 1
serout 0,n9600,[noparse][[/noparse]dec currentLength]
pause 1

RETURN





> >
> >Time Var Word
> >
> >Again:
> > pulsin 1, 1, Time
> > if Time = 0 then Again
> > debug dec ? Time
> >goto Again
> >
> >This seems to work. Time returns a value of around 1900. Since the 2sx
> >pulsin command increments time in .8us segments, I (correct me if I am
> >wrong!!) multiply 1900 by .8 which yields 1520. Move the decimal over
three
> >places to change to ms, giving 1.52ms. The length of the projectile is
25mm
> >so 25/1.52 = 16.4 meters/second. Is this all true? The values I am
getting
> >are believable for this coil gun. I just need someone to verify that this
> >all makes sense.
>
> Those calculations look fine to me. Or you can go direct:
> velocity = 31250 / time ' when time is in units of 0.8 us
> or for the stamp calculation (below):
> velocity*10 = 62500 / (time/5)
>
> >As the stamp only deals with whole numbers, how do I code the math
required
> >to make "Time" read in meters/second?
> >
> Again:
> pulsin 1, 1, Time
> if Time = 0 then Again
> debug dec ? Time
> velocity = 62500/(time/5)
> debug "velocity=",dec velocity/10,".",dec1 velocity," m/s"
> goto Again
>

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-06-29 02:50
    >Tracy and All,
    >
    >While the code as you wrote it works great, (Thanks!) I don't understand it.
    >Which makes it impossible for me to change! What I need to do is be able to
    >change the input parameter projectile length.
    >
    >What I have going is this:
    >
    >I wrote a basic BUTTON routine. The default projectile length is 5mm,
    >established at startup. When the button is pressed, it increments the
    >projectile length in 5mm increments. Going over 50 resets the value to 5mm.
    >What I want to be able to do is take the value from here and do the math to
    >display the m/s correctly based on varying projectile lengths.
    >



    > > >Again:
    >> > pulsin 1, 1, Time
    >> > if Time = 0 then Again
    >> > debug dec ? Time
    >> >goto Again
    >> >
    >> >This seems to work. Time returns a value of around 1900. Since the 2sx
    >> >pulsin command increments time in .8us segments, I (correct me if I am
    > > >wrong!!) multiply 1900 by .8 which yields 1520. Move the decimal over
    >three places to change to ms, giving 1.52ms. The length of the projectile is
    >25m so 25/1.52 = 16.4 meters/second. Is this all true? The values I am
    >getting are believable for this coil gun. I just need someone to
    >verify that this all makes sense.
    > >
    > > Those calculations look fine to me. Or you can go direct:
    > > velocity = 31250 / time ' when time is in units of 0.8 us
    > > or for the stamp calculation (below):
    > > velocity*10 = 62500 / (time/5)

    from first principles,
    velocity = length of projectile / seconds obscured
    As you noted, the stamp 2sx counts in units of 0.0008 millisecond,
    0.8 microsecond.
    velocity = 25 / (timeSX*0.0008) ' still calculator math, not stamp math!
    or dividing 25/0.0008
    velocity = 31250/timeSX
    That will work for the stamp. It is easy to extend the formula to
    projectile lengths of 5,10,15...45mm. note 1/0.0008 = 1250.
    velocity = (projectile_length) * 1250 / timeSX.
    (e.g., when projectile length = 25, 25*1250=31250)

    However the formula is not as accurate as you would like. When
    timeSX=1900, it kicks out velocity=16. The Stamp's integer math has
    dropped the remainder.

    For improved precision you can use:
    velocity = (projectile_length) * 1250 / (timeSX/5) *2
    debug "velocity=",dec velocity/10,".",dec1 velocity," m/s"
    and that would kick out 164 when timeSX=1900. In effect you are
    reducing the number of significant figures in the denominator
    (timeSX/5), in order to increase the precision of the division
    result. That formula is good for accuracy to 0.2.

    My original suggestion was for projectile_length=25...
    velocity = 62500/(timeSX/5)
    Or in general with accuracy to 0.1:
    velocity = (projectile_length) * 2500 / (timeSX/5)
    But you can't carry that out formula out past projectile lengths of
    26mm, e.g. 45*2500 = 112500, which is greater than the word capacity
    of the Stamp. That is always what you have to be checking in integer
    math--overflow.

    There are other options! Here is a formula that improves the
    precision to 0.1 for all projectile lengths up to 52:

    factor = projectile_length*1250
    velocity = factor/timeSX *10 + factor//timeSX*10/timeSX
    debug "velocity=",dec velocity/10,".",dec1 velocity," m/s"

    There are also ways to get 4 digits of precision, too, if you really need it.

    -- regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com/BS2index.htm <-- more math tricks here
    mailto:tracy@e...


    > > >As the stamp only deals with whole numbers, how do I code the math
    >required
    > > >to make "Time" read in meters/second?
    >> >
    >> Again:
    >> pulsin 1, 1, Time
    >> if Time = 0 then Again
    >> debug dec ? Time
    >> velocity = 62500/(time/5)
    > > debug "velocity=",dec velocity/10,".",dec1 velocity," m/s"
    > > goto Again
    >>
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-29 14:17
    Tracy,,

    Thanks a ton! I *think* I get it. Why doesn't the Stamp do decimals? I'm
    sure there is a reason, I just wonder what it is.

    Regards,

    Jonathan Peakall
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-29 20:17
    >Tracy,,
    >Thanks a ton! I *think* I get it. Why doesn't the Stamp do decimals? I'm
    >sure there is a reason, I just wonder what it is.
    >Regards,
    >Jonathan Peakall

    I hope that helps-ask again if need be!

    The Stamp 2 was (and is) an amazing accomplishment, to fit the BASIC
    interpreter into the limited memory space of the PIC chip. I recall
    from talking with Chip or Chuck Gracey that there were only a few
    bytes left free once everything was packed in. "Decimals" (by which
    you probably mean floating point math) takes quite a bit of memory,
    both for the program and for the variables. Consider that one decent
    floating point variable requires 8 bytes of memory. Yikes, the Stamp
    would have room for only 3 variables! Floating point math brings
    with it a whole new class of potential errors. I would really rather
    have higher precision integer math, say 24 or 32 bit, with a carry
    flag. If you want floating point math, take a look at Al Williams'
    Pak addons.

    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-30 03:35
    Tracy,

    I'm glad you said to ask if I had any more questions...

    The code below is yielding incorrect results in the m/s. With an input of
    1956 time units, I get 0.7m/s. What am I doing wrong?

    '
    ' Here are the LCD declarations

    n9600 con $40f0 'baud rate
    I con 254 'prefix value for LCD addresses
    CLR con 1 'LCD clear command
    line2 con 192 'Address for 1st char of second line
    Pause 1000 'To let LCD wake up

    '
    '

    Velocity Var Word
    Time Var word
    'debug "Timer Ready", CR
    factor Var Word
    length Var Nib
    length = 25
    factor = length*1250



    Again:
    pulsin 1, 1, Time
    if time = 0 then again
    velocity = factor/time *10 + factor//time*10/time
    serout 0,n9600,[noparse][[/noparse]I,CLR]
    pause 1
    serout 0,n9600,[noparse][[/noparse]"Timer Count:", dec Time]
    pause 1
    serout 0,n9600,[noparse][[/noparse]I,line2]
    pause 1
    serout 0,n9600,[noparse][[/noparse]"Velocity = ", dec velocity/10,".",dec1 velocity," m/s"]
    pause 1
    goto Again
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-30 03:47
    Hi Jonathan,

    Try making the length variable a byte instead of a nib. A nib variable can
    only hold values from 0 to 15

    Steve




    At 07:35 PM 6/29/02 -0700, you wrote:
    >Tracy,
    >
    >I'm glad you said to ask if I had any more questions...
    >
    >The code below is yielding incorrect results in the m/s. With an input of
    >1956 time units, I get 0.7m/s. What am I doing wrong?
    >
    >'
    >' Here are the LCD declarations
    >
    >n9600 con $40f0 'baud rate
    >I con 254 'prefix value for LCD addresses
    >CLR con 1 'LCD clear command
    >line2 con 192 'Address for 1st char of second line
    >Pause 1000 'To let LCD wake up
    >
    >'
    >'
    >
    >Velocity Var Word
    >Time Var word
    >'debug "Timer Ready", CR
    >factor Var Word
    >length Var Nib
    >length = 25
    >factor = length*1250
    >
    >
    >
    >Again:
    > pulsin 1, 1, Time
    > if time = 0 then again
    > velocity = factor/time *10 + factor//time*10/time
    > serout 0,n9600,[noparse][[/noparse]I,CLR]
    > pause 1
    > serout 0,n9600,[noparse][[/noparse]"Timer Count:", dec Time]
    > pause 1
    > serout 0,n9600,[noparse][[/noparse]I,line2]
    > pause 1
    > serout 0,n9600,[noparse][[/noparse]"Velocity = ", dec velocity/10,".",dec1 velocity," m/s"]
    > pause 1
    >goto Again
    >
    >
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and
    >Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-30 04:14
    Steve,

    Tried that, up to a Word. No luck. Thanks though!

    Regards,

    Jonathan



    > Hi Jonathan,
    >
    > Try making the length variable a byte instead of a nib. A nib variable can
    > only hold values from 0 to 15
    >
    > Steve
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-30 07:05
    Hi again Jonathan,

    Oh, in addition to length as a byte, you need some parentheses. My
    bad, I blanked on them in my previous message.

    factor = length*1250
    velocity = (factor/time *10) + (factor//time*10/time)
    debug "velocity=",dec velocity/10,".",dec1 velocity," m/s"

    That should give you 15.9 when time=1956. I hope that works this time.

    What is happening is this: you compute the first division,
    (factor/time), then multiply it by ten to clear out the units place.
    Then you take the remainder (factor//time) from the first division,
    multiply it by ten and divide it by time, and that fills in the ones
    spot in your result.

    -- Tracy

    >Tracy,
    >
    >I'm glad you said to ask if I had any more questions...
    >
    >The code below is yielding incorrect results in the m/s. With an input of
    >1956 time units, I get 0.7m/s. What am I doing wrong?
    >
    >'
    >' Here are the LCD declarations
    >
    >n9600 con $40f0 'baud rate
    >I con 254 'prefix value for LCD addresses
    >CLR con 1 'LCD clear command
    >line2 con 192 'Address for 1st char of second line
    >Pause 1000 'To let LCD wake up
    >
    >'
    >'
    >
    >Velocity Var Word
    >Time Var word
    >'debug "Timer Ready", CR
    >factor Var Word
    >length Var Nib
    >length = 25
    >factor = length*1250
    >
    >
    >
    >Again:
    > pulsin 1, 1, Time
    > if time = 0 then again
    > velocity = factor/time *10 + factor//time*10/time
    > serout 0,n9600,[noparse][[/noparse]I,CLR]
    > pause 1
    > serout 0,n9600,[noparse][[/noparse]"Timer Count:", dec Time]
    > pause 1
    > serout 0,n9600,[noparse][[/noparse]I,line2]
    > pause 1
    > serout 0,n9600,[noparse][[/noparse]"Velocity = ", dec velocity/10,".",dec1 velocity," m/s"]
    > pause 1
    >goto Again
    >
    >
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the
    >Subject and Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-06-30 14:55
    Tracy,

    Works fine now. Thanks a ton for your help. If you ever need help
    constructing odd high voltage devices, let me know.

    Regards,

    Jonathan Peakall
Sign In or Register to comment.