ROBOTC.net forums
http://www.robotc.net/forums/

nMotorEncoderTarget[] not working..
http://www.robotc.net/forums/viewtopic.php?f=52&t=2774
Page 1 of 3

Author:  nanoplane [ Mon Nov 22, 2010 2:36 am ]
Post subject:  nMotorEncoderTarget[] not working..

So this code from the Samples included with RobotC doesn't seem to be working for us. The motors start, then run for some seemingly random period of time...

Any thoughts? I kind of expected that the sample code provided would more or less work :-)

--marcel

Code:
task main()
{

  nMotorEncoder[motorE] = 0;  //clear the TETRIX motor encoders
  nMotorEncoder[motorD] = 0;

  nMotorEncoderTarget[motorE] = 1440; //set the target stoping position
  nMotorEncoderTarget[motorD] = 1440;

  motor[motorE] = 20; //turn both motors on at 30 percent power
  motor[motorD] = 20;

  while (nMotorRunState[motorE] != runStateIdle || nMotorRunState[motorD] != runStateIdle) //while the encoder wheel turns one revolution
  {
    // This condition waits for motors D & E to come to an idle position. Both motors stop
    // and then jumps out of the loop
  }

  motor[motorE] = 0; //turn both motors off
  motor[motorD] = 0;

  wait1Msec(3000);  // wait 3 seconds to see feedback on the debugger screens
                    // open the "NXT Devices" window to see the distance the encoder spins.
                    // the robot will come to a stop at the nMotorEncoderTarget position.
}

Author:  l0jec [ Tue Nov 23, 2010 9:35 am ]
Post subject:  Re: nMotorEncoderTarget[] not working..

Can you post the pragmas as well?

The most obvious things to check is that your wiring matches your configuration and that you do not have any hardware problems. I assume you have the encoders installed correctly and the encoder wires plugged into the motor controller?

Author:  nanoplane [ Tue Nov 23, 2010 5:34 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

here's the pragmas


Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorNormal, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorNormal, openLoop, reversed, encoder)


The encoder's seem to be working, ie: we wrote a little code to watch the encoder values as we pushed the bot around and the values go up and down as expected

--marcel

Author:  MHTS [ Wed Nov 24, 2010 6:25 am ]
Post subject:  Re: nMotorEncoderTarget[] not working..

I usually don't use the built-in PID control so I am not 100% sure but if you are trying to use EncoderTaget, I would think it is using built-in PID control. If that's the case, you should enable PID in your pragma. I could be wrong though.

Author:  l0jec [ Wed Nov 24, 2010 9:53 am ]
Post subject:  Re: nMotorEncoderTarget[] not working..

MHTS, I agree; it doesn't looks like PID is enabled in the pragmas.

nanoplane, can you see the encoder values go up past 1440 on both motors in the debug window when you run the program? I would suggest enabling the PID checkbox in the motor setup as MHTS pointed out.

Also, maybe just me, but I would add a small wait inside the while loop, perhaps 50ms or so.... This suggestion isn't related to the current issue; but is more a matter of best practice.

Author:  mightor [ Wed Nov 24, 2010 11:53 am ]
Post subject:  Re: nMotorEncoderTarget[] not working..

You should always add a small delay between resetting the encoders and executing a new motor command. I usually add something like 50-100ms.

- Xander

Author:  nanoplane [ Wed Nov 24, 2010 3:53 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

We've tried it with and without PID.. and we can see the encoder values for both motors go past 1440.

Note this is only an issue with the Tetrix motors... the same basic code works fine with the NXT motors..

At this point we're probably going to punt (we've got competition this saturday..) and write some routines that just track the encoder values and slow the motor to stop at the desired point..

We'll try again as a side task..

'tis a pain though

--marcel

Author:  mightor [ Wed Nov 24, 2010 4:16 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

By how much do the motors go beyond their target?

- Xander

Author:  MHTS [ Wed Nov 24, 2010 11:36 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

I never trust runStateIdle. If for some reason the motor is not stopping (e.g. PID decided to approach the target too slowly and PID doesn't give it enough power to reach the target), then your loop will never exit. Normally, I would tune the Kp of PID to give it more power to reach the target, but I am not sure how to do that with built-in PID. I would try the following instead. But since you said both encoders past 1440, the following won't really help you anyway.

Code:
  while (abs(nMotorEncoder[motorE] - 1440) > Threshold ||
           abs(nMotorEncoder[motorD] - 1440) > Threshold) //while the encoder wheel turns one revolution
  {
    // This condition waits for motors D & E to come to an idle position. Both motors stop
    // and then jumps out of the loop
      wait1Msec(100);
  }


Author:  gh9310 [ Wed Feb 02, 2011 11:21 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

our team is having that same problem, but we have tried everything including the sample programs and we seem to have no solution

Author:  jorge_the_awesome [ Fri Feb 25, 2011 2:17 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

The nMotorEncoderTarget functionality is also inherently flawed. You can't alter the constants, which is annoying, but primarily, when the motor gets close to the encoder target, it switches from speed based PID to encoder based PID (I think-correct me if I'm wrong). If the stress gets high, and the motor cannot turn, once the encoder based PID is activated, the motors become much less responsive to speed changes. When driving, this is a problem, because the robot can stop before it gets to the target.

Also, this isn't your problem, but the motors glitch when the sign changes, either by crossing zero or looping past 32767. And the encoder based PID doesn't activate for negative targets, so you get different results based on which direction you drive in.

Author:  mightor [ Fri Feb 25, 2011 3:52 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

Jorge,

The encoder ticks are not integers, they are longs, so it's not a problem until you go beyond 2147483648 encoder ticks, which, with standard NXT 2.0 wheels will have driven your little robot about 80.5 kilometres. I highly doubt you will ever have this problem.

Your other points are valid.

- Xander

Author:  jorge_the_awesome [ Wed Mar 02, 2011 9:22 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

The encoder ticks are in longs, yes.
But the nMotorEncoderTarget is in pseudo-intergers.
It will accept values greater than 32767, but it becomes very inaccurate.
We tested it, and the behavior significantly changed between a target of 32767 and 32768.

Author:  mightor [ Thu Mar 03, 2011 3:07 am ]
Post subject:  Re: nMotorEncoderTarget[] not working..

If something is not working right, send a mail to support@robotc.net with a full report and some sample code to reproduce the issue.

- Xander

Author:  RTO [ Mon Feb 13, 2012 12:02 pm ]
Post subject:  Re: nMotorEncoderTarget[] not working..

We are having similar problems. One thing that does seem to help. If we play with motor speed we can get better results; that is a speed of 70 seems to work lots, lots better than speeds below this. A particular problem we are having is that the motor sometime does not jump out of the loop - It is basically at the target. If I touch the wheel driven by the motor ever so slightly, the program goes ahead and ends properly. Another thing I noticed in the debugger is that runstateidle does not seem to be reached; even when the program completes, runstate still seems to be running - is this correct?

Page 1 of 3 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/