Micromouse Lab 2

Encoders

When we are navigating the maze, we would like to keep track of a couple things.

To answer these questions, we'll use the quadrature encoder sensors mounted on the back of your motors. They consist of a spinning magnet and two Hall effect sensors mounted 90° apart. Hall effect sensors measure the presence of magnetic fields. In our case, they output a digital HIGH when closer to a north pole and LOW when closer to a south pole.

Waveforms plot Hall sensor signal (y-axis) vs. time (x-axis)

Let's try reading from the encoder! Upload the following code.

Now open up the Serial Monitor or Serial Plotter. Spin the left motor by hand using various speeds and note how the printout changes. Note that with just one input, it's impossible to tell which direction the motor is spinning.

Checkoff #1

Going the Distance

Next let's try tracking the position of your left wheel. We're going to use a technique called polling which we'll go over more in the next section. Fill in the TODO in the following code. Hint: You'll need digitalRead().

After uploading the code, open up the Serial Monitor or Serial Plotter and spin the motor in both directions. The output count should go up and down as you do it!

Checkoff #2

Polling (aka are we there yet?)

The polling technique involves repeatedly checking a signal to see if it changes. This can be inefficient and even miss edges if the wheel is spinning too quickly. Moreover, constantly having to check a signal takes up a lot of CPU time that could be used for other purposes. While polling is adequate in some cases, for an encoder we need something better.

Interrupts (aka hey kids we're here!)

The interrupt technique basically involves interrupting the CPU when something happens and making it run some code. After running that code, the CPU can resume normal operation from where it was interrupted. In the case of our Arduino, it has specialized hardware to generate an interrupt when an input pin changes state.

As is the theme for Arduino, all of the complexity of setting up interrupts is abstracted away so all you need to do is call one function.

Using interrupts, we can also get an accurate approximation of velocity (in ticks/second) by measuring the time between encoder ticks and taking the reciprocal.

Fill in the TODOs in the following code to reimplement your encoder tick counting from before and also add velocity measurements. Hint: The Arduino micros() function gives us the time elapsed since the code started running. Also keep an eye on the data types of variables and constants you're using (integer division discards the decimal!). Ask your mentor for help!

Checkoff #3

Aside