Lab 4: Parallel I/O and the Lego Motors and Sensors

The purpose of this lab is to learn how to use the Parallel Interface (JP1 and JP2 connectors on the DE2 board) to drive the LEGO motors and read the LEGO sensors. You will also learn to use the Timer.

In the first part of this lab, you are to build a light-tracking device using the DE2 board, the Lego controller, and the Lego kit, as pictured below. In the second part you are to slow down the motor speed using pulse width modulation (no physical changes).

Picture of Lab 4 setup
Figure 1: Lego Nose Diagram

Part 1

The basic idea is this: the Nios II processor will control a motor (via the JP1 or JP2 port and the Lego controller) that rotates a platform, as illustrated above. On that platform are two light sensors, separated by a "nose". You must build the platform and write a Nios II program that will rotate the platform until the nose is pointing at a light source. The trick is to realize that the nose prevents the light source from shining on both sensors at the same time, until light is directly in front of the nose. Your program should attempt to line up the nose with the light source as quickly as possible. This means that your program should choose the direction of rotation of the platform intelligently - i.e. it shouldn't just always rotate in one direction until both sensors are illuminated.

The Lego Controller device documentation has a link (in the "Reference" section) to a website with a number of booklets containing sample Lego constructions. Booklet E describes the building of a Lego robot arm. Use the diagrams in that booklet to build the base of the device described above. Steps 1 to 8 on pages 3 to 5 (and substeps 1-4 on page 5) show you how to create a rotating platform. After that, you have to construct the platform, nose, and sensor assembly.

The light will be a Lego light that can be powered directly from the Lego controller, through the connector on the top (see the Lego Controller device documentation).

Part 2

Without making any physical changes to your lego setup, you will slow down the motor by using a technique called Pulse Width Modulation (PWM). The idea is to toggle the Motor On bit rapidly so that the motor doesn't get a full voltage. So whenever there is no light sources detected, you will turn the motor on for X cycles and then off for Y cycles.

You will use the Timer peripheral to accurately time the X and Y cycles, DO NOT USE DELAY COUNTERS, YOU MUST USE THE TIMER. You can learn about the timer in the Nios II Devices section on the MSL website. Create a function that takes as argument the number of cycles to wait, the function then initializes the timer, tells it to go, and waits for it to timeout before returning. Using this function you will implement the code described below and merge it with Part 1 to create a new program. Feel free to experiment with different X and Y values (we use X=5000 and Y=3000 in the example):

/* NOTE: This is meant as pseudo-code, you are to use assembly */
if (no light detected)
{
  Turn Motor ON
  Delay 5000 cycles
  Turn Motor OFF
  Delay 3000 cycles
}

Preparation

  1. Read the Lego Controller Device Documentation, which describes how the Lego motors and sensors work, and how they are connected through the Lego controller to the DE2 board's parallel port (either JP1 or JP2). Study all diagrams and get a sense of how to build the device described above. For help on how to read the documentation you may wish to consult the Device Documentation Template. Also read the Timer Documentation.
  2. Answer the following questions, assuming the Lego controller is plugged into JP1:
    1. To configure all the motors as outputs I have to write the value ________ into address ________.
    2. To configure all the sensors as inputs I have to write the value ________ into address ________.
    3. To turn on motor 2 I have to set bit ______ at memory location ________ to the value ______.
    4. To read the sensors I have to read from memory location ________.
    5. After reading the sensor values, write a snippet of code that branches to "LABEL" if sensor #4 is on.
  3. Answer the following questions about the Timer:
    1. Does the timer count up or down?
    2. How do you initialize the timer's period?
    3. How do you read the timer's current value? HINT: you have to take a "snapshot" before reading.
    4. How can you check if the timer has completed its period?
  4. Write and assemble both assembly language programs described above.

In the Lab

Demonstrate both your working programs (separately) to the TAs.

Tips

Good luck!