bmereportlab2

6
Preston Hoang (ID: 58904993) & Alex Almanza (ID: 32214258) BME Report Lab 2 Dear Erik Mikula, In this project, we are tasked to find the “maximum” voltage of the light detector output in both the ideal and nonideal cases. For the ideal case, we took the gradient of the V output starting with the initial values. The motor then readjust its speed and direction depending on how steep the gradient is. Essentially, if the gradient is equal to zero, then the Voutput max has been detected. With a physical system, the motor movements are not precise and there is some noise when collecting voltage data. With these constraints, using the gradient approach is impractical. As such, we developed an algorithm that finds a “close enough” peak using a bestfit line approach. To explain, when the motor moves, it samples data every several seconds until the Voutput is equal to zero. This data is then plotted under a bestfit polynomial function, removing all the noise from the original data. We then find the maximum of this ideal graph, which “approximates” the maximum of the actual voltage data. From there, we move the motor until the light detector reaches at least 95% of this maximum value. The approach is noniteration dependent in both cases, as it compares the input voltage data to the maximum threshold during each cycle.

Upload: preston-hoang

Post on 14-Aug-2015

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BMEReportLab2

Preston Hoang (ID: 58904993)  & 

Alex Almanza (ID: 32214258)  

BME Report Lab 2  

 Dear Erik Mikula,  

In this project, we are tasked to find the “maximum” voltage of the light detector output in both the ideal and nonideal cases.   

For the ideal case, we took the gradient of the V output starting with the initial values. The motor then readjust its speed and direction depending on how steep the gradient is. Essentially, if the gradient is equal to zero, then the Voutput max has been detected.     With a physical system, the motor movements are not precise and there is some noise when collecting voltage data. With these constraints, using the gradient approach is impractical. As such, we developed an algorithm that finds a “close enough” peak using a best­fit line approach. To explain, when the motor moves, it samples data every several seconds until the Voutput is equal to zero. This data is then plotted under a best­fit polynomial function, removing all the noise from the original data. We then find the maximum of this ideal graph, which “approximates” the maximum of the actual voltage data. From there, we move the motor until the light detector reaches at least 95% of this maximum value.  The approach is non­iteration dependent in both cases, as it compares the input voltage data to the maximum threshold during each cycle.  

   

    

Page 2: BMEReportLab2

Phase I: Arduino without Noise  This program essentially has two steps, finding the gradient at a certain point and changing the motor speed and direction that correspond with the gradient’s magnitude.   

  In order to find the gradient, the motor is set at 1, 1, and 1 for the PWM, the duration, and the direction respectively. Two outputs with very similar values — one after one step and one after two steps  — are stored in an array for future calculations. The x­axis values (i.e. calculated by PWM * duration * index) are also similarly stored in the array as well. With these two arrays, the gradient of the Voutput at a certain position can be calculated by the “linear regression” function. In short, this is the mathematical description of this code:  

   After finding this gradient, it then takes that value and moves the motor (or possibly change direction) depending on the size of that value. To describe this in equation form, this is essentially what is happening.    

    

Page 3: BMEReportLab2

    

 From the code above, what is essentially happening is that the slope is multiplied by a constant (i.e. C * dV/dx). It is then inputted into the PWM value in order to direct the motor how far should it go. Depending on the sign of the slope, the motor will move left or right. The “Virtual system add Uno” then executes this action, and both steps (i.e. finding the gradient, moving the motor) are repeated again until the gradient of the Voutput is approximately equal to zero. When the absolute value of the slope gradient is less than 0.02 (typically when it hovers around the max value), the program stops (i.e. the motor stops) and it displays the Max Vdetector value. For diagnostic purposes, I display the “Current Slope”, “Current Vdetector” and “Current Direction” as the program is running.  Depending on the initial position of the motor, finding the max value can take as few as 2 iterations or as many as 17 iterations. When running this program, the average number of iterations used is around 10 or so.             

Page 4: BMEReportLab2

 Phase II: Arduino with Noise 

 This program can be described as having three different stages:  

(1) Sweep left 

 

(2) Sweep Right 

 

3) Sweep Back Left 

 

 At stage (1), our program begins by setting values 20, 1, and 0 to the variables PWM, 

duration, and direction respectively. By setting those variables to those values, the program tells the motor to move in the leftward direction. But why begin the program with such leftward 

Page 5: BMEReportLab2

movement? By doing so, the program will move the motor to the leftward extreme of all the Vdetector values. The reason for this step will be explained more thoroughly during the discussion of stage (2).   

At stage (2), our program retains all the variable values used in stage (1), excluding the variable for direction. In this stage, direction is given a value of 1, which means that the motor will be moving continually toward the right. It stops once it reaches a Vdetector value less than or equal to zero. Stated differently, our program tells the motor to take itself to the rightward extreme. Since stage (1) had already brought the motor to the leftward extreme, this current stage will sweep across the entire range of Vdetector values. Such an all encompassing sweep is crucial for our program, because it allows us to synthesize a possible max for Vdetector.  

 Given the conditions of this project (i.e. the presence of noise), the true Vdetector max 

cannot be found by simply looking at the largest value of those collected during stage (2). Rather, the values collected in stage (2) must be related in a way that yields an approximation of the Vdetector max. When the motor sweeped from left to right, we obtained an array of Vdetector values that, when plotted with respect to the iteration, resembled a parabola. As a result, we used a built in VI function called "General Polynomial Fit" (Figure A) to obtain a parabola that approximately equal to that of the Vdetector’s own quasi­parabolic plot. 

 

 Figure A:Image of “General Polynomial Fit” 

  What "General Polynomial Fit" essentially did for us is take the sample points we obtained from the Vdetector and used them to draw a clean, noise free parabola. The maximum of the "General Polynomial Fit" is then taken by the program as an approximation of the maximum Vdetector value.  

Stage (3) is the stage where our program finds the Vdetector max. Upon entering stage (3), the motor is found located at the rightward extreme. Moreover, as our program enters this third and final stage, an approximate value for the Vdetector max is being held on memory. At 

Page 6: BMEReportLab2

the start of this stage, we begin to move the motor in the leftward direction (using these defined values for the motor’s variables: PWM=10, Duration=1, Direction=0). As the motor moves leftward, the program simultaneously checks to see if the motor detects a value for Vdetector, which is equal to the one calculated from the max of "General Polynomial Fit". If it finds such a value, the motor stops and a boolean LED on our front panel lights up.  

 Once the motor has stopped, we can only make the assumption that it has stopped where it will give the max voltage for Vdetector. The reason why this is only an assumption and not a matter of pure knowledge is because of the way we calculated the Vdetector max: we used the maximum given by "General Polynomial Fit", which means we used a created value and not one that must necessarily exists within the raw data. As a result, we have told our program to look for a value that exists within a 95% threshold of the calculated value. This approach may sound as if it is susceptible to error, but after running 100 trials on our program we have shown that this is clearly not the case.  Our program is accurate and dependable. 

 

*Avg. Accuracy of GPF Calculated Max 

Avg. Accuracy of Final Vdetector Value 

Avg. Number of Iterations Used  

99.75%  99.13%  10.32  *The values in this table were obtained by running our program in a for loop with n=100. The values for “Avg. Accuracy of GPF Calculated Max” and “Avg. Accuracy of Final Vdetector Value” were obtained by relating the GPF calculated max and the final Vdetector value as percentages of what was the true Vdetector max value as obtained from Vdetector’s raw data. “Avg. Number of Iterations Used” is  assumed as being self explanatory.