Monday, March 10, 2014

SciBorgs Day Three and Four: The Puzzle of Proportional Feedback and Control...


After completing all of the tasks from days one and two, my partner and I were ready for new challenges. However, the next puzzles were almost identical to the ones we had already accomplished.

Almost...

We began to learn about Proportional Control.

Proportional Control is a response that measures the situation and then alters the programed maneuver to adequately adapt to the given situation. For example, if a woman is driving and car and begins to veer slightly into the wrong lane, she will simply turn the wheel a small fraction and the car will slowly adjust its alignment with the road. She would not violently jerk the car away from the opposite lane because now she is heading the wrong way again. Such a reaction would be dangerous and unnecessary. Instead, she would analyze the situation and decide how much of a response is needed.

That is what our bug had to do...determine how much a response was needed.


We started by trying to make it go ten feet.

The initial procedure was simple enough. We calculated the proportional control using error (how much our bug was off the mark by) and gain (a variable we could control)

P=(error)(gain)

We knew that ten feet was approximately 3060 rotations on countb. All we had to do was measure the error between ten feet and a 3060 countb run, calculate the gain and plug the variable into the motor power. We called this program proc (proportional control)

Once our bot was extremely close to the line, we added a small addition to our code called nudge. This helped the robot jerk forward slightly to bring it right to the line.


proc and nudge codes
One the lab floor, this process worked extremely well.

However, once we began to change the surface and incline, things became more complicated.

On the carpet in the hallway, we had to nudge twice in order to have the bug reach the ten foot mark. This was due to the excess friction caused by the
rug on the wheels. 


On the inclined plane, moving upwards, nudge had to be extremely adjusted. Even running at full power straight up was not enough to get the bug to the line. 



Moving down the inclined plane was another hassle. Even if we applied the breaks before the bug reached ten foot mark, the momentum it had built up caused it to slide until it hit the garbage can at the end of the plank. We adjusted nudge again, but this time the main difference was that we had it run in the opposite direction. Once the bug was a certain distance away from the line, the wheels began to run in reverse and succeeded in stopping it just before it flew over the line.


One of the early attempts. We Adjusted nudge to begin later so that the car would
be able to reach the blue line.


Each time, we were able to adjust our code to get the bug to be within 1cm of the desired distances, though some surfaces were more reliable than others. Going down the inclined plain is still a matter of luck. We could only fiddle with the nudge and gain variable. On the upward incline, we left the variable at 1 instead of 9/10 (which had worked on the floor) and it still could not reach, so we had to make nudge extremely powerful.

After we completed the fixed distances challenge, we moved onto the conga line. The goal of this challenge was to see if we could get the bug to follow something in front of it. It had to move forward when the object did, stop when it did and move backward if it did.

We started with a bang-bang version of the code before we implemented proportional control.


Conga Line Bang-Bang Code

Once our bug was able to make jerky stops and basically follow a Delrin board in front of it, we implemented proportional control. This addition to the code would help the bug start and stop more gradually instead of instantaneously by calculating the power supplied to the motors according to the distance between the bug and the object in front of it. If it was close, only a little power was needed. If it was far, the power could go up to one hundred.



Conga Line Proportional Control Code


It worked very nicely. Rather than jerking to sudden stops and stars, the bug approached the board quickly and then began to slow down the closer it got until it came to a gentle stop just before it. We could have balanced small objects on top of it and felt relatively confident that they would not be thrown off the by movement of the car,  but we were not ready in time to participate in the large group conga line due to a hardware malfunction. 


Finally, we began to work on the proportional control line follower. Everything we had learned up to this point had prepared us to make a line follower that could follow a white line, use only one sensor and run extremely straight.

We started simply by experimenting. 

We knew that the bug would have to use small amounts of power when it was very close to the line, but if it 'jumped the track' or made too wide a turn, it would need the full capacity of both its motors to bring it back around to the correct alignment with the line. 

Line Follower 2 Code 

Line Follower 1 Code

