View unanswered posts | View active topics It is currently Sun Apr 20, 2014 11:55 am






Reply to topic  [ 4 posts ] 
BT? 
Author Message
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post BT?
Code:
#pragma config(Sensor, S1,     touch1,         sensorTouch)
#pragma config(Motor,  motorC,          FB,            tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,          LR,            tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//


task main()
{

 const int kMaxSizeOfMessage = 2;
 const TMailboxIDs kQueueID = mailbox1;
 ubyte nXmitBuffer[kMaxSizeOfMessage];




 typedef struct
 {
  int FowardBack;
  int LeftRight;
  bool Tou1;
 }joystickNXT;
 joystickNXT XY;
 XY.ForwardBack = 0;
 XY.LeftRight = 0;
 XY.Tou1 = false;
 
 
 setBluetoothOn();
 setBluetoothVisibility(true);
 nxtDisplayCenteredTextLine(3, "~~~~~~~~~~~~~~~~~~~");
 nxtDisplayCenteredTextLine(4, "Welcome to the user");
 nxtDisplayCenteredTextLine(5, "interface Board!");
 wait1Msec(2000);
 nMotorEncoder[FB] = 0;
 nMotorEncoder[LR] = 0;
 if(touch1)
 {
  XY.tou1 = true;
 }
 else
 {
  XY.tou1 = false;
 }
 StartTask(color12);
 while(true)
 {
  XY.FowardBack = nMotorEncoder[FB];
  XY.LeftRight = nMotorEncoder[LR];
 
  if(FowardBack < 0)
 
  {
   
 
   strncat("-", "%d", XY.ForwardBack, 1);
  }
  if(LeftRight < 0)
 
  {
   
 
   strncat("-", "%d", XY.LeftRight, 1);
  } 
 
  }
  if(bBTBusy)
  {}
  else
  {
   nXmitBuffer[0] = XY.FowardBack;
   nXmitBuffer[1] = XY.LeftRight;
   cCmdMessageWriteToBluetooth(0, nXmitBuffer, kMaxSizeOfMessage, kQueueID);
   nXmitBuffer[0] = XY.Tou1;
   cCmdMessageWriteToBluetooth(0, nXmitBuffer, kMaxSizeOfMessage, kQueueID);
   wait1Msec(50);
  }
 }
}


Is this correct?
If not, what's the right way?
Also, What's the recieving NXT's code? :cry:

_________________
string Robored = "Awesome"
~~Neil Balch~~


Sun Dec 30, 2012 12:57 pm
Profile WWW
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: BT?
What is task color12 supposed to do? You call it in your code, but I can't see the declaration.


Sun Dec 30, 2012 2:34 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: BT?
You have several problems in your code. I made some corrections below:
Code:
#pragma config(Sensor, S1,     touch1,         sensorTouch)
#pragma config(Motor,  motorC,          FB,            tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,          LR,            tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//
// If you want the joystickNXT definition be visible to other functions and tasks,
// you need to declare it outside of task main.
//
typedef struct
{
    int FowardBack;
    int LeftRight;
    bool Tou1;
} joystickNXT;

task main()
{
    const int kMaxSizeOfMessage = 2;
    const TMailboxIDs kQueueID = mailbox1;
    ubyte nXmitBuffer[kMaxSizeOfMessage];
    char textString[20];
    //
    // You can do in-line initialization when declaring a variable.
    //
    joystickNXT XY = {0, 0, false};

    setBluetoothOn();
    setBluetoothVisibility(true);
    nxtDisplayCenteredTextLine(3, "~~~~~~~~~~~~~~~~~~~");
    nxtDisplayCenteredTextLine(4, "Welcome to the user");
    nxtDisplayCenteredTextLine(5, "interface Board!");
    wait1Msec(2000);

    nMotorEncoder[FB] = 0;
    nMotorEncoder[LR] = 0;
    //
    // I assume you are checking the state of the touch sensor. If so,
    // it should be done using SensorValue[touch1] instead of just
    // testing touch1. touch1 is the sensor port ID. It doesn't tell you
    // the state of the touch sensor. Better yet, use the code below.
    //
    XY.tou1 = SensorValue[touch1] != 0;
    //
    // What does color12 task do? If you can, avoid using tasks unless you
    // really know what you are doing.
    //
    StartTask(color12);

    while(true)
    {
        XY.FowardBack = nMotorEncoder[FB];
        XY.LeftRight = nMotorEncoder[LR];
 
        if(FowardBack < 0)
        {
            // This is not how strncat is used. The first parameter should be
            // a char array variable. In fact, if I guess it correctly, you shouldn't
            // use strncat at all. You probably meant sprintf.
            // strncat("-", "%d", XY.ForwardBack, 1);
            //
            // Actually, I am confused. I am not sure what you are trying to do with concatenating the string.
            // There is no use of it in the code below???
            //
            sprintf(textString, "- %d", XY.ForwardBack);
        }

        if (LeftRight < 0)
        {
            //
            // Again, this is probably wrong.
            // strncat("-", "%d", XY.LeftRight, 1);
            //
            // Again, I am not sure what you are trying to do here.
            //
            sprintf(textString, "- %d", XY.LeftRight);
        }

        if(!bBTBusy)
        {
            //
            // nXmitBuffer is an array of ubytes. XY.ForwardBack is an integer.
            // This will cause truncation. So I am not sure what you are doing here.
            //
            nXmitBuffer[0] = XY.FowardBack;
            nXmitBuffer[1] = XY.LeftRight;
            cCmdMessageWriteToBluetooth(0, nXmitBuffer, kMaxSizeOfMessage, kQueueID);
            nXmitBuffer[0] = XY.Tou1;
            cCmdMessageWriteToBluetooth(0, nXmitBuffer, kMaxSizeOfMessage, kQueueID);
            wait1Msec(50);
        }
    }
}

Also, you have one extra closing brace, so I am making a guess on which one to remove. I don't think the above code will work even after my correction because I don't know its intention. At least it should compile now.


Sun Dec 30, 2012 3:58 pm
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post Re: BT?
Thanks.

_________________
string Robored = "Awesome"
~~Neil Balch~~


Sun Dec 30, 2012 10:37 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.