Hello, we were attempting to use the integrated motor encoders on our drive. However, the program doesn’t stop running the motor as if it isn’t working. Sorry if it is something silly. Our programmer is a bit rusty and hasn’t used the IME’s before. Please give us some help or some sample code. Here is our test program:
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, I2C_1, leftIEM, sensorQuadEncoderOnI2CPort, , AutoAssign)
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign)
#pragma config(Motor, port1, frontleft, tmotorVex393HighSpeed_HBridge, openLoop, encoderPort, I2C_1)
#pragma config(Motor, port2, frontright, tmotorVex393HighSpeed_MC29, openLoop, encoderPort, I2C_2)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main
{
wait1Msec(2000);
//Clear the encoders associated with the left and right motors
nMotorEncoder[frontleft] = 0;
nMotorEncoder[frontright] = 0;
//While less than 1000 encoder counts of the right motor
while(nMotorEncoder[frontright] < 2)
{
//Move forward at 80 power
motor[frontleft] = 80;
motor[frontright] = 80;
}
}
I don’t know this for sure, but I’ll make my best educated guess at what I think is wrong, but don’t take my word for it. What I think you need to add after saying that if the encoder is less than 2, do this, is that you need to say if the encoder is greater than 2, stop the motors. Also have you tested in the debugger window’s to see how far “2” really is. “2” could be pretty far, you never know.
2 encoder counts is no distance at all (with 4 inch wheels and 1:1 gearing it is 0.04 inches). 1000 counts is approximately 20 inches. Aiden has the right idea, you never stop the motors, try this.
Code:
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, I2C_1, leftIEM, sensorQuadEncoderOnI2CPort, , AutoAssign)
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign)
#pragma config(Motor, port1, frontleft, tmotorVex393HighSpeed_HBridge, openLoop, encoderPort, I2C_1)
#pragma config(Motor, port2, frontright, tmotorVex393HighSpeed_MC29, openLoop, encoderPort, I2C_2)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main
{
wait1Msec(2000);
//Clear the encoders associated with the left and right motors
nMotorEncoder[frontleft] = 0;
nMotorEncoder[frontright] = 0;
//While less than 1000 encoder counts of the right motor
while(nMotorEncoder[frontright] < 1000)
{
//Move forward at 80 power
motor[frontleft] = 80;
motor[frontright] = 80;
}
//Stop
motor[frontleft] = 0;
motor[frontright] = 0;
}