CPS 6: Assignment #5

Embedded Real Time Systems
A Cardioverter/Defibrillator Simulator

Due: Tuesday, Oct. 31, 8am

12 points

1 Introduction

This program is based on the excursion section from Chapter 5, but uses a modified heart class (and some other classes). The program is designed to simulate a cardioverter/defibrillator -- an electric device implanted in a person's heart to monitor heart rhythms and to administer an electric shock when those rhythms indicate that the heart is not functioning correctly. You should read the information in Section 5.14 to familiarize yourself with what a zero-crossing-count is.

In this program you will be modifying both the client program, cardiov.cc and the implementation of the Heart class in heart.cc.

The program is designed to enable you to get used to the idea of developing a program by a process of iterative enhancement --- of increasing the functionality of a program in steps rather than trying to develop it all at once.

1.1 Getting Started

You should create a subdirectory named assign5 in your cps6 directory.

This should copy the following file(s): Makefile, cardiov.cc, heart.cc, heart.h, monitor.cc, monitor.h, h1.dat.

You'll need to link to an animator by typing the following command:

    ln -s ~rodger/bin/animator anim

Verify that these file(s) have been copied and linked by typing ls to see all the files in the current directory. In particular, the Makefile for this assignment is different than makefiles from previous assignments.

2 Using the animator

We'll be using the Xtango animator to display the monitor and heartbeats. The animator will only work under X windows; any of the Sparc 5's should work fine. Each time you login to work on this program, you'll need to type the following command once:

    xrdb -merge ~rodger/bin/xtango.res

Now to use the animator, first compile cardiov.cc by typing make cardiov. The monitor class outputs animator commands that are understood by the Xtango animator. To run your program, all output will be piped into the animator by typing:

          cardiov | anim

An Xtango window should appear (if it doesn't, did you type the xrdb command above?). You might need to move the Xtango window so you can see it and the xterm window at the same time. The column on the right edge of the Xtango window is a speed control bar which is currently set on the highest speed. Move the rectangle in this column to midway to slow the animation down. To start the animation, click on the button run animation. In the xterm window, you are asked to type in the name of an input file. Type h1.dat and press return. You should see the monitor warm up, and then display heart beats. To quit at any time, click on the quit button.

IMPORTANT NOTE: All output is being sent to the animator. If you want to print anything to the screen (for debugging purposes or otherwise) you'll need to print the word comment at the begining of each output line). That is, each cout statement in your program should start with:

    cout << "comment " << .... 

3 What to Do

You will first modify cardiov.cc so that zero-crossing-counts are determined and the heart shocked. Then you will modify the implementation of the Heart class in heart.cc.

In this simulation, all heart beats are integer voltages in the range of -15 to +15. A file of numbers can be used to simulate a heart or the heart can generate its own voltages. To debug your program, you should use the file of voltages since this is predictable. After your program works with the file of voltages you'll be able to change it so that the heart generates its own voltages.

One file of voltages, h1.dat, is provided to you. This file is barely adequate for testing your program, you should create your own file of voltages (using emacs or some other editor) so that you can be sure that your program works

You should be sure to do the following program enhancements in stages. Make sure that each stage is finished and working before proceeding to the next stage.

3.1 Modifying cardiov.cc

(Based on exercise 5.8 on page 262)

  1. Enhance the program cardiov.cc to count each series of 20 readings and print a message each time that a period of 20 readings has occurred. (Hint: don't use another loop, use a counter and an if statement).

  2. Enhance the program to count zero-crossings for each period of 20 readings. Be sure to be careful about boundary cases (first reading, first reading in a new period, etc.).

  3. Finally, monitor the zero-crossings and administer a shock using the Shock method when necessary according to the specifications given in the excursion section. When calling the shock function you'll write either

        heart.Shock(Heart::SpeedUp);
    

    to speed the heart up, or you'll write

        heart.Shock(Heart::SlowDown);
    

    to slow the heart down.

Be sure to test your program completely by developing good test files. Your grade for this part of the assignment will be based on whether your program correctly computes zero-crossings (70\%) and on style (30\%).

3.2 Modifying heart.cc

Once your program calculates zero-crossing counts, you'll change the definition of the variable theHeart in cardiov.cc so that no data file is used. Instead, use the default constructor as shown below.

    Heart theHeart;             // heart will 'sense itself'

Remake the program (make cardiov) and run it. It will ``beat forever'', so to stop it you should type control-C (hold the control key down and press C).

You'll make several changes to the heart.cc program. Be sure that each change works before proceeding to the next change.

  1. Change the heart so that instead of beating forever it stops after 1,000 senses. To do this change the member function Sense() so that INT_MAX is returned when the number of senses is greater than 1,000 similarly to what is done when there is no more data in the data file. Type make cardiov and rerun the program to check your modifications. Note that you typed make cardiov since that is the name of the executable program, even though it was heart.cc that was changed.

  2. Change the parameterless constructor Heart() so that it takes an int parameter specifying the number of senses before the heart ``shuts down''. To do this you'll need to modify the header file heart.h. You'll need to add a private data variable, call it myMaxBeats. You'll then have a constructor whose header, in heart.cc, is shown below; in the body of the constructor you should set myMaxBeats and then use this variable in the function Sense.

        Heart::Heart(int maxbeats)
    

    Be sure to make cardiov to check your work.

  3. Modify the function SelfSense() so that each time the heart is sensed, there is a five-percent chance that the heart speeds up and a five-percent chance that the heart slows down. To do this, roll a 100-sided die and check to see if the value is less than or equal to 5 (speed up) or greater than or equal to 96 (speed up). To speed up, multiply the value of myFrequency by 1.1. To slow down, divide the value by 1.1.

    Be sure to check that this works.

  4. Modify the function SelfSense() so that there is a one-tenth of one percent chance (one chance in a thousand) that the heart stops beating altogether (no shocking will change it). You may decide to add other private data variables or write other member functions.

You can be creative, if you want to add other functionality to either cardiov.cc or to the Heart class be sure to document what you've done in both your README file and in the appropriate .cc and .h files.

4 What to submit

You should submit all programs that you modified as part of this assignment. This should include cardiov.cc, heart.cc, heart.h, and a data file that you think is a good test of the program (a sequence of values to be read) called heart.dat. As always, submit a README file indicating about how long it took for this assignment, who (if anyone) you worked with, and any comments you have about the assignment.

     submit6 assign5 README cardiov.cc heart.dat heart.cc heart.h