Connecting an encoder

From ROBOTC API Guide
Jump to: navigation, search

Mechanical Connection

The first thing we need to do is physically attach the encoders to the robot. Since we moved the bearings to the outside of the base Swervebot to allow room for the battery box, the instruction's method of attaching the encoders will not work without a simple adjustment. If you used a different battery, or you wish to modify the instruction's way of attaching it, feel free to use that method.

Alternatively, you may want to use a different method of attachment. The images below show an example method to attach the encoders, however feel free to devise your own system if you wish.

Step 1
Connect the encoder with the front side facing outwards
Connect the other side the same way.

Wiring

Diagram of encoder connections.

Since each encoder uses two cables the task may seem daunting at first, however it is actually quite simple. Attaching the wires is very similar to attaching the servos; you simply need to connect the black wires to ground, the red wires to 5V and the white signal cables to individual digital inputs. We will be using digital inputs 7 and 8 for the left encoder and inputs 2 and 3 for the right encoder.

You will notice that the plugs are slightly overlapping. This is not something you will always need to do when connecting encoders, nor is it a special feature common to encoders. It simply serves as a way to conserve space by letting us use fewer wires for the ground connection by putting both ground wires on the same bus - they will all need to be connected eventually, anyway.

Notepad.gif NOTE: Do NOT disconnect the motors. They are still necessary, however this diagram is shown without them for simplicity's sake.

First, plug in the cables. Be sure to match up top and bottom connections appropriately (the top wire of the encoder is the one closest to the mounting bracket). You may need to click on the image to have a closer look.

Step 1

Next, connect the ground wires.

Step 2

Plug in the power wires.

Step 3

Notepad.gif NOTE: The signal wires are shown as orange so you can distinguish them. In real life, we made them yellow to be consistent with the other signal wires.

Finally, connect the signal cables. Be sure to match up the ports correctly.

Step 4
A view of the wiring.

Configuring RobotC

Start a new RobotC Arduino project, and open Motors and Sensors setup. Configure your motors, then open 'Digital 0-13' and configure dgtl2 as rightEncoder and dgtl7 as leftEncoder. You will notice that the secondary ports are automatically configured. Do not name the secondary ports.

Configuring the VEX encoders

Your RobotC configuration code should look like this:

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl7,  leftEncoder,    sensorQuadEncoder)
#pragma config(Motor,  servo_10,        rightServo,    tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl10, None)
#pragma config(Motor,  motor_11,        leftServo,     tmotorServoContinuousRotation, openLoop, IOPins, dgtl11, None)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

Testing

Now that the encoders are connected and configured, it is important to test them as there is always the chance that some wires have been inserted backwards or into the wrong port.

You are going to write task main() so that you can use the debugger to determine if your encoders are connected correctly. You will not even run the code, just download it.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl7,  leftEncoder,    sensorQuadEncoder)
#pragma config(Motor,  servo_10,        rightServo,    tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl10, None)
#pragma config(Motor,  motor_11,        leftServo,     tmotorServoContinuousRotation, openLoop, IOPins, dgtl11, None)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  //Empty task main so that the program will compile. The program will not run.
}

Download the code, then go to Robot > Debugger Windows > then click on Sensors.

Opening the Sensors debug window

In the Debugger window (at the bottom of the screen) you should see the two encoders you configured. Spin the wheels by hand and check if the encoder values tick up/down correctly, as well as corresponding to the correct side. If it does not work correctly, swap the plugs accordingly.

Now you have two fully functional encoders.