Difference between revisions of "Variable Frequency Electrosense"

From Mech
Jump to navigationJump to search
Line 50: Line 50:
*'''Pause''': board will stop moving (if it was already moving)
*'''Pause''': board will stop moving (if it was already moving)
*'''Forward''': board will move forward, towards the right (if not already moving forward) at a constant speed
*'''Forward''': board will move forward, towards the right (if not already moving forward) at a constant speed
*'''Reverse''': module will move reverse, towards the left (if not already moving reverse) at a constant speed
*'''Reverse''': board will move reverse, towards the left (if not already moving reverse) at a constant speed
*'''Accelerate''': module will increase speed (by decreasing the delay between clock pulses of the bipolar stepper motor by 1 ms)
*'''Accelerate''': board will increase speed (by decreasing the delay between clock pulses of the bipolar stepper motor by 1 ms)
*'''Slow''': module will decrease speed (by increasing the delay between clock pulses of the bipolar stepper motor by 1 ms)
*'''Slow''': board will decrease speed (by increasing the delay between clock pulses of the bipolar stepper motor by 1 ms)


'''Object Detection'''
'''Object Detection'''
Line 58: Line 58:
In order to run the Object Detection routine, you must press the "Data Acquisition" and "Activate" in sequence.
In order to run the Object Detection routine, you must press the "Data Acquisition" and "Activate" in sequence.


"Data Acquisition" should be pressed first and "Activate" after. The module will then perform calculations and plotting at its current position, then move a halfinch forward, and then repeat this process until it hits the limit switch at the end.
"Data Acquisition" should be pressed first and "Activate" after. The board will then perform calculations and plotting at its current position, then move a halfinch forward, and then repeat this process until it hits the limit switch at the end.


If you press "Activate" first, then "Data Acquisition" after, the module will move a halfinch forward from its current position, perform calculations and plotting after it stops, and then it will repeat this process until it hits the limit switch at the end. In this case, the step and location (according to the hashes) in the text file output will be off by 1 index.
If you press "Activate" first, then "Data Acquisition" after, the board will move a halfinch forward from its current position, perform calculations and plotting after it stops, and then it will repeat this process until it hits the limit switch at the end. In this case, the step and location (according to the hashes) in the text file output will be off by 1 index.


'''Object Identification'''
'''Object Identification'''


The Object Identification routine is performed automatically when "Data Acquisition" and "Activate" are pressed in sequence. Once the module has stopped moving and all 19 magnitude and phase bode plot points have been calculated and plotted, the program will compare the bode plot with library of bode plots of different object type in which the object was directly underneath the sensor and display the object index of the object with the closest matching bode plot for magnitude and phase in the space between the buttons. Thus, the program will try to identify whatever is underneath the sensor at its current position after each calculation and plotting routine (each time it has moved and stopped).
The Object Identification routine is performed automatically when "Data Acquisition" and "Activate" are pressed in sequence. Once the board has stopped moving and all 19 magnitude and phase bode plot points have been calculated and plotted, the program will compare the bode plot with library of bode plots of different object type in which the object was directly underneath the sensor and display the object index of the object with the closest matching bode plot for magnitude and phase in the space between the buttons. Thus, the program will try to identify whatever is underneath the sensor at its current position after each calculation and plotting routine (each time it has moved and stopped).


===Code===
===Code===
Line 80: Line 80:
*'''Display Object Type and Object Location''' - assuming that the sensor passes an object as it moves forward and runs through routines, program will run through an algorithm to determine where the object is according to the hashes on the back and what the object type is according to a bode plot library
*'''Display Object Type and Object Location''' - assuming that the sensor passes an object as it moves forward and runs through routines, program will run through an algorithm to determine where the object is according to the hashes on the back and what the object type is according to a bode plot library


*'''Debug space''' - You can uncomment some portions of our code to display the values of arrays and variables on the empty right side of the window. Currently, for a few seconds after a calculation and plotting routine (until the module finishes moving a halfinch), the program will display the 19 magnitude and phase points of the bode plot. It also currently continuously displays many of the variables used in the calc() function.
*'''Debug space''' - You can uncomment some portions of our code to display the values of arrays and variables on the empty right side of the window. Currently, for a few seconds after a calculation and plotting routine (until the board finishes moving a halfinch), the program will display the 19 magnitude and phase points of the bode plot. It also currently continuously displays many of the variables used in the calc() function.




Line 133: Line 133:
The sin1[] and sin2{} arrays collect the mapped nsin1[] and nsin2[] values respectively. They are mapped according to the sine wave plotting window dimensions.
The sin1[] and sin2{} arrays collect the mapped nsin1[] and nsin2[] values respectively. They are mapped according to the sine wave plotting window dimensions.


