Page 1 of 1

NXT Bluetooth StartProgram Command

Posted: 22 Jun 2011, 08:49
by robotx
Hi,
I'm currently working on a Mindstorms NXT software project where I use a bluetooth connection and direct commands to control the brick.
I got the motor commands to work properly, but now I have problems with the StartProgramm command as it is written in the Mindstorms documentation.

The format of the command should look like this: Byte 0 - 0x00, Byte 1 - 0x00, Byte 2 to 21 - Filename (ASCIIZ String + Null Termination).

I tried quite everything, but I can't get the brick to start a programm. Maybe someone has an example that uses this command? My programming language is JavaScript, if you are intrested in, and my current approach looks like that:

Code: Select all

bytes[0] =   0x0D;                     // Length of the command (LSB)
   bytes[1] =   0x00;                      // Length of the Command (MSB)
   bytes[z+0] = 0x00;                      // Direct Command Mode
   bytes[z+1] = 0x00;                     // Start Program command type
   
   bytes[z+2] = 0x31;   //Filename in Little Endian Format - "Untitled-1" ---> "1-deltitnU"  + Null-Termination
   bytes[z+3] = 0x2D;
   bytes[z+4] = 0x64;
   bytes[z+5] = 0x65;
   bytes[z+6] = 0x6C;
   bytes[z+7] = 0x74;
   bytes[z+8] = 0x69;
   bytes[z+9] = 0x74;
   bytes[z+10] = 0x6E;
   bytes[z+11] = 0x55;   
   bytes[z+12] = 0x00;
Than I convert it into a string, that I send to the brick via bluetooth. I'm just wondering if I miss anything important?
Thanks in advance, bye havanab!

Re: NXT Bluetooth StartProgram Command

Posted: 22 Jun 2011, 10:05
by linusa
The "little endian" doesn't apply for strings (at least I've never seen it in the wild), so you should put the filename "forward". Also you have to add the .rxe extension if not present.

So max filename length is 15 + 1 + 3 (name + "." + "rxe"). Then the 0x00 terminator.

In cases where things aren't working, you should request the NXT's status byte reply, and evaluate the error message. I don't know the error codes off my head, but I guess in your case, you should've received something like "file not found", or maybe even "malformed message".

Re: NXT Bluetooth StartProgram Command

Posted: 22 Jun 2011, 11:01
by timpattinson
robotx wrote: My programming language is JavaScript,
Just interested: how do you use Bluetopth in Javascript?
-tim

Re: NXT Bluetooth StartProgram Command

Posted: 22 Jun 2011, 13:17
by robotx
Thank you very much, linusa. Just tested it and works fine now! Missing the file extension, what a silly mistake. ;)

@timpattinson: I use a JS framework running on an bluetooth handy (Palm WebOS). :)

Edit:
General question about the motor movement:
How can I send negative values like -60 to the motor power signal? Power range is -100 to 100 regarding to the documentation.
I want to get a backward movement and thought a two's complement value will do it. Or is the rotation direction depending on some other values? ;)

Re: NXT Bluetooth StartProgram Command

Posted: 22 Jun 2011, 15:38
by linusa
robotx wrote: General question about the motor movement:
How can I send negative values like -60 to the motor power signal? Power range is -100 to 100 regarding to the documentation.
I want to get a backward movement and thought a two's complement value will do it. Or is the rotation direction depending on some other values? ;)
We form the power byte for the SetOutputState is formed this way:

Code: Select all

content(2)    = dec2wordbytes(Power, 1, 'signed'); % convert to signed byte
In this case, this evaluates to this line

Code: Select all

bytes = typecast(int8(val), 'uint8');
All MATLAB code (val input, bytes output).

If you're stuck, make an NXC or NXT-G program that sets a negative power and keeps spinning the motor. Run that program. Then use the GetOutputState command. Retrieve the current motor state. Look at the power byte. It has the right encoding.

Re: NXT Bluetooth StartProgram Command

Posted: 22 Jun 2011, 15:40
by linusa
Oh, and you could of course just experiment. Values of 0 - 100 are positive motor values, that's for sure. And somewhere between 101 and 255 there are the negative 100. So just go ahead and try it! I believe the answer was something like "255 - power" or "127 + power", if you want power to be negative. Look up how a signed byte is expressed in binary.