View unanswered posts | View active topics It is currently Fri Apr 25, 2014 12:18 am






Reply to topic  [ 5 posts ] 
Cant get third movement in task to happen 
Author Message
Rookie

Joined: Sat Mar 10, 2012 4:09 pm
Posts: 4
Post Cant get third movement in task to happen
We are working on a Fire bot for this years robogames and are running into a issue on the TCorner task. The task is only doing the first 2 movements. What are we missing to make the third happen?

Thanks in advance!!!
Mike & Jordan

Code:
#pragma config(Sensor,S1,Sonar1,sensorSONAR)
#pragma config(Sensor,S2,Touch1,sensorTouch)
#pragma config(Sensor,S3,Light1,sensorLightActive)
#pragma config(Sensor,S4,Flame,sensorTouch)
#pragma config(Motor,motorA,motorA,tmotorNormal,openLoop)
#pragma config(Motor,motorB,motorB,tmotorNormal,PIDControl, encoder)
#pragma config(Motor,motorC,motorC,tmotorNormal,PIDControl, encoder)

int C;
float z;

///////
//////
/////
////
///
//
task Extinguish()
{
  // Put textinguish here
}
/////////////
///////////
/////////
///////
task Roomsearch() // room search task
{
  while(SensorValue[Flame] == 0)
  {
    motor[motorB] = 30;
    motor[motorC] = -30; //start search
    wait1Msec(500);

    motor[motorB] = -30;
    motor[motorC] = 30;
    wait1Msec(1000);

    motor[motorB] = 30;
    motor[motorC] = -30;
    wait1Msec(500); //exit room start

    motor[motorB] = -30;
    motor[motorC] = -30;
    wait1Msec(2250);

    motor[motorB] = 30;
    motor[motorC] = -30;
    wait1Msec(600);

    motor[motorB] = 30;
    motor[motorC] = 30;
    wait1Msec(2000);

    }

    return;

  }

//////////////
/////////////
////////////
///////////
//////////
task TCorner()  //the turn corner
{
  nMotorEncoder[motorC] = 0;
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorC] = 400;
  nMotorEncoderTarget[motorB] = 400;
  motor[motorC] = 30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget[motorC] = 480;
  nMotorEncoderTarget[motorB] = 0;
  motor[motorC] = 30;
  motor[motorB] = 0;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoder[motorC] = 0;
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget [motorC] = 400;
  nMotorEncoderTarget [motorB] = 400;
  motor[motorC] = 30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  return;
}

/////////////
///////////
/////////
///////
task main()
{
while(true)
{
  z = SensorRaw[Sonar1] - 23;

  if(SensorRaw(Sonar1) > 70) // Start corner turn
  {
  StartTask(TCorner);
  wait1Msec(10000);
  }

  if(SensorValue(Light1) > 90) // start room search sensitivity was 40
  {
    C = C + 1;//add 1 to count rooms
    StartTask(Roomsearch);
    wait1Msec(7150);
  }

  z = z* 1.5; // aggression

  motor[motorB] = (40 - z);
  motor[motorC] = (40 + z);

 if(SensorValue(Touch1) == 1) //bump turn
  {
  nMotorEncoder[motorC] = 0;
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorC] = 240;
  nMotorEncoderTarget[motorB] = 240;
  motor[motorC] = -30;
  motor[motorB] = -30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget[motorC] = 200;
  nMotorEncoderTarget[motorB] = 200;
  motor[motorC] = -30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  }
 }
}


Sat Mar 10, 2012 4:16 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3116
Location: Rotterdam, The Netherlands
Post Re: Cant get third movement in task to happen
Hi there,

You don't need to use tasks the way you do. You're much better off using a function instead. As for your while loops, try this instead:
Code:
while((nMotorRunState[motorC] != runStateIdle) || (nMotorRunState[motorB] != runStateIdle))


- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Mar 10, 2012 4:21 pm
Profile WWW
Rookie

Joined: Sat Mar 10, 2012 4:09 pm
Posts: 4
Post Re: Cant get third movement in task to happen
That did the trick for the while statment. That way it wull stop if ether or has stopped. As for the tasks - I see what you mean. We have changed the program. I think we are using it in a better way. As for the function I am not sure what you mean. I can't find an example of it in the help files. (Sorry I am new to C). Can you give me an example or direct me to an example?

Again thank for the help so far!!!

Mike
Reno Robot Club

PS... here is a copy of our current code. Any other suggestions are greatly appreciated!!!!

