About these ads

RobotC: Precise Motor Positioning Tool

27 05 2011

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
But, since the block’s for NXT-G, that wouldn’t work for me. So, I made a similar (but not quite as versatile) RobotC function:
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;
  }
}
All you do is input the motor you want to move – like “motorA” or “motorC” – and the amount of degrees, measured from your “0 position” (by default, this is the position your motor is in when the program starts), and it does all the work for you.
About these ads

Actions

Information

5 responses

27 05 2011
Stryker

You don’t use NXC? It has that feature implemented quite nicely.

28 05 2011
DiMastero

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…)

28 05 2011
Stryker

” . . . [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)

28 05 2011
DiMastero

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)

2 06 2011
Stryker

I see. My bad! :P

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 41 other followers

%d bloggers like this: