Tuesday, April 14, 2015

MATLAB Part 2

Today, we used MATLAB to simulate a simple thermal system -- the temperature of a cup of coffee. We asked hypothetical questions and used MATLAB to play out the situations.


A Cooling Cup of Coffee

First we simulated a hot cup of coffee cooling down. 
We used the equation

where dT is the change in temperature, dE is the change in thermal energy, C is the heat capacity, and Rth is the thermal resistance.

We put the equation into MATLAB and got the following script:



This script created the following plot:
We predicted that by increasing the thermal resistance (Rth) or heat capacity (C), the slope of the curve would become less steep.

When we changed the Rth to 1.0, compared to the original 0.85, the curve became less steep.

Then, when the C was changed from 1000 to 2000, we got this plot:

Adding a Heater

The next part was to simulate heating up and maintaining the temperature of a pot of coffee. We calculated that in order to heat up a pot of coffee from room temperature to 84C, a good value for the power is 75.

From a given plot of temperature vs time, we deduced that C = 1000 and Rth  = 0.853.

Bang-Bang 

By incorporating power into a new script, we created a plot for temperature vs time when heating up a cup of coffee. We first simulated a bang-bang control heating system, so that when the temperature is at or below the desired temperature, the power is fully on, and when the temperature is above the desired temperature, the heating is turned off.


The zoomed-in plot shows that the temperature jumps up and down once it reaches close to the desired temperature.
With this bang-bang control, we see that when the desired maximum is reached, the temperature fluctuates between slightly above and slightly below the actual value. Bang-bang can be sometimes desirable, as it is easier to program compared to proportional control. However, it is not very accurate in reaching a desired value, for example temperature, as there is only the choice of having the system being completely on or completely off.

Proportional 

Next, we modified the script so that the heating system uses proportional control to heat the coffee. 




By using proportional control, we avoid the fluctuation of the temperature as it reaches the goal. However, we also realized that as the error, which is Tmax - Tcurrent, approaches zero, the system no longer has enough energy to continue to heat the system. As a result, the system never actually reaches the desired temperature, 357, as seen above. We saw that increasing the gain factor makes the system get closer to 357, but never actually does reach it. 

Delay

Lastly, we simulated a delay in the reading of the temperatures in the bang-bang and proportional control heating systems.

First, we modified the bang-bang heating script from earlier to incorporate a delay in the reading of the temperature. The plot shows that because of the delay, the system heats up the coffee past the desired temperature, and in response, stops heating and the temperature drops. Then, when the system senses that the temperature is too low, it starts heating it up again. The temperature bounces back and forth between over heated and under heated.




Next, we modified the proportional heating script to incorporate the delay. With this delay incorporated, the system never reaches the desired temperature.





Apart from sensor delays, in the bang-bang system, there could be a delay in when the power is turned on or off. There may also be delays in the change in the power setting of the proportional heating system. When the system changes the power supply of the system to account for the current temperature approaching the desired temperature, the power may not be adjusted right away.







Thursday, April 9, 2015

MATLAB Introduction

Today, we started our section on MATLAB. This program can be used for a variety of calculations and functions. Using Arduino and having a bit of programming experience made starting the MATLAB programming an easier transition. 

We started by introducing ourselves to MATLAB by reading Physical Modeling in MATLAB by Allen B. Downey. In chapters one through four, he writes about basic functions of the program, such as writing loops, and functions, and vectors.

As we read the chapters, we also did some of the exercises.

Fibonaci Sequence

The first exercise was to write a script to compute the nth term of the Fibonaci sequence, which can be calculated using the following equation. 

We translated the equation into something that MATLAB could read. Each portion of the equation was set as a separate variable, and then put together in one line of code at the end. We also set n as 10, so that the program computed the 10th term of the sequence. 

We ran the program and got the 10th term as 55, which was what we expected.

Car Update