We experimented with two different types of code, the first set the power to each motor individually depending on the vale the brightness sensor detected. A vale above 400 had 250 subtracted from it and was divided by three. If the value was lower than 400, the it was subtracted from 650 and then divided by three. It took several attempts to get the right values to assure that the power could fluctuate between 70 and 100 for each motor. Line follower 2 was an attempt to set the power to each motor using one strand of code, but due to the massive differences in the read outs between the white tape and the dark floor, it would not work. One motor was always a little too fast while the other was two slow or did not move at all. Our gain variables were much smaller than the first challenge....about 1/3. Because the bug only had to be adjust by tiny increments, we fiddled more with the values the sensor value was subtracting/being subtracted from as these made smaller differences in the motor power output and made the bug very precise. 




Our little line follower worked exceptionally well, will noticeably less jerking and a much straighter path. Though the second code implemented the original bang-bang line follower code, it could not properly balance the power to each motor using only one line of code. Individual lines had to be constructed to accommodate the sensor value for each motor. Though this was more complicated, it worked far better. 


All of our final versions of our various code's can be seen below.

Final Fixed Distance Code with Nudge

Final Line Follower Code Using Proportional Control

Final Conga Line Code Using Proportional Control

If I could have changed one aspect of this exercise, I would have liked to have had less hardware problems. Our original bug had several issues and sometimes did not work or turn on altogether, even after a battery change. The frustration concerning the hardware detracted from the fun and excitement of the coding process. Then again, instant gratification and engineering never go together. 

Aside from this, the experiment was extremely fun and informative. 

Now that our little bug (Belle) has proportional control in it, I feel safer letting it drive around the lab. It doesn't crash into things (quite) as often as it used to.


Monday, March 3, 2014

SciBorgs Day Two: The Puzzle of Feedback and Control Continues...

The journey to understanding feedback and control continues...

For today's challenge, my partner Michelle and I had to make our SciBorg (Snow White) drive ten feet in a straight line. It sounded easy enough.

But it was WAY more difficult than we expected.

We started with a simple 'move forward' command.

Up a Little and to the Right...

Our particular bug had a tendency to bear right. If we were ever hoping to have a straight bug, then we would have to find a way to fix this. We decided to find out why this unexpected turning was happening. We needed to see what the difference was between motor a and motor b.

We achieved this by measuring the discrepancy between counts (at the moment an unspecific unit of measure). Over a distance of ten feet, countb measured 3056, while counta measured 2978.

The discrepancy between the two motors was approximately 78 counts when they were simply commanded to go forward at equal power levels.

After much guessing and checking, we finally managed to get the bug to go a full ten feet. We then measured the value of a count. The bug moved 3056 counts in ten feet. That's 305.6 counts per foot. Which is 25.47 counts per inch.

Although these measurements were accurate for this particular trial, we found that the discrepancy between the motors and the distance in counts covered in ten feet varied depending on where the bug ran, how fast it ran and how much friction there was between the wheels and ground. Certain areas of the floor were bumpier than others, and even though the lab floor provided the lowest friction possible for our bug, it still had an effect. Any skidding or slipping that occurred was reflected when we measured the discrepancy multiple times. If the discrepancy was smaller, slipping occurred, because more rotations happened. If the discrepancy was larger, skidding occurred because fewer rotations were counted.

We then wrote a program to try to account for these variables.

Go Ten Feet and Account for Count a and b Discrepancy

Entitled GoStraight, the program gave specific commands depending on where the car was and how large the discrepancy was between counta and countb. When the bug was one third of the way along the track, the power to motor b would drop to 99%. 

Mistakenly, however, we dropped the power on the wrong motor. After checking Snow White, we discovered that it was motor b, not motor a, that was running slower. We tried switching out the count to reflect this correction, but then the bug began turning left.

We needed a more complicated program that effectively accounted for the differences between motor a and motor b. 
Go Ten Feet and Account for Count a and b Discrepancy Version 2

We dropped the power to both motors to 90%, set the counts both at 0 initially, and had motor a drop to 89% one third of the way along the route. 

Better...Sort Of

Snow White definitely went straighter. However, it still had a tendency to veer slightly rightwards the farther away from the starting point it moved. 

