<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://hades.mech.northwestern.edu//api.php?action=feedcontributions&amp;feedformat=atom&amp;user=BrettPihl</id>
	<title>Mech - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://hades.mech.northwestern.edu//api.php?action=feedcontributions&amp;feedformat=atom&amp;user=BrettPihl"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php/Special:Contributions/BrettPihl"/>
	<updated>2026-04-18T07:24:12Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8416</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8416"/>
		<updated>2008-03-20T23:57:03Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Supply Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. An optional component is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). The 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. Learn more about external memory and EEPROM&amp;#039;s [http://hades.mech.northwestern.edu/wiki/index.php/Interfacing_to_External_EEPROM here].&lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convenient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:learnoscnoeeprom |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital to Analog Conversion]&lt;br /&gt;
* [http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf EEPROM Chip]&lt;br /&gt;
* [http://www.emtel.com/product-p/61-emv15012v.htm Emtel Power Supply]&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers Accelerometers]&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8415</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8415"/>
		<updated>2008-03-20T23:55:37Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Supply Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. An optional component is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). The 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convenient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:learnoscnoeeprom |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital to Analog Conversion]&lt;br /&gt;
* [http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf EEPROM Chip]&lt;br /&gt;
* [http://www.emtel.com/product-p/61-emv15012v.htm Emtel Power Supply]&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers Accelerometers]&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8413</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8413"/>
		<updated>2008-03-20T23:55:11Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Supply Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. An optional component is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). The 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convenient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:learnoscnoeeprom |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital to Analog Conversion]&lt;br /&gt;
* [http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf EEPROM Chip]&lt;br /&gt;
* [http://www.emtel.com/product-p/61-emv15012v.htm Emtel Power Supply]&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers Accelerometers]&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Learnoscnoeeprom&amp;diff=8409</id>
		<title>File:Learnoscnoeeprom</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Learnoscnoeeprom&amp;diff=8409"/>
		<updated>2008-03-20T23:52:43Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8408</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8408"/>
		<updated>2008-03-20T23:52:07Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Supply Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convenient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:learnoscnoeeprom |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital to Analog Conversion]&lt;br /&gt;
* [http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf EEPROM Chip]&lt;br /&gt;
* [http://www.emtel.com/product-p/61-emv15012v.htm Emtel Power Supply]&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers Accelerometers]&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8396</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8396"/>
		<updated>2008-03-20T23:24:55Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Supply Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convenient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital to Analog Conversion]&lt;br /&gt;