The next exercise was to create a script that updates the number of cars at two rental car companies, one in Albany and one in Boston. 5% of the Albany cars get dropped off in Boston  and 3% from Boston to Albany each week. Each location started off with 150 cars, so we set the initial a and b as 150 in the command window. Then we set A and B as the number of cars in each location after one week. Then, we set a as A and b as B so that the new initial number of cars would be changed. That way, if we run the function once, it displays the number of cars in each location after one week; if we run it 10 times, it shows the cars for after 10 week, and so on.
The round function is to round the number displayed to a whole number, as one cannot have half a car.



Car Loop

Next, we were to create a loop for the previous script so that the function automatically runs a specified number of times. In this case, we set the loop to run 52 times to simulate the number of cars in each location in one year. The initial conditions for a and b were set as 150.


When we ran the loop, we found that after a while, the values of A and B stopped changing. A stopped at 118 cars while B stopped at 182 cars.

Car Loop Plot

Next, we created a script that would also plot the values of the car loop script from above. Again, the initial values were 150. The hold on ensure that the graph doesn't automatically clear the points each time. The cars in Albany were plotted by red circles, while the Boston cars were plotted in blue diamonds.


As you can see from the above graph, the values eventually stop changing. 

We also tried using initial values of 10,000 cars. The result was that the graph resembled a better parabola shape. 



Fibonaci 2

The next exercise was to write a script to compute the elements of the Fibonaci sequence using a loop. The Fibonaci sequence is calculated using the equation 

Fi = F(i-1) + F(i-2)

In order to display the first 10 terms of the sequence, F1 and F2 are set as 1. Then, inside the loop, the nth term of the sequence is the sum of the previous two terms. Then, the previous two terms are updated to the newer two terms.

Then, we generalized the loop so that any number of terms could be calculated. The value of n just needs to be specified before the script is run.

Fibonaci Ratios

The last exercise was to write a script to first calculate the vector of the nth number of terms in the Fibonaci sequence, and then calculate the vector of the ratios of F(i+1)/F(i) -- that is the ratio of two consecutive terms. Then the ratios were plotted on a graph to see if the values do indeed converge. 

The ratios were plotted versus X, which was the value of n of that specific ratio.






Friday, April 3, 2015

Arduino Day 5

We continued to work with the sciborg and were introduced to some sensors that can be used with the sciborg. We also worked on bang-bang control and proportional control.


Encoders

The first sensor that we used is called an encoder. This sensor reads the number of times the shaft of the wheel has rotated and displays the reading on the serial monitor.



The starting value is and should be zero because the wheel has not rotated yet. The ending value is not zero because the wheel has rotated a number of times, both forward and backward, and cannot get back to exactly zero.


Touch Switch

The next sensor we worked with is called the touch switch. At the front of the sciborg is a switch that you can press by lifting the big arm up and down. We wired the touch switch to the Arduino board pin 9 using the breadboard and also connected an LED light to pin 3. We ran the sample sketch of the touch switch. 




The LED stays on forever when the button is pressed because the sketch does not include that when the button is released, the light will turn off. To make the light go on only when the button is pressed, we created an if else statement in the loop.



Ultrasonic Sensor

Another cool sensor we worked with is the ultrasonic sensor. This sensor detects objects in the vicinity. The sample code allows the sensor to display the distance of the nearest object using the serial monitor. 

The smallest reading on the serial monitor is associated with an object being extremely close to the sensor. By playing with the sensor, we observed the smallest reading to be about 11 when I put my hand directly in front of the sensor. 


Feedback & Control

Fixed distance, bang-bang control

Bang-bang control means that the system is either completely on, at full speed, or completely off. In the following sketches, the sciborg uses bang-bang control. 

Last time, we were able to get the sciborg very close to the ten foot mark after several trials with the delay time. We started off with 12.5 seconds, but because the sciborg didn't go in a straight line, the 12.5s was not enough time to make it to the line. We then tested 13.5 and 14.5 seconds. The 14.5 seconds brought us the closest to the 10-foot mark -- the sciborg stopped with the center of its body on the white line!

Using the previous sketch with the 14500ms delay and the encoder sample code, we measured the rotation of the wheel shafts using the encoder sensor. The encoder reading was 14336 after traveling forward for 14.5 seconds. 

We then used the touch sensor and the ultrasonic sensor to stop the sciborg at the 10 feet, instead of using the time delay. 


