Assignment #1
Interactive C Programming

Overview

The goal of this assignment is to familiarize yourself with the 6.270 microprocessor board and learn how to program it using Interactive C on the Apple Macintosh computers in the robot lab.

Note: If you are a computer novice, or have used computers but never programmed them, or have programmed in other languages but never in C, you may wish to work through Assignment #1 - Tutorial outside of class or during an extra session. Note that the tutorial is not a substitute for successful completion of Assignment #1.

Text

IC Manual
all
IC Quick Reference
all
Rich's Hints & Kinks
Batteries Computer Boards Push-Button Switches
Control Software I Interactive C (IC) Warning About Hot-Swapping of Components

Exercises (375 and 475 required)

  1. Plug your board into the computer and initialize and reset it as described in the IC Manual, pp. 2-3. Start IC, download the music.c program to the microprocessor board and play the theme song from The Pink Panther by interactively calling pp();

  2. Interactively evaluate the following C expressions (and any others you would like to try):

    1. 1+2;

    2. printf("Hello, world!");

    3. {int i; for(i=0; i<10; i++) printf("%d ",i);}

    4. Try making some sounds with the beeper using the tone() function with different frequencies and durations. Note that the arguments to tone() must be of type float.

    5. Plug a motor into motor port 0 and set its speed using the function call motor(0,50); Try other values for the power level.

    6. Plug a photoresistor into analog port 20 and read its value using analog(20); Explore how this value changes as you vary the light intensity.

  3. Write, download, and run a C program that continuously adjusts the speed of the motor so that it is directly proportional to the light intensity received by the photoresistor. That is, the motor should run fast in bright light and slow in dim light. Your program should use global variables to define the sensor and motor ports. Demonstrate your running program to one of the lab assistants and show them your code.

  4. Write a function int hailstone(int n) that returns the next integer in the hailstone sequence, defined by:

    ni + 1 = ni / 2 if ni is even
    ni + 1 = 3ni + 1 if ni is odd

    The hailstone sequence terminates when ni = 1. Use your function in a program that generates the hailstone sequence beginning with ni = 13 and saves each element in an array. After the sequence terminates, your program should display each element of the array in turn on the LCD, using the Choose button to advance to the next element. Demonstrate your running program to one of the lab assistants and show them your code. Be sure to clearly comment and modularize your code.

  5. Use the multi-tasking abilities of IC to play a song with the beeper using the tone() function while controlling the motor speed with light as described in Exercise #3. Below are the frequencies in Hertz for the octave starting at A below middle C on the piano:

    A A# B C C# D D# E F F# G G# A
    440 466 494 523 554 587 622 659 698 740 784 831 880

    Double the values to get the next higher octave; halve the values to get the next lower octave. Demonstrate your running program to one of the lab assistants and show them your code. Be sure to clearly comment and modularize your code.

Graduate Exercises (475 required, 375 optional)

Do any 2 of the following:

  1. Write a program that monitors and displays the status of the Choose and Escape buttons and the 4 DIP switches on the expansion board.

  2. Count the elapsed seconds by flashing LED0 and LED1. Use your imagination: blink them synchronously, alternately, count by 4s in binary, etc.

  3. Implement a stopwatch. Use the Choose button to start/stop and the Escape button to reset. Display the output on the LCD screen in the format mm:ss:mss. Use persistent global variables (see IC Manual, p. 9) to store the time elapsed between power on/power off cycles.

Graduate Lab Report (475 required)

Your report should include:

  1. A short introduction;

  2. A written description of the design, function, and performance of the programs you wrote for Exercises 3, 4, 5, and the two graduate exercise programs you wrote, including (and integrated with) block diagrams of the code;

  3. An appendix containing all of the commented code listings; and

  4. A discussion of any unusual or unexpected behavior of IC (especially if you have used other dialects of C).

This lab report is the first (of two) during the "grace period" for grading. That is, the instructors will grade this report (using the rubric matrix criteria given in the Graduate Lab Reports document) and discuss it with you, but the grade will not count towards your course grade.

In addition, you must self-grade your report (using the same rubric matrix) and submit this self-evaluation at the same time you hand in the report. This will be most effective if you perform the self-grading at least a few hours (preferably an entire day, if possible) after you complete the report. The instructors will compare your grade with theirs, hopefully to ensure that you understand the basis for assigning the grade. A Lab Report Grade Blank is available on the course webpage, as well as at the end of the Graduate Lab Reports Document.

Note: Since there is nothing mechanical about this Assignment (it's all programming), no grade for Mechanical Design can be assigned.


A Sample IC Program

/* A sample IC program for a "singing" and "dancing" robot */
/* RDB 8/14/96                                             */
/* 9901.14  debugged by Josh Kershner, Spring 1999         */

/* Motor port assignments */   
int LEFT_MOTOR  = 0;
int RIGHT_MOTOR = 1;

/* Globals */
int LINEAR_SPEED     = 100;
int ROTATIONAL_SPEED = 50;

/* Motor control routines                                            */
/* Each routine generates a particular movement for a given duration */
void Forward(float duration)
{
    motor(LEFT_MOTOR,LINEAR_SPEED);
    motor(RIGHT_MOTOR,LINEAR_SPEED);
    sleep(duration);
    alloff();
}

void Backward(float duration)
{
    motor(LEFT_MOTOR,-LINEAR_SPEED);
    motor(RIGHT_MOTOR,-LINEAR_SPEED);
    sleep(duration);
    alloff();
}

void LeftTurn(float duration)
{
    motor(LEFT_MOTOR,-ROTATIONAL_SPEED);
    motor(RIGHT_MOTOR,ROTATIONAL_SPEED);
    sleep(duration);
    alloff();
}

void RightTurn(float duration)
{
    motor(LEFT_MOTOR,ROTATIONAL_SPEED);
    motor(RIGHT_MOTOR,-ROTATIONAL_SPEED);
    sleep(duration);
    alloff();
}

/* The main program                                                       */
/* Note that other "songs" and "dance steps" can be substituted for these */
void main()
{
    while (1)
    {
        beep();
        Backward(0.25);
        Forward(1.0);
        LeftTurn(0.5);
    }
}

Back to Getting Started in LEGO 375/475
Forward to Assignment #2
Back to the Autonomous Robotics Home Page