Difference between revisions of "Variable Frequency Electrosense"

From Mech
Jump to navigationJump to search
Line 96: Line 96:
===AD9833 Chip control===
===AD9833 Chip control===


AD9833 requires 16 binary bits for its input. The ad9833_set_freq(int value[]) function will send an array of binary numbers to the AD9833 chip. In this function, if the binary number is 0, we set the SDATA pin to low and if the binary number is 1, we set the SDATA pin to high. This process is performed for all 16 bits. According to the datasheet for AD9833, see [http://www.analog.com/static/imported-files/Data_Sheets/AD9833.pdf here], the chip reads the data when FSYNC is low and at the low edge of the SCLK signal. Therefore, we set the FSYNC pin to low only when we are sending SDATA bits. Also, we initially set the SCLK pin to high and then set it back to low right after we send out SDATA.
AD9833 is supposed to function under SPI control, however we found that bit-banging the registers worked better. The chip requires 16 binary bits for its input. The ad9833_set_freq(int value[]) function will send an array of binary numbers to the AD9833 chip. In this function, if the binary number is 0, we set the SDATA pin to low and if the binary number is 1, we set the SDATA pin to high. This process is performed for all 16 bits. According to the datasheet for AD9833, see [http://www.analog.com/static/imported-files/Data_Sheets/AD9833.pdf here], the chip reads the data when FSYNC is low and at the low edge of the SCLK signal. Therefore, we set the FSYNC pin to low only when we are sending SDATA bits. Also, we initially set the SCLK pin to high and then set it back to low right after we send out SDATA.
<pre>
<pre>
#define PIN_A2 LATAbits.LATA2 //FSYNC
#define PIN_A2 LATAbits.LATA2 //FSYNC

Revision as of 14:23, 18 March 2010

Introduction

Our objective was to build upon existing research being done at Northwestern utilizing Electrosense technology by testing if information can be derived from varying the emitter frequency. We sought to send sinusoidal waves at discrete frequencies between 100 Hz and 10 kHz and to read in the sensed wave using a PIC 32’s ADC. We then sent the gathered information to a PC for plotting and analysis. By mounting the sensor on a one dimensional linear actuator we are able to gather additional data about objects and perform object detection and identification algorithms. While our initial results have revealed exciting trends, farther research is necessary before any significant conclusions can be made. A video of the project is available on YouTube.

Team

  • Tod Reynolds, EECS 2010
  • Pill Gun Park, EECS 2011
  • Joshua Peng, BME 2017

Team26-Group Pic.jpg

Mechanical Design

Electrosense Water Tank

Linear Actuator

Electrical Design

Stepper Motor Circuit

TR JP PP-stepper circuit.png

Signal Generation Circuit

The AD9833 is a surface mount function generating chip that allows us to emit analog signals at discrete frequencies under control from the PIC. Because the the signal out of the chip is between 0 and 900 mV an amplifier is used to boost the signal by a factor of 2.2.

TR JP PP-AD9833 circuit.jpg

Team26-ad9833.jpg

Parts

  • IC's
    • 1x AD9833
      • Surface Mount Chip
      • Custom PCB
    • 1x Op amp(LM741)
  • Capacitors
    • 2x.01uF
  • Resistors
    • 1x 1K
    • 1x 2.2K
  • Miscellaneous
    • 20 MHz Clock


Signal Amplification/Level Shifting Circuit

The sensed wave is generated by reading in the two waves at the sensor electrodes and comparing their signals.

  • Each signal is initially buffered using a voltage follower
  • Two signals are run though balancing circuitry to allow for a "zeroing" of the sensor
  • Two signals serve as inputs to Instrumentation amp which has a fixed gain of 1000
  • Output signal is run through a bandpass filter
  • Final signal is level shifted so that it can be read into the PIC's ADC which requires the signal be between 0 an 3.3 volts.

Note that this circuit is built in solder due to the extremely small voltage changes that need to be detected between the two sensed waves by the instrumentation amp.


TR JP PP-Amplification Circuit.png

TR JP PP-Amplifier Pic.jpg

Parts

  • IC's
    • 3x Op Amps(LM741)
    • 1x Inst Amp(INA129)
  • Capacitors
    • 2x .1uF
    • 1x 1uF
    • 1x .02uF
  • Resistors
    • 1x 1K Potentiometer
    • 2x 47K
    • 2x 5K
    • 1x 2K
    • 1x 3K
    • 2x 1K
    • 1x 6.4K
    • 1x 3.3K
  • Miscellaneous
    • 50 Pin Header
    • Solder Board


Code

PIC Code

The PIC completes 5 main tasks:

  • controls the AD9833 chip to generate voltage sinusoids
  • receives the emitted and received waves and generates values corresponding to the analog signals such that each sinusoid period is 16 points long for each of the 19 frequencies
  • calculates the FFT of the emitted and received waves
  • controls the motor and linear actuator
  • sends data to the PC (Processing) via RS232


Download the full version of PIC code

AD9833 Chip control

AD9833 is supposed to function under SPI control, however we found that bit-banging the registers worked better. The chip requires 16 binary bits for its input. The ad9833_set_freq(int value[]) function will send an array of binary numbers to the AD9833 chip. In this function, if the binary number is 0, we set the SDATA pin to low and if the binary number is 1, we set the SDATA pin to high. This process is performed for all 16 bits. According to the datasheet for AD9833, see here, the chip reads the data when FSYNC is low and at the low edge of the SCLK signal. Therefore, we set the FSYNC pin to low only when we are sending SDATA bits. Also, we initially set the SCLK pin to high and then set it back to low right after we send out SDATA.

#define PIN_A2			LATAbits.LATA2  //FSYNC
#define PIN_A3			LATAbits.LATA3	//SCLK
#define PIN_A14			LATAbits.LATA14	//SDATA

//send the AD9833 16 bits
void ad9833_set_freq(int value[]){
	 int i;
	 PIN_A3 = 1;                          // set SCLK pin high
	 PIN_A2 = 0;                          // set FSYNC pin low
	 for (i=0;i<=BITS16-1;i++){
	   PIN_A3 = 1;                        // set SCLK pin high
	   Delayms(1);
	     if (value[i] == 0){
	       PIN_A14 = 0;                   // set SDATA pin low
	     }
	     else {
	       PIN_A14 = 1;                   // set SDATA pin high
	     }
	   Delayms(1); 
	   PIN_A3 = 0;                        // set SCLK pin low
	   Delayms(1); 
	 }
	 PIN_A3 = 1;                          // set SCLK pin high
	 PIN_A2 = 1;                          // set FSYNC pin high
	 Delayms(3);
}

In order to generate a wave from an AD9833 chip, we need to reset the chip and send certain control bits to configure the type of wave that will be sent out. Then, we send the lsb and msb of the frequency register value. This value will be saved in the frequency register of the chip and will be used to calculate the actual frequency of the output wave. Next, we have to "un-reset" the chip. This function will generate sine waves if you give a msb and lsb as its parameter.

void generateWave(int msb[], int lsb[]){
        int ad_sine[BITS16] = {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}; // reset the chip and set output to be a sine wave
	int unreset[BITS16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // unreset the ad9833 chip.
        ad9833_set_freq(ad_sine);
        ad9833_set_freq(lsb);
	ad9833_set_freq(msb);
	ad9833_set_freq(unreset);
}

The frequency of actual wave out of chip is calculated using the equation below and our master clock frequency, Fmclk is 20 MHz.

                   fout = (Fmclk/2^28) * frequency register value.

More details on the AD9833 chip can be found here.

Receiving Waves and Calculating Sampling Frequencies

We will read in two different sine waves from the analog inputs B4 and B5.

  • B4: Emitted wave
  • B5: Received wave

We are using T3 timer interrupt to sample the emitted wave and received wave. The sampling frequency of the timer is calculated to produce 16 points in one period of the sine wave. Therefore, we need 19 different sampling frequencies for all sine waves from 100Hz to 10KHz. With the PR value equation from Lab4, we set our global int variable 'samfreq' equal to the PR values we calculated for each sampling frequency.

 void initInterruptController(void)
{
	// init Timer3 mode and period (PR3) 
	OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, samfreq); 	
	mT3SetIntPriority( 7); 	// set Timer3 Interrupt Priority
	mT3ClearIntFlag(); 		// clear interrupt flag
	mT3IntEnable( 1);		// enable timer3 interrupts
}

Sampling Frequency and PR value for each input frequency are provided in table below.

Input Frequency Sampling Frequency PR Value(samfreq)
100 1600 0xC34F
200 3200 0x61A7
300 4800 0x411A
400 6400 0x30D3
500 8000 0x270F
600 9600 0x208D
700 11200 0x1BE6
800 12800 0x1869
900 14400 0x15B3
1000 16000 0x1387
2000 32000 0x09C3
3000 48000 0x0682
4000 64000 0x04E1
5000 80000 0x03E7
6000 96000 0x0341
7000 112000 0x02CA
8000 128000 0x0270
9000 144000 0x022B
10000 160000 0x01F3

FFT

A good FFT reference and explanation is found here.

/** Interrupt Handlers *****************************************/
// timer 3 interrupt
void __ISR( _TIMER_3_VECTOR, ipl7) T3Interrupt( void)
{
	int i;
	
        sampleBuffer1[sampleIndex].re = ReadADC10(0); // read the ADC from B4 into the real part
	sampleBuffer1[sampleIndex].im = ReadADC10(0); // read the ADC from B4 into the imaginary part

	sampleBuffer2[sampleIndex].re = ReadADC10(1); // read the ADC from B5 into the real part
	sampleBuffer2[sampleIndex].im = ReadADC10(1); // read the ADC from B5 into the imaginary part
	
	// you could shave a little time off this ISR by just zeroing the .im value once, outside the ISR

	// increment the sampleIndex
	if (sampleIndex == (N-1))
	{
		sampleIndex = 0;
	}
	else
	{
		sampleIndex++;
	}	 

	// clear interrupt flag and exit
	mT3ClearIntFlag();
} // T3 Interrupt

void computeFFT()
{
	// when using 256 samples, we measured this function to take about 500 microseconds
	// (not including the time to send rs232 data)
	int i;
	
	// generate frequency vector
	// this is the x-axis of your single sided fft
	for (i=0; i<N/2; i++)
	{
		freqVector[i] = i*(SAMPLEFREQ/2)/((N/2) - 1);
	}
	mT3IntEnable(0); //turns off interrupt while computing FFT
	
	//LOOP_TIME_PIN = TRUE;
	
	for (i=0; i<N; i++)
	{
		if (i<sampleIndex)
		{
			// old chunk
			calcBuffer1[i+(N-sampleIndex)] = sampleBuffer1[i];
			calcBuffer2[i+(N-sampleIndex)] = sampleBuffer2[i];
		}	
		else // i >= sampleIndex
		{
			// new chunk
			calcBuffer1[i-sampleIndex] = sampleBuffer1[i];
			calcBuffer2[i-sampleIndex] = sampleBuffer2[i];
		}	
		
	}	

	// load complex input data into din
	mips_fft16(dout1, calcBuffer1, fftc, scratch1, log2N);
	mips_fft16(dout2, calcBuffer2, fftc, scratch2, log2N);	

	// compute single sided fft
	for(i = 0; i < N/2; i++)
	{
		real[i] = dout1[i].re;
		imag[i] = dout1[i].im;
		real[i+N/2] = dout2[i].re;
		imag[i+N/2] = dout2[i].im;
	}
	
//	LOOP_TIME_PIN = FALSE;
	
	computeFFTflag = FALSE;

	// do something with dout
	mT3IntEnable(1); //turn interrupt back on
}

Motor Control

We are using the interrupt handler for communication with Processing. The interrupt first checks which type of interrupt flag was generated (receive or transmit). If the interrupt was a "receive" interrupt, it first reads the key pressed and echos it back to the terminal. It then goes into a switch statement depending on the letter. The letters are described below:

  • 'a' - make the motor move an half inch
  • 't' - start the system
  • 'u' - stop motor movement
  • 'v' - motor moves forward until stop requested
  • 'w' - motor moves in reverse until stop requested
  • 'x' - accelerates the motor
  • 'y' - slows down the motor
  • 'A' - stop the motor and sends data to Processing for further calculation and plotting
  • 'c' - sends the motor to the zero position
// UART 2 interrupt handler
// it is set at priority level 2
void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void)
{
	char data;

	// Is this an RX interrupt?
	if(mU2RXGetIntFlag())
	{
		// Clear the RX interrupt Flag
		mU2RXClearIntFlag();
	
		data = ReadUART2();
		// Echo what we just received.
		putcUART2(data);
		
		switch(data)
		{	
			case 'a': // initialize
					go = TRUE;
                                        keepmoving = FALSE;
				break;
			case 't': // start
					start = TRUE;
                                        keepmoving = FALSE;
				break;	
			case 'u': // stop
					start = FALSE;
					go = FALSE;
                                        keepmoving = FALSE;
				break;	
			case 'v': // forward
					go = TRUE;
					DIR = Forward;
                                        keepmoving = TRUE;
				break;	
			case 'w': // reverse
					go = TRUE;
                                        keepmoving = FALSE;
					DIR = Reverse;
				break;	
			case 'x': // accelerate
					//go = TRUE;
					speed--;
				break;	
			case 'y': // slow down
					//go = TRUE;
					speed++;
				break;	
			case 'z': // reset
					go = TRUE;
					DIR = Reverse;
				break;
			case 'A': // activate
					go = FALSE;
					calcandplot();
				break;
			case 'c': // calibrate
					calibrate = TRUE;
				break;
		}			
	}

The code below is the infinite while loop in the main() function of our code. In order to move the sensor a half inch, our stepper motor will proceed through 1288 steps. After 1288 steps, the motor will stop moving and it will start to read in and send data. After it sends data to Processing, the motor will start to move again. If the stepper motor clock pulse periods are decreased, the motor will move faster. We control the motor speed with Delayms(speed). If 'speed' ('speed' is an integer variable) is incremented, then the motor slows down and if 'speed' decremented, then the motor will accelerate.

	while(1) //2575 steps = 1 inch
	{
		if (calibrate == TRUE){    //calibrate button from process pressed
			dcalibrate();      //sends the motor to initial position
			calibrate = FALSE;
		}
		if (start == TRUE){		//Data acquisition button from process pressed
			while(go == TRUE) {	//activate button from process pressed
					CLK = 0;
					Delayms(speed);
					CLK = 1;	
					Delayms(speed);
						if(INPUT_A9 == FALSE){
						go = FALSE;
						step = 0;
						DIR = 1;
							while(step<200) {
							CLK=0;
							Delayms(speed);
							CLK=1;
							Delayms(speed);
							step++;
							}
						}
						if(INPUT_A10 == FALSE){
						go = FALSE;
						step = 0;
						DIR = 0;
							while(step<200) {
							CLK=0;
							Delayms(speed);
							CLK=1;
							Delayms(speed);
							step++;
							}
						}
						//run it for 1288 steps and then stop
						if(step2>=1288) {
						step2 = 0;
						go = FALSE;
						halfinch++; 
							    if(keepmoving==TRUE){
                                                  go = TRUE;
                                              }
						}
						step2++;
					}
			  //receive data, calculate fft, send data to processing
			  calcandplot();
			  go = TRUE;
		}
	}
CloseOC1();
} //end main

//sends the motor to initial position
void dcalibrate(){
	DIR = Reverse;
	while(INPUT_A10 == TRUE){
		CLK = 0;
		Delayms(speed);
		CLK = 1;	
		Delayms(speed);
	}
		step = 0;
		DIR = Forward;
		while(step<200) {
			CLK=0;
			Delayms(speed);
			CLK=1;
			Delayms(speed);
			step++;
		}
} 

Sending Data to the PC (Processing)

We send 6 variables out to the PC (Processing) via RS232. We send out the frequency array (256 points and 256 zeros) that holds the frequency values used in the FFT, the voltage sine wave values for the emitted wave (512 points), the voltage sine wave values for the received wave (512 points), the real component of the complex result of the FFT of the emitted wave (first 256 points) and received wave (last 256 points), the imaginary component of the complex result of the FFT of the emitted wave (first 256 points) and received wave (last 256 points), and 'halfinch' which is an integer that counts how many half inches the motor has moved forward.

void sendDataRS232()
{
	int i;
	char RS232_Out_Buffer[128]; // max characters per line (line feed and carriage return count)
	
	sprintf(RS232_Out_Buffer,"\n\rSTART\n\r"); //print START. MATLAB uses this as a start delimiter
	putsUART2(RS232_Out_Buffer);
	
	sprintf(RS232_Out_Buffer,"ROWS=%d\n\r", N); //print the number of rows, so matlab can make the correct sized buffer
	putsUART2(RS232_Out_Buffer);
	
	for(i = 0; i < N; i++)
	{
		sprintf(RS232_Out_Buffer,"%d %d %d %d %d %d\n\r",freqVector[i], calcBuffer1[i].re, 
                calcBuffer2[i].re,  real[i], imag[i], halfinch); 
		putsUART2(RS232_Out_Buffer);
	}
	
	sprintf(RS232_Out_Buffer,"END\n\r"); //output end so matlab knows we're done
	putsUART2(RS232_Out_Buffer);
	
}
  


Processing

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.


Team26-window2.GIF Team26-window.GIF

The screenshot on the left is when it is currently calculating and plotting. The screenshot on the right is after it has finished calculating and plotting.


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 (if not already moving forward) at a constant speed
  • Reverse: board will move reverse (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).


Window Contents

  • Emitted and sensed sinusoidal signals plots - 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.
  • FFT Magnitude plot - 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.
  • FFT Phase plot - 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.
  • Magnitude and Phase Bode plots - an experimental magnitude and phase bode plot is plotted at 19 frequencies between 100Hz and 10kHz
  • Display of 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
  • Space for data display and debugging - 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.


Code

Description of Functions

Our full Processing code is much too long to post here, so we will instead post a link below and at the end of this section to a zip file with the actual code with comments. Some important portions of code are posted underneath their descriptions. Please note that you need to install the "ControlP5" custom GUI element library for Processing 1.0 by Andreas Schlegel in order for this Processing program to work. The following section will describe what each function in our code does.


Download a zip file of our Processing code and the ControlP5 library.


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).

  • 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)
  • y axis points of the emitted sinusoidal signal (512 emitted wave points)
  • y axis points of the sensed sinusoidal signal (512 sensed wave points)
  • 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)
  • 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)
  • 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()

