我正在尝试在爪子机器人上使用lineTrackForTime命令,但似乎机器人只是跑过了线,无法检测到它。
然而,当我切换到square robot时,lineTrackForTime命令起作用了。
以下代码适用于square机器人,但不适用于爪子机器人。
#pragma config(StandardModel, "RVW SQUAREBOT")
#pragma config(RenamedStdModelSensor, in1, leftline)
#pragma config(RenamedStdModelSensor, in2, centerline)
#pragma config(RenamedStdModelSensor, in3, rightline)
//*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();
}我怎样才能让代码和爪子机器人一起工作来完成下面的代码呢?
发布于 2020-12-17 04:41:14
不应该将与爪子机器人一起使用
为什么不行?
用于vex的PLTW具有硬编码的马达端口。这些端口是2和3个用于移动的端口。方形机器人在端口2和3上确实有马达,使其工作。但是爪子在端口1和10有电机。正如我所说的,这些都是硬编码到软件中的,并且需要改变每一条线路。这基本上禁用了你的整个程序,因为转发函数也不起作用。
该怎么办呢?
切换到自然语言2.它可能没有确切的lineTrackForTime功能,但它提供了lineTrackLeft和lineTrackRight,你可以这样使用(如果你需要时间能力,可以从documentation中获取)。这种语言允许更大的灵活性,如果操作正确,可以与两个机器人一起使用(而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);
}
}一种更好的方法是创建一个新函数,并将其包含在其中;只需调用此函数而不是普通函数,但是如果编辑大量函数,这将是一个麻烦。但是,我再次强烈建议您更改语言。
https://stackoverflow.com/questions/63269631
复制相似问题