* [http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf EEPROM Chip]&lt;br /&gt;
* [http://www.emtel.com/product-p/61-emv15012v.htm Emtel Power Supply]&lt;br /&gt;
* [http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers Accelerometers]&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8395</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8395"/>
		<updated>2008-03-20T23:20:18Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Supply Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convenient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8394</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8394"/>
		<updated>2008-03-20T23:19:18Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here].&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]. To learn more about serial communication between a PC and PIC, see [http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8393</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8393"/>
		<updated>2008-03-20T23:18:20Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([[http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers([[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here]]).&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]]). To learn more about serial communication between a PC and PIC, see ([[http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page]]).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8392</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8392"/>
		<updated>2008-03-20T23:17:41Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to MATLAB. MATLAB would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([[http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply ([[http://www.emtel.com/product-p/61-emv15012v.htm Emtel EMV15012v]]). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers([[http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers here]]).&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]]). To learn more about serial communication between a PC and PIC, see ([[http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab this page]]).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8387</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8387"/>
		<updated>2008-03-20T23:14:46Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip ([[http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf 24FC515 Data Sheet]]). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply (http://www.emtel.com/product-p/61-emv15012v.htm). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers here: http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers.&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option here: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232. To learn more about serial communication between a PC and PIC, see this page: http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8385</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8385"/>
		<updated>2008-03-20T23:12:55Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip (http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply (http://www.emtel.com/product-p/61-emv15012v.htm). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers here: http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers.&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option here: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232. To learn more about serial communication between a PC and PIC, see this page: http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |900px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8384</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8384"/>
		<updated>2008-03-20T23:12:37Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip (http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply (http://www.emtel.com/product-p/61-emv15012v.htm). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers here: http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers.&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option here: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232. To learn more about serial communication between a PC and PIC, see this page: http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |500px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8383</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8383"/>
		<updated>2008-03-20T23:11:13Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip (http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply (http://www.emtel.com/product-p/61-emv15012v.htm). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers here: http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers.&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option here: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232. To learn more about serial communication between a PC and PIC, see this page: http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit |thumb|500px|Left| Circuit Diagram]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Ampoutput&amp;diff=8382</id>
		<title>File:Ampoutput</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Ampoutput&amp;diff=8382"/>
		<updated>2008-03-20T23:09:22Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Ampinput&amp;diff=8381</id>
		<title>File:Ampinput</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Ampinput&amp;diff=8381"/>
		<updated>2008-03-20T23:08:52Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8380</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8380"/>
		<updated>2008-03-20T23:08:34Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampinput |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ampoutput |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip (http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply (http://www.emtel.com/product-p/61-emv15012v.htm). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers here: http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers.&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option here: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232. To learn more about serial communication between a PC and PIC, see this page: http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Poweradapter&amp;diff=8379</id>
		<title>File:Poweradapter</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Poweradapter&amp;diff=8379"/>
		<updated>2008-03-20T23:07:57Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8378</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8378"/>
		<updated>2008-03-20T23:07:36Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Power Adaptor Wiring]]&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Amplifier Input]]&lt;br /&gt;
&lt;br /&gt;
[[Image:poweradapter |thumb|150px|right| Amplifier Output and Power]]&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Our circuit diagram is shown below. Included this diagram is a 24FC515 EEPROM chip (http://ww1.microchip.com/downloads/en/devicedoc/21673E.pdf). This is an optional component in the circuit, the 512 KB of memory could be used to store data to later be retrieved and processed by MATLAB. We found this to be unnecessary in our experimentation. &lt;br /&gt;
&lt;br /&gt;
The speaker car amplifier must be powered by a 12 volt DC source, to accomplish this we used an Emtel EMV15012v power supply (http://www.emtel.com/product-p/61-emv15012v.htm). A picture of the wiring terminals is shown in a figure at the right. When powering your amplifier, the &amp;quot;REMOTE&amp;quot; terminal must be powered high with 12 V for the amplifier to remain on. We included a switch here to be able to power on our amplifier separately from the power supply. Using the left low impedance input on the amplifier means the output will be on the CH1/L terminals. Using the low impedance inputs also requires an RCA cable. We had to cut this cable in half and strip the insulation coating in order to access the positive and negative terminals of the RCA jack. The outside ring is negative, and the inside hole is positive. A picture of the amplifier input is shown to the right.&lt;br /&gt;
&lt;br /&gt;
We used a surface mount LIS2L02AS4 accelerometer. We set pins 9, 11, and 13 LOW to give us a 2g resolution. These chips read acceleration in 2 dimensions, we used the X-direction (pin 10) read by pin 02/RC0 on the PIC. You can read more about accelerometers here: http://hades.mech.northwestern.edu/wiki/index.php/Accelerometers.&lt;br /&gt;
&lt;br /&gt;
Serial communication between the PIC and MATLAB was accomplished by using a FTDIChip TTL-232R USB to RS232 Cable. It is a bit more expensive then using a leveler chip and a DB-9 connector, but much more convienient. You can read more about this cable and the alternative option here: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232. To learn more about serial communication between a PC and PIC, see this page: http://hades.mech.northwestern.edu/wiki/index.php/Serial_communication_with_Matlab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Smlearningoscillationcircuit&amp;diff=8367</id>
		<title>File:Smlearningoscillationcircuit</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Smlearningoscillationcircuit&amp;diff=8367"/>
		<updated>2008-03-20T22:40:05Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8365</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8365"/>
		<updated>2008-03-20T22:39:48Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile. A circuit diagram is shown below.&lt;br /&gt;
&lt;br /&gt;
[[Image:smlearningoscillationcircuit]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8329</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8329"/>
		<updated>2008-03-20T21:32:53Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuitry */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile. A circuit diagram is shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Springattach&amp;diff=8314</id>
		<title>File:Springattach</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Springattach&amp;diff=8314"/>
		<updated>2008-03-20T21:14:33Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Rod2block&amp;diff=8313</id>
		<title>File:Rod2block</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Rod2block&amp;diff=8313"/>
		<updated>2008-03-20T21:14:08Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8312</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8312"/>
		<updated>2008-03-20T21:13:46Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Mechanics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:rod2block |thumb|150px|right| Mass-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
[[Image:springattach |thumb|150px|right| Spring-Mass Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. The basic construction method is described below.&lt;br /&gt;
&lt;br /&gt;
First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The other end of the rod screws into a threaded hole in a piece of polycarbonate which is attached to the block atop the mass, as shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. A piece of modeling foam was machined and attached to the bearings through threaded holes (shown in the figures to the right). The purpose of this block was to provide a connection surface for both the rod and spring, it also was where the accelerometer was mounted. A piece of sheet metal was screwed into the spring side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection. The spring mass connections are shown in the figure to the right.&lt;br /&gt;
&lt;br /&gt;
The final step is to mount the linear slide atop a block such that the rod, mass, and spring are all level. It is very important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8305</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8305"/>
		<updated>2008-03-20T20:59:43Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Mechanics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. &lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (approximately 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. The other end of the rod simply attached to the block that we machined and mounted atop the bearing. A piece of sheet metal was screwed into the other side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection.&lt;br /&gt;
&lt;br /&gt;
It is important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to the loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
The current design has much greater adaptability suitable for the experimental nature of this project. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the two threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system and experimented upon. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Spkrattach&amp;diff=8302</id>
		<title>File:Spkrattach</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Spkrattach&amp;diff=8302"/>
		<updated>2008-03-20T20:53:44Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8296</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=8296"/>
		<updated>2008-03-20T20:47:48Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Mechanics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
[[Image:spkrattach |thumb|150px|right| Speaker-Rod Connection]]&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex (See the figure to the right). This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. &lt;br /&gt;
&lt;br /&gt;
The forces exerted by the speaker are small enough (our measurements resulted in values around 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. The other end of the rod simply attached to the block that we machined and mounted atop the bearing. A piece of sheet metal was screwed into the other side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection.&lt;br /&gt;
&lt;br /&gt;
It is important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
This current design is advantageous  because it is modular. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscMC |thumb|150px|right| Main Circuit]]&lt;br /&gt;
&lt;br /&gt;
[[Image:LearningOscAM |thumb|150px|right| Accelerometer on mass]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main elements of the circuit were a PIC, DAC ([[http://hades.mech.northwestern.edu/wiki/index.php/PIC18F4520:_Serial_Digital-to-Analog_Conversion Digital-to-Analog converter]]) and accelerometer. The PIC would store a discretized sine waveform with integer values from 0-255. It would then output a function of this sine wave (our control signal) to the DAC. The analog signal output from the DAC would be sent to the amplifier, which would then power the speaker. The accelerometer on the mass would feedback the actual acceleration profile of the mass back to Matlab. Matlab would then recompute the next control signal and repeat the cycle until the mass was moving with the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
=== PIC Code ===&lt;br /&gt;
In our project, a PIC was used to drive the speaker via its I2C digital-to-analog converter and record accelerometer values at fixed intervals.  Since the control system’s algorithm requires too much processing for the PIC, all the computations are performed in Matlab after the accelerometer data is transported to a computer via a serial cable.  This setup simplified the program for the PIC.&lt;br /&gt;
&lt;br /&gt;
To generate the waveform of voltage levels sent to the speaker, a single quantized period was used.  Since the waveform is periodic in nature, the wave can be repeated indefinitely in a continuous fashion.  The nature of our processing algorithm constrained the number of samples for the accelerometer data to be equal to the number of samples for the control voltage.&lt;br /&gt;
&lt;br /&gt;
Since our system used a waveform of fixed intervals, we used an interrupt service routine (ISR) to change the wave and record accelerations at precise intervals.  We chose to sample each signal every 1ms (as this is an achievable I2C speed and ISR).  To oscillate our system at 10 and 20Hz, we needed at least 100 samples per waveform (1/10Hz / 1ms = 100 samples/waveform).  For this reason, we created two 100-byte long vectors for the control voltage ‘u’, and the acceleration data ‘acc’.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_main.c|&amp;#039;&amp;#039;&amp;#039;PIC Learning Control&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_pic_bode.c|&amp;#039;&amp;#039;&amp;#039;PIC Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Matlab Code ===&lt;br /&gt;
The Matlab code simply follows the protocol as established above.  The user specifies in Matlab the parameters for the amplitude and phase of the desired acceleration waveform.  *Note that the frequencies of these waves are set to 10 and 20Hz (as constrained by the transfer functions captured from the Bode and the periodic nature of 100 samples/wave).&lt;br /&gt;
&lt;br /&gt;
Below are the 4 m files used in Matlab.  Their names must be changed to the last part of their filenames as they are functions.&lt;br /&gt;
&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_main.m|&amp;#039;&amp;#039;&amp;#039;Main Matlab Loop&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_learn.m|&amp;#039;&amp;#039;&amp;#039;Learning Control Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_smooth.m|&amp;#039;&amp;#039;&amp;#039;Smoothing Algorithm&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_startplot.m|&amp;#039;&amp;#039;&amp;#039;Plot Initialization Code&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
 [[Media:ME333_Learning_Oscillator_bode.m|&amp;#039;&amp;#039;&amp;#039;Bode Plot Generator&amp;#039;&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Program Flow ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Program_Flow.jpg|center|thumb|400px|Program Flow]]&lt;br /&gt;
&lt;br /&gt;
=== Control System ===&lt;br /&gt;
To “learn” the control voltages to create a desired waveform, a simple proportional control system was used.  We should note much of this code was developed, tested and debugged by Tom Vose, who was invaluable to our final project.  Below is our understanding of Tom’s control algorithm.&lt;br /&gt;
&lt;br /&gt;
The system first guesses a control voltage of all zeros.  This ideally results in no forcing of the speaker and a flat response of acceleration.  After this initial guess, the program uses proportional control to match the desired acceleration waveform.  Mathematically, it multiplies the error by a proportional factor k.  The error is computed by subtracting the Fast Fourier Transform (fft) of the measured acceleration from the fft of the desired acceleration.  This error is multiplied by the proportional control k, and two discrete Bode plot values corresponding to the transfer function of voltage to acceleration at 10 and 20Hz.  The resulting signal is the control signal u, in the frequency domain.  From here, the control signal is converted back to the time domain via an inverse fft, and sent to the PIC.  All of the math is computed in discrete time, for the waveforms of 100 samples long.  Below is a block diagram of this control system.  Note that it is in the standard unity feedback form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Oscillator_Control_System.jpg|center|933x200 px|Learning Control System]]&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
Below are three tests performed on various superpositions of sin waves at 10 and 20Hz.  We have adjusted the phase of the waves to produce different results.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;gallery Caption=&amp;quot;Matlab Plots Acceleration Desired vs. Experimental Acceleration&amp;quot;&amp;gt;&lt;br /&gt;
Image:Oscillator_test1.jpg|Wave 1&lt;br /&gt;
Image:Oscillator_test2.jpg|Wave 2&lt;br /&gt;
Image:Oscillator_test3.jpg|Wave 3&lt;br /&gt;
&amp;lt;/gallery&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each diagram, shows one period of an acceleration profile.  The faint blue curve in the left subplot is the desired acceleration motion.  The red lines on top of the blue curve are experimental accelerations as recorded by the pic.  We can see that the red lines converge on top of the desired blue motion.  The right subplot (green) shows the learned voltage waveform to create the desired acceleration profile.&lt;br /&gt;
&lt;br /&gt;
Each wave follows the equation below with the given parameters:&lt;br /&gt;
&lt;br /&gt;
acceleration_desired = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2)&lt;br /&gt;
&lt;br /&gt;
Wave 1: phi1 = 1, phi2 = 2&lt;br /&gt;
Wave 2: phi1 = 0, phi2 = 0&lt;br /&gt;
Wave 3: phi1 = 1, phi2 = 0&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
Great results were achieved from the learning algorithm. For engineers working on it in the future, here are some topics for further investigation to get a more complete understanding about the control system:&lt;br /&gt;
&lt;br /&gt;
* In this project, the control signal was manually phase-shifted by pi radians before outputting it to the speaker&lt;br /&gt;
** This made the algorithm work perfectly&lt;br /&gt;
** It is not well-understood why this step was necessary&lt;br /&gt;
** The algorithm would not work otherwise&lt;br /&gt;
&lt;br /&gt;
* In the input for the FFT, the transfer function used in this project did not have an imaginary component&lt;br /&gt;
** The robust algorithm still worked perfectly, and would shift phase and &amp;#039;learn&amp;#039; when the program was run&lt;br /&gt;
** In future experiments, a transfer function including an imaginary term could be used, to fully utilize the capabilities of the algorithm&lt;br /&gt;
&lt;br /&gt;
* Faster learning&lt;br /&gt;
** The various constants in the algorithm equations can be tweaked for faster learning&lt;br /&gt;
** Currently, it takes about 10-20 iterations to hit the desired waveform&lt;br /&gt;
** More rapid learning would be a huge benefit in real-world applications&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
* [http://electronics.howstuffworks.com/speaker5.htm How speakers work]&lt;br /&gt;
* [[Iterative Learning Control]]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/~lynch/ Professor Kevin Lynch]&lt;br /&gt;
* [http://lims.mech.northwestern.edu/students/vose/ Tom Vose], author of the learning algorithm used in this project&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7559</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7559"/>
		<updated>2008-03-17T04:30:23Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile (a combination of a 10 and 20 Hz sine wave for our system) on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex. This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The forces exerted by the speaker are small enough (our measurements show only about 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. The other end of the rod simply attached to the block which we machined and mounted atop the bearing. A piece of sheet metal was screwed into the other side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection.&lt;br /&gt;
&lt;br /&gt;
It is important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
This current design is advantageous  because it is modular. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7558</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7558"/>
		<updated>2008-03-17T04:28:36Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The overall goal of this project is to create a system that induces a forcing function upon a basic, spring, mass, wall system to achieve an arbitrary periodic acceleration profile on the mass. An accelerometer is mounted upon the mass in the system. A PIC microprocessor records this acceleration data as well as controls a speaker (with the help of a DAC) that provides the external force to the system. This PIC communicates with MATLAB via Serial RS-232 communication. MATLAB processes this data and sends back a control signal for the speaker. After several iterations the actual mass acceleration profile begins to match the chosen profile it is told to &amp;quot;learn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex. This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The forces exerted by the speaker are small enough (our measurements show only about 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. The other end of the rod simply attached to the block which we machined and mounted atop the bearing. A piece of sheet metal was screwed into the other side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection.&lt;br /&gt;
&lt;br /&gt;
It is important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
This current design is advantageous  because it is modular. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7557</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7557"/>
		<updated>2008-03-17T04:18:03Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Potential Applications */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex. This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The forces exerted by the speaker are small enough (our measurements show only about 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. The other end of the rod simply attached to the block which we machined and mounted atop the bearing. A piece of sheet metal was screwed into the other side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection.&lt;br /&gt;
&lt;br /&gt;
It is important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
This current design is advantageous  because it is modular. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
A common question regarding this project is its applications to the real world. In the Northwestern University LIMS lab, a similar type of undertaking is being researched, but on a much grander scale. This same type of oscillation control is being done for 6 dimensions (X, Y, Z, Roll, Pitch, Yaw). However, the microprocessors used in this type of control are extremely expensive, and this one dimensional test of a learning system provides a possibly cheaper solution. The six dimensional control system has possible real-world applications in product assembly.&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7556</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7556"/>
		<updated>2008-03-17T04:00:58Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Mechanics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
The basic mechanical system for this device is a simple one, but must be assembled with precision. The major components are the speaker, linear ball bearings on a precision rod, and a spring. First, the speaker is mounted perpendicular to the ground. This must then be attached via a rod to the mass. We tried several approaches, but what seemed to be the best solution was to epoxy about a one inch section of PVC pipe to the center of the speaker. The diameter of the pipe we used matched the diameter of the junction in the speaker where the cone turns from concave to convex. This pipe also had two tapped holes running along the length of the section for a plate to attach to. The plate attached to this PVC also had a threaded hole in the center for the rod that attaches to the mass. The forces exerted by the speaker are small enough (our measurements show only about 3 Newtons) that only about an 8 ounce mass is needed. The bearing we used didn&amp;#039;t require any additional mass to satisfy this constraint. The other end of the rod simply attached to the block which we machined and mounted atop the bearing. A piece of sheet metal was screwed into the other side of the block with spacers. This piece of metal is used to anchor spring to the mass, and allows the spring to easily be removed. A similar piece of sheet metal is attached to the wall on the opposite side of the spring. However, this sheet has a vertical slot about an eighth of an inch wide cut from the bottom. This allows the coil of the spring to slide up further on the plate, thereby creating a more solid connection.&lt;br /&gt;
&lt;br /&gt;
It is important to design each component with all other components in mind at the same time. Mainly, making sure that the linear slide is level, and the rod attached to the speaker is centered, level, and in line with the mount on the mass and spring on the opposite side of the mass. This will help to ensure that all motion in the system is one dimensional.&lt;br /&gt;
&lt;br /&gt;
The above was not the first iteration of our mechanical design. We originally had a homemade linear slide. However, we found the lack of precision resulted in unreliable bode plots of the system due to loose tolerances of the design creating side-to-side motion. The first iteration also had the rod epoxied directly to the mass and speaker. During initial testing the connection between the speaker and rod actually severed. &lt;br /&gt;
&lt;br /&gt;
This current design is advantageous  because it is modular. The threaded rod allows for minor distance changes to ensure the spring attached to the wall is at its natural rest length. To attach it the plate is detached from the PVC on the speaker and screwed onto the rod. With the spring detached, the other end of the rod is screwed into the threaded hole on the mass. Finally, the plate is then screwed into the PVC through the threaded holes. The non-permanent spring attachment also allows for springs with different k-values to be added to the system. If the spring is longer or shorter then desired, a simple change in rod length is all that is needed to incorporate the new spring into the system.&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7555</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7555"/>
		<updated>2008-03-17T03:14:24Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Team Members */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Scott Mcleod:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Electrical Engineering Class of 2009&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Brett Pihl:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Sandeep Prabhu:&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;Mechanical Engineering Class of 2008&lt;br /&gt;
&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7554</id>
		<title>Intelligent Oscillation Controller</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Intelligent_Oscillation_Controller&amp;diff=7554"/>
		<updated>2008-03-17T03:11:26Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Team Members ==&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mechanics ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Circuitry ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Potential Applications ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reflections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=ME_333_final_projects&amp;diff=7553</id>
		<title>ME 333 final projects</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=ME_333_final_projects&amp;diff=7553"/>
		<updated>2008-03-17T03:08:12Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* ME 333 Final Projects 2008 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;[[ME 333 end of course schedule]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== ME 333 Final Projects 2008 ==&lt;br /&gt;
&lt;br /&gt;
=== [[IR Tracker]] ===&lt;br /&gt;
&lt;br /&gt;
[[Image:IR_Tracker_Main.jpg|right|thumb|200px]]&lt;br /&gt;
&lt;br /&gt;
The IR Tracker (aka &amp;quot;Spot&amp;quot;) is a device that follows a moving infrared light. It continuously detects the position of an infrared emitter in two axises, and then tracks the emitter with a laser.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[Robot Snake]] ===&lt;br /&gt;
&lt;br /&gt;
The intro for the snake will be added later.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[Learning Oscillator]] ===&lt;br /&gt;
&lt;br /&gt;
[[Image:vibrator.jpg|right|thumb|200px]]&lt;br /&gt;
&lt;br /&gt;
The Learning Oscillator is given a desired acceleration profile for a linear oscillating mass. It then &amp;#039;learns&amp;#039; and adjusts the frequency of its force source so the oscillating mass eventually matches the desired acceleration profile. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[Programmable Stiffness Joint]] === &lt;br /&gt;
&lt;br /&gt;
The Programmable Stiffness Joint varies rotational stiffness as desired by the user.  It is the first step in modeling the mechanical impedance of the human ankle joint (both stiffness and damping) for the purpose of determining the respective breakdown of the two properties over the gait cycle.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[Magnetic based sample purification]] ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== [[Continuously Variable Transmission]] ===&lt;br /&gt;
&lt;br /&gt;
A continuously variable tramsission is intended to provide a transition from low to high gear ratios while keeping the engine input running at the max efficient speed. It is achieved by a system of variable radius pulleys and a v-belt.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[Granular Flow Rotating Sphere]] ===&lt;br /&gt;
&lt;br /&gt;
This device will be used to study the granular flow of particles within a rotating sphere. The sphere is filled with grains of varying size and then rotated about two different axes according to a series of position and angular velocity inputs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[WiiMouse]] ===&lt;br /&gt;
&lt;br /&gt;
[[Image:HPIM1027.jpg|right|thumb|200px]]&lt;br /&gt;
&lt;br /&gt;
The WiiMouse is a handheld remote that can be used to move a cursor on a windows-based PC, via accelerometer input captured through device movement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=all&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [[Intelligent Oscillation Controller]] ===&lt;br /&gt;
&lt;br /&gt;
This device &amp;quot;learns&amp;quot; a forcing function that is applied to a spring and mass system to match an arbitrary, periodic acceleration profile.&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Team26-SerialComCircuit.jpg&amp;diff=6806</id>
		<title>File:Team26-SerialComCircuit.jpg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Team26-SerialComCircuit.jpg&amp;diff=6806"/>
		<updated>2008-02-06T23:36:37Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6805</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6805"/>
		<updated>2008-02-06T23:36:13Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC microcontroller and demonstrate bidirectional communication between the PIC and a Matlab program. For demonstration purposes, the PIC will send digital potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to connect the computer to the PIC. In this lab, we used a cheap cable found at http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&amp;amp;item=220199148938&amp;amp;ih=012&amp;amp;category=41995&amp;amp;ssPageName=WDVW&amp;amp;rd=1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;**Important! DO NOT connect the serial Rx/Tx lines DIRECTLY to the PIC!!!**&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A level shifter chip is necessary to convert the high and low logic voltages from the desktop computer (+12V/-5V) to (+5V,0V) for the PIC. A standard RS232 connection is called a DB9 connector and follows the pin diagram shown here: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
This cable requires 1 driver installation as included on the mini-cd.  To install this driver, you must first plug in the USB cable, and run the installation program located in the &amp;lt;CDROM&amp;gt;/.  To configure the Matlab script to connect to the proper serial port, use the device manager (Right click My Computer-&amp;gt;manage) and expand the section Ports.  Here you will see USB- make a note of the COM number.  In our program, our serial port was COM14.&lt;br /&gt;
&lt;br /&gt;
A female DB9 connector was wired to our level shifter to convert the voltages, with the level shifter connected to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
The PIC was programmed with our C code as shown below. Our program was designed to read a potentiometer through the PIC&amp;#039;s ADC (Analog to Digital Converter) port and transmit the digitized readings over the serial cable to the PC (upon request).  In Matlab, if a users sends data to the PIC by entering a character, the PIC responds with the current potentiometer reading and the last received byte from the PC.  The PIC is also programmed to display the character received from the PC on its LED array (D register) as a 8-bit ASCII number.  The programs can easily be modified to create any custom protocol, but are designed to show simple 2-way communication between Matlab and the PIC.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is converted to a digital signal through the PIC&amp;#039;s analog to digital converter pin. The MAX232N level converter provides bidirectional voltage shifting for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Team26-SerialComCircuit.jpg]]        [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
The data sheet for the MAX232 can be found here: [http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6626</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6626"/>
		<updated>2008-02-06T04:04:35Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the high and low logic voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is converted to a digital signal through the PIC&amp;#039;s analog to digital converter pin. The MAX232N level converter provides bidirectional voltage shifting for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:SerialCom.jpg]]        [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
The data sheet for the MAX232 can be found here: [http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6624</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6624"/>
		<updated>2008-02-06T04:02:47Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is converted to a digital signal through the PIC&amp;#039;s analog to digital converter pin. The MAX232N level converter provides bidirectional voltage shifting for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:SerialCom.jpg]]        [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
The data sheet for the MAX232 can be found here: [http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6593</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6593"/>
		<updated>2008-02-06T03:13:30Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is converted to a digital signal through the PIC&amp;#039;s analog to digital converter pin. The MAX232N level converter provides bidirectional voltage shifting for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:SerialCom.jpg]]        [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6592</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6592"/>
		<updated>2008-02-06T03:10:29Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. Five volts are run across the potentiometer, with the wiper going to the PIC&amp;#039;s analog to digital converter pin. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:SerialCom.jpg]]        [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6591</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6591"/>
		<updated>2008-02-06T03:03:42Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:SerialCom.jpg]]        [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6590</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6590"/>
		<updated>2008-02-06T03:03:28Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port.&lt;br /&gt;
 &lt;br /&gt;
[[Image:SerialCom.jpg]] [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6589</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6589"/>
		<updated>2008-02-06T03:03:07Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port. &lt;br /&gt;
[[Image:SerialCom.jpg]] [[Image:P1120664.JPG |Thumb|640x470 px|Image of wiring for serial communication between PIC 18F4520 and PC]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:P1120664.JPG&amp;diff=6588</id>
		<title>File:P1120664.JPG</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:P1120664.JPG&amp;diff=6588"/>
		<updated>2008-02-06T02:58:58Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6587</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6587"/>
		<updated>2008-02-06T02:57:00Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port. &lt;br /&gt;
[[Image:SerialCom.jpg]] [[Image:P1120664.JPG]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6586</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6586"/>
		<updated>2008-02-06T02:53:00Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232 here]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port. &lt;br /&gt;
[[Image:SerialCom.jpg]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6585</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6585"/>
		<updated>2008-02-06T02:46:29Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki: [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port. &lt;br /&gt;
[[Image:SerialCom.jpg]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:SerialCom.jpg&amp;diff=6583</id>
		<title>File:SerialCom.jpg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:SerialCom.jpg&amp;diff=6583"/>
		<updated>2008-02-06T02:44:30Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6579</id>
		<title>Serial communication with Matlab</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Serial_communication_with_Matlab&amp;diff=6579"/>
		<updated>2008-02-06T02:37:11Z</updated>

		<summary type="html">&lt;p&gt;BrettPihl: /* Circuit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Original Assignment ==&lt;br /&gt;
&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port.  This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program, using, for example, a USB to RS232 adapter and level shifter chip.  The Matlab program could simply log data from the PIC (e.g., the angle of a potentiometer knob), or plot it on a user interface in real time.  Keystrokes from the user of the Matlab program should be obviously received by the PIC, perhaps by lighting the LEDs on the PIC board.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Matlab has a &amp;quot;serial&amp;quot; function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC and demonstrate bidirectional communication between the PIC and a Matlab program. To demonstrate this, the PIC will send potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.&lt;br /&gt;
&lt;br /&gt;
A USB to RS232 adapter and level shifter chip were used to hook up the computer to the PIC. A level shifter is necessary because the power/ground voltages are different for the desktop computer and the PIC. An RS232 plug looks like this: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htm&lt;br /&gt;
&lt;br /&gt;
The RS232 was wired to our level shifter to convert the voltages, and then the level shifter was wired to our PIC. Refer to the Circuit section for details.&lt;br /&gt;
&lt;br /&gt;
First, the PIC was stamped with our C code (Refer to PIC Code below). It was programmed to read the potentiometer readings from its ADC (Analog to Digital Convertor) port and transmit the digitized readings through the RS232 cables to Matlab. If the Matlab user typed a character, the PIC was also programmed to display the character on its LED array. (8 on/off LEDs represent the 1 byte binary equivalent of any character). In real-time, Matlab would display the potentiometer reading from the PIC and the PIC LEDs would display the keystroke from the user.&lt;br /&gt;
&lt;br /&gt;
== Circuit ==&lt;br /&gt;
The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is later interpreted by the PC. The MAX232N level converter provides bidirectional voltage shifting for for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki: [http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232]). Finally, the female DB-9 connector allows the circuit to connect to the PC&amp;#039;s serial port. &lt;br /&gt;
[[Image:Lab5-Circuit.bmp]]&lt;br /&gt;
&lt;br /&gt;
== PIC Code ==&lt;br /&gt;
&lt;br /&gt;
 /*&lt;br /&gt;
    SerialComm.c Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
    This program is designed to communicate to a computer using RS232 (Serial) Communication.&lt;br /&gt;
    &lt;br /&gt;
    The main loop of this program waits for a data transmission over the Serial port, and&lt;br /&gt;
    responds with a current reading of an analog input (potentiometer) and the last received data.&lt;br /&gt;
   &lt;br /&gt;
    Note the analog input is only for testing purposes, and is not necessary for serial communication.&lt;br /&gt;
    Lines unnecessary for RS232 communication are commented with enclosing asterisks (&amp;#039;*..*&amp;#039;).&lt;br /&gt;
  */&lt;br /&gt;
  &lt;br /&gt;
 #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 #fuses HS,NOLVP,NOWDT,NOPROTECT&lt;br /&gt;
 #DEVICE ADC=8                          // *set ADC to 8 bit accuracy*&lt;br /&gt;
 #use delay(clock=20000000)             // 20 MHz clock&lt;br /&gt;
 #use rs232(baud=19200, UART1)          // Set up PIC UART on RC6 (tx) and RC7 (rx)  &lt;br /&gt;
  &lt;br /&gt;
 int8 data_tx, data_rx = 0;             // Set up data_tx (transmit value), data_rx (recieve value)&lt;br /&gt;
  &lt;br /&gt;
 void main()&lt;br /&gt;
 {&lt;br /&gt;
    setup_adc_ports(AN0);               // *Enable AN0 as analog potentiometer input*&lt;br /&gt;
    setup_adc(ADC_CLOCK_INTERNAL);      // *the range selected has to start with AN0*&lt;br /&gt;
    set_adc_channel(0);                 // *Enable AN0 as analog input*&lt;br /&gt;
    delay_us(10);                       // *Pause 10us to set up ADC*&lt;br /&gt;
    &lt;br /&gt;
    while (TRUE)&lt;br /&gt;
    {&lt;br /&gt;
       data_tx = read_adc();            // *Read POT on analog port (0-255)*&lt;br /&gt;
       output_d(data_rx);               // Output last recieved value from computer&lt;br /&gt;
       delay_ms(10);&lt;br /&gt;
       &lt;br /&gt;
       if (kbhit())                     // If PIC senses data pushed to serial buffer&lt;br /&gt;
       {&lt;br /&gt;
          data_rx = fgetc();            // Read in recieved value from buffer&lt;br /&gt;
          printf(&amp;quot;Pot: %u Char: %u\n&amp;quot;, data_tx, data_rx);  // Once data sent and read, PIC sends data back&lt;br /&gt;
          delay_ms(100);&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Matlab Code ==&lt;br /&gt;
 %  SerialComm.m  Scott McLeod, Sandeep Prabhu, Brett Pihl 2/4/2008&lt;br /&gt;
 %  This program is designed to communicate to a PIC 18F4520 via RS232 (Serial) Communication.&lt;br /&gt;
 %  &lt;br /&gt;
 %  The main loop of this program waits for a character input from the user,&lt;br /&gt;
 %  upon which it transmits the ascii value and waits for data to be written.&lt;br /&gt;
 &lt;br /&gt;
 s = serial(&amp;#039;COM14&amp;#039;,&amp;#039;BAUD&amp;#039;,19200);           % Create serial object (PORT Dependent)&lt;br /&gt;
 fopen(s)                                    % Open the serial port for r/w&lt;br /&gt;
 &lt;br /&gt;
 myChar = &amp;#039;a&amp;#039;;                               &lt;br /&gt;
 prompt = &amp;#039;Enter a character (q to exit): &amp;#039;; &lt;br /&gt;
 &lt;br /&gt;
 while (myChar ~= &amp;#039;q&amp;#039;)                       % While user hasn&amp;#039;t typed &amp;#039;q&amp;#039;&lt;br /&gt;
     myChar = input(prompt, &amp;#039;s&amp;#039;);            % Get user input&lt;br /&gt;
     fprintf(s, &amp;#039;%s&amp;#039;, myChar(1))             % Write first char of user input to serial port&lt;br /&gt;
     fprintf(fscanf(s))                      % Read Data back from PIC&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 fclose(s);                                  % Close the serial port&lt;br /&gt;
 delete(s);                                  % Delete the serial object&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If your program doesn&amp;#039;t close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 delete(instrfind)&lt;/div&gt;</summary>
		<author><name>BrettPihl</name></author>
	</entry>
</feed>