Some 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.

  • int finish;

counts the number of halfinches the emitter/sensor board has moved forward starting from the zero position

  • float bx = width/3; | float by = height/3;

all the buttons and plots are laid out in the window using these 2 variables

  • float magnitude; | float phase;

there are several ways that we are calculating magnitude and phase (as you can see with magnitude_pic, abs_phase, etc.). The value set to "magnitude" and "phase" are the actual, final magnitude and phase we plot and record.

  • float[] freq2 = new float[19];

later on in the code, this array is set to hold the 19 expected frequencies (100, 200, 300,...,8000, 9000, 10000) to compare with the actual frequencies of the emitted and received waves calculated using the FFT in the PIC.

The setup() function basically creates rectangles in which plot curves appear, initializes the font used, sets the frame rate, initializes the output text write, and initializes the controlP5 buttons.


draw()

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

  • labels() is run to display labels
  • “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
  • 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().
  • 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()

  • 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.
  • The fft magnitude and phase points are calculated and stored in arrays.
  • The first fft magnitude point for both emitted and sensed sine waves arrays is ignored.
  • The index of the peak points of the fft magnitude arrays of the emitted and sensed waves are found.
  • The first 4 points on the emitted and sensed sine waves are averaged and stored in arrays to be used in plotting in sinewaves().
  • 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.
  • Display of variable and array values.
  • Records and plots the magnitude and phase bode plot points
  float zero1 = 0;
  float zero2 = 0;
  float max1 = 0; 
  float max2 = 0; 
  boolean foundzero1 = false;
  boolean foundzero2 = false;
  boolean foundzero1a = false;
  boolean foundzero2a = false;
  boolean foundzero1b = false;
  boolean foundzero2b = false;
  
  //First, 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.
  for (int i=0;i<L/2;i++) {
  fftreal1[i] = real[i];        //real component of the fft of the emitted wave
  fftreal2[i] = real[i+L/2];    //real component of the fft of the sensed wave
  fftimag1[i] = imag[i];        //imaginary component of the fft of the emitted wave
  fftimag2[i] = imag[i+L/2];    //imaginary component of the fft of the sensed wave
  }
  
  //fft1[] and fft2[] hold the magnitude of the emitted and sensed wave, respectively
  //fftphase1[] and fftphase2[] hold the phase of the emitted and sensed wave, respectively
	for (int i=0;i<L/2;i++) {
  fft1[i] = sqrt(fftreal1[i]*fftreal1[i] + fftimag1[i]*fftimag1[i]);
  fft2[i] = sqrt(fftreal2[i]*fftreal2[i] + fftimag2[i]*fftimag2[i]);
  fftphase1[i] = atan2(fftimag1[i], fftreal1[i])*180/PI;
  fftphase2[i] = atan2(fftimag2[i], fftreal2[i])*180/PI;
	}
  
  //the first data point in the fft magnitude tends to be very high and not important, so eliminate this first value
  //by putting them in new arrays (sfft1[] and sfft2[]) that don't include the first value
  for (int i=0;i<(L/2)-1;i++)
	{
	sfft1[i] = fft1[i+1];
	sfft2[i] = fft2[i+1];
	}

  //finds the peak value the magnitude fft plot
	max1 = max(sfft1);
	max2 = max(sfft2);
  
  //finds the index of the peak value of the magnitude fft plot, the index (a and b) are used in the freq[] (frequency vector)
  //to find at what frequency this peak occurs
	for (int i=0;i<(L/2);i++) {
		if (fft1[i] == max1) {
			a = i;
		}	
		if (fft2[i] == max2) {
			b = i;
		}
	}
 
 //averages that last 4 points in the sinusoidal signals and puts them in new arrays (msin1[] and msin2[])
 for (int i=0;i<L-3;i++) {
    msin1[i] = (sin1[i]+sin1[i+1]+sin1[i+2]+sin1[i+3])/4;
    msin2[i] = (sin2[i]+sin2[i+1]+sin2[i+2]+sin2[i+3])/4;
  }

  //finds the mid point of the sine waves, note that this point is nonzero, the boolean variables "foundzero1" and "foundzero2"
  //are used to ensure that this calculation is not repeated again before this function has completed
  if(foundzero1 == false && foundzero2 == false) {
  zero1 = min(msin1)+((max(msin1)-min(msin1))/2);
  zero2 = min(msin1)+((max(msin1)-min(msin1))/2);
  foundzero1 = true;
  foundzero2 = true;
  }
  
  //algorithm for finding the zero/mid point of the sinewaves, the boolean variables "foundzero1a" and "foundzero2a"
  //are used to ensure that this calculation is not repeated again before this function has completed
  //"a1" is the first zero point found on the emitted wave, "b1" is the first zero point found on the sensed wave
  for(int i=5;i<L-3;i++) {
    if(foundzero1a == false&&(i+1<L-3)&&(abs(msin1[i]-zero1)<=abs(msin1[i+1]-zero1))
       &&(abs(msin1[i]-zero1)<=abs(msin1[i-1]-zero1))&&msin1[i-1]>msin1[i+1]) {
    a1 = i;
    foundzero1a = true;
    }
    if(foundzero2a == false&&(i+1<L-3)&&(abs(msin2[i]-zero2)<=abs(msin2[i+1]-zero2))
       &&(abs(msin2[i]-zero2)<=abs(msin2[i-1]-zero2))&&msin2[i-1]>msin2[i+1]) {
    b1 = i;
    foundzero2a = true;
    }
  }
  
  //algorithm for finding the next zero/mid point of the sinewaves, the boolean variables "foundzero1b" and "foundzero2b"
  //are used to ensure that this calculation is not repeated again before this function has completed
  //"a2" is the index of the second and next zero point found on the emitted wave
  for(int i=(int)a1+2;i<L-3;i++) {
    if(foundzero1b == false&&(i+1<L-3)&&(abs(msin1[i]-zero1)<=abs(msin1[i+1]-zero1))
       &&(abs(msin1[i]-zero1)<=abs(msin1[i-1]-zero1))&&msin1[i-1]>msin1[i+1]) {
    a2 = i;
    foundzero1b = true;
    }
  }
  
  //"b2" is the index of the next zero point found on the sensed wave after "a2" of the emitted wave
  for(int i=(int)a1+2;i<L-3;i++) {
    if(foundzero2b == false&&(i+1<L-3)&&(abs(msin2[i]-zero2)<=abs(msin2[i+1]-zero2))
       &&(abs(msin2[i]-zero2)<=abs(msin2[i-1]-zero2))&&msin2[i-1]>msin2[i+1]) {
    b2 = i;
    foundzero2b = true;
    }
  } 
  
   //u2 is the period, note that u2 is always calculated to be 16  
   //we are forcing "a2" to come after "b2"
   u2 = a2-a1;
   while(b2-a2>u2) { b2=b2-u2; }
  
  //magnitude and phase calculations
  //"magnitude_pic," "magnitude_pic2," "phase_pic," and "phase_pic2" are calculated from the fft
  //"magnitude_wave," and "phase_wave" are calculated from looking at the sine waves 
  //(magnitude_wave = peak divided by peak, phase_wave = visually inspecting zero point to zero point distance)
  magnitude_wave = max(nsin2)/max(nsin1);
  magnitude_pic = sqrt((fftreal2[b]*fftreal2[b])+(fftimag2[b]*fftimag2[b]))/
                  sqrt((fftreal1[a]*fftreal1[a])+(fftimag1[a]*fftimag1[a]));
  magnitude_pic2 = max2/max1;
  phase_wave = (b2-a2)/u2*360;
  phase_pic = (atan2(fftimag2[b],fftreal2[b])-atan2(fftimag1[a],fftreal1[a]))*180/PI;
  phase_pic2 = fftphase2[b]-fftphase1[a];
  
  //what we chose to use for the final value for "magnitude" and "phase," 
  //we felt these two methods of calculating the magnitude and phase gave us the most consistent values
  magnitude = magnitude_pic2;
  phase = phase_wave;
  
  //the frequency of the peak in the magnitude fft plot, "a" and "b" should be equal and so just "a" is used
  freq1 = freq[a];


