ROBOTC.net forums
http://robotc.net/forums/

Marble Sorter With Line Follower Help
http://robotc.net/forums/viewtopic.php?f=11&t=11110
Page 1 of 1

Author:  whs001 [ Sun May 10, 2015 2:29 pm ]
Post subject:  Marble Sorter With Line Follower Help

Hello,

For my school project I need to make a marble sorter that automatically sorts 3 different types of marbles (Clear, Metal, and Wooden). In my design I have a gate using a servo, 2 conveyor belts that combined use a total of 3 motors, a line follower, and a bump switch. In my code I am multitasking to allow the belts and gate to operate at the same time, but I came across an issue. In my task main() I have 1 if statement which has 2 sub-if statements. When the first If statement runs, I want to stop the task which runs the first conveyor belt so my line follower can get an accurate reading to then run one of the 2 sub-if statements. My task does not stop when I use the stopTask() command. A friend told me to just make another task which will stop the task that controls my first belt, but that did not work. He then told me to have it manually stop the first belt (So that task does not have stopTask(taskname), it has the stopMotor(), wait(), and startMotor() commands), but that also did not work. Can someone here please help me out to see why this one section of the code will not run?

Code:
  #pragma config(Sensor, in1,    linefollower,   sensorLineFollower)
#pragma config(Sensor, dgtl1,  bumpswitch,     sensorTouch)
#pragma config(Motor,  port1,           belt1a,        tmotorVex393_HBridge, openLoop)
#pragma config(Motor,  port2,           gate,          tmotorServoStandard, openLoop)
#pragma config(Motor,  port3,           belt1,         tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           belt2,         tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port10,          flashlight,    tmotorVexFlashlight, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int m;   //Marble Count




task third()      //Controls Belt 1, which has 2 motors moving it (belt1 and belt1a).
{
while(1==1)
{
   startMotor(belt1, -27);
   startMotor(belt1a, -27);
   wait(120);
   stopMotor(belt1);
   stopMotor(belt1a);
}
}

task four()         //Stops Belt1 and Belt1a
{


       stopMotor(belt1);
      stopMotor(belt1a);
      wait(.5);
      startMotor(belt1, 0);
      startMotor(belt1a, 0);

}


task secondary()   // Controls the Gate
{
   while(m <= 12)
   {
      setServo(gate, 35);
      wait(.1);
      setServo(gate, 0);
      m = m + 1;
      wait(5.5);
}

}

task main()   // Controls the Line Follower and Belt 2
{
untilBump(bumpswitch);
startTask(secondary);
startTask(third);
while(1==1)
{

   {
   if(SensorValue[linefollower]<2700)   //Clear Marble has a value that is greater than 2700.
   {
      startTask(four);
      if(SensorValue[linefollower]>1900 && SensorValue[linefollower]< 2400 && SensorValue[linefollower]>2600)   // Steel
      {
         startTask(third);
         startMotor(belt2, 55);
         wait(.5);
         stopMotor(belt2);
         wait(3.5);
         startMotor(belt2, -55);
         wait(.5);
         stopMotor(belt2);
      }
      if(SensorValue[linefollower]>600 && SensorValue[linefollower]<1800)   // Wood
      {
         startTask(third);
         startMotor(belt2, -55);
         wait(.5);
         stopMotor(belt2);
         wait(3.5);
         startMotor(belt2, 55);
         wait(.5);
         stopMotor(belt2);
      }
   }
   }
}
}

Author:  jpalnick [ Mon Jun 22, 2015 9:54 am ]
Post subject:  Re: Marble Sorter With Line Follower Help

You may have already solved this issue. However, this is a perfect example of how multitasking is not always the solution. There are times where multitasking can help, but this is not one of them.

In this particular case, you should look at your process. Is there anything that can't happen out of a sequential process order? If you don't need to process multiple things at the same time, there is almost never a reason to use multitasking.

From my understanding of your code and problem, you could simply have a single task with a single loop. In this one loop, you constantly check for a marble. When you detect a marble, you execute a specific set of commands for that marble type. In this case you could use something like the psudocode below as a base to work from.

Code:
#pragma config(Sensor, in1,    linefollower,   sensorLineFollower)
#pragma config(Sensor, dgtl1,  bumpswitch,     sensorTouch)
#pragma config(Motor,  port1,           belt1a,        tmotorVex393_HBridge, openLoop)
#pragma config(Motor,  port2,           gate,          tmotorServoStandard, openLoop)
#pragma config(Motor,  port3,           belt1b,         tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           belt2,         tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port10,          flashlight,    tmotorVexFlashlight, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void setBelt1Speed(int speed)
{
   startMotor(belt1a, speed);
   startMotor(belt1b, speed);
}

task main()   // Controls the Line Follower and Belt 2
{
   untilBump(bumpswitch);
   
   setBelt1Speed(-27); // start belt 1
   
   while (1==1)
   {
      if (detected marble)
      {
         if (clear marble)
         {
            setBelt1Speed(0); // stop belt 1
            // do what you need to do for a clear marble
            setBelt1Speed(-27); // start belt 1
         }
         else if (steel marble)
         {
            setBelt1Speed(0); // stop belt 1
            // do what you need to do for a steel marble
            setBelt1Speed(-27); // start belt 1
         }
         else if (wood marble)
         {
            setBelt1Speed(0); // stop belt 1
            // do what you need to do for a wood marble
            setBelt1Speed(-27); // start belt 1
         }
      }

      wait(2); //add a slight pause as there is no point checking the sensor(s) at max speed
   }

}

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/