Coding Issues with my Robot

Ok, so I am getting a robot to follow a line and stop at an intersection. I am having trouble with the code for it stopping at an intersection. I don’t know why but the task intersection doesn’t work. The robot can still follow the line and the emergency stop still works when I compile and download it.

task intersection()
{
while(true)
{
if(SensorValue(lineFollowerRIGHT) > 2750)
if(SensorValue(lineFollowerCENTER) > 2708)
if(SensorValue(lineFollowerLEFT) > 2810)
{
stopMotor(port2);
stopMotor(port3);
wait(3);
}
}
}

task e_stop()
{
while(true)
{
if(SensorValue(dgtl1) == 1)
{
stopAllTasks();
}
wait1Msec(10); // this prevents the current task from hogging the CPU
}
}

task main()
{
startTask(e_stop);
startTask(intersection);

wait1Msec(500);          // The program waits for 500 milliseconds before continuing.

int threshold = 505; /* found by taking a reading on both DARK and LIGHT /
/
surfaces, adding them together, then dividing by 2. */
while(true)
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
displayLCDCenteredString(0, “LEFT CNTR RGHT”); // Display |
displayLCDPos(1,0); // Sensor |
displayNextLCDNumber(SensorValue(lineFollowerLEFT)); // Readings |
displayLCDPos(1,6); // to LCD. |
displayNextLCDNumber(SensorValue(lineFollowerCENTER)); // |
displayLCDPos(1,12); // L C R |
displayNextLCDNumber(SensorValue(lineFollowerRIGHT)); // x x x |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+

// RIGHT sensor sees dark:
if(SensorValue(lineFollowerRIGHT) > 2750)
{
  // counter-steer right:
  motor[leftMotor]  = 63;
  motor[rightMotor] = 0;
}
// LEFT sensor sees dark:
if(SensorValue(lineFollowerLEFT) > 2810)
{
  // counter-steer left:
  motor[leftMotor]  = 0;
  motor[rightMotor] = 63;
}
  // CENTER sensor sees dark:
if(SensorValue(lineFollowerCENTER) > 2708)
{
  // go straight
  motor[leftMotor]  = 33;
  motor[rightMotor] = 33;
}

}
}