With the touch sensor, we created the sketch so that the sciborg would stop when we pressed and released the button. We pressed the button when the sciborg reached the ten-foot mark.


With the ultrasonic sensor, we programmed the sciborg to move when there is an object within a reading of 30 on the ultrasonic sensor reading. That way, the sciborg could follow a hand or wooden board until it reaches the ten-foot mark. When that object is taken away and there are no objects close to sciborg, it stops moving. 


Fixed distance, proportional control

The next assignment was to use proportional control instead of bang-bang control. Proportional control means that the sciborg adjusts its speed according to how far away it is from its target. 

Proportional control involves the following equation:

Output = error x gain factor 
            = (target - current position) x gain factor

The output is the power supply, or in the case of the sciborg, the speed. The target is the desired distance travelled, in our case, ten feet, and the current position is the distance from the ten-foot mark. 

Using the ten-foot encoder reading from last time, we set the encoder reading of 14366 as the desired target, and the current position as the current encoder reading. We wanted the maximum output, or speed, to be -150. So using the equation, we calculated the gain factor to be -0.010441.

The encoder reading was put into the loop so that it would be constantly updated. The speed was set as shown below so that as the sciborg approached the target, it would slow down. 

We also created a nudge function using the ultrasonic sensor. We found that when using only proportional sketch, the sciborg would not reach the target because in the end, there would not be enough power for it to continue moving. So the nudge function makes the sciborg travel in short intervals at a low speed until it reaches the desired distance. 




We incorporated the ultrasonic sensor into the final nudge function so that we could control when the sciborg stops performing the nudge function. We used an if else statement, so that if the nearest object is greater than a reading of 15, the sciborg performs the nudge function, and if the reading is under 15, the sciborg stops.



Conga Line

The last part of the feedback and control section was to use the ultrasonic sensor to have the sciborg follow another object. This was similar to what we did for the fixed distance, bang-bang control with the ultrasonic. However, this time, we also programmed the sciborg to slow down if it got too close to the object in front of it -- that is, if the ultrasonic reading became too small.


First, is the bang-bang control conga. The sciborg is programmed to follow an object within an ultrasonic reading of 30 at full speed.




Then, we have the proportional control conga. We put the ultrasonic reading into the loop as the integer value, and set the maximum speed of the sciborg equal to (value - 11)*-10

The 11 is the minimum ultrasonic reading, so when something is extremely close to the sciborg, the speed will be zero.

We wanted the maximum speed to be between -100 and -200, so we determined the gain factor to be -10.

This way, as the object that the sciborg is following gets further away, the sciborg speeds up. If the object gets closer, the sciborg slows down, or stops.








Thursday, April 2, 2015

Final project ideas

Before spring break, we visited some children and observed them playing in their classrooms and interacting with each other and the teachers. 

Our final project is to design a project that will enhance the children and teachers' experience in the classroom. 

I saw a few things that may be potential for my final project. 

One, the bathroom sinks: one of the teachers said that the children sometimes forget to wash their hands after using the bathroom. I think that a possible project would be to design some kind of feedback and control so that if a child doesn't wash his/her hands after using the bathroom, there will be some kind of flashing of lights or other reminder for him/her to wash hands. 

Two, the children seem to have trouble remaining seated on their carpet squares. Perhaps if the child starts to get up off the carpet square, or is sitting on the edge of it, the carpet square could vibrate or make a sound to remind the child to sit down. Some kind of pressure sensor would have to be built into the mats. 

And three, the children easily spill water or sand from out of the tubs. There needs to be some kind of reminder for them to clean up the water or sand when there is a large spill. Maybe there could be a sensor on the legs of the table that detect spills. And, if there is a spill, a short song could be played that reminds the children to clean up the mess.

All of these ideas involve some kind of visual or auditory simulation to remind the children to do or to not do something. It is also important to keep in mind that whatever the simulation is, it cannot over simulate the children. We don't want to encourage them do things on purpose to make the lights go off or make the song play. 







Arduino Day 4


The next segment of our work with Arduino involves using a sciborg -- basically a robot. To start, we first learned how to make functions.

