0

我正在尝试lineTrackForTime在爪形机器人上使用命令,但似乎机器人刚刚越过线路并且无法检测到它。
但是,当我切换到方形机器人时,该lineTrackForTime命令起作用了。

以下代码适用于方形机器人,但不适用于爪形机器人。

#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();
}

如何使代码与爪机器人一起工作以执行以下行?

4

1 回答 1

0

TLDR:不应将 PLTW 与爪式机器人一起使用


为什么不?

vex 的 PLTW 具有硬编码的电机端口。这些端口是 2 和 3 用于移动。方形机器人在端口 2 和 3 处确实有电机,使其工作。然而,爪在端口 1 和 10 处有电机。正如我所说,这些是硬编码到软件中的,并且需要更改每一行。这基本上会禁用您的整个程序,因为转发功能也无法正常工作


该怎么办?

切换到自然语言 2。它可能不lineTrackForTime完全具有该功能,但它提供了lineTrackLeft&供lineTrackRight您使用(如果您需要时间能力,请从文档中获取)。这种语言具有更大的灵活性,如果操作正确,可以与两个机器人一起使用(而 PLTW 仅限于方形机器人)。

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

这样做的好处是这些命令使用 setMotorSpeeds(speedSecondary, speedPrimary);而不是硬编码值。

我不希望这也会禁用该untilSonarLessThan();功能,但这可以很容易地重新实现到您的主代码中(取自源代码),

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

此外,这也会禁用该stop()命令;但这应该替换为return 0;.

进一步说明,该forward()功能与 PLTW 中的不同,但仍应按您的预期工作。


如果您想使用 PLTW 怎么办?

您可以直接修改自然语言以更改其电机端口。但这不推荐。所有的命令都是硬编码的,如果你修改它,它只会对你有用,而不是对其他任何使用该程序的人。这也会禁用爪形机器人的能力。

但是,如果您确实想这样做,请右键单击lineTrackForTime()并单击Go to definition/declaration

步骤1

然后修改它以像这样采用电机端口。

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);
  }
}

一个更好的方法是创建一个新函数并将其包含在其中;并且只调用这个函数而不是普通函数,但是如果编辑很多函数,这会很麻烦。但同样,我强烈建议更改语言。

于 2020-12-16T20:41:14.220 回答