<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://www.robotc.net/w/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.robotc.net/w/index.php?title=Moving_until_position&amp;feed=atom&amp;action=history</id>
		<title>Moving until position - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://www.robotc.net/w/index.php?title=Moving_until_position&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://www.robotc.net/w/index.php?title=Moving_until_position&amp;action=history"/>
		<updated>2013-06-18T07:05:35Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.18.0</generator>

	<entry>
		<id>http://www.robotc.net/w/index.php?title=Moving_until_position&amp;diff=3455&amp;oldid=prev</id>
		<title>Bfeher: Created page with &quot;{{DISPLAYTITLE: Moving until a certain position is reached}} &lt;yambe:breadcrumb self=&quot;Moving until a certain position is reached&quot;&gt;Programming_Tips_Tricks|Programming Tips Trick...&quot;</title>
		<link rel="alternate" type="text/html" href="http://www.robotc.net/w/index.php?title=Moving_until_position&amp;diff=3455&amp;oldid=prev"/>
				<updated>2012-05-15T20:39:52Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{DISPLAYTITLE: Moving until a certain position is reached}} &amp;lt;yambe:breadcrumb self=&amp;quot;Moving until a certain position is reached&amp;quot;&amp;gt;Programming_Tips_Tricks|Programming Tips Trick...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{DISPLAYTITLE: Moving until a certain position is reached}}&lt;br /&gt;
&amp;lt;yambe:breadcrumb self=&amp;quot;Moving until a certain position is reached&amp;quot;&amp;gt;Programming_Tips_Tricks|Programming Tips Tricks&amp;lt;/yambe:breadcrumb&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There have been several questions recently about moving part of the robot until it has reached a certain position. I though I would post a RobotC example showing how to drive a motor forwards or backwards while monitoring an encoder. This same idea could be used for raising a lift or anything else where an encoder is used to monitor the rotation of a motor.&lt;br /&gt;
&lt;br /&gt;
Anytime a loop is used waiting for a terminal condition to be found it's important to think about what will happen if something goes wrong. This code also shows how to use a timeout so that if an error occurs, the motor stalls for example, the test will stop after a period of time.&lt;br /&gt;
&lt;br /&gt;
This code is a simple example and does not use multi tasking or the built in timer both of which could be used as part of this:&amp;lt;br /&amp;gt;&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;ROBOTC&amp;quot;&amp;gt;&lt;br /&gt;
#pragma config(Sensor, dgtl1,  encoder, sensorQuadEncoder)&lt;br /&gt;
#pragma config(Motor,  port2,  MyMotor, tmotorNormal, openLoop)&lt;br /&gt;
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------------------------------------*/&lt;br /&gt;
/*                                                                             */&lt;br /&gt;
/* Drive motor until an encoder has counted for a number of counts             */&lt;br /&gt;
/* or a timeout period has passed.                                             */&lt;br /&gt;
/*                                                                             */&lt;br /&gt;
/* Assumes the encoder will count up when the motors are running forwards      */&lt;br /&gt;
/*                                                                             */&lt;br /&gt;
/*-----------------------------------------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
#define TIMEOUT_CNT_PER_SEC    10&lt;br /&gt;
#define MOTOR_FWD_SPEED        64&lt;br /&gt;
#define MOTOR_REV_SPEED      (-64)&lt;br /&gt;
#define MOTOR_STOP_SPEED        0&lt;br /&gt;
&lt;br /&gt;
int &lt;br /&gt;
DriveByEncoder( int encoder_count, int timeout_in_seconds = 5 )&lt;br /&gt;
{&lt;br /&gt;
    int  timeout;&lt;br /&gt;
    &lt;br /&gt;
    // Drive motor until encoder has moved a number counts or&lt;br /&gt;
    // timeout_in_seconds seconds have passed&lt;br /&gt;
&lt;br /&gt;
    // Zero the encoder&lt;br /&gt;
    SensorValue[ encoder ] = 0;&lt;br /&gt;
   &lt;br /&gt;
    // Run the motor forwards or backwards&lt;br /&gt;
    if( encoder_count &amp;gt; 0 )&lt;br /&gt;
        motor[ MyMotor ] = MOTOR_FWD_SPEED;&lt;br /&gt;
    else&lt;br /&gt;
        motor[ MyMotor ] = MOTOR_REV_SPEED;&lt;br /&gt;
        &lt;br /&gt;
    // run loop until encoder hits encoder_count counts or timeout reached&lt;br /&gt;
    &lt;br /&gt;
    for( timeout=(timeout_in_seconds*TIMEOUT_CNT_PER_SEC); timeout &amp;gt; 0; timeout-- )&lt;br /&gt;
        {&lt;br /&gt;
        // check encoder&lt;br /&gt;
        if( encoder_count &amp;gt; 0 )&lt;br /&gt;
            {&lt;br /&gt;
            // going forwards&lt;br /&gt;
            if( SensorValue[ encoder ] &amp;gt;= encoder_count )&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
        else&lt;br /&gt;
            {&lt;br /&gt;
            // going backwards&lt;br /&gt;
            if( SensorValue[ encoder ] &amp;lt;= encoder_count )&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
        &lt;br /&gt;
        // wait 1/10 second&lt;br /&gt;
        wait1Msec( 100 );&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
    // Stop the motor&lt;br /&gt;
    motor[ MyMotor ] = MOTOR_STOP_SPEED;&lt;br /&gt;
    &lt;br /&gt;
    // See if we sucessfully found the right encoder value&lt;br /&gt;
    if( timeout &amp;lt;= 0 )&lt;br /&gt;
        {&lt;br /&gt;
        // there was an error - perhaps do something&lt;br /&gt;
        // return error&lt;br /&gt;
        return (-1);&lt;br /&gt;
        }&lt;br /&gt;
    else&lt;br /&gt;
        // return success&lt;br /&gt;
        return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------------------------------------*/&lt;br /&gt;
/*                                                                             */&lt;br /&gt;
/*  Test the new function                                                      */&lt;br /&gt;
/*                                                                             */&lt;br /&gt;
/*-----------------------------------------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
task main()&lt;br /&gt;
{&lt;br /&gt;
    // forwards until 200 counts&lt;br /&gt;
    DriveByEncoder( 200 );&lt;br /&gt;
&lt;br /&gt;
    // short wait&lt;br /&gt;
    wait10Msec( 200 );&lt;br /&gt;
    &lt;br /&gt;
    // backwards for 3000 counts with a 10 second timeout&lt;br /&gt;
    DriveByEncoder( -3000, 10 );&lt;br /&gt;
    &lt;br /&gt;
    // demo done&lt;br /&gt;
    while(true)&lt;br /&gt;
        wait1Msec( 10 );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{tip-from-author|name=jpearman|link=http://www.vexforum.com/showpost.php?p=222061&amp;amp;postcount=9}}&lt;/div&gt;</summary>
		<author><name>Bfeher</name></author>	</entry>

	</feed>