cass
cass

Reputation: 37

how to program vex claw robot to do line following?

I am trying to use lineTrackForTime command on claw robot but it seems like the robot just run over the line and can't detect it.
However when I switch to square robot, the lineTrackForTime command worked.

The following code works on the square bot but not on the claw bot.

#pragma config(StandardModel, "RVW SQUAREBOT")
#pragma config(RenamedStdModelSensor, in1, leftline)
#pragma config(RenamedStdModelSensor, in2, centerline)
#pragma config(RenamedStdModelSensor, in3, rightline)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// already set the line follower sensors and sonar sensor. Didn't rename the Sonar sensor

// set a forward function, will use it with waitInMilliseconds command later
void straight()
{
    startMotor(leftMotor,127);
    startMotor(rightMotor,127);
}

// set a turnright function, will use it with waitInMilliseconds command later

void turnRight()
{
    startMotor(leftMotor,30);
    startMotor(rightMotor,-30);
}

task main()
{
// move the robot out of the start area , go straight and turn right
    straight();
    waitInMilliseconds(2000);
    turnRight();
    waitInMilliseconds(2700);

// after robot out of start area, keep moving forward until meet the dark line
    forward();

//use lineTrackForTime command to make the robot follow the line
    lineTrackForTime(45,505,in1,in2,in3);

//after the line ends, robot will keep move forward until the sonar sensor detect the distance to the wall(less than 30cm), then it stops
    forward();
    untilSonarLessThan();
    stop();
}

How can I make the code work with the claw bot to do the line following?

Upvotes: 0

Views: 431

Answers (1)

ChilliPenguin
ChilliPenguin

Reputation: 715

TLDR: Dont shouldnt use PLTW with the claw robot


Why not?

The PLTW for vex has hardcoded motor ports. These ports are 2 & 3 for movement. The square robot does have motors at ports 2 & 3, making it work. However the claw has motors at ports 1 & 10. As I said, these are hardcoded into the software, and would require changing each line. This basically disables your entire program as the forward function wont work as well.


What to do?

Swap to Natural Language 2. It may not have the lineTrackForTime function exactly, but it offers lineTrackLeft & lineTrackRight which you can utilize as such(taken from documentation if you need the time ability). This language allows for more flexibility, and if done correctly can be used with both robots(while the PLTW is limited to the square robot).

resetTimer(T1);
while(getTimer(T1,milliseconds)< 2000)
{
 lineTrackRight(centerLineFollower, 2100, 63, 0); //centerLineFollower is just utilizes one sensor
}

The nice thing about this is that these commands use setMotorSpeeds(speedSecondary, speedPrimary); instead of having hardcoded values.

I would like to not this would also disable the untilSonarLessThan(); function, but this could be easily reimplemented into your main code as such(taken from the source code),

void untilSonarLessThan(short distance = 30, tSensors sensorPort = dgtl8) { 
    while(SensorValue[sensorPort] > distance || SensorValue[sensorPort] == -1) {
        wait1Msec(1);
    } 
}

Additionally, this disables the stop() command as well; but this should be replaced with return 0;.

For further note, the forward() function is different than in PLTW, but should still work as you intended.


What if you want to still use PLTW?

You could directly modify the natural language to change its motor ports. But this is not recommended. All of the commands are hardcoded, and if you do modify it, it will only work for you, and not to anyone else with the program. This would also disable the clawbot's abilities.

However, if you do want to do this, then right click lineTrackForTime() and click Go to definition/declaration

Step 1

Then modify it to take motor ports like so.

void lineTrackForTime(float trackTime = 5.0, int threshold = 2048, tSensors leftSensorPort = in1, tSensors centerSensorPort = in2, tSensors rightSensorPort = in3, )
{
  float timeStart = ((float)nPgmTime / 1000);

  while(((float)nPgmTime / 1000) - timeStart < trackTime)
  {
    // RIGHT sensor sees dark:
    if(SensorValue(rightSensorPort) > threshold)
    {
      // counter-steer right:
      motor[port1] = 0;
      motor[port10] = 45;
    }
    // CENTER sensor sees dark:
    if(SensorValue(centerSensorPort) > threshold)
    {
      // go straight
      motor[port1] = 45;
      motor[port10] = 45;
    }
    // LEFT sensor sees dark:
    if(SensorValue(leftSensorPort) > threshold)
    {
      // counter-steer left:
      motor[port1] = 45;
      motor[port10] = 0;
    }
    wait1Msec(1);
  }
}

A better way to do this would create a new function and have this within; and just call this function instead of the normal function, however this would be a hassle if editing a lot of functions. But again, I highly recommend changing langauges.

Upvotes: 0

Related Questions