gcronin
Rookie
Joined: Thu Mar 24, 2011 8:45 pm Posts: 14
|
 Encoders Locked in a While Loop
Hi,
This is a post from the mentor of Team 2856, from Seattle. We're working to get an arm working for nationals. We've switched from labview to robotC and have mostly been happy, but are now having some seemingly random problems with our arm. The arm is programmed to run on joystick 2, using the up and down motion to correlate with down/up on the arm.
The function which is running one of the joints is shown below along with the relevant variables defined at the beginning of the program. We are running RobotC2.26 with whatever version of firmware that installs. The function executes inside task main with several other functions. A 10 ms wait is built into task main. The full program is attached.
The function below seems to be hanging in any of the motor positioning while loops. The evidence for this is that the motor makes noise as though trying to move, but does not move at all. As you can see, the NXT displays values on the screen, and we're getting encoder values which make sense (eg at the beginning the encoder reads 0, if the joystick is pressed down, we see "Up -6 -10" on the screen). It's as though it knows it needs to move to the enPos2 value (-10), but is stuck at the current position (06). This hangs the rest of the program. If the joystick is released and then the motor is moved physically, the function exits the while loop and goes back to the "Zeroed" reading on the NXT output.
We have tried changing motor speed, and also changing the value of increment.
This seems to be a sporadic problem; for example, we sometimes can drive the robot and manipulate the arm fine for several minutes, and then the problem kicks in. Today I tried flashing the firmware and recompiling the program which seemed to fix the problem but then it started again.
We're newbies to robotC, so any suggestions you could suggest would be most welcome!!!
#pragma config(Motor, mtr_S1_C2_1, motorF, tmotorNormal, PIDControl, encoder) //Joint 2 #include "JoystickDriver.c" int enPos2 = 0; const int UpBound2 = 3000; const int increment = 10; const int LowBound2 = -3000; // goes over 15-20
void joint2() // controls the second joint of the arm, refer to the notes above { int joint2 = joystick.joy2_y2;
if (joint2 > 6 && nMotorEncoder[motorF] < UpBound2) //Joystick pressed up { enPos2 = enPos2 + increment;
while (nMotorEncoder[motorF] < enPos2) //while the encoder wheel turns to setpoint {
nxtDisplayCenteredTextLine(3, "DOWN: %d, %d", nMotorEncoder[motorF], enPos2); motor[motorF] = 20;
} motor[motorF] = 0; //turn motor off }
else { if (joint2 < -6 && nMotorEncoder[motorF] > LowBound2) { enPos2 = enPos2 - increment;
while (nMotorEncoder[motorF] > enPos2) //while the encoder wheel turns to setpoint { nxtDisplayCenteredTextLine(3, "UP: %d, %d", nMotorEncoder[motorF], enPos2); motor[motorF] = -20;
} motor[motorF] = 0; //turn motor off
} else { motor[motorF] = 0; nxtDisplayCenteredTextLine(3, "ZEROED: %d, %d", nMotorEncoder[motorF], enPos2); } } }
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: Encoders Locked in a While Loop
I see many potential problems in that code. But for the particular issue you have, the joint2 function is basically trying to drive the arm to the next increment if the joystick is pressed in one direction. But you are using a motor power of 20. Depending on whether the arm is working against gravity or not, it may not have enough power to achieve target. So your while loop will never exit. I wouldn't set the power anything less than 30. And in general a while loop in a function is not a good thing to do especially if it could get stuck and not exiting. Also, you have enabled PID control in your pragma. That will make NXT regulate the speed of the motor. It's probably why it doesn't have enough power to achieve target when it's close to it. If you don't intend to use PID, you should turn it off. The following code isn't exactly the same as what your code does, but it makes more sense. Instead of doing a fixed power according to the direction of the joystick, the following code adjusts the motor with variable power according to how far you push the joystick. By the way, you should not have a joint2 variable which is the same name as the function. I don't know why RobotC doesn't give you an error. Any other C compiler would have refused to compile it. If you insist to control the arm with constant power, then use the next piece of code.
|