There is a corresponding integer variable to “finish” called “halfinch” in the PIC code that counts the 37 half-inches that the sensor/emitter module can move along the water tank. It is incremented each time after the module moves a half-inch forward. Right after the PIC finishes moving that half-inch, it needs to tell Processing that it is ready to start fft calculation and will begin to send data points over by sending this incremented “halfinch.” Even though we just want to send “halfinch” over, when you send data via rs232, you must send all the information over. So the PIC will first send out dummy data points plus an incremented “halfinch”. It is compared to “finish,” “kk” is reset back to zero and the bode plots are cleared.
There is a corresponding integer variable to “finish” called “halfinch” in the PIC code that counts the 37 half-inches that the sensor/emitter board can move along the water tank. It is incremented each time after the board moves a half-inch forward. Right after the PIC finishes moving that half-inch, it needs to tell Processing that it is ready to start fft calculation and will begin to send data points over by sending this incremented “halfinch.” Even though we just want to send “halfinch” over, when you send data via rs232, you must send all the information over. So the PIC will first send out dummy data points plus an incremented “halfinch”. It is compared to “finish,” “kk” is reset back to zero and the bode plots are cleared.




Line 249: Line 249:
Object identification algorithm code and object detection algorithm code.
Object identification algorithm code and object detection algorithm code.


Each time this function is run, it creates or appends data to a text file ("bodeplotdata.txt") in the folder where this sketch is located and records the step (how many halfinches the module has moved), location (where the module is according to the hashes etched on the back of the water tank), frequency (of voltage sine waves sent out), magnitude (in dB, at each frequency), and phase (in degrees, at each frequency).
Each time this function is run, it creates or appends data to a text file ("bodeplotdata.txt") in the folder where this sketch is located and records the step (how many halfinches the board has moved), location (where the board is according to the hashes etched on the back of the water tank), frequency (of voltage sine waves sent out), magnitude (in dB, at each frequency), and phase (in degrees, at each frequency).


=Test Results and Analysis=
=Test Results and Analysis=

Revision as of 13:01, 16 March 2010

Introduction

INSERT INTRO

Team

  • Tod Reynolds
  • Pill Gun Park
  • Joshua Peng

INSERT TEAM PIC

Concept Overview

Mechanical Design

Electrosense Water Tank - Mechanical Design

Linear Actuator - Mechanical Design

Electrical Design

Bipolar Stepper Motor Circuit - Electrical Design

Signal Generation Circuit - Electrical Design

Signal Amplification/Level Shifting Circuit - Electrical Design

Code

PIC Code

Processing Code

The Processing code will display a window on the computer monitor that allows the user to control the motion of the linear actuator and sensor, run through a single routine of calculation and plotting, and run through full object identification and object detection routines.

INSERT PIC OF WINDOW

Buttons

Sensing Control

  • Calibrate: returns the emitter/sensor board to the zero position. Unless it is in the middle of calculating and plotting, the board will immediately head in the reverse direction until it hits the limit switch and then it will go forward 200 steps so that the limit switch is completely released. All of our tests have been designed and analyzed according to and starting from this zero position. Once the Calibrate button is pushed, this process cannot be stopped unless someone flips the PIC power switch or the +12V/-12V power switch, etc.
  • Activate: activates the linear actuator so that the board can move
  • Data Acquisition: activates calculation and plotting (runs the Object Detection routine)
  • Calculate/Plot: runs through a single routine of calculation and plotting, the board will not move after the bode plots are generated.

Linear Actuator Control

Note: for these buttons to have any effect, the "Activate" button needs to be pressed before these buttons

  • Pause: board will stop moving (if it was already moving)
  • Forward: board will move forward, towards the right (if not already moving forward) at a constant speed
  • Reverse: board will move reverse, towards the left (if not already moving reverse) at a constant speed
  • Accelerate: board will increase speed (by decreasing the delay between clock pulses of the bipolar stepper motor by 1 ms)
  • Slow: board will decrease speed (by increasing the delay between clock pulses of the bipolar stepper motor by 1 ms)

Object Detection

In order to run the Object Detection routine, you must press the "Data Acquisition" and "Activate" in sequence.

"Data Acquisition" should be pressed first and "Activate" after. The board will then perform calculations and plotting at its current position, then move a halfinch forward, and then repeat this process until it hits the limit switch at the end.

If you press "Activate" first, then "Data Acquisition" after, the board will move a halfinch forward from its current position, perform calculations and plotting after it stops, and then it will repeat this process until it hits the limit switch at the end. In this case, the step and location (according to the hashes) in the text file output will be off by 1 index.

Object Identification

