ROBOTC - 自动编程集成编码器

问题描述:

我有一个编码在ROBOTC中的X驱动器。我和我的团队已经在机器人上安装了集成的电机编码器(在自主阶段)。但是他们运行的代码不正确。下面是当前的自治代码。当我运行它时,它会以不同的速度永远前进。ROBOTC - 自动编程集成编码器

我已经看过多个教程,但都没有工作。 有没有人有让电机(393电机)的计数为720的代码?


#pragma config(I2C_Usage, I2C1, i2cSensors) 
#pragma config(Sensor, I2C_1, sensorQuadEncoderOnI2CPort, AutoAssign) 
#pragma config(Motor, port2, FL, tmotorVex393_MC29, PIDControl, encoderPort, I2C_1) 
#pragma config(Motor, port3, BR, tmotorVex393_MC29, PIDControl, reversed, encoderPort, I2C_1) 
#pragma config(Motor, port8, BL, tmotorVex393_MC29, PIDControl, encoderPort, I2C_1) 
#pragma config(Motor, port9, FR, tmotorVex393_MC29, PIDControl, reversed, encoderPort, I2C_1) 
//*!!Code automatically generated by 'ROBOTC' configuration wizard    !!*// 

task main() 
{ 
// Autonomous with Integrated Encoders 
nMotorPIDSpeedCtrl[FL] = mtrSpeedReg; 
nMotorPIDSpeedCtrl[FR] = mtrSpeedReg; 
nMotorPIDSpeedCtrl[BL] = mtrSpeedReg; 
nMotorPIDSpeedCtrl[BR] = mtrSpeedReg; 

//Clears motor values 
nMotorEncoder[FL] = 0; 
nMotorEncoder[FR] = 0; 
nMotorEncoder[BL] = 0; 
nMotorEncoder[BR] = 0; 

//Forward 
motor[FL] = 63; 
motor[FR] = 63; 
motor[BL] = 63; 
motor[BR] = 63; 
    while(nMotorEncoder[FL] < 720) { 
} 

//Clears motor values 
nMotorEncoder[FL] = 0; 
nMotorEncoder[FR] = 0; 
nMotorEncoder[BL] = 0; 
nMotorEncoder[BR] = 0; 

} 
+0

能编码器值减少,而不是增加?尝试将'while'条件改为'nMotorEncoder [FL]> -720'。 – qxz

您需要while循环后,明确地停止电机(不只是零出编码器)。否则机器人不知道停止;它只知道它通过了编码器目标。

所以这个代码应该为你工作:

//Clears motor values 
nMotorEncoder[FL] = 0; 
nMotorEncoder[FR] = 0; 
nMotorEncoder[BL] = 0; 
nMotorEncoder[BR] = 0; 

motor[FL] = 63; 
motor[FR] = 63; 
motor[BL] = 63; 
motor[BR] = 63; 
//Forward 
while(nMotorEncoder[FL] < 720) { 
} 
//stops motors 
motor[FL] = 0; 
motor[FR] = 0; 
motor[BL] = 0; 
motor[BR] = 0; 
//Clears motor encoder values 
nMotorEncoder[FL] = 0; 
nMotorEncoder[FR] = 0; 
nMotorEncoder[BL] = 0; 
nMotorEncoder[BR] = 0;