【问题标题】:how to program vex claw robot to do line following?如何对 vex 爪机器人进行编程以进行线路跟踪?
【发布时间】:2020-08-05 16:36:07
【问题描述】:

我正在尝试在爪形机器人上使用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();
}

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

【问题讨论】:

    标签: robotc


    【解决方案1】:

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


    为什么不呢?

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


    怎么办?

    切换到自然语言 2。 它可能不完全具有 lineTrackForTime 功能,但它提供了 lineTrackLeftlineTrackRight,您可以这样使用(取自 documentation if你需要时间能力)。这种语言提供了更大的灵活性,如果操作正确,可以与两个机器人一起使用(而 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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多