sinewaves()

  • Plots the voltage sine waves of the emitted and sensed signals.
  • Displays vertical guidelines that hit the zero point measured in the emitted and sensed sine waves.
  • 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.
float logmag = 0;
 float where = map(finish,0,37,2.36,20.64);
 
 float compare_water_m = 0;
 float compare_acrylic_m = 0;
 float compare_iron_m = 0; 
 float compare_aluminum_m = 0;
 float compare_plastic_m = 0;
 float compare_grape_m = 0;
 
 float compare_water_p = 0;
 float compare_acrylic_p = 0;
 float compare_iron_p = 0; 
 float compare_aluminum_p = 0;
 float compare_plastic_p = 0;
 float compare_grape_p = 0;
   
   //computing the sum of the absolute value of the differences
 for(int i=0;i<10;i++) {
   compare_acrylic_m = compare_acrylic_m + abs(acrylic_m[i] - 20*log(recmag[i+9])/log(10));
   compare_water_m = compare_water_m + abs(water_m[i] - 20*log(recmag[i+9])/log(10));
   compare_grape_m = compare_grape_m + abs(grape_m[i] - 20*log(recmag[i+9])/log(10));
   compare_iron_m = compare_iron_m + abs(iron_m[i] - 20*log(recmag[i+9])/log(10));
   compare_plastic_m = compare_plastic_m + abs(plastic_m[i] - 20*log(recmag[i+9])/log(10));
   compare_aluminum_m = compare_aluminum_m + abs(aluminum_m[i] - 20*log(recmag[i+9])/log(10));
   
   compare_acrylic_p = compare_acrylic_p + abs(acrylic_p[i] - recphase[i+9]);
   compare_aluminum_p = compare_aluminum_p + abs(aluminum_p[i] - recphase[i+9]);
   compare_grape_p = compare_grape_p + abs(grape_p[i] - recphase[i+9]);
   compare_water_p = compare_water_p + abs(water_p[i] - recphase[i+9]);
   compare_iron_p = compare_iron_p + abs(iron_p[i] - recphase[i+9]);
   compare_plastic_p = compare_plastic_p + abs(plastic_p[i] - recphase[i+9]);
 }
 
 float[] identify_m = new float[6];
 float[] identify_p = new float[6];
 
   //dividing by 10 to get the average
  identify_m[0] = compare_water_m/10;
  identify_m[1] = compare_acrylic_m/10;
  identify_m[2] = compare_iron_m/10;
  identify_m[3] = compare_aluminum_m/10;
  identify_m[4] = compare_plastic_m/10;
  identify_m[5] = compare_grape_m/10;
 
  identify_p[0] = compare_water_p/10;
  identify_p[1] = compare_acrylic_p/10;
  identify_p[2] = compare_iron_p/10; 
  identify_p[3] = compare_aluminum_p/10;
  identify_p[4] = compare_plastic_p/10;
  identify_p[5] = compare_grape_p/10;
  
  //finding which array had the smallest average, by putting all 6 object type averages into 2 arrays, 
  //"identify_m" (for magnitude) and "identify_p" (for phase), respectively.
  //then we find the minimum of the 2 arrays using min() and put that value into "choose_m" (for magnitude)
  //and "choose_p" (for phase).  a for loop is used to run through the values of the "identify_m" and "identify_p" arrays
  //until there is a match between "choose_m" and "identify_m[i]" and "choose_p" and "identify_p[i]" where "i" is the index
  //of the arrays.  the index "i" then determines the object type, one for magnitude and one for phase.
  float choose_m = min(identify_m);
  float choose_p = min(identify_p);
  float identify1 = 0;
  float identify2 = 0;
  
  for(int i=0;i<6;i++) {
   if(choose_m == identify_m[i]) {
     identify1 = i;
   }
  }
  
    for(int i=0;i<6;i++) {
   if(choose_p == identify_p[i]) {
     identify2 = i;
   }
  }
  text("0: water, no object",bx+bx/10,by*2+101);
  text("1: acrylic",bx+bx/10,by*2+110);
  text("2: iron",bx+bx/10,by*2+119);
  text("3: aluminum",bx+bx/10,by*2+128);
  text("4: plastic",bx+bx/10,by*2+137);
  text("5: grape",bx+bx/10,by*2+146);
  
  noStroke();
  fill(0);
  rect(bx+bx/10,by*2+63,200,27);
  fill(255);m
  text("object type (derived from magnitude): "+int(identify1),bx+bx/10,by*2+72);
  text("object type (derived from phase): "+int(identify2),bx+bx/10,by*2+81);
   
   
   //the following code describes how we determine where the object is in the water tank (object detection)
   //the object detection algorithm is as follows:
   //assuming that the object is placed somewhere in the tank along the path of the emitter/sensor board 
   //and not shifted out perpendicularly from the board and the board has scanned at least a length 
   //covering a distance starting before reaching the object (at least 1 halfinch before), directly 
   //above the object, and after moving past the object (at least 1 halfinch after),
   //experimental results have shown that all magnitude bode plot curves, especially at higher 
   //frequencies, will continue or start to decrease, but when the board is directly above the object, 
   //the magnitude bode plot curve will have the greatest values.  we find the curve with the maximum 
   //magnitude bode plot points and then look at where we are in the tank by counting many times we have 
   //moved (each time we move, we move a halfinch) to determine the object location.
   
   //each time this program is run, it creates 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)
   for (int k=0;k<19;k++) {
     logmag = 20*log(recmag[k])/log(10);
   output.println("step: "+nf(finish,2,0)+" | location: "+nf(where,2,2)+" | frequency: "+nf(freq2[k],5,0)+" 
    | magnitude: "+nfp(logmag,2,3)+ " | phase: " +nfp(recphase[k],3,1)); 
   }
   
   //since all magnitude bode plots decrease in magnitude and each magnitude point is either 0dB or less, 
   //at the current position, we take the absolute value of each magnitude (dB) point (0Hz to 10kHz), 
   //then take the average, and if the current average of the current position is less than the previous 
   //average at a previous position, we have found that the object is directly below the board.
   for(int k=1;k<19;k++){
       currentAvg = currentAvg + abs(20*log(recmag[k])/log(10));
     }
      currentAvg = currentAvg/19;
      println("currentAvg");
      println(currentAvg);
      //currentAvg = abs(k);
      if (currentAvg < prevAvg){
          prevAvg = currentAvg; 
          location = where;
          println("location");
          println(location);
      }
      
      text("object @ "+location+" inches",bx+bx/10,by*2+90);
      
    output.println(" ");
    output.flush();
     if (finish >=37) { // Writes the remaining data to the file
     output.close(); // Finishes the file
     }
     smooth();
  • 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).


Download a zip file of our Processing code and the ControlP5 library.

Test Results and Analysis

In the limited time between getting our circuit to function correctly with the Processing program and demostrating our project, we were only able to run a number of trials scanning past different objects to determine object type and object location. Our test results seem to indicate some trends but we cannot make any firm conclusions because this is only a first pass of experiments, hopefully the results obtained thus far will warrant future research.

Experimental Dimensions

Team26-diagram.GIF Team26-diagram2.GIF

Water tank measurements:

Emitter/sensor board = square with equal length and width = 3.19in / 81.02mm

Distance from floor to emitter/sensor board = 3.13in / 79.50mm

Distance from center of board to side of water tank = 3.13in / 79.50mm


Bipolar stepper motor measurements:

1 inch (25.4mm): 2576 steps

full distance of water tank the board can traverse (limit switch to limit switch) = 18.5 inches (470mm)

full distance in steps = 47656 steps

37 full halfinches in 18.5 inches (full distance)

1 halfinch (12.7mm) = 1288 steps


Team26-objects.jpg

Aluminum

L = 1.25in / 31.69mm

W = 0.58in / 14.76mm

D = 0.36in / 9.15mm

H = 2.77in / 70.29mm


Iron

L = 1.26in / 31.88mm

W = 0.96in / 24.32mm

