View unanswered posts | View active topics It is currently Thu Sep 18, 2014 2:52 pm






Reply to topic  [ 7 posts ] 
Bluetooth remote control 
Author Message
Rookie

Joined: Mon Oct 10, 2011 4:46 pm
Posts: 15
Post Bluetooth remote control
I am trying to make a set of programs that will allow you to remote control an NXT with another NXT but I am stuck
I don't know how to send messages on the NXT the documentation is confusing can anyone help?
Code:
#pragma config(Sensor, S1,     Touch1,              sensorTouch)
#pragma config(Motor,  motorA,          motorA,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorB,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorC,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main
{
  while(SensorValue(touchSensor) == 0)    // While the Touch Sensor is ! pressed
  {
  }
  while(SensorValue(touchSensor) == 1)    // While the Touch Sensor is pressed
  {
  }
  nMotorEncoder[motorA]=0; //reset all encoder valus after the totch sensor is bumped
  nMotorEncoder[motorB]=0;
  nMotorEncoder[motorC]=0;
  while(1==1) //main progam
  {
    nMotorEncoder[motorB]=
  }
}
/*
1. get the encoder values
2. send them to slave NXT
3. Repeat
*/


Thu Nov 03, 2011 12:14 pm
Profile
Novice

Joined: Thu Apr 17, 2008 6:04 am
Posts: 70
Location: Plymouth, Indiana, USA
Post Re: Bluetooth remote control
Hi ferret_guy,
I recently learned a little about bluetooth and saw that your post was unanswered.
I'm asuming you are using RobotC 3.something.

The command to send is:
cCmdMessageWriteToBluetooth(pData, nLength, nQueueID)

The command to receive is:
cCmdMessageRead(pData, nLengthToRead, nQueueID)

Where:
pData is an array of unsigned bytes (in other words, a list of numbers where each number is between 0 to 255 inclusive).
nLength or nLengthToRead is the number of elements in the array.
nQueueID is a mailbox number. -Like 1, 2, or 3.

So in its simplest form:

On the sending NXT:
ubyte valueToSend[2]; //Declares an Unsigned BYTE array with 2 elements
valueToSend[0] = 10; //Value of first element (arrays start at zero).
valueToSend[1] = 23; //Value of second element.
cCmdMessageWriteToBluetooth(valueToSend, 2, 1);
---The above 1 or 3 lines are in a while loop or something to keep them alive.

On the recieving NXT:
ubyte valueReceived[2];
valueReceived[0] = 0;
valueReceived[1] = 0;
cCmdMessageRead(valueReceived, 2, 1);
nxtDisplayStringAt(2,50,"%d %d",valueReceived[0], valueReceived[1]);
---The above TWO lines are in a while loop or something to keep them alive.

