Thursday, March 31, 2016

MATLAB Introduction

MATLAB is yet another programming language, frequently known as the "glorified calculator."  Code some equations, assign numbers, and MATLAB runs like magic!

2.1 Fibonacci:

A Fibonacci sequence is a list of numbers adding the previous consecutive numbers.  We coded the Fibonacci equation into MATLAB and set n = 10.  The result for adding the first 10 numbers in the Fibonacci sequence is 55.


2.3 Car_Update:

In this problem, there are two car companies: one in Albany, one in Boston.  In the beginning of the year, each location has 150 cars.  Each week, 5% of cars go into Boston from Albany and 3% of cars are dropped off in Albany from Boston.

anew and bnew are the equations for number of cars in each car company.  Afterwards, we set anew and bnew to a new variable a and b and rounded them to a whole number using round().  In the two equations:

  • a, b: constants of 150 cars
  • % cars coming in 
  • % cars leaving
We then assigned the value for a and b in the bottom.














3.1 Car_Loop:

This exercise takes the script from Car_Update and runs it 52 times in a for loop.














3.2 Car_Loop with Plot:

Taking Car_Loop, we plotted the values of the number of cars in each location to the different week numbers.  We put the script for car_update in our car_loop_plot script and it works the same if they are in the same folder.  You can assign different colors and shapes for each plot on the graph.

















The graph on the left is when a and b are initially 150.


The graph on the right is when a and b are initially 10000.













3.5 Fibonacci2:

Going back on the Fibonacci1 script, in this exercise, we are to re-code the equation to where
F(i) = F(i-1) + F(i-2).  In the for loop, we set the lowest number in the sequence to start at 3 because since F(1) and f(2) equal 1 and would not initially work in the equation.




















4.6 Fibonacci2_Ratio with Plot:

Using what we coded in Fibonacci2, we coded the equation for the ratios of two consecutive fibonacci numbers and proceeded to plot the ratios in the equation g(i).  If we wrote "fibonacci2" in fibonacci_ratio_plot, there would be a for loop within a for loop, which will not work.  Instead, we wrote out the equation and the two constants.





















In the x axis, the ratios of the two consecutive numbers in the fibonacci sequence.

In the y axis, the term in the fibonacci sequence.














Monday, March 28, 2016

SciBorg: Proportional Control

Proportional-Integral-Derivative (PID):

PID control continuously calculates the error of a device.  The error value is the difference between the goal set and the reality of the device. Using that error value, the device continuously adjusts.
Here are the equations illustrating PID control:
  • power ∝ error
  • error = goal - reality
  • power = error * Kp
Power of the device is proportional to the error.  Kp is the proportional gain constant.  While setting the gain constant, if the value is too big, the device will overshoot; if the value is too small, the device will undershoot.

Straight-Proportional:

Using proportional control, Jennifer and I coded our SciBorg to move in a straight line by calculating the difference of speeds between the two motors.  We used that to scale the speed the wheel 1 to line up with wheel 2, therefore moving in a straight line.



We tested our Sciborg on three different surfaces - lab floor, carpet, ramp.


On the right, the SciBorg goes straight on the lab floor.

On the left, we lined the SciBorg to a line outside the engineering lab right before the entrance/exit to the science center.  This allowed us to know for sure that our SciBorg was moving straight. 

Despite the SciBorg going straight on the other two surfaces, every time it went up the ramp, it veered off to the left.  Jennifer and I believe the ramp is tilted.






The results using proportional control is very similar to bang bang control when testing our SciBorg to move in a straight line.  

Reminder:  In straight-bang-bang, we coded the two motors to move at different speeds depending on the different conditions.  The conditions were based on how fast each motor was moving.

10-Feet-Proportional:


Using proportional control, we coded our SciBorg to move 10 feet using the same distance (wheel revolutions and 12643 from the motor encoder reading) calculated in our 10 feet task.  


Using the equation for PID, we created a constant Kp.  Only the position of one wheel is needed to tell whether the SciBorg has moved 10 feet.  Therefore, if the position of wheel 1 was not at 12643, the speed of the motor would continue moving, but would scale itself using the PID equation.   The motors would move slower and slower as it reached its finish line.  We gave the wheels an initial speed of 110 to ensure the SciBorg would move.  


Jennifer and I examined the SciBorg moving the supposed 10 feet, but every time our SciBorg would stop about 6 inches short.  We created a nudge function for the car to slowly inch up to the finish line.  Our SciBorg stopped right on the line.  


Note:  Without realizing, our SciBorg was low on battery so it was not moving in as straight line a as it would with new batteries.