Moving into the feedback portion of this challenge, we tried turning respective motors off whenever the counts were uneven to see if the car would continually correct itself along its path. 





The bug did continually correct itself, but along a rightward trajectory. Even turning only motor a off for a small interval of time could not correct this behavior. We returned to our original program, fiddled with the power allowed to each motor, but always our bug had a slight rightwards tendency when it traveled over long distances. We decided to see if sensors and more feedback could potentially fix this problem.

Because ten feet was placing us in the way of other working groups, we lowered our track distance to five feet and tried to see how close we could get the car to adhere to this distance. 

We started with the touch switch on the from of the bug. When the button could either read 999 (unpressed) or 0 (pressed), so it was a simple measuring device. Basically, it asked a yes or no question. 

Go Ten Feet and Stop When Touch is Pressed

Entitled gtfst (go ten feet stop touch), our program ran the previous course, but after a while, a sensor was activated, that when the button was pressed, would stop all power to the motors. 



The sensor worked. When the car came into contact with the bin, however, it moved slightly over the line. Because the car had momentum when it encountered the box, it could not immediately stop, so the box was moved a little. However, if the box had been a solid wall, the car may have bounced off it, or even sustained some damages as as result of the collision. Additionally, the button had to encounter the object dead on. Any turns or odd angles of contact might now have provided the proper force to activate it.

We moved on to the ultrasonic sensor. Entitled, gtfu, this program had the robot stop initially one hundred units from the box. After we were sure it worked properly, we lowered the distance to 1 unit, which should have placed the bug right on the line before the box. 

Go Ten Feet Stop with Ultrasonic



Even with the unexpected left arch, the bug was still a few inches away from the line. Because 1 was the lowest possible setting for the ultrasonic sensor, we could not rely on it to place our bug exactly on the line if the box was close to it. It could not effectively approach the wall. However, the bug did stop effectively, and without the requirement of contact with the wall to stop it, there was no risk of Snow White breaking. The lower the power was on the motors, the more precise the stop. Even though the bug did not have to hit the wall, it still had some momentum when the sensor tells it to stop and tended to jerk and then stop rather than stop immediately. In an annoying twist, we ended up having to tie our wires down more securely because whenever they fell in front of the ultrasonic sensor, the bug ground to a halt. Despite its setbacks, this sensor had potential to be useful for our program.

Finally, we tested the brightness sensor. We had to measure the difference between a light and dark color so that the brightness sensor could know what to 'look' for. We found that a dark color was in the 500s range while lighter colors, like white, were in the 300s. We set the middle ground as 400. If the sensor recognized anything above that, it let the motors run, Anything below 400 caused the motors to turn off.

A Nice Stop


Go Forward and Stop with Brightness Sensor

Go Forward and Stop with Brightness Sensor

The brightness sensor worked extremely well. So long as the sensor eventually ran over the line, it did not matter if the bug's path was perfectly straight. Additionally, the large discrepancy between the light and dark colors of the floor and line made it easy for the sensor to distinguish the two. However, if the power on the motors was set anywhere above 85, the bug ran the risk of overshooting the line. Even though the sensor would see it, and the bug would stop momentarily, the bug would be moving so fast the sensor would end up on the opposite side of the line, see the floor, and tell the bug to keep going. So long as the power was low, however, the brightness sensor worked well. It stopped right on the five foot line, whereas the touch sensor landed the car one inch over and the ultrasonic had it stop several inches too short. The brightness sensor worked the best by far. 

To get the bug to reach an exact distance of ten feet with minimal turning, we used both a touch and brightness sensor. Whenever we turned the bug on, the act of pressing the small white button put extra force on the right side wheel. In order to reduce friction and help with the turning problem, we instead activated our code by pushing the touch sensor. The bug moved forward and stopped when it found the line. We also incorporated our original code that changed the power settings to the motors after a set distance. We called this procedure sbb (straight bang bang).

Go Ten Feet Exactly and Stop


We then tested this procedure on a variety of different surfaces. It ran relatively straight on the lab floor, although by the time it reached ten feet it was still bearing a little bit to the left. On the carpet, it performed just as well, though the white line needed to be thicker to prevent the bug from overshooting.



