Tuesday, March 13, 2012

NXT2 Thin Serial (python3.x) Wrapper

Probably I should name it: Yet Another NXT Wrapper. I often use python as prototyping language, so I decided to give it a shot, create my own thin wrapper around the NXT functions. I am using Bluetooth connections as serial ports, and for serial communication the great pyserial library.

Today I just want to share this wrapper with the possible users, there is no documentation yet, I will spend more time extending in case more people become interested in it.

I use this wrapper together with my conversion blocks to command the NXT. I am looking forward to receive feedback on this.

Thursday, March 1, 2012

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

In my last post, I presented a way of serializing one or multiple numbers to string GSAFloat2String. As may you already expected, I will provide you today with the counter part or mirror function block GSAString2Float.

This block provide us with the possibility to deserialize a string or a portion of a string (previously serialized by GSAFloat2String or any other means to have the IEEE 754 format contained in the string) to a floating point number.

1. Input String - any string containing IEEE 754 blocks
2. Value Offset - the offset of the IEEE 754 block within the Input String.
3. Default Value - the value provided in case of conversion errors

1. Output value - a single point precision floating number

In case of strings containing multiple numbers we can de-serialize all one by one providing the offset for each number within the string. Remember each value is encoded on 4 bytes (IEEE 754).

You can download  GSAString2Float here.

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.

1. Float Number - actually can be any numeric value

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.

Monday, February 27, 2012

NXT Blocks - Conversion Utils - Text to Int (GSAText2Int)

Now that we have means of manipulating strings (splitting, concatenating, etc.) I started to think on ways of expanding the usage of strings. One usage is for storage and transport of other type of data. So next come to mind is to build ways to serialize and un-serialize different kind of data.

We already have means to serialize numbers within the NXT software: the Number To Text Building Block. So I decided the inverse of this Block is needed: Text To Int (GSAText2Int).

Practically this Block is capable to convert a a string containing a number in decimal notation to an integer, given the string and the starting offset of the number. The end of the number is marked by the first non numeric character.

1. Input String
2. Offset
3. Default (in case conversion fails this number will be presented 
at the output)

1. Output Value
2. New Offset (this is the point where our number ends)

Input String = "12345", Offset = 0 => Number = 12345, New Offset = 5
Input String = "12345.678", Offset = 0 => Number = 12345, New Offset =5
Input String = "12345.678", Offset = 3 => Number = 45, New Offset = 5

Notice this Block converts integers so the '.' (decimal point) is treated as non numeric.

You can download GSAText2Int here.

NXT Blocks - String Utils - String Splitting (GSAStringSplit)

Today I want to present another block from the String manipulation family, named GSAStringSplit.

This block will allow us to split a string (text) in two substrings.

1. Input String - the source string
2. Split Point Index - the start position of the second string

1. Output String 1
2. Output String 2

Example usage:

the Output String 1 = "ab" and Output String 2 = "cde".

You can download GSAStringSplit here.

NXT Blocks - String utils - Sub String (GSASubString)

I must confess, I am pretty new with both LEGO and Mindstorms. In searching for a platform to build some of my robot tests I opted for this ingenious kit named LEGO MINDSTORMS® NXT 2.0 [8547].

While trying to write my programs on the LEGO's visual platform I suddenly fall in need of more sophisticated Building Blocks. After a few days of trial and error I found a way to create such of blocks. But there is no joy without sharing no? :)

The first block to share is the GSASubString block from the String manipulation family. This block practically allows us to extract a substring from another string (text).

1. Input String - the source string
2. Offset - the start position of the substring
3. Length - the substring length

1. Substring - the resulting substring

Example usage:

The example returns "bc".

You can download the GSASubString here.