View unanswered posts | View active topics It is currently Sat Jun 23, 2018 2:12 pm

Reply to topic  [ 4 posts ] 
Motor Location ? 
Author Message

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Motor Location ?
Hello guys
I've seen an NXC program that scans something and display it on the screen and i'm trying to do the same. After getting more into RobotC.

NXC code
   OnRev(OUT_B, -50);
      while ( Pos < width )

I suppose that Pos is the position of the motor.
Is there a function or do you have any idea how to get the location of the motor?

What i want to do with it is by defining the width of the display
while scanning using light or color sensor, the value will be checked when the value apply to the condition i'm having it will locate the position of the motor and will do a simple equation which is
x = screen width - (position of the motor * screen width) / width of travelling area
something like that
i'll leave you the full NXC code and i hope you might able to help me with

Last edited by SayedA7med on Tue Jan 22, 2013 3:22 pm, edited 1 time in total.

Fri Jan 18, 2013 4:46 pm
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: Motor Location ?
Use this to get the tachometer reading from the motor.

  nxtDisplayCenteredBigTextLine(4, "%d", nMotorEncoder[motorA]);

A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.

Fri Jan 18, 2013 4:54 pm
Profile WWW
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
Post Re: Motor Location ?
NeXT-Gen is correct, the command you are looking for is 'nMotorEncoder'. This will let you read and write values for the specified motor. For instance, to clear the motor's encoder to a value of zero:
nMotorEncoder[motorA] = 0;

There are multiple ways to keep a motor running until an encoder value is reached; one of the more common ways is to use a while loop:

nMotorEncoder[motorA] = 0; //Clear encoder values

while (nMotorEncoder[motorA] < 1000) //While the encoder value is less than 1000 ticks...
motor[motorA] = 50; //run motor A at a power level of 50...

motor[motorA] = 0;//but once the encoder value reaches 1000, exit the loop and stop the motor.

This will run motor A at a value of 50 power until it reaches 1000 encoder ticks; once 1000 encoder ticks are reached, the motors will turn off. Always make sure to either clear the encoders beforehand (or tweak your while loop condition accordingly) and to turn the motor off after the loop is over (if you want the motor to stop at that point).
You may notice that the motors drift past the desired encoder value by a bit; to avoid this, you will want to use the 'nMotorEncoderTarget' command:

nMotorEncoder[motorB] = 0;                // reset the Motor Encoder of Motor B
nMotorEncoderTarget[motorB] = 360;        // set the  target for Motor Encoder of Motor B to 360 ticks
motor[motorB] = 75;                       // motor B is run at a power level of 75
while(nMotorRunState[motorB] != runStateIdle)  // while Motor B is still running (hasn't reached target yet):
  // do not continue to next line of code; stay in loop.
motor[motorB] = 0;                       // motor B is stopped at a power level of 0

To learn more about the encoder commands in ROBOTC, check out our NXT Motor Functions wiki page. You may also be interested in our [url]online curriculum video trainer[/url] which covers all of the basic ROBOTC commands and functions (including encoders and while loops).

Check out our Blog! And our Facebook page!
Need help? Take a look at our updated help documentation and the ROBOTC Forums.

Fri Jan 18, 2013 5:17 pm

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Re: Motor Location ?
Thanks guys
That was so helpful

Sat Jan 19, 2013 4:33 am
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.