Ultrasonic-Sensor-Proportional:

We used proportional control to code our SciBorg to follow an object - a sheet a Delrin - at different speeds depending on the distance the SciBorg was away from the object.  There are four different speeds - fast, medium, slow, stop.

Again, we set up a gain constant to be proportional to the speed of the SciBorg.  If the SciBorg was far from the object, it would move at a fast pace, but if the SciBorg was close to the object, it would move at a slower pace, eventually close enough with a low enough power to stop moving.




Using proportional control, the SciBorg transitions to different speeds much smoother as opposed to bang bang control.










Line-Following-Proportional:

Lastly, Jennifer and I coded our SciBorg to follow the white line using PID control.  Like in line-following-bang-bang, we set the light sensor value to react to the edge of the white line in order to follow that rather than the entire white line.

Both wheels are set to adjust to different speeds based on what the light sensor reading is, but both wheels are set to move in opposite directions in order for the SciBorg to turn and adjust.





With proportional control, the SciBorg moves a lot smoother while not only going in a straight line but also while making turns.  This makes the SciBorg look a lot more natural while in motion.

In real life, if a car was using bang bang, turns would be sudden but using PID, turns would be smooth like we experience these days in safe driving.

Reflections:

Although learning how PID and the equation took some time and frustration, once we understood the equation and how it works, it made it much simpler to complete the rest of the tasks at a much faster rate as it all has to do with setting up an equation.

PID control is much more effective and reliable than bang bang control.  With PID control, machines are able to perform at ease without two simple commands - on and off.  PID allows the object to calculate an error and use the error to continuously make adjustment changes.

Tuesday, March 15, 2016

Feedback/Control Systems

Feedback and Control:

Feedback loops are used to design control systems.

There are three types of feedback systems:
  • sensing
    • giving the robot the ability to sense - such as touch, sight, or movement
  • computation
    • taking in data and making use of it
  • actuation:
    • the end result, output, what the robot actually does

There are three types of control:
  • open loop
    • computing the input using only the most current state of the model
    • cheap and feedback is not essential
  • closed loop
    • bang-bang
      • feedback controller with two options: on or off
    • proportional-integral-derivative (PID)
      • feedback controller capable of calculating an error value and adjusting with calculated error

Motion Light Switch: 

Sensing Mechanism:  

A light switch senses movement.

Control Computation:

This uses bang-bang control.  The light switch will take the data from the sensor and make it work.  For example, when someone enters a room, the sensor will automatically sense movement and the lights will turn on.  After a certain amount of given time, when no movement has been detected, the lights will turn off again.

Actuation Mechanism: 

Turning the light switch on and off will not only turn on the lights but also turn on the movement sensor.





Air Conditioning:

Sensing Mechanism:

An air conditioner senses temperature change in the room.

Control Computation:

Using bang-bang control, the sensor will sense the temperature and turn on or off based on the temperature in the room.  For example if we set the temperature at 70 degrees, the air conditioner runs around that temperature.

Actuation Mechanism:

The air conditioner will be on until the temperature in the room is a little above the set temperature.




Automatic Hand Dryer:

Sensing Mechanism:

A Hand Dryer also senses distance.

Control Computation:

This also uses bang-bang control.  When hands come a certain distance from the hand dryer, it turns off, otherwise, hands are not near the dryer and it turns off.

Actuation Mechanism:

The hand dryers stay on until the hands are a certain distance away from the hand dryer.






Gas Pumps:

Sensing Mechanism:

Gas pumps sense air pressure in the tank.

Control Computation:

In the nozzle is a pipe called venturi which measures the pressure due to the amount of gas in the tank.  As the level of gas rises, the pressure increases and the venturi pipe automatically stops the flow of gas from the nozzle.

Actuation Mechanism:

Gas pumps continuously pump gas into the car until the air pressure is met.

Thursday, March 10, 2016

SciBorg: Sensors & Bang Bang Control

MotorEncoder:

Looking at a downloaded code, we realized although the motor is set to move forward a second and backwards a second at the same speed, the position of the motor never returns back to 0.  Jennifer and I concluded that the motors are old and the power provided is different every time despite the constant speed integer.  This also helps explain why our SciBorg does not move in a straight line.

The first screenshot is what to include from the Arduino Library for this specific task.
The second screenshot includes a block of commented out code which is what we used to get the position of the motor to be read and have it printed out in the serial monitor.




