PDA

View Full Version : Question about the function:signedToUnsigned


guonaldo
01-13-2010, 12:01 AM
readAdc got some changes in SNAPpy Built-ins for 2.2.16 according to "SNAP Firmware 2.2 Release Notes"
In page 14 a function "signedToUnsigned" is defined as follows:

def signedToUnsigned(value):
if value > 0:
return value # already a positive number, no adjustment needed
unsignedValue = 65535 + value + 1
return unsignedValue

But SNAPpy only supports 8-bit signed integers! For example I got -1 from readAdc(17),then the true number should be -1+1+65535=65535.

Obviously it's out of range. And I use the function peek & poke to test the result.The defined function just does nothing to a negative number.And for a number greater than 32767 the result is negative.

Is the function signedToUnsigned incorrect or I have done something wrong ?

Thanks
guonaldo

kbanks
01-13-2010, 02:45 PM
That function is meant to be used up in a Portal script.

The node reports the signed value (as you point out, it has no choice).

It's Portal that does the fixup.

kbanks
01-13-2010, 02:46 PM
Also, SNAPpy integers are not 8-bit, they are 16-bit: -32768 to +32767.

(Signed 8-bit integers are -128 to +127)

guonaldo
01-13-2010, 07:45 PM
That function is meant to be used up in a Portal script.

The node reports the signed value (as you point out, it has no choice).

It's Portal that does the fixup.

I didn't quite get your idea. Did U mean that the function can only be used during the data processing? We can't test the result according to the output of CEL2410 since SNAPpy only support 16-bit integers.

So should I add this function when I am writing the Portal script if necessary?
I got this problem in my current work. I use several CEL2410 to form a mesh connect. And I place a "main node" there. One of its tasks is to moniter the others' battery level. Since 0-255 is not accurate enough ,I'd like to get the corresponding relationship so that I can get the result in response to the battery's power supply. Referring to the 2410 datasheet 1.7.15 I got confused. When I got the function signedToUnsigned I think there is a way out.

Maybe U can give me some solutions abouy my problem or U could teach me how wo use the signedToUnsigned.

Thanks a lot!

guonaldo

mgenti
01-15-2010, 08:27 AM
The signedToUnsigned function will only work as part of a Portal script. Its job is to help you display a signed integer as an unsigned when used as a part of a Portal script. For example, if you were having Portal log a value that was unsigned you would need to use this function since the node sends the value over as signed.

Warhol
02-01-2010, 03:13 PM
data= readAdc(10)
if data<0: # print 16bit number
thousands = data/10 + 6553
ones = (data+ ((data/-10)*10))*-1
if ones>6:
thousands = thousands -1
ones = 16 - ones +1
else:
ones = 6- ones + 1
print "ACH(0)16 Bit = ", th,ones
else:
print "ACH(0)16 Bit = ",data

# Proof
# thus -25345 should be + 1+ 65535 = 40191

# my way..
# thousands = -25345/10 + 6553 = 4019
# ones = (-25345 + ((-25345/-10)*10))*-1
# = (-23345 + 23340 -1) = 6
# ones = 6-ones+1 = 6-6 +1 = 1

# thus print "ACH(0)16 Bit = ", th,ones #40191

guonaldo
02-04-2010, 09:15 AM
if data<0: # print 16bit number
thousands = data/10 + 6553
ones = (data+ ((data/-10)*10))*-1
if ones>6:
thousands = thousands -1
ones = 16 - ones +1
else:
ones = 6- ones + 1
print "ACH(0)16 Bit = ", th,ones
else:
print "ACH(0)16 Bit = ",data



# Proof
# thus -25345 should be + 1+ 65535 = 40191

# my way..
# thousands = -25345/10 + 6553 = 4019
# ones = (-25345 + ((-25345/-10)*10))*-1
# = (-23345 + 23340 -1) = 6
# ones = 6-ones+1 = 6-6 +1 = 1

# thus print "ACH(0)16 Bit = ", th,ones #40191


I'm very interested in this algorithm,since my platform is ZIC2410.This script could be a lot helpful!
Could u give proof in math language?

kbanks
02-04-2010, 10:47 AM
I would put the conversion code into a standalone function that was not tied to readAdc.

Rather than a "proof", I would then test that conversion routine using Portal. (Just invoke the routine with various test inputs using the "function tree" over in the Node Info pane).

You already know your boundary cases are at 0, 32767, 32768, 65535 (unsigned) or to say it in signed: 0, 32767, -32768, -1, plus you will want to check some in-between values.

You could also have a node script generate a new value every half-second or so, and have the node make a RPC to the portal logData() routine. You would then get a plot of the conversion curve, and could see if anything looked funny.

I'm not knocking software proofs, I'm just saying SNAPpy is meant for "hands-on" usage.

topazx2
06-14-2011, 01:29 PM
What's the chance of getting a Union option added to SNAPpy so we can combine registers for comparing larger unsigned integers?

I can see many good uses for this feature and at least one reason why it's almost imperative, referring to the 10 bit analog inputs.

Most languages have the ability to splice multiple storage registers together for manipulation.

IbarraE
06-14-2011, 09:02 PM
If you read the extending SNAPpy with C functions app note, you can compile C code to do pretty much anything you want. I've already provided some math functions on this forum for Atmel platforms.

Jheath
06-15-2011, 02:08 PM
Eric posted some 32 bit math routines that use the call routine (see link)

http://forums.synapse-wireless.com/showthread.php?t=1332