UPDATE: Download this program w/ setup, usage instructions and a demo here; get the driver (optional) here.
Programming my current project, Bionic NXTPod 3.0, I broke the actuators a few times by having the slide run into either the top or bottom. This was caused by my motor control: every time it went up or down, the encoders were reset, not considering whether or not the motor was exactly on target. As the errors built up, the actuators were torn down.
Looking for a solution, I saw HiTechnic‘s motor PID block – for NXT-G – which lets the motor move to an absolute encoder position, without resetting in between. Using this method, the error – usually not more than 2-3 degrees – is eliminated every time the motor moves to a new position, because the block compares the desired position to the actual position, not the assumed “0 point”. For example:
- The motor needs to go to 200 degrees and then back to 100
- The program starts, and the motor overshoots to 201 degrees
- Without the HiTechnic block: the motor would move back 100 degrees – because it thinks it’s at exactly 200 degrees – ending up at 101
- With the HiTechnic block: the motor would move back 101 degrees (201-100) ending up at 100
void PositionMotor(char motorToTurn, int absoluteDegrees){
if(nMotorEncoder[motorToTurn] == absoluteDegrees){}
else if(nMotorEncoder[motorToTurn] < absoluteDegrees){
bMotorReflected[motorToTurn] = false;
nMotorEncoderTarget[motorToTurn] = absoluteDegrees-nMotorEncoder[motorToTurn];
motor[motorToTurn] = 75;
while(nMotorRunState[motorToTurn] != runStateIdle){}
motor[motorToTurn] = 0;
}
else{
bMotorReflected[motorToTurn] = false;
nMotorEncoderTarget[motorToTurn] = nMotorEncoder[motorToTurn]-absoluteDegrees;
bMotorReflected[motorToTurn] = true;
motor[motorToTurn] = 75;
while(nMotorRunState[motorToTurn] != runStateIdle){}
motor[motorToTurn] = 0;
}
}
You don’t use NXC? It has that feature implemented quite nicely.
O. I didn’t know that :). That’s kinda nice actually. But I just like RobotC for some reason – it got me into actual programming (you know, with text and stuff :P). And now that I’ve got this code, I can do the same with RobotC right? :) But thx for telling anyways, I’ll take a look at NXC some time in the near future (after my exams…)
” . . . [RobotC] got me into actual programming (you know, with text and stuff :P ). . . .”
And what, exactly, do you mean by that? NXC is text-based too! (As opposed to NXT-G, for example)
I mean RobotC was the first text-based programming language I ever used, and it really got me into that opposed to the graphical programming I used to do (NXT-G, sketch, etc)
I see. My bad! :P