First off, a couple of cute exercises to reorient myself into the world of Matlab, I have been using Python for my senior thesis, and it may take a bit of adjusting :)
Exercise 1: Write a MATLAB program that evaluates the following:
Pretty easy, Here is the answer for x=-5, mu=2, and sigma=4 : 0.0216
Exercise 2: Write a function that will compute the Nth Fibonacci number.
Here's what my program found for numbers 1-10:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Exercise 3: Imagine that you are the owner of a car rental company with two locations, Albany and Boston. Some of your customers do “one-way rentals,” picking up a car in Albany and returning it in Boston, or the other way around. Over time, you have observed that each week 5% of the cars rented in Albany are dropped off in Boston, and 3% of the cars rented in Boston get dropped off in Albany. At the beginning of the year, there are 150 cars at each location. Write a Program that takes in a & b and returns the updated values for the week. What do you think the trend will be?
- I think that it will trend towards some equilibrium. It did, after 23 Week, the number of cars were 116 and 184.
Exercise 4: write a for loop that will run the program for 52 weeks. Yup, 116 & 184
Exercise 5: make the program plot the #of Cars at each. a is red b is blue:
On to the heavy lifting!
______________________________________________________________________________
Serial Communications!
______________________________________________________________________________
First we need to understand how electronics talk to eachother, in bits (0 or 1) strung together in bytes (8bits)
Here is a little diagram for counting with bytes:
The Baud Rate is the number of bits per second. Using the LogoChip from way back when, I calculated a bit time of ~52sec, or a baud rate of 19,200 bits/second. If you go into the picoblock source code, you can change the bit time directly, which is pretty neat, but in order to talk to anything, you need to have a standard baud rate.
There is a slight predicament with using bytes, you can only count to 255! If you want to count anything higher, you need more bytes! A 10 bit number can go up to 1023. In this case there is a high byte and a low byte, the high byte only uses the first 2 bits, and represents the most significant digits. To talk back and forth with something, you need to standardize which byte is first, I am using high-low.
If you want to look at negative numbers, the fist bit denotes the sign, 0 is +, 1 is - , and you can only count to 127.
I attached a light sensor to the logo chip, and programmed the chip to read out (in bytes) the value of the light sensor, which was fed to Matlab. Matlab can read or write bytes, once you set up the port, <s= serial('COM4');> for example, and you set the baud rate <set(s,'BaudRate',19200);>. To write a byte you can use <fwrite(s,5)> to write the number 5. To read, use <value = fread(s,2);> The number you specify is the number of bytes you will read, since I am using 10bit numbers, I need to read 2 bytes to get each number. This plot shows the light sensor data read into Matlab: at t=30 I put my hand over the sensor
I also made a program, in which the logo chip would make a LED turn on when Matlab fed it 1, and turn off when fed 0.
______________________________________________________________________________
Matlab and the General Purpose Instrumentation Bus (GPIB)!
______________________________________________________________________________
So a bus is a way for multiple instruments to share the same line (like a public transportation bus lets multiple people go different places)
We are going to use it to control a Function Generator, which will be attached to a speaker.
Connecting to MATLAB is pretty simple. If you don't know the address of the instrument, use the command "instrhwinfo(‘gpib’, ‘ni’)" and use the ObjectConstructorName: (‘gpib(‘ni’, 0, 10);’)
Then assign it to a variable using : gpib('NI', 0, 10);
The function generator was very simple to talk to,
fwrite(GPIB1, 'output on’);
fwrite(GPIB1, 'apply:square 440, 2.0, 0.0'); %(shape, Frequency, Voltage, DC-offset);
There was a pretty simple script provided, that played octaves on the speaker (Video Below)
But I decided to write a much more exciting program, see if you can guess the song!
Next I created a "Theremin" an instrument that is controlled by the proximity of your hands to the instrument. I'm pretty sure real Theremins use radio wave signals, but my make shift one works by shadows.
Here is a video. There is a lag between the shadowing and the speaker playing, most likely due to the function generator taking a while to turn on.
The final activity of the my portion of the class was to write a program to read voltages from a Keithley multimeter. This was done using the command "tmtool" which opens a GUI that makes it very easy to set up and connect with an instrument. tmtool also generates the code needed to connect to the machine.
Here is a pic of it sampling a sine wave... doesn't look like it because it samples over a discrete interval.
No comments:
Post a Comment