i’m using a glutTimerFunc(…) to achieve motion of robot arm, my problem is left side 0 degree to 90 is easily done,
when i try 0 to -90 degree, the arm is not stoping? i tried various methods, but all falied, can you suggest better options?
here’s my timer function,
void myTimerFunc(int var) { switch(var) { case 1: if(shoulder>=90) stop=1;if(!stop) { shoulder = (shoulder + 5); glutPostRedisplay(); glutTimerFunc(100,myTimerFunc,1); break; } if(shoulder<=0) stop1=1; if(!stop1) { shoulder = (shoulder - 5); glutPostRedisplay(); glutTimerFunc(100,myTimerFunc,1); break; }case 2: if(shoulder>=360)
stop2=1; if(!stop2) { shoulder = (shoulder - 5); glutPostRedisplay(); glutTimerFunc(100,myTimerFunc,2); break; }// here robot arm is not stopping...........
if(shoulder<=270) stop2 = 0; stop3 = 1; if(!stop3) { shoulder = (shoulder + 5); glutPostRedisplay(); glutTimerFunc(100,myTimerFunc,2); break; }default:break;
}
}i’m calling this from keyboard function…….
void keyboard (unsigned char key, int x, int y) { switch (key) { case 's': glutTimerFunc(100,myTimerFunc,1); break; glutTimerFunc(100,myTimerFunc,2); break;
Looks to me like you’re confused about how to treat angles. Are you using a [0, 360] scale for a full circle or [-180, +180]? I see a check against 270 in your code, but your prose mentions -90. Yes, they’re “the same”, but if it’s not working perhaps some confusion has crept into your code.
If you’re going with [-180, +180], with zero degrees as the neutral position for your robot arm, perhaps you can take advantage of symmetry and use the absolute value function. You only have to code it once that way. If it’s successful for [0, +90] it’ll also work for [-90, 0] as well.
It also seems to me that you’re not taking advantage of object-oriented thinking. This method has motion and timing and graphical refresh all in the same method. Writing smaller methods and unit testing each one to be sure that it works is a better approach. It’s called “decomposition”. The moment you say “it’s a big method”, it suggests to me that perhaps it’s too big and needs to be refactored.