It was the ramp that presented the most difficulties.

When it was traveling down the ramp, the bug had very little grip on the smooth wood and effectively slid the entire way down. It it could not stop itself when it saw the line (we changed the sensor settings so that when it saw a dark color the motors would turn off). 

Where are the brakes on this thing?!?

We were afraid at the speed it was going that if it hit the trashcan at the bottom it might break. It did make it all the way down the ramp without falling off, though.

Going up the ramp was even harder than going down it.

The rightward tendency was more pronounced going up the ramp than it was on any other surface. On every attempt the bug eventually tumbled over the right side of the ramp (we caught it each time, fortunately).

We Tried Again...

and Again...

and Again...

and Again...

...

...

...

...you get the picture

On one attempt, we managed to get our bug to traverse more than half the distance, but ultimately our procedure that worked on the ground could not work on the inclined plane. We tried using the brightness sensor to help the bug know when it was crossing over the edge, but the gap did not register correctly or consistently enough to work sufficiently. It is possible that the friction between the wheel was different depending on the side. Even with dramatic right turns and the power on motor a reduced to 75%, the bug still toppled over the edge. 

Finally, we attempted to make a line follower. Surprisingly, despite the procedure's more complicated program, it was actually easier to make. We used a simple if-then-else statement to activate the motors. When the brightness sensor registered a light color, one motor turned on and the other turned off. When it registered a dark color, the opposite happened. In addition, we also reduced the power on the motors to 80% so that the bug would not overshoot the line.

Bang Bang Line Follower




Needless to say, it worked extremely well. 

If we had more time, I would have liked to have fiddled more with the uphill procedure, as several other groups demonstrated that a successful procedure is possible. Aside from this, I was happy with our results, even if our little Snow White had a tendency to pull to the right. If we just lay tape down all over the floor, she can travel perfectly straight. 



Sunday, March 2, 2014

Real World Feedback and Control Systems: The Puzzle of Feedback and Control on a Daily Basis

So feedback and control is a science of gathering, interpreting and acting on information.

So how does this apply to everyday life?

There a loads of examples all around us that incorporate feedback and control methods.

1.) Smoke Detectors

Because they are so commonplace, it is easy to overlook smoke detectors as feedback and control mechanisms. Smoke detectors work by having a beam of light directed across its inside radius. When smoke rises and encounters the smoke detector, this beam is interrupted (sensing mechanism). When the light cannot be detected, the alarm is activated (actuation). The control computations for a smoke detector a very basic. Is the light interrupted? Yes or no. The smallest amount of smoke can set them off, which is why they are both a fire safety and fire prevention device: they can sense the fire before it is big enough to cause any substantial damage.

Despite their simplicity, these little alarms do have their setbacks. Anyone who has ever burned something by accident can tell you that smoke detectors, though advertised as fire prevention, can not distinguish between smoke caused by a fire and smoke caused by ruined Christmas dinner or a candle. Sometimes, when their batteries are running low, they beep for a different reason altogether, and can cause their owners a lot of frustrations. I learned that the hard way this past weekend.

(technical help provided by http://chemistry.about.com/cs/howthingswork/a/aa071401a.htm

2.) Thermostat

The older version of the thermostat operated using a bimetallic strip. I believe some rooms in my house still use this old design. As the temperature around the thermostat changes, the two metals in the strip expand and contract at different rates. This causes the strip to bend (the way depending on the temperature). The bend it was tells the old thermostat that the house is getting too cold and it's time to heat things up (sensing mechanism). The thermostat then turns the heat on (sometimes full throttle) until the bimetallic strip returns to its original state (actuation) and then it shuts off. Once again, the control calculation is very simple and can be broken down into a yes or no question for simplicity. Modern thermostat's can sense the temperature in the room using a digital thermometer and then compare that temperature with the input temperature. If it is too cold the it will warm the house until the appropriate temperature is achieved. The one flaw I see in this design is the preference of the owner. Maybe he or she does not want the house at a constant temperature when they are not home. Maybe they can not afford that. A timer run thermostat seems like a more environmentally and economically safe choice.


3.) Automatic Doors

We have all encountered these at the grocery store. You step on the black tarp (sensing mechanism) and the door slides open (actuation). However, the control computation's for this device are slightly more complex than those of the previous two. For example, the doors do not open if a cat or bird happens to stand on the mats. The sensor has been programmed to measure the applied force on the mats and then compare it to the weight of the average human, or a set weight like fifty pounds. Anything below that will not activate the doors. For the most part, these kinds of entrance ways have always seemed well designed and functional. However, there is the risk of injury if the sensor fails. From personal experience, my mother once witnessed a man mistake the wrong door for the automatic one. He was ok, but had he been walking faster or been holding scalding hot coffee in his hands, things may not have ended as well. If a store experiences a power outage, will people be trapped inside? There are too many situational problems to consider for this product. While they are helpful, I would not deem them 'ideal'.

4.) Fish Tank Heater

This device will always have a special place in my heart, as it keep my little friends alive through the long Maine winters. A fish tank heater, like a thermostat, measures the temperature of the surrounding water (sensing mechanism) and compares it to a set temperature (my tank is set currently for 80F). If the water is too cold, then it will automatically turn on until the temperature in the tank is equal to the set temperature (actuation). The calculation, while similar to the thermostat, is much more complex, as an error of a few degrees can result in some very dead fish. The extended length of the heater allows it to get a more accurate reading by exposing more of its surface area to the water. I have had one for two years now and so far no complaints. I have, however, heard the horror stories of heaters malfunctioning due to water damage and becoming so hot that the fish are boiled alive. Despite my heater's intense insulation and shatter proof casing, I always worry just a little bit and check it for signs of damage whenever I clean it.
My Late Betta Albert with the Heater


My Current Betta Newton with the Heater

Bettas only make bubble nests when they are extremely happy
...like when the water is just right

Response to Control in an Information Rich World: The Puzzle of Feedback and Control

What exactly are feedback and control?

According to R. M. Murray, control is a way of modifying behavior in machines or other manufactured products based on feedback from the surrounding environment. A product achieves this feedback by first performing a task in the environment and analyzing the computational feedback it receives from sensors.

Control and feedback are a way of limiting uncertainties in a products performance. They allow a system to compare its current performance with a desired one so that adjustments can be made to appropriate variables in its performance to help it more closely resemble the ideal performance. Even is the environment in which the product is located is constantly changing and altering what the ideal performance constitutes, the control and feedback can help it achieve a closer approximation to ideal than the product could without them.

Simply put, control allows the system to correct itself when it is not performing as well as it could based on feedback on how it is currently performing.

Control and feedback are not just limited to electronics. Chemical and biological examples include the human brain and nervous system. There's a reason why a person quickly jumps and pulls their hand away from a hot stove.

"Control is information science" - R. M. Murray

Control can be traced back to the industrial revolution. If a train engineer had to make a certain deadline, and suddenly he finds himself burdened with a few extra cars, then he or she will have to change his speed in order to meet the deadline. The same would be true for a cart and mule, or a modern day tractor trailer. Fundamentally, control and feedback have remained the same, it's just the ways we received feedback, analyze it and act on have changed. Dramatically.

The one thing about this article that I did not understand was the concept of feedforward. It is defined as "actions taken based on plans", but to me that sounds like a machine without a control and feedback feedback mechanism. If a machine is running blindly, without any information about what's going on around it, then is it not technically acting 'according to plan'? That won't necessarily mean the plan will work.

I was VERY intrigued by the aerospace applications of feedback and control. From its early forms to modern day, feedback and control have become so sophisticated that they can be used to create unmanned aircraft, capable of targeting specific locations, if not people, and destroying them with little margin of error. The amount of sensors, decisions and calculations a single unmanned vehicle, like a drone, would have to use and make is baffling to me, and all in a series of a few minutes. Though the topic is controversial, the science behind it is breathtaking. Even though the article did not expressly talk about drones (given its age), it hinted at the possibility of such machines being within reach.