Making Functions

The purpose of making functions is to avoid retyping code multiple times. Our functions programmed an LED light to blink in the pattern on morse code: dot-dot-dot-dash-dash-dash-dot-dot-dot. The program is shown below.
The use of the dot and dash functions effectively shortened the loop function.

Setting up the sciborg

The first step in setting up the sciborg was to solder the battery pack so that we could attach it to the board of the sciborg. We then screwed down the battery pack to the sciborg, taped over the screws, and installed the batteries. Next, we screwed down the Arduino board to the sciborg board, and set the bricktronics shield on top of the Arduino board. 

Using the sciborg

Motors and speed

There are two main wheels on the sciborg, each of which are powered by a motor. The first step in using the sciborg was to run the example sketch "single motor". 



Next, we ran both motors at the same speed by including both motors in the sketch.

When the motors are run at a speed of 0 or 1, nothing happens. This is because there is a minimum speed that the sciborg needs in order to overcome static friction.

We tested various motor speeds and found the that the approximate minimum speed that the sciborg needs is 40.

Turning

The next step was to create a sketch to make the sciborg make a "hard turn" -- that is, have one motor on at full speed and the other at reverse full speed. 

We also created a sketch for the sciborg to make gentler turns. We did this by having one motor at full speed and the other motor at a reduced speed.

Driving straight and 10 feet

Then, we created a sketch to make the sciborg travel 10 feet and automatically stop. We timed how long it took for the sciborg to reach the 10 ft mark, 14.5 seconds, and used that time for the delay. After the delay, we used another function, dot, to stop the two motors. 


The most difficult aspect of this part was getting the sciborg to travel in a straight line. We realized that the two motors spun at slightly different speeds. Therefore, the left motor needed to have a slightly higher speed than the right motor so that the sciborg would not travel in curves. 

We also took a close look at the two wheels and saw that one wheel was closer to the center of the sciborg than the other, so we adjusted the position of the wheels to be equal.

Also, the small front wheel of the sciborg contributes to the difficulty of traveling in a straight path. Sometimes the wheel gets stuck in a certain direction and the sciborg thus can only travel in that direction.

Touch sensor and hitting the wall

Next, we incorporated a touch sensor into the sciborg and Arduino program so that the sciborg would go forward until it hits a wall (or the touch sensor is pressed), back up a bit, turn around and continue to go forward. 

First, we ran the example sketch "motor button", where when the button (of the touch sensor) is pressed, the sciborg reverses direction. 

Then, we modified the code so that it would behave as mentioned above. 


At the beginning of the loop, the motors momentarily stop when the touch sensor button is pressed. There is then a short delay to "debounce the button", or in other words, give it time to react to the pressing of the button. Then, the button is released, and there is another delay for the debounce. The press and release of the button mimic the sciborg hitting a wall or other obstacle. 

The speed of the sciborg is reversed for 5 seconds, using another delay function, so that the sciborg can back up away from the wall or obstacle. The speed is reversed again at the end of those 5 seconds. After the second reverse speed, motor 2 travels at half the speed of motor 1 for 5 seconds so that the sciborg can turn around. 



There was no great location to attach the sensor on the sciborg. We tried to attach the sensor to the front of the sciborg, but when the sciborg hit a wall, the sensor would not get pressed enough to activate the reverse speed. I would build some kind of structure on the front of the sciborg to securely hold the sensor in order to improve the sciborg's mobility.




Arduino Day 3

Photocell

One of our assignments was to program a photocell, which senses the amount of light it is exposed to and displays whether it is "Dark", "Dim", "Light", "Bright" or "Very Bright". The original program is shown below. 



Our job was to take this original program and modify it so that a servos motor moves and an LED light blinks in accordance to the reading of the photocell. 

Using the setup for the servos motor and LED from previous Arduino programs, we incorporated the two into the program and connected them to the arduino board.


We then incorporated a series of else if statements into the loop so that the motor would spin more quickly as the photocell senses more light. This was done by increasing the size of the steps that the servos takes. The LED was also programmed to blink more quickly as the photocell sense more light using the different delays.