We then proceeded to calculate the position, starting from 0, the motors would be at after 10 feet.  We calculated the circumference of the wheels and multiplied that by 10 feet.  We calculated the number of revolutions needed for our SciBorg in order to reach 10 feet, which was approximately 18 revolutions.  Next we used the test code above to see what the position of the motors were at after 18 revolutions, which was approximately 12643.  We used that number to keep our motors running in the loop - if (motor1<=12643)




























Our Sciborg stopped less than an inch from the 10 feet mark.












Touch_Switch_Sciborg_Front:

In front of the SciBorg is a button that when pressed and coded correctly can stop the motors connected to the robot.  Before coding the touch switch to control the motors, Jenny and I first made sure when the switch was pressed, the serial monitor would say "button is pressed" or "button is not pressed" when the switch was untouched.

Then we coded the motors to stop when the switch was pressed and go if the switch was not pressed. We had some fun with it and even coded the SciBorg to move backwards and turn around when it hit something in its path.  After turning around, the loop repeats.





LightSensorNXT:

Initially, we coded a light sensor to sense the amount of light in front of it and for that value to be printed in the serial monitor.

In the serial monitor, two values were being read and printed - ls.value and ls.scaledValue.  By holding the light sensor above a board with a darker color and a white color, we looked at the scaled values in the serial monitor and concluded that a value of 90-92 was when the light sensor was hovering above a white color.  A value of 10-11 was for a darker color.

We then turned on the LED light for the light sensor but that only hurt the sensitivity of the readings which only produced an one number difference between a dark and light color.

Lastly, by placing the light sensor about two centimeters above the surface, we received the best results.  We attached the light sensor to the side of our SciBorg and coded our Sciborg to move when sensing a dark color: if (ls.scaledValue() >= 70), and stop when sensing a white color.  We used the value 70 in our code for leeway.






























Ultrasonic_sensor:

Connected to the front of our SciBorg also has an ultrasonic sensor.  An ultrasonic sensor senses how far or close something is in front of it.  After connecting the ultrasonic sensor to our breadboard and Brickstonic Lego NXT, we tested the sensor by moving a Delrin sheet back and forth in front of the sensor.  In the serial monitor, values of how far the Delrin sheet was relative to the sensor was read and printed out.

Using that knowledge, we coded the ultrasonic sensor to stop the motors once something a certain distance in front of it was sensed.


We got our SciBorg to stop right before hitting something in front of it.

Straight-Bang-Bang:

Bang-bang control is a feedback controller with two options - off and on.  The SciBorg and its motors are far from perfect.  Although coded at the same speed, the motors do not run at the same speed with the same power.  Using bang-bang control, Jenny and I coded our SciBorg to move in a straight line.  By combining the MotorEncoder task, if the position of one wheel was less than another, the speed of both motors would be different.  This allows the SciBorg to make slight left or right turns and adjust itself again to move in a straight line.


The video shows the Sciborg moving in a straight line.  The Sciborg is slightly off centered so although it does not end in the center of the screen, the SciBorg is moving in a straight line.

Line-Following-Bang-Bang:

Using partial bang-bang control learned in Straight-Bang-Bang, we adapted bang-bang control to have our SciBorg follow a white path.  We coded the SciBorg to follow the edge of the tape rather than the center of the tape as it was easier for the SciBorg to readjust and because the tape was not clean white.

If the light sensor read values between 70 and 75, our Sciborg was centered on the edge of the line and he SciBorg would move forward.  If the light sensor read values over 75 - sensor more over the white tape - it would readjust by slightly turning left.  If the light sensor read values under 70 - sensor more over the dark surface - it would readjust by slightly turning right.  Through that whole process, the SciBorg would readjust the position every 5 milliseconds for the most up-to-date reading to keep the SciBorg from wandering from the line.



Object-Following-Bang-Bang:

Continuing with bang-bang control, Jenny and I coded the SciBorg to follow an object - delrin sheet.  We coded the Sciborg to move at a fast pace, medium pace, and stop depending on how far the SciBorg was from the delrin sheet.


Our SciBorg tends to veer off to the right so we included code so the right motor goes faster than the left motor.  This way, our SciBorg would move in a straighter line.

Behind our SciBorg is a SciBorg of another team.


Reflections:

I had fun learning about the three different sensors and the MotorEncoder.  Instead of coding the SciBorg to move a certain distance forward, move backwards for a certain amount of time, or turn right 90 degrees, different sensors with different functions could control motors for us.

Bang-bang control itself is not too difficult, but when using bang-bang to have our SciBorg follow a line, many challenges surfaced.  Our first obstacle was figuring out that having our SciBorg follow the edge of the tape would be easier.  Our next obstacles the actual coding.

Although coding can be frustrating, the end result is beneficial.