Wednesday, February 29, 2012

NXT Blocks - Conversion Utils - Float To String (GSAFloat2String)

One of the challenges of NXT programming is to be able to quickly move data between different NXT units, or NXT-PC. Most usually we need to move numbers, for example sensor data, measurements, command parameters, etc.

We can already move numbers one by one with the Bluetooth Send and Receive, but is quite inefficient. First we can move only one number, true/false (boolean) value or one string at time. Combine this with the slowish Bluetooth ping-pong, moving sightly bigger number of data can be a problem.

Now wait! The string can have up to 50 characters... if we are able to serialize and un-serialize numbers to string then we most probably can have more than a number in one Bluetooth packet!

In my last post I presented a way to de serialize Integer numbers from string (GSAText2Int), an inverse function fro the NXT Number To String block. However pairing the original block with my GSAText2Int has some pretty serious limitations like: being limited to integers, the size of the resulting text is not optimal and maybe the most unpleasant thing is the fact that each number can have a different size.

The solution is to have some fixed size serialization-deserialization blocks with a compact format. Single precision floating point numbers are representable on 4 bytes (IEEE 754) or 4 characters of space.

 So first I will present a way to serialize a floating point number to a 4 bytes string with GSAFloat2String NXT block.

Inputs:
1. Float Number - actually can be any numeric value

Outputs:
1. Output String - a 4 bytes string

We can concatenate (using NXT Text block) up to 12 serialized floating point number in one string (48bytes) that fits a Bluetooth message!



You can download  GSAFloat2String here.

No comments:

Post a Comment