For an RC car, the array being sent would contain speed and steering information (I'm sure you knew that).

The two NXTs must have their bluetooth connected manually (from the NXT's display and buttons) before running the programs. -Let us know if you need a procedure for that.

Also let us know if any of the above does not make sense.
There is a lot more to bluetooth (judging by the list of available commands), but hopefully this will get you started.

Dave

_________________
Dad (Dave) and Adam


Wed Dec 21, 2011 2:15 am
Profile
Rookie

Joined: Thu Dec 22, 2011 6:33 pm
Posts: 7
Post Re: Bluetooth remote control
I'm trying the same, but stuck too :(
The NXTs are connected, but the receiving NXT only receives zero.
I'm sending a value from the other NXT every 1 ms.

The RobotC manual isn't really clear about how it works :(
And they removed some really simple massaging functions (probably because of removing RCX support?)

Hope someone can help us with it, maybe someone can write some very simple working sample code.
I'm sure it would be helpful to many people which are trying to do the same.


Thu Dec 22, 2011 6:44 pm
Profile
Novice

Joined: Thu Apr 17, 2008 6:04 am
Posts: 70
Location: Plymouth, Indiana, USA
Post Re: Bluetooth remote control
Hi Sander,
Could you post a sample of your code? I wonder if the 1 ms is too fast.

Below is a working sample (at least it worked for me). Ran in RobotC 3.04. It just sends two unchanging numbers from one NXT to another. It would be a small step to make the numbers change within the loop.

On the sending NXT:
task main()
{
while(true)
{
ubyte valueToSend[2]; //Declares an Unsigned BYTE array with 2 elements
valueToSend[0] = 10; //Value of first element (arrays start at zero).
valueToSend[1] = 23; //Value of second element.
cCmdMessageWriteToBluetooth(valueToSend, 2, 1);
}
}

On the recieving NXT:
task main()
{
ubyte valueReceived[2];
valueReceived[0] = 0;
valueReceived[1] = 0;
while(true)
{
cCmdMessageRead(valueReceived, 2, 1);
nxtDisplayStringAt(2,50,"%d %d",valueReceived[0], valueReceived[1]);
}
}

Don't forget to manually connect the bluetooth of the NXTs.

Dave

_________________
Dad (Dave) and Adam


Thu Dec 22, 2011 7:04 pm
Profile
Rookie

Joined: Mon Oct 10, 2011 4:46 pm
Posts: 15
Post Re: Bluetooth remote control
thanks so mutch this is what I have and it works awesome
On the car
Code:
#pragma config(Sensor, S1,     Touch1,              sensorTouch)
#pragma config(Motor,  motorA,          motorA,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorB,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorC,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
task main()
{
ubyte valueReceived[4];
valueReceived[0] = 0;
valueReceived[1] = 0;
valueReceived[2] = 0;
valueReceived[3] = 0;
while(true)
   {
   cCmdMessageRead(valueReceived, 4, 1);
   motor[motorA]=(valueReceived[0]-100);
   motor[motorB]=(valueReceived[1]-100);
   motor[motorC]=(valueReceived[2]-100);
   }
}

On the controller
Code:
#pragma config(Sensor, S1,     Touch1,              sensorTouch)
#pragma config(Motor,  motorA,          motorA,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorB,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorC,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int A = 0;
int B = 0;
int C = 0;
int T = 0;


task main
{
  while(SensorValue(Touch1) != 0)    // While the Touch Sensor is ! pressed
  {
  }
  nMotorEncoder[motorA]=0; //reset all encoder valus after the totch sensor is bumped
  nMotorEncoder[motorB]=0;
  nMotorEncoder[motorC]=0;
  while(1==1) //main progam
  {
  A = (nMotorEncoder[motorA]+100);
   B = (nMotorEncoder[motorB]+100);
   C = (nMotorEncoder[motorC]+100);
   T = SensorValue(Touch1);

      ubyte valueToSend[4]; //Declares an Unsigned BYTE array with 4 elements
      valueToSend[0] = A; //Value of first element (arrays start at zero).
      valueToSend[1] = B; //Value of second element.
      valueToSend[2] = C; //Value of third element.
      valueToSend[3] = T; //Value of fourth element.
      cCmdMessageWriteToBluetooth(valueToSend, 4, 1);
   }

  }


Fri Dec 23, 2011 2:51 pm
Profile
Novice

Joined: Thu Apr 17, 2008 6:04 am
Posts: 70
Location: Plymouth, Indiana, USA
Post Re: Bluetooth remote control
Hi ferret_guy,
Glad to hear you had success!

Interesting (at least to me) that for all it's complexity, bluetooth only took one line to accomplish.

Adding and subtracting 100 to be able to pass a negative number (with the otherwise positve ubyte) was clever.

Merry Christmas,
Dave

_________________
Dad (Dave) and Adam


Sat Dec 24, 2011 6:39 am
Profile
Rookie

Joined: Thu Dec 22, 2011 6:33 pm
Posts: 7
Post Re: Bluetooth remote control
Thank you very much! Your example works great.
I'm using two NXTs. One will be in a car, the other one will be a motioncontroller (using HiTechnic Accelerometer).
Now I need to pass on the values from the motioncontroller NXT to the car. I think it will be a bit like Mario Kart on the Nintendo Wii.

Merry christmas!


Sat Dec 24, 2011 5:34 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.