Page 1 of 2

NXC: detecting robot turns

Posted: 22 Feb 2012, 02:22
by floydbloke
Using NXC, driving a robot with two motors (let’s say B & C) I’m trying to determine the best way to detect when the robot turns.

My thoughts so far are to do a continuous comparison of the tachocount of B & C (with some tolerance I guess because I assume the two will never be exactly identical) , detecting the turn when the delta reaches a certain threshold.

I realise though that there is also a whole raft of other motor functions out there that I don’t fully understand the meaning of (especially those to do with synch) so if there is a more effective way to do this I would be keen to hear about it.

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 05:45
by mattallen37
Is the idea that you want it to detect a turn to correct itself?

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 07:47
by floydbloke
mattallen37 wrote:Is the idea that you want it to detect a turn to correct itself?
No. I want to keep a record of the rotation count, logging it each time that the robot makes a turn.

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 08:24
by HaWe
For this issue I am starting a seperate task which does just the same what you already proposed:
calculate the delta count of both encoders.

Code: Select all

task EncMonitor() {
  while(1) {  
    long delta=(MotorRotationCount(OUT_B)-(MotorRotationCount(OUT_C)
    int threshold=1;
    if (delta>threshold) //... turn clockwise
    else
    if (delta<threshold) //... turn anti clockwise
    Wait(3);
  }
}

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 20:53
by floydbloke
doc-helmut wrote:For this issue I am starting a seperate task which does just the same what you already proposed:
calculate the delta count of both encoders.
...
Thanks. That looks promising, will try it out at home tonight.

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 21:07
by mattallen37
You'd need to keep resetting the encoders, or else use an offset for that to work (and fix the syntax).

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 22:39
by HaWe
yes, matt is right.
I wrote this from memory, I actually was comparing cached values
(encCounter - last_delta)
so I didn't need to reset the counters itself.

Re: NXC: detecting robot turns

Posted: 22 Feb 2012, 22:54
by floydbloke
Both posts above understood (and they are common sense anyway :D )

Re: NXC: detecting robot turns

Posted: 23 Feb 2012, 05:47
by floydbloke
partial success, had to tweak the code slightly as per below otherwise it would trigger when the motors are running at the same speed (i.e. delta == 0)

Code: Select all

  while(1) {
    long delta=MotorRotationCount(OUT_B)-MotorRotationCount(OUT_C);
    int threshold=20;
    if (delta &&  delta>threshold)
      {
       ...
      }
    else
    if (delta && delta<(threshold*-1))
       {
       ...
       } 
  Wait(3);
  }
testing this using the joystick tool in BrixCC and the motors spinning freely they obivously don't quite turn at the same speed and the threshold is exceeded after a few seconds. Might be different if they are robot-wheels and under (the same) load.
It does proof the concept though, and I can go certainly go forth from here with my little project.

Re: NXC: detecting robot turns

Posted: 23 Feb 2012, 06:14
by mattallen37
Like I said before, you either need an offset, or you need to reset the encoders.

Something like this should work:

Code: Select all

int threshold = 1;
long offset;
long delta;
while(1) {
  delta = MotorRotationCount(OUT_B) - MotorRotationCount(OUT_C) - offset;
  if (delta > threshold){

  }
  else if (delta < (threshold * -1)){

  }
  offset = delta; 
  Wait(3);
}