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).
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).
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 }