Welcome to VexForum @lemertre! I am going to give you my speculation on why your robot isn’t working. Btw for the future, placing code in ``` around the beginning and end formats your code nicely, so that way it isn’t as much of a mess.

I am going to assume that there aren’t any syntax errors.

(Random thought simply for better code) Typically in these programs, the threshold is used like this:

if(SensorValue(lineFollowerCENTER) > threshold)

That way if the values the line sensors detect change or you use a different robot or sensor or something, you don’t have to change all the values in your code, you can just change the single variable. If you don’t want to code it that way, that is fine, but I would take out the threshold variable if you aren’t using it.

Code explanation so that I understand it (also known as thinking out loud in text):
The task intersection checks if all the line sensors are triggered (more or less). The code isn’t the easiest to read, but it looks correct for this part. If they are all triggered, than the code stops the motors and waits 3 seconds before checking again. If I am interpreting the stopMotor command correctly, it simply stops the motor or sets the voltage to 0. (this is my source on that). That is all well and good. The main task checks if the line sensors are triggered and then adjusts the motor speeds accordingly. The last sensor that actually checks is the center line sensor. Since the e_stop function is working, you shouldn’t need to worry about that.

My guess as to why this isn’t working is as follows:
Assuming I understand the stopMotor command correctly, it simply stops the motor. Because the intersection task and the main task are both running at the same time, when it detects all three, the intersection tells the motors to stop and then waits three seconds. At the same time the main task detects all three and tells both motors to move forward (since the center sensor check is last). Cortex does whatever, probably cutting off power to the motors. The next instant, the intersection code is waiting, but the main task is not waiting and it tells the motors to go forward. Bam! before the weight, air resistance or whatever forces can influence the robot enough to start it slowing down, the motors are already moving forward again, carrying the robot through the intersection before the intersection task kicks back in.

How would I fix this (if my previous guess was correct)?:
I would not use a seperate task for the intersection code, because to me it seems unnecessary. I would put the intersection code at the end of the infinite loop in task main, and then work from there. There are other methods that would work though.

Hopefully this helps!

5 Likes

You were correct with your guess on what is wanting to happen. I’ll try that as soon as possible thank you for the response!

1 Like

So, now I have to have a sonar see something that is put in front of the robot, stop, then once the object is gone it must start again. It still has to follow the lines and all previous rules. The issue isn’t with the sonar, but it’s the fact that it doesn’t follow the black line anymore. It just goes straight.

#pragma config(Sensor, in1,    lineFollowerRIGHT, sensorLineFollower)
#pragma config(Sensor, in2,    lineFollowerCENTER, sensorLineFollower)
#pragma config(Sensor, in3,    lineFollowerLEFT, sensorLineFollower)
#pragma config(Sensor, dgtl1,  button,         sensorTouch)
#pragma config(Sensor, dgtl2,  ,               sensorSONAR_inch)
#pragma config(Motor,  port2,           rightMotor,    tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*----------------------------------------------------------------------------------------------------*\
|*                             	    - Triple Sensor Line Tracking -                                   *|
|*                                      ROBOTC on VEX 2.0 CORTEX                                      *|
|*                                                                                                    *|
|*  This program uses 3 VEX Line Follower Sensors to track a black line on a light(er) surface.       *|
|*  There is a two second pause at the beginning of the program.                                      *|
|*                                                                                                    *|
|*                                        ROBOT CONFIGURATION                                         *|
|*    NOTES:                                                                                          *|
|*    1)  Reversing 'rightMotor' (port 2) in the "Motors and Sensors Setup" is needed with the        *|
|*        "Squarebot" mode, but may not be needed for all robot configurations.                       *|
|*    2)  Lighting conditions, line darkness, and surface lightness change from place to place,       *|
|*        so the value of 'threshold' may need to be changed to better suit your environment.         *|
|*                                                                                                    *|
|*    MOTORS & SENSORS:                                                                               *|
|*    [I/O Port]          [Name]              [Type]                [Description]                     *|
|*    Motor  - Port 2     rightMotor          VEX 3-wire module     Right side motor                  *|
|*    Motor  - Port 3     leftMotor           VEX 3-wire module     Left side motor                   *|
|*    Analog - Port 1     lineFollowerRIGHT   VEX Light Sensor      Front-right, facing down          *|
|*    Analog - Port 2     lineFollowerCENTER  VEX Light Sensor      Front-center, facing down         *|
|*    Analog - Port 3     lineFollowerLEFT    VEX Light Sensor      Front-left, facing down           *|
\*-----------------------------------------------------------------------------------------------4246-*/


//+++++++++++++++++++++++++++++++++++++++++++++| MAIN |+++++++++++++++++++++++++++++++++++++++++++++++
task e_stop()
{
   while(true)
   {
      if(SensorValue(dgtl1) == 1)
      {
         stopAllTasks();
      }
      wait1Msec(10);  // this prevents the current task from hogging the CPU
   }
}

task main()
{
	startTask(e_stop);

	wait1Msec(500);          // The program waits for 500 milliseconds before continuing.

  int threshold = 505;      /* found by taking a reading on both DARK and LIGHT    */
  
  motor[leftMotor]  = 33;
  motor[rightMotor] = 33;
  
  while(true)
  {
  	if(SensorValue(dgtl1) == 1)
    {
      motor[leftMotor]  = 33;
    	motor[rightMotor] = 33;
    }
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
    displayLCDCenteredString(0, "LEFT  CNTR  RGHT");        //  Display   |
    displayLCDPos(1,0);                                     //  Sensor    |
    displayNextLCDNumber(SensorValue(lineFollowerLEFT));    //  Readings  |
    displayLCDPos(1,6);                                     //  to LCD.   |
    displayNextLCDNumber(SensorValue(lineFollowerCENTER));  //            |
    displayLCDPos(1,12);                                    //  L  C  R   |
    displayNextLCDNumber(SensorValue(lineFollowerRIGHT));   //  x  x  x   |
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
    // RIGHT sensor sees dark:
    if(SensorValue(lineFollowerRIGHT) > 2750)
    {
      // counter-steer right:
      motor[leftMotor]  = 60;
      motor[rightMotor] = 0;
    }
    // LEFT sensor sees dark:
    if(SensorValue(lineFollowerLEFT) > 2810)
    {
      // counter-steer left:
      motor[leftMotor]  = 0;
      motor[rightMotor] = 60;
    }
      // CENTER sensor sees dark:
    if(SensorValue(lineFollowerCENTER) > 2708)
    {
      // go straight
      motor[leftMotor]  = 25;
      motor[rightMotor] = 25;    
    }
		if(SensorValue(dgtl2) < 6)
		{
		stopMotor(port2);
		stopMotor(port3);
		}
		if(SensorValue(lineFollowerRIGHT) > 2750)
		if(SensorValue(lineFollowerCENTER) > 2708)
		if(SensorValue(lineFollowerLEFT) > 2810)
		{
		stopMotor(port2);
		stopMotor(port3);
		wait(3);
		}
	}
}

You might want to read this old post.

Not exactly your problem (jittering motors that is) but you are setting the same motors multiple times in the while loop and that’s most likely the issue.

4 Likes

Ok, so I could see how that is the issue but how do I get around that issue without leaving something out?