Project 3: Follow the Spot

This project reinforces the resource sharing concepts learned in class by extending the ppservo program written for project 2. The basic idea of this project is to project a spot using an LED mounted on a servo and have second servo track the motion of the spot. The shared resource is the parallel port data register. Assuming each servo is controlled by a different thread, each thread can read the current port value, modify the bit for its servo, and write it back, but if both threads try to change the port value at the same time, one of thread's changes may be lost.

The Setup

For this project your RTLinux module will control two servos instead of one. Servo0 is connected to D0 of the parallel port as in the previous project, and will be controlled by a user-interface program. This servo has an LED attached that projects a spot. The angle of the LED should be controlled by the user. For convenience, you can download a graphical version of the servo control program.

Servo1 is controlled by D1 on the parallel port, and has a photoresistor attached to it. The photoresistor's resistance is proportional to the amount of light hitting the sensor. The photoresistor is part of a resonant circuit (using a 555 timer) that produces a square wave with frequency proportional to the resistance. As the light hitting the photoresistor increases the frequency of the square wave decreases.

You are to design an RTLinux module that makes servo1 search for and follow the bright spot projected by the LED on servo0. The control for servo0 is essentially the same as was used in project 2. The only difference is that two servos are controlled by the parallel port instead of one. Since the frequency of the square wave input is proportional to the light seen by the sensor, your module should move the servo1 to maximize the frequency of the signal (i.e., minimize the period).

The following code segment reads the square wave from the parallel port and computes a "tick" count that is proportional to the period of the signal from the photoresistor circuit.

void *freq_thread(void *arg)
{
        int cnt;                /* # counts since last period start */

        pthread_make_periodic_np(pthread_self(), gethrtime(), 500*US);
        cnt = 0;
        /* sync up with next falling edge (discard partial period) */
        rtl_printf("starting sync");
        while(pulse_in() == 0) {
                pthread_wait_np();
                cnt++;
        }
        rtl_printf("looking for 0\n");
        while(pulse_in() == 1) {
                pthread_wait_np();
                cnt++;
        }
        rtl_printf("synch'ed in %d micro seconds\n", cnt * 500);

        /* start computing the frequency here */
        while(1) {
                pthread_wait_np();
                cnt = 0;
                /* count ticks over one period them */
                while(pulse_in() == 0) {
                        pthread_wait_np();
                        cnt++;
                }
                while(pulse_in() == 1) {
                        pthread_wait_np();
                        cnt++;
                }

                /* insert control code here */

                /* write computed angle to servo1 fifo buffer */
                rtf_put(SERVO1_FIFO, &out_byte, 1);
        }

        return NULL;
}		
		
This code segment measures the number of 0.5 ms ticks in one period of the pulse generated from the photoresistor and writes the angle to which to move the servo into a real-time fifo (you have to compute the angle yourself.) The precise period is unimportant; your control should just try to minimize it. By communicating to the thread controlling servo1 through the fifo gives servo1 the same control interface as servo0.

Random Hints:

What to Submit

For this project you will submit the source code for your RTLinux module and a documentation file in either ASCII text or PDF format. In your report, identify shared resources in your program, briefly discuss how many threads you used and what they all do, and describe your control algorithm. Please be brief. Several pages should be sufficient. As with project 2 we will do interactive grading for project 3.