NXT Bluetooth StartProgram Command

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
robotx
Posts: 2
Joined: 21 Jun 2011, 15:36

NXT Bluetooth StartProgram Command

Post 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!
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXT Bluetooth StartProgram Command

Post 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".
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXT Bluetooth StartProgram Command

Post by timpattinson »

robotx wrote: My programming language is JavaScript,
Just interested: how do you use Bluetopth in Javascript?
-tim
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
robotx
Posts: 2
Joined: 21 Jun 2011, 15:36

Re: NXT Bluetooth StartProgram Command

Post 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? ;)
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXT Bluetooth StartProgram Command

Post 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.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXT Bluetooth StartProgram Command

Post 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.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 18 guests