User Tools

Site Tools


cso:c_proj

C language lab

Exercise 1

The main purpose of this exercise is to program a very simple diagnostic system. It reads the values of the induction motor current, and detects the overload (for now, detects exceeding some value of current), and gives RMS. It also provides self-diagnostics which makes sure that data is read correctly.

Skills:

  • Reading data from file
  • Doing some operations on data
  • Using “watchdog”

Grading: points are given in square brackets.

Lab Contents

1. Reading Data Example

  1. Consider example file: read_data.c
  2. download it, read and compile
  3. download file with some data data1.dat (or make it - text file with a few numbers in a column)
  4. see what happen when you pass the name of that file to the compiled executable
     ./a.out data1.dat 
  5. modify file read_data.c to count number of values read from file

2. Simple Diagnostic System

  1. Download a data file ac_current.dat. This file contains measurements of induction motor current under a varying load recorded with sampling freqency 1000Hz.
  2. using your previous program (read_data.c) write a program which reads the data in and stops after reading the value exceeding some given x [2] (where x is a value given as a parameter [2]). The program should write out the value as well as the number of values read so far (index) on the standard error output [2] (use fprintf(stderr,…)).
  3. modify your program to calculate a mean of three recent numbers [4], and stop after the mean is greater than y [1] (where y is given as a a parameter [2]). The program should write out the value as well as the number of values read so far on the standard error output [1]. You may use an array to collect required data and modulo (%) operator. Example:
    float array[3]={71,72,73};
    int i=0,k=0;
    for(i=0;i<30;i++)
    {
      k=i%3;
      printf("%f \n",array[k]);
    }

    In this example, while i changes from 0 to 30, k changes only from 0 to 2. Remember that in c language the first index in array is 0 and the last is n-1 while n is the size of array.

  4. modify your program to calculate RMS value of the given current [6]. The program should write out calculated values [1] on the standard output. To see the waveform of the RMS value as well as input current, you can use program gnuplot. Saving data from your program is possible by output stream redirection redirection mark '>' in the command line i.e:
     ./a.out ac_current.dat > rms_value.dat 


    Note if the output file doesn't exist, it will be created, if it exists it will be overwritten.
    To plot data from a file, you can write the following command:

    echo 'plot "ac_current.dat" with lines; pause mouse' | gnuplot
  5. Test your program to make sure that it works.

3. Watchdog

A watchdog is a process which makes sure that other processes work properly. It is often used to make sure that data from sensors is read.

Enhance your program to accommodate a watchdog in the following way.

Stage 1

Introduce a shared memory data containing information about how many measurements have been read in, it will be needed to set up a watchdog.

  1. Declare a shared memory region which will hold a single integer [2].
  2. Alter your program to store current index value (number of measurements read in so far) in this region [2].

Stage 2

Program the watchdog. It will check if data is read at least 1000 times a second.

  1. Make two processes out of your program: a child process which reads data and provides calculations and a parent process which behavior is described below [4].
  2. The parent process sleeps for 1 second, then [1]
  3. reads the shared memory index value, then [1]
  4. if the difference between current index value and the previous one is greater than 1000 goto 2, else [1]
  5. display a message 'Input data flow is too slow at index:' followed by the index value [1] on the standard error output (use fprintf(stderr,…)).
  6. Verify if the watchdog works by introducing a delay (using usleep() or sleep() in the child process while reading some given index) [2].
cso/c_proj.txt · Last modified: 2021/01/08 14:10 (external edit)