NXC: detecting robot turns

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

NXC: detecting robot turns

Post 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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: detecting robot turns

Post by mattallen37 »

Is the idea that you want it to detect a turn to correct itself?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

Re: NXC: detecting robot turns

Post 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.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: detecting robot turns

Post 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);
  }
}
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

Re: NXC: detecting robot turns

Post 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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: detecting robot turns

Post by mattallen37 »

You'd need to keep resetting the encoders, or else use an offset for that to work (and fix the syntax).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: detecting robot turns

Post 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.
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

Re: NXC: detecting robot turns

Post by floydbloke »

Both posts above understood (and they are common sense anyway :D )
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

Re: NXC: detecting robot turns

Post 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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: detecting robot turns

Post 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);
}
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests