[NXC]TachoCount problem

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
matzzn
Posts: 10
Joined: 29 Nov 2011, 17:51

[NXC]TachoCount problem

Post by matzzn »

I'm just trying to wirte a function for fast motor-movements.
I have a problem with "GetOutput( OUT_A, TachoCount);"
After resetting the TachoCount, GetOutput returning not 0.

Code: Select all


inline void RunMotor ( byte out, char pwr, long angle)
{
   long tacho;
   long pre_tacho;
   unsigned int stady_state;
   int i;

   ResetTachoCount(OUT_A);
   SetMotorRegulationTime (10);
   
   if (angle < 0)
   {
      pwr = -pwr;
   }
   OnFwd (out, pwr);
   
   do
   {
      i++;
      tacho = GetOutput( out, TachoCount);
      if (tacho == pre_tacho) {stady_state++;}

      if ( i%50 == 0) {pre_tacho = tacho;}  // lower the resolution

   } while (tacho < angle &&  !(stady_state > (angle *100 / pwr) ) );

   OffEx (out, RESET_ALL);  // i think off  causes the problem
   Yield();
}
   

task main ()
{
   long tacho;

   RunMotor (OUT_A, 100, 90);

   Wait (1000);
   
   // Lets reset TachoCount so it should be 0
   ResetTachoCount(OUT_A);
   
   tacho = GetOutput( OUT_A, TachoCount);
   
   // tacho != 0 , tacho = 50!!!   << whats wrong?
   NumOut(5, LCD_LINE1, tacho);
   
   Wait(5000);
}

HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC]TachoCount problem

Post by HaWe »

hey,
no solotution to your problem - but a hint:
to my experience, all my NXC code with functions using TachoCount are causing REAL BIG CRA.... aaahm: MISTAKES ;)

The fw seems to use this for obscured internal purposes.

IMHO, maybe you better wish to try RotationCount instead!
matzzn
Posts: 10
Joined: 29 Nov 2011, 17:51

Re: [NXC]TachoCount problem

Post by matzzn »

The fw seems to use this for obscured internal purposes.
That was my first thought (I think OffEx() )
RotationCount causes this problem also.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC]TachoCount problem

Post by afanofosc »

After issuing a command to set an output module IOMap field you have to wait at least one millisecond before the output module will actually read the value you set in the IOMap and do something about it (i.e., reset the tacho or block tacho or rotation count). So

Code: Select all

   // Lets reset TachoCount so it should be 0
   ResetTachoCount(OUT_A);
   Wait(MS_1); // wait for Output module to actually reset the tacho count
   tacho = GetOutput( OUT_A, TachoCount);
   
However, as documented and as suggested by Doc, you should never use the Tacho count in your own code since it is used by the firmware internally. Use instead the RotationCount which is not used at all by the firmware.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
matzzn
Posts: 10
Joined: 29 Nov 2011, 17:51

Re: [NXC]TachoCount problem

Post by matzzn »

Thanks seems fixed now. That's a really tricky bug ;)
I'm going to change everything to "Rotation Count".
matzzn
Posts: 10
Joined: 29 Nov 2011, 17:51

Re: [NXC]TachoCount problem

Post by matzzn »

Ok next problem.

Code: Select all


   ...
   } while (tacho < angle &&  !(stady_state > (angle *100 / pwr) ) );

   Off (out);
   
   /*This solves the problem
   Wait(200);
   Coast(out);
   */
   
   Wait(1);
   ResetRotationCount(OUT_A);
   Wait(1);
}

....

   RunMotor (OUT_A, 100, 90);
   Wait (1000);
   tacho = GetOutput( OUT_A, RotationCount);
   // tacho != 0 , tacho = 50!!!   << whats wrong?
   NumOut(5, LCD_LINE1, tacho);


If I put a wait after RunMotor I get again wrong values.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC]TachoCount problem

Post by HaWe »

just a guess -
what, if you changed "inline void RunMotor()" into
"safecall void RunMotor()"
?

maybe you wish to post the whole source code again?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC]TachoCount problem

Post by afanofosc »

I do not think you need to change inline to safecall as Doc suggests you try.

Instead, I think that RunMotor is no longer resetting all the counters at the end of the function call. But why should it anyway? It seems best to explicitly reset the rotation counter outside of a generic motor rotation function, unless you can pass in a parameter saying whether to reset or not. And it is best to not reset the internal counters if you don't need to.

Just call ResetRotationCount(OUT_A); Wait(MS_1); after RunMotor() and then your rotation counter will be zero when you read it next with MotorRotationCount(OUT_A);

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests