BS2 math operations
Archiver
Posts: 46,084
Hey gang, slowly but surely making progress!!
This is what I have so far!
Seems to work ok from 0 to 45degrees...but then as EEEE becomes
greater than NNNN I get decimal numbers and it drops those...then of
course, anything divided by 0 is zero!
It works OK in the first 45...not accurate(lost decimal places!).
I have a feeling I'm going to have problems using this stamp for
this. Is there a mathcoprocessor out there for these? Or a way to
keep decimal places?!
NNNN var word
EEEE var word
TT var byte
theta var word
output 15
loop:
serin 0, 11505, [noparse][[/noparse]wait (2), hex4 NNNN, skip 1, hex4 EEEE, skip 1, hex2
TT]
'speed conversion
TT = (TT * (24/23))
'POS angle deduction
NNNN = (NNNN / 128)
EEEE = (EEEE / 128)
theta = ((cos(NNNN/EEEE))/(sin(NNNN/EEEE)))
This is what I have so far!
Seems to work ok from 0 to 45degrees...but then as EEEE becomes
greater than NNNN I get decimal numbers and it drops those...then of
course, anything divided by 0 is zero!
It works OK in the first 45...not accurate(lost decimal places!).
I have a feeling I'm going to have problems using this stamp for
this. Is there a mathcoprocessor out there for these? Or a way to
keep decimal places?!
NNNN var word
EEEE var word
TT var byte
theta var word
output 15
loop:
serin 0, 11505, [noparse][[/noparse]wait (2), hex4 NNNN, skip 1, hex4 EEEE, skip 1, hex2
TT]
'speed conversion
TT = (TT * (24/23))
'POS angle deduction
NNNN = (NNNN / 128)
EEEE = (EEEE / 128)
theta = ((cos(NNNN/EEEE))/(sin(NNNN/EEEE)))
Comments
>This is what I have so far!
>Seems to work ok from 0 to 45degrees...but then as EEEE becomes
>greater than NNNN I get decimal numbers and it drops those...then of
>course, anything divided by 0 is zero!
>
>It works OK in the first 45...not accurate(lost decimal places!).
>
>I have a feeling I'm going to have problems using this stamp for
>this. Is there a mathcoprocessor out there for these? Or a way to
>keep decimal places?!
>
>NNNN var word
>EEEE var word
>TT var byte
>theta var word
>
>output 15
>
>loop:
>serin 0, 11505, [noparse][[/noparse]wait (2), hex4 NNNN, skip 1, hex4 EEEE, skip 1, hex2
>TT]
So, you are now sure are you that NNNN and EEEE are HEX, not BCD?
How about a sign? (The Stamp does have the option for signed HEX,
using the SHEX modifier instead of HEX, but it has to be in the
correct format.)
>
>'speed conversion
>TT = (TT * (24/23))
The trouble is, (24/23) = 1, exactly, in integer math. The remainder
is dropped. To do this and keep the precision, multiply before
divide:
TT = TT*24/23
debug dec TT
or even better:
TT = TT*2400/23
debug dec TT/100,".",dec2 TT
You keep the "decimal" by scaling to fixed point values, e.g., 2400
to represent 24.00.
>
>'POS angle deduction
>NNNN = (NNNN / 128)
>EEEE = (EEEE / 128)
How did you determine the /128 conversion? What is the range of NNNN
and EEEE? Is is signed?
>
>theta = ((cos(NNNN/EEEE))/(sin(NNNN/EEEE)))
There are ways to do this on the Stamp, But are you sure that is the
right formula? It is kind of weird! You may need an arctangent? I
just don't know how this device works.
The Stamp does have an (undocumented!) arctangent function built in:
http://www.emesystems.com/BS2math3.htm#ATN_HYP
Trig problems usually require segmentation. From NNNN and EEEE you
can probably determine quadrant of the wind direction. But for the
calculation you have to trick the angle into the first quadrant, and
then use trig identities to finish up.
There is indeed a math co-processor that has trig functions, from Al Williams.
--good luck,
Thomas Tracy Allen PhD
electronically monitored ecosystems
http://www.emesystems.com
mailto:tracy@e...
--- In basicstamps@yahoogroups.com, Tracy Allen <tracy@e...> wrote:
> >
> >It works OK in the first 45...not accurate(lost decimal places!).
> >
> >NNNN var word
> >EEEE var word
> >TT var byte
> >theta var word
> >serin 0, 11505, [noparse][[/noparse]wait (2), hex4 NNNN, skip 1, hex4 EEEE, skip 1,
hex2
> >TT]
>
> So, you are now sure are you that NNNN and EEEE are HEX, not BCD?
> How about a sign? (The Stamp does have the option for signed HEX,
> using the SHEX modifier instead of HEX, but it has to be in the
> correct format.)
Well, the way it works, I've been told...is that so long as the wind
vectors (NNNN and EEEE) are in the positive region (0 to 90), both
HEX digits are positive. Plot the numbers in an x,y plane (going CCW)
Between 91&180: -X,Y; between 181&270: -X,-Y; between 271&359: X,-Y.
So at different times they the N's and E's become negative.
Can I still use SHEX with a positive number?
>
> >
> >'speed conversion
> >TT = (TT * (24/23))
>
> The trouble is, (24/23) = 1, exactly, in integer math. The
remainder
> is dropped. To do this and keep the precision, multiply before
> divide:
> TT = TT*24/23
> debug dec TT
> or even better:
> TT = TT*2400/23
> debug dec TT/100,".",dec2 TT
>
> You keep the "decimal" by scaling to fixed point values, e.g., 2400
> to represent 24.00.
Ok, but I need to use the decimal value in more equations below
(weren't added in this example).
>
> >
> >'POS angle deduction
> >NNNN = (NNNN / 128)
> >EEEE = (EEEE / 128)
>
> How did you determine the /128 conversion? What is the range of
NNNN
> and EEEE? Is is signed?
The 128 is a conversion back to a proper number. For some reason the
*128 on the original data in the sensor (a way to defeat the decimal?)
>
> >
> >theta = ((cos(NNNN/EEEE))/(sin(NNNN/EEEE)))
>
> There are ways to do this on the Stamp, But are you sure that is
the
> right formula? It is kind of weird! You may need an arctangent?
I
> just don't know how this device works.
>
> The Stamp does have an (undocumented!) arctangent function built in:
>
> http://www.emesystems.com/BS2math3.htm#ATN_HYP
ArcTan is EXACTLY what I need!! Couldn't find any info on it.
Basically...what I need to do, is take the NNNN and EEEE (rectangular
vector values) and convert them to polar (resultant @ Angle).
The equation is [noparse][[/noparse]angle = (arctan(y/x))] or in my case [noparse][[/noparse]angle = arctan
(NNNN/EEEE)]
Of course there are issues whenever X is larger than Y (0.0n's-
decimal numbers).
>
> Trig problems usually require segmentation. From NNNN and EEEE you
> can probably determine quadrant of the wind direction. But for the
> calculation you have to trick the angle into the first quadrant,
and
> then use trig identities to finish up.
>
> There is indeed a math co-processor that has trig functions, from
Al Williams.
>
>
>
> --good luck,
> Thomas Tracy Allen PhD
> electronically monitored ecosystems
> http://www.emesystems.com
> mailto:tracy@e...
Williams.
http://www.al-williams.com/pak1.htm has the details.
Al Williams
AWC
LOL, send money!
>--- In basicstamps@yahoogroups.com, Tracy Allen <tracy@e...> wrote:
>> >
>> >It works OK in the first 45...not accurate(lost decimal places!).
>> >
>> >NNNN var word
>> >EEEE var word
>> >TT var byte
>> >theta var word
>
> > >serin 0, 11505, [noparse][[/noparse]wait (2), hex4 NNNN, skip 1, hex4 EEEE, skip 1,
>hex2
>> >TT]
> >
>> So, you are now sure are you that NNNN and EEEE are HEX, not BCD?
>> How about a sign? (The Stamp does have the option for signed HEX,
>> using the SHEX modifier instead of HEX, but it has to be in the
>> correct format.)
>
>Well, the way it works, I've been told...is that so long as the wind
>vectors (NNNN and EEEE) are in the positive region (0 to 90), both
>HEX digits are positive. Plot the numbers in an x,y plane (going CCW)
>Between 91&180: -X,Y; between 181&270: -X,-Y; between 271&359: X,-Y.
>
>So at different times they the N's and E's become negative.
>Can I still use SHEX with a positive number?
Yes, you should try to capture the minus sign using the SHEX modifier.
serin 0,11505,[noparse][[/noparse]wait(2),shex4 NNNN,skip 1,shex4 EEEE,skip 1,hex2 TT]
examples:
A in SHEX is the same as 10 in decimal and is represented by the
bit pattern %1010 in the computer.
but...
-A in SHEX is -10 in decimal (signed), and in the computer it is
represented by the 16-bit, twos complement bit pattern,
%1111111111110110 binary
$FFF6 denoted as hex
65526 denoted as decimal
All of these notations represent the same quantity in twos
complement. It can be added subtracted and multiplied, but be
careful--division does not work the way you might expect with twos
complement negative integers.
When NNNN=-00A arrives in the data string, and the stamp brings that
in with SHEX, it will store it in memory as the twos complement value
just mentioned.
> > >'speed conversion
>> >TT = (TT * (24/23))
>>
>> The trouble is, (24/23) = 1, exactly, in integer math. The
>remainder
>> is dropped. To do this and keep the precision, multiply before
>> divide:
>> TT = TT*24/23
>> debug dec TT
>> or even better:
>> TT = TT*2400/23
>> debug dec TT/100,".",dec2 TT
>>
>> You keep the "decimal" by scaling to fixed point values, e.g., 2400
>> to represent 24.00.
>
>Ok, but I need to use the decimal value in more equations below
>(weren't added in this example).
In the computer it is stored as a binary value that can be used in
computations. The fact that it is multiplied times 100 allows you to
maintain precision in the calculations. For example, instead of a
windspeed of 5, you might like to work with a more accurate 5.25. In
the calculations, you follow it through as 525. Then at the end you
print or adjust the decimal point.
> > >
>> >'POS angle deduction
>> >NNNN = (NNNN / 128)
>> >EEEE = (EEEE / 128)
>>
>> How did you determine the /128 conversion? What is the range of
>NNNN
>> and EEEE? Is is signed?
>
>The 128 is a conversion back to a proper number. For some reason the
>*128 on the original data in the sensor (a way to defeat the decimal?)
It may be then that the NNNN and EEEE range from -128 to +128, to
represent the sine and cosine components of the wind direction. That
is, NNNN/128 and EEEE/128, since the sine and cosine have a range of
0--1.
If that is the case, then you are in luck. Because that is also the
way that the BASIC Stamp treats the sine and cosine and arctangent
functions, as numbers from -127 to +127 (with the denominator 128
implied). Yes, it is a way to "defeat" the decimal--but I don't
think that is exactly the right term!!
> > >
>> >theta = ((cos(NNNN/EEEE))/(sin(NNNN/EEEE)))
>>
>> There are ways to do this on the Stamp, But are you sure that is
>the
>> right formula? It is kind of weird! You may need an arctangent?
>I
>> just don't know how this device works.
>>
>> The Stamp does have an (undocumented!) arctangent function built in:
>>
>> http://www.emesystems.com/BS2math3.htm#ATN_HYP
>
>ArcTan is EXACTLY what I need!! Couldn't find any info on it.
>Basically...what I need to do, is take the NNNN and EEEE (rectangular
>vector values) and convert them to polar (resultant @ Angle).
It sounds like you may have what you need in order to use the
(undocumented) ATN function.
angle = (EEEE ATN NNNN) ' where EEEE and NNNN are in the range +/- 127
The angle is in brads. To convert to degrees, use
angleDegrees = angleBrads */360
There will be an issue of angle conventions, because math convention
is to measure angles counterclockwise from the positive X axis, while
wind direction is measured clockwise from north. Maybe it will come
out okay.
>
>The equation is [noparse][[/noparse]angle = (arctan(y/x))] or in my case [noparse][[/noparse]angle = arctan
>(NNNN/EEEE)]
>Of course there are issues whenever X is larger than Y (0.0n's-
>decimal numbers).
>
>>
>> Trig problems usually require segmentation. From NNNN and EEEE you
>> can probably determine quadrant of the wind direction. But for the
>> calculation you have to trick the angle into the first quadrant,
>and
>> then use trig identities to finish up.
>>
>> There is indeed a math co-processor that has trig functions, from
>Al Williams.
>>
>
> >
>>
>> --good luck,
>> Thomas Tracy Allen PhD
>> electronically monitored ecosystems
>> http://www.emesystems.com
> > mailto:tracy@e...
NNNN var word
EEEE var word
TT var byte
angle var word
Npos con $7fff
Epos con $7fff
loop:
serin 0, 11505, [noparse][[/noparse]wait (2), shex4 NNNN, skip 1, shex4
EEEE, skip 1, shex2 TT]
*As you see here, I found that I DO have signed HEX coming in.
Here's one of my problems here....These statements are always false!
What I want to do is convert whichever (NNNN or EEEE) is negative
back to it's proper complement (no sign).
I've tried a $ infront of the 0 but nope.
if 0 > NNNN then CONVERTN
debug "NO N convert! ", CR
if 0 > EEEE then CONVERTE
debug "NO E convert! ", CR
if (NNNN <= Npos) AND (EEEE <= Epos) then ONE
'Determine if in 0-90 Quadrant
if (NNNN >= $8000) AND (EEEE <= Epos) then TWO
'Determine if in 90-180 Quadrant
if (NNNN <= Npos) AND (EEEE >=$8000) then THREE
'Determine if in 270-360
END
*haven't finished adding the last IF here...but these IF's decide
which vector the direction is in.
CONVERTN:
NNNN = (~NNNN)
'performs 2s complement
DEBUG sHEX4 NNNN, CR
END
CONVERTE:
EEEE = (~EEEE)
DEBUG sHEX4 NNNN, CR
END
These work great...no prob here!
Ideas!!
Not sure if I've organized this well enough....
This is what I ended up with....
Just have to interface to a parallel LCD...
'{$STAMP BS2}
NNNN var word
EEEE var word
TT var byte
angle var word
Nneg var byte
Eneg var byte
output 15
loop1:
serin 0, 11505, [noparse][[/noparse]wait (2), shex4 NNNN, skip 1, shex4 EEEE, skip 1,
hex2 TT]
out15=0
pause 1000
out15=1
'speed conversion
TT = (TT * (24/23))
if NNNN >= 64000 then CONVERTN 'NNNN is neg
when southerly
debug "NO N convert! ", CR
if EEEE >= 64000 then CONVERTE 'EEEE is neg
when westerly
debug "NO E convert! ", CR
goto loop2
END
Loop2:
if (Nneg = 1) AND (EEEE > 100) then TWO 'Determine if in 90-
180 Quadrant
if (NNNN > 100) AND (Eneg = 1) then THREE 'Determine if in 270-
360 Quadrant
if (Nneg = 1) AND (Eneg = 1) then FOUR 'Determine if in 180-
270 Quadrant
if (NNNN > 100) AND (EEEE > 100) then ONE 'Determine if in 0-90
Quadrant
END
CONVERTN:
Nneg = 1
NNNN = (~NNNN) 'performs 2s
complement
DEBUG "ConvertN: ",sHEX4 NNNN, CR
goto Loop2
END
CONVERTE:
Eneg = 1
EEEE = (~EEEE)
DEBUG "ConvertE: ",sHEX4 EEEE, CR
goto Loop2
END
ONE:
debug DEC 1, CR '0-90
Angle deduction
NNNN = (NNNN / 128) 'Reduce from
Sensor Value (see notes)
EEEE = (EEEE / 128) 'Reduce from
Sensor Value (see notes)
angle = ((EEEE ATN NNNN) */ 360) 'Calc for
angle based on N,E vectors converted to degrees
angle = 90 - angle
'Adjust to quadrant
goto format
TWO:
debug DEC 2, CR
NNNN = (NNNN / 128) 'Reduce from
Sensor Value (see notes)
EEEE = (EEEE / 128) 'Reduce from
Sensor Value (see notes)
angle = ((EEEE ATN NNNN) */ 360) 'Calc for
angle based on N,E vectors converted to degrees
angle = angle + 90 'Adjust to
quadrant
goto format
THREE:
debug DEC 3, CR
NNNN = (NNNN / 128) 'Reduce from
Sensor Value (see notes)
EEEE = (EEEE / 128) 'Reduce from
Sensor Value (see notes)
angle = ((EEEE ATN NNNN) */ 360) 'Calc for
angle based on N,E vectors converted to degrees
angle = 270 + angle 'Adjust to
quadrant
goto format
FOUR:
debug DEC 4, CR
NNNN = (NNNN / 128) 'Reduce from
Sensor Value (see notes)
EEEE = (EEEE / 128) 'Reduce from
Sensor Value (see notes)
angle = ((EEEE ATN NNNN) */ 360) 'Calc for
angle based on N,E vectors converted to degrees
angle = angle 'Adjust to quadrant
goto format
FORMAT:
debug " ",CR ,CR
debug DEC angle,"/",DEC TT, CR
goto loop1
END