Code:
#pragma config(Sensor,S1,Sonar1,sensorSONAR)
#pragma config(Sensor,S2,Touch1,sensorTouch)
#pragma config(Sensor,S3,Light1,sensorLightActive)
#pragma config(Sensor,S4,Flame,sensorTouch)
#pragma config(Motor,motorA,motorA,tmotorNormal,openLoop)
#pragma config(Motor,motorB,motorB,tmotorNormal,PIDControl, encoder)
#pragma config(Motor,motorC,motorC,tmotorNormal,PIDControl, encoder)

int C = 0;
float z = 0;
bool Flm = false;

///////
//////
/////
////
///
//
task FlameDetect()
{
  while (true)
if(SensorValue(Flame) == 1)
 {Flm = true;
}
}
///////
//////
/////
////
///
//

task main()
{
while(true)
{
  z = SensorRaw[Sonar1] - 23;

  if(SensorRaw(Sonar1) > 60) // Start corner turn
  {
    if(C==3)
    {
  nMotorEncoder[motorC] = 0;
  nMotorEncoder[motorB] = 0;           // fourth room search
  nMotorEncoderTarget[motorC] = 20;
  nMotorEncoderTarget[motorB] = 20;
  motor[motorC] = -30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
  nMotorEncoderTarget[motorC] = 930;
  nMotorEncoderTarget[motorB] = 930;
  motor[motorC] = 30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
  {}
}
  nMotorEncoder[motorC] = 0;      // normal turn corner
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorC] = 450;
  nMotorEncoderTarget[motorB] = 450;
  motor[motorC] = 30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget[motorC] = 420;
  motor[motorC] = 30;
  motor[motorB] = 0;
  while (nMotorRunState[motorC] != runStateIdle)
{}
  nMotorEncoderTarget [motorC] = 350;
  nMotorEncoderTarget [motorB] = 350;
  motor[motorC] = 30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  }

  if(SensorValue(Light1) > 40) // start room search sensitivity was 40
  {
  C = C + 1;//add 1 to count rooms
  StartTask (FlameDetect);
  nMotorEncoder[motorC] = 0;     //    room search
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget [motorC] = 200;
  nMotorEncoderTarget [motorB] = 200;
  motor[motorC] = 20;
  motor[motorB] = 20;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget [motorC] = 210;
  nMotorEncoderTarget [motorB] = 210;
  motor[motorC] = -20;
  motor[motorB] = 20;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget [motorC] = 210;
  nMotorEncoderTarget [motorB] = 210;
  motor[motorC] = 20;
  motor[motorB] = -20;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
StopTask(FlameDetect);
if(Flm == false)    // if flame is false backout
{
  nMotorEncoder[motorC] = 0;
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget [motorC] = 840;
  nMotorEncoderTarget [motorB] = 840;
  motor[motorC] = -30;
  motor[motorB] = -30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget[motorC] = 205;
  nMotorEncoderTarget[motorB] = 205;
  motor[motorC] = -30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget [motorC] = 400;
  nMotorEncoderTarget [motorB] = 400;
  motor[motorC] = 30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
}
}
  z = z* 1.8; // aggression

  motor[motorB] = (40 - z);
  motor[motorC] = (40 + z);

 if(SensorValue(Touch1) == 1) //bump turn
  {
  nMotorEncoder[motorC] = 0;
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorC] = 240;
  nMotorEncoderTarget[motorB] = 240;
  motor[motorC] = -30;
  motor[motorB] = -30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  nMotorEncoderTarget[motorC] = 210;
  nMotorEncoderTarget[motorB] = 210;
  motor[motorC] = -30;
  motor[motorB] = 30;
  while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
{}
  }
 }
}


Sat Mar 10, 2012 7:33 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3116
Location: Rotterdam, The Netherlands
Post Re: Cant get third movement in task to happen
I have not tested this code, but please start using indentation properly. It was very hard to to figure out which code belonged to which "if" or "while" statement. That will make debugging very hard, especially when your programs get bigger.

- Xander

Code:
#pragma config(Sensor,S1,Sonar1,sensorSONAR)
#pragma config(Sensor,S2,Touch1,sensorTouch)
#pragma config(Sensor,S3,Light1,sensorLightActive)
#pragma config(Sensor,S4,Flame,sensorTouch)
#pragma config(Motor,motorA,motorA,tmotorNormal,openLoop)
#pragma config(Motor,motorB,motorB,tmotorNormal,PIDControl, encoder)
#pragma config(Motor,motorC,motorC,tmotorNormal,PIDControl, encoder)

int C = 0;
float z = 0;
bool Flm = false;

///////
//////
/////
////
///
//
task FlameDetect()
{
  while (true)
  {
    if(SensorValue[Flame] == 1)
    {
      Flm = true;
    }
    wait1Msec(3);  // sensors are only polled every 3ms
  }
}
///////
//////
/////
////
///
//