D = 0.39in / 9.84mm

H = 2.74in / 69.60mm


Acrylic (flipped so that the well is on the bottom facing the floor)

L = 1.26in / 32.00mm

W = 0.99in / 25.10mm

D = 0.80in / 20.54mm

H = 2.33in / 58.9mm


Plastic

L = 1.36in / 34.62mm

W = 0.76in / 19.24mm

D = 0.39in / 9.93mm

H = 2.74in / 69.51mm


Grape on acrylic block

One grape was put in the small well etched on the top face of the acrylic block.

Grapes were approximately 20mm diameter spheres.

D = 0.393in / 10mm


Brass cylinder

Diameter (L and W) = 1.75in / 44.61mm

D = 0.52in / 13.25mm

H = 2.61in / 66.19mm

Test Results

Bode plots for different objects with the emitter/detector board directly above the object.

Team26-dom.gif Team26-dop.gif

Aluminum seems to have a characteristic response significantly different than the other objects.

For this experiment, all objects were placed in the water tank underneath the center of the board approximately at these specifications.

Team26-diffobjects.jpg Team26-diffobjects2.jpg

A = 0.00in / 0.00mm

B = 0.00in / 0.00mm

The recorded data and excel plots can be found here.


Bode plots as the board moves past a brass cylinder

Team26-middlem.GIF Team26-middlep.GIF

Especially at higher frequencies (4kHz to 10kHz), the magnitude values are the greatest when the board is directly above the brass cylinder.

For this experiment, the brass cylinder was placed in the water tank such that the center of the board would pass over the brass cylinder.

Team26-brass.jpg Team26-brass2.jpg

A = 0.00in / 0.00mm

B = 1.48in / 37.60mm

The recorded data and excel plots can be found here.


Bode plots as the board moves past a brass cylinder shifted off to the side

Team26-shiftm.GIF Team26-shiftp.GIF

There is a big jump and plateau for several frequencies in the 3 phase bode plot curves where the board is near the object.

For this experiment, the brass cylinder was placed in the water tank shifted from the midline such that the detector electrodes on the underside of the board would pass over the brass cylinder.

Team26-brass shifted.jpg Team26-brass shifted3.jpg

A = 0.80in / 20.00mm

B = 4.45in / 113.07mm

The recorded data and excel plots can be found here.