Introduction: FTC Tetrix Encoders

We are Cougar Robotics 4251 and this is for the Instructables Sponsorship Program. This Instructable will cover the mounting and basic usage in RobotC of the US Digital Encoders used in FTC. Encoders allow for RobotC's built in PID and speed control. This means that if you set a target speed, the motor controller will apply whatever power necessary (within its safety limits) to achieve the desired speed. They are underrated by many FTC teams and are a valuable tool, if nothing else. It will also show you how to safely remove an encoder from a burned out motor. You will need:

1. Tetrix DC Motor + motor cable
2. Optical Encoder + mounting pack (it all comes together)
and for control/programming
3. HiTechnic Motor Controller
4. NXT Brick
5. a computer with RobotC and bluetooth or a USB cable
6. a Tetrix 12V battery (charged)

Also, a useful tools include
1. small flat head screwdriver
2. small Philips head screwdriver
3. rubber-tipped pliers

Step 1: Attach the Encoder to the Motor

So to begin, we just need the motor and the encoder pack. The encoder should come in several pieces. Take the one that is flanged with screw holes and insert it onto the motor shaft. There should be two small screws in the pack use a small philips head screwdriver to carefully attach the encoder base to the motor. NOTE: it is important not to strip or round out the heads of the screws, these can be reused if the motor burns out!

Now that the base is secured, carefully and without touching the flat parts, push the silver disc onto the shaft, black side towards the rest of the encoder. This is where the small, black, plastic disc that comes in the encoder pack is so useful. This is actually a spacing tool. Use the tool to push down the optical disc until the black plastic touches the clips on the encoder base. Now the disc is at the optimal spacing for the encoder to work.

Next, we just need to put the cap on the encoder. This protects everything, controls ambient light, and keeps dust and gunk out of the whole thing.

Step 2: Wire Everthing

This is where the we start putting things together in a state where we can run the motor.

The Tetrix motor cable goes into the positive and negative of "motor 1" on the DC Motor Controller.

The battery connects to the "battery +" and "battery -" on the Motor Controller. 

The NXT gets the Motor Controller connected to it on sensor port 1.

The Encoder wire is the most delicate. The small white end goes into the encoder itself. If you will look closely at the encoder wire, there are two little bumps that stick out of the encoder end. Those go facing towards the wall of the plug. Otherwise, the wires will all be switched, your encoder wont work and the wire will not stay in place. 

Finally, the black end of the encoder wire goes into the "Enc 1" port on the Motor Controller. It only goes in one way, make sure you don't force it.

Step 3: Setup Base Program and Read Encoders

Now we are ready for some software. In RobotC, open a new blank program. If you go to the menu Robot/Motors and Sensors Setup you will see a new dialog window show up. click on the tab that says "external controllers". now choose custom from the options at the top. add one motor controller on the first sensor port. next, go to the"motors" tab. i like to rename motors to more useful names so i will call my motor "motorDemo". apply the settings and a block of code will appear at the top of your program that should look something like this...

so we have one main command for reading the values of a motor encoder. the built in functions in RobotC act as variables. so to read the encoder the line is
dummyVariable = nmotorEncoder[motorDemo];

so a program that will display the current encoder value world look something like this...

nmotorEncoder[motorDemo] = 0;
motor[motorDemo] = 100;
while(true)
{
dummyVariable = nmotorEncoder[motorDemo];
nxtDisplayTextLine(3,"%d",dummyVariable);
}

Step 4: Spin Until Encoder Distance

https://www.instructables.com/id/FTC-Sporadic-Encoder-Values/so now we are getting into one of the most common uses for motor encoders, driving a specific distance or running a motor a certain amount. here is the simple way of doing this:

motorEncoder[motorDemo] = 0;
motor[motorDemo] = 50;
while(nmotorEncoder[motorDemo] < targetValue)
{
wait1Msec(5);
}
motor[motorDemo] = 0;

This will run the motor at 50% speed until the targetValue has been hit. If you were to replace "targetValue" with 1440, the motor will turn 1 complete revolution. This could be expanded on to include multiple motors and go until they have both, or just one, have hit a target duration. Now our team, in the past, had an issue with the encoder putting out a sporadic value "very rarely" that we have described a solution to here

Step 5: Conclusion

encoders are very useful and are underrated by many FTC teams. our team never has a DC motor without an encoder. we do often combine it with other sensors, depending on the application but encoders allow the built in speed control and PID logic to work on the motors.