task main()
{
  while(true)
  {
    z = SensorRaw[Sonar1] - 23;

    if(SensorRaw[Sonar1] > 60) // Start corner turn
    {
      if(C == 3)
      {
        nMotorEncoder[motorC] = 0;
        nMotorEncoder[motorB] = 0;           // fourth room search
        nMotorEncoderTarget[motorC] = 20;
        nMotorEncoderTarget[motorB] = 20;
        motor[motorC] = -30;
        motor[motorB] = 30;
        while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
        {
          EndTimeSlice();  // avoid using tight loops, better to stick in a EndTimeSlice() to signal
                           // the firmware that you're waiting for something so it can go do other things
        }

        nMotorEncoderTarget[motorC] = 930;
        nMotorEncoderTarget[motorB] = 930;
        motor[motorC] = 30;
        motor[motorB] = 30;
        while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
        {
          EndTimeSlice();
        }
      }

      nMotorEncoder[motorC] = 0;      // normal turn corner
      nMotorEncoder[motorB] = 0;
      nMotorEncoderTarget[motorC] = 450;
      nMotorEncoderTarget[motorB] = 450;
      motor[motorC] = 30;
      motor[motorB] = 30;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }

      nMotorEncoderTarget[motorC] = 420;
      motor[motorC] = 30;
      motor[motorB] = 0;
      while (nMotorRunState[motorC] != runStateIdle)
      {}

      nMotorEncoderTarget [motorC] = 350;
      nMotorEncoderTarget [motorB] = 350;
      motor[motorC] = 30;
      motor[motorB] = 30;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }
    }

    if(SensorValue(Light1) > 40) // start room search sensitivity was 40
    {
      C = C + 1;//add 1 to count rooms
      StartTask (FlameDetect);
      nMotorEncoder[motorC] = 0;     //    room search
      nMotorEncoder[motorB] = 0;
      nMotorEncoderTarget [motorC] = 200;
      nMotorEncoderTarget [motorB] = 200;
      motor[motorC] = 20;
      motor[motorB] = 20;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }

      nMotorEncoderTarget [motorC] = 210;
      nMotorEncoderTarget [motorB] = 210;
      motor[motorC] = -20;
      motor[motorB] = 20;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }

      nMotorEncoderTarget [motorC] = 210;
      nMotorEncoderTarget [motorB] = 210;
      motor[motorC] = 20;
      motor[motorB] = -20;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }

      StopTask(FlameDetect);
      if(Flm == false)    // if flame is false backout
      {
        nMotorEncoder[motorC] = 0;
        nMotorEncoder[motorB] = 0;
        nMotorEncoderTarget [motorC] = 840;
        nMotorEncoderTarget [motorB] = 840;
        motor[motorC] = -30;
        motor[motorB] = -30;
        while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
        {
          EndTimeSlice();
        }

        nMotorEncoderTarget[motorC] = 205;
        nMotorEncoderTarget[motorB] = 205;
        motor[motorC] = -30;
        motor[motorB] = 30;
        while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
        {
          EndTimeSlice();
        }

        nMotorEncoderTarget [motorC] = 400;
        nMotorEncoderTarget [motorB] = 400;
        motor[motorC] = 30;
        motor[motorB] = 30;
        while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
        {
          EndTimeSlice();
        }
      }
    }

    z = z* 1.8; // aggression

    motor[motorB] = (40 - z);
    motor[motorC] = (40 + z);

    if(SensorValue(Touch1) == 1) //bump turn
    {
      nMotorEncoder[motorC] = 0;
      nMotorEncoder[motorB] = 0;
      nMotorEncoderTarget[motorC] = 240;
      nMotorEncoderTarget[motorB] = 240;
      motor[motorC] = -30;
      motor[motorB] = -30;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }

      nMotorEncoderTarget[motorC] = 210;
      nMotorEncoderTarget[motorB] = 210;
      motor[motorC] = -30;
      motor[motorB] = 30;
      while (nMotorRunState[motorC] != runStateIdle || nMotorRunState[motorB] != runStateIdle)
      {
        EndTimeSlice();
      }
    }
  }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Mar 11, 2012 3:42 am
Profile WWW
Rookie

Joined: Sat Mar 10, 2012 4:09 pm
Posts: 4
Post Re: Cant get third movement in task to happen
Again thanks so much for the help. I see what you mean about the tight loops and indentation. I did not know about the timeslice command. Thanks again for the help!!!!

Mike
Reno Robot Club


Sun Mar 11, 2012 11:36 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 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.