The Object Identification routine is performed automatically when "Data Acquisition" and "Activate" are pressed in sequence. Once the board has stopped moving and all 19 magnitude and phase bode plot points have been calculated and plotted, the program will compare the bode plot with library of bode plots of different object type in which the object was directly underneath the sensor and display the object index of the object with the closest matching bode plot for magnitude and phase in the space between the buttons. Thus, the program will try to identify whatever is underneath the sensor at its current position after each calculation and plotting routine (each time it has moved and stopped).

Code

Processing Window Contents

  • Plot the emitted and sensed sinusoidal voltage signals - in the top left quadrant, these signals are plotted with horizontal guidelines that hit the maximum peak of the waves which also display the amplitude (mapped analog voltage values) so that you can visually compare and calculate the magnitude just by analyzing this plot. These signals also have vertical guidelines that hit the vertical mid point of both waves so that you can check the phase shift between the emitted and sensed waves. The amplitude difference and phase shift is displayed in the top corners of the plot.
  • Plot the FFT Magnitude - the whole magnitude spectrum of the FFT of the emitted and sensed wave is plotted in the middle left quadrant. There are vertical guidelines showing the frequency at the peak for both the emitted and sensed wave and what it should be (the frequency of waves we are sending out of the AD9833 function generator chip). If the transfer function from the input (emitted wave) to the output (sensed wave) is linear, then there should only be a single peak in this plot.
  • Plot the FFT Phase - the whole phase spectrum of the FFT of the emitted and sensed wave is plotted in the bottom left quadrant. The same vertical guidelines used in the FFT Magnitude plot are used here.
  • Plot the Magnitude and Phase Bode Plots - an experimental magnitude and phase bode plot is plotted at 19 frequencies between 100Hz and 10kHz
  • Display Object Type and Object Location - assuming that the sensor passes an object as it moves forward and runs through routines, program will run through an algorithm to determine where the object is according to the hashes on the back and what the object type is according to a bode plot library
  • Debug space - You can uncomment some portions of our code to display the values of arrays and variables on the empty right side of the window. Currently, for a few seconds after a calculation and plotting routine (until the board finishes moving a halfinch), the program will display the 19 magnitude and phase points of the bode plot. It also currently continuously displays many of the variables used in the calc() function.


Processing Code - Description of Functions

Our full Processing code is much too long to post here, so we will instead post a link here and at the end of this section to a zip file with the actual code with comments. The following section will describe what each function in our code does.

initserial()

Processing is set up so that it is continuously looking for data on the rs232 lines and once it sees data it will automatically start retrieving it. Once the sensor has finished moving and fully stopped, the pic will send 5 arrays each with 512 data points to Processing. The first set of arrays (data points) retrieved are of the 100 Hz voltage sinusoidal signals emitted and sensed. Next, there is a delay in the PIC, the sampling frequency is adjusted, the frequency of the signals is increased to 200 Hz, the fft of the signals is calculated, and then the next set of arrays corresponding to the 200 Hz wave is sent out and received by Processing. This process is repeated until it goes through all 19 frequencies (100 Hz, 200 Hz, 300 Hz,…,10 kHz).

1. vector of frequencies (x axis) used to calculate the fft in the PIC, note that the first half (first 256 points) is nonzero, the second half is deleted (all zeros) in the PIC because it is essentially the same as the first half but in reverse order (256 frequency points + 256 zeros)

2. y axis points of the emitted sinusoidal signal (512 emitted wave points)

3. y axis points of the sensed sinusoidal signal (512 sensed wave points)

4. real component of the complex values resulting from the fft of both emitted and sensed signals. First 256 points are of the emitted signal, second 256 points are of the sensed signal. (256 emitted wave points + 256 sensed wave points)

5. imaginary component of the complex values resulting from the fft of both emitted and sensed signals. First 256 points are of the emitted signal, second 256 points are of the sensed signal. (256 emitted wave points + 256 sensed wave points)

6. integer variable “finish” that corresponds to “halfinch” in the PIC code. (more on this below)

    // grab the data
    if ((line_received.length == 6) && (portNumber==0)){
      if (index >= L){
        index = 0;
        newdata = true;
        for (int i=0;i<L;i++) {
        sin1[i] = map(nsin1[i],min(nsin1)-300,max(nsin1)+300,-1*int(by*3/8),int(by*3/8));
        sin2[i] = map(nsin2[i],min(nsin2)-300,max(nsin2)+300,-1*int(by*3/8),int(by*3/8)); 
        }
      }
      freq[index] = line_received[0];
      nsin1[index] = line_received[1];
      nsin2[index] = line_received[2];
      real[index] = line_received[3];
      imag[index] = line_received[4];
      
      if (line_received[5] > finish) {
        kk = 0;
        reinitializebp();
        finish = int(line_received[5]);
      }
      index++;

Note that we use this “newdata” Boolean variable to ensure that once we start collecting data points, we will finish collecting all 512 data points before we can use the data points for calculating and plotting in the other functions.

The sin1[] and sin2{} arrays collect the mapped nsin1[] and nsin2[] values respectively. They are mapped according to the sine wave plotting window dimensions.

There is a corresponding integer variable to “finish” called “halfinch” in the PIC code that counts the 37 half-inches that the sensor/emitter board can move along the water tank. It is incremented each time after the board moves a half-inch forward. Right after the PIC finishes moving that half-inch, it needs to tell Processing that it is ready to start fft calculation and will begin to send data points over by sending this incremented “halfinch.” Even though we just want to send “halfinch” over, when you send data via rs232, you must send all the information over. So the PIC will first send out dummy data points plus an incremented “halfinch”. It is compared to “finish,” “kk” is reset back to zero and the bode plots are cleared.


global variables and setup()

The most important variables:

int kk = 0; frequency counter, kk is incremented by 1 each time draw() is run. Once it reaches 19 (frequency of signals at 10 kHz), it set to 0 (frequency of signals at 100 Hz) again.

int L = 512; total number of data points retrieved from PIC each time data is sent over.

draw()

The sequence in which our functions are carried out is important.

1. labels() is run to display labels

2. “newdata” allows us to run the rest of the functions only if we have finished getting all the data from the pic via rs232 and we have not reached the final 19th frequency

3. calc(), sinewaves(), fftmagplot(), and fftphaseplot() each have a Boolean variable “stepx” (x = 1 to 4, respectively) to ensure that the next function can only be run once the previous one has finished. The Boolean variable “calculated” ensures that these first 4 functions have all finished before draw is run again; essentially “calculated” in draw() serves the same purpose as “newdata” in initserial().

4, Once we reach the last frequency (kk reaches 19), bodecurve() and record() are run.

void draw() {
  
  labels();  
  if (newdata == true && kk<19) {
    if (calculated == false && step4 == false) {
    delay(200);
    calc();   
    if(step1 == true) { sinewaves(); }
    if(step2 == true) { fftmagplot(); }
    if(step3 == true) { fftphaseplot(); }
    calculated = true;
    }
    if(kk<19 && calculated == true && step4 == true) {
    calculated = false;
    newdata = false;
    step1 = false;
    step2 = false;
    step3 = false;
    step4 = false;
    println(kk);
    kk++;
    }
    if (kk==19) {
    bodecurve();
    record();
    newdata = false;
    delay(100);
    } 
  }
}


labels()

This function is full of text() functions used to display labels for the plots.


calc()

1. The data in real[] and imag[] arrays are split up into four 256 point arrays to carry the real and imaginary components of the complex fft values of the emitted and sensed waves.

2. The fft magnitude and phase points are calculated and stored in arrays.

3. The first fft magnitude point for both emitted and sensed sine waves arrays is ignored.

4. The index of the peak points of the fft magnitude arrays of the emitted and sensed waves are found.

5. The first 4 points on the emitted and sensed sine waves are averaged and stored in arrays to be used in plotting in sinewaves().

6. Two zero/mid points of both sinewaves are found to determine a single period (zero point to zero point) in both waves and the phase shift.

7. Display of variable and array values.

8. records and plots the magnitude and phase bode plot points


sinewaves()

1. Plots the voltage sine waves of the emitted and sensed signals.

2. Displays vertical guidelines that hit the zero point measured in the emitted and sensed sine waves.

3. Displays a horizontal guideline that hits the peak of the sine waves and displays the value of the peak of both emitted and sensed waves.


fftmagplot()

Plots a curve of the fft magnitude with vertical guidelines hitting the peak point which are the actual, calculated frequency of the emitted and sensed waves. There is also a vertical guideline for the expected frequency of the emitted and sensed waves. These frequency values should be equal at all times, but you will see that the actual frequencies deviate from the expected frequency a little.


fftphaseplot()

Plots a curve of the fft phase with vertical guidelines of the actual, calculated frequency of the emitted and sensed waves. These are the same guidelines in fftmagplot().


bodecurve()

This function is run after all 19 different frequency sine waves are sent from the PIC and plotted. The main purposes of this function are to connect the magnitude and phase bode plot points plotted in calc() with a curve line and to display the magnitude and phase values at each frequency.


reinitializebp()

Clears the bode plots


record()

Object identification algorithm code and object detection algorithm code.

Each time this function is run, it creates or appends data to a text file ("bodeplotdata.txt") in the folder where this sketch is located and records the step (how many halfinches the board has moved), location (where the board is according to the hashes etched on the back of the water tank), frequency (of voltage sine waves sent out), magnitude (in dB, at each frequency), and phase (in degrees, at each frequency).

Test Results and Analysis