Monday, March 30, 2015

Encoder, Touch Switch, and Ultrasonic Sensor

Most recently we've been working with three new sensors which are really useful tools in feedback & control engineering: encoders, touch switches, and ultrasonic sensors. To really understand each of these sensors my partner and I completed tasks based around each one, which I will detail below.

Encoder

The encoder counts the number of rotations of the shaft on the sciborg. This is a useful way to control how far your sciborg drives or for how long. Especially if you don't want to use other tools like Millis or delay. Because we set x to equal zero, the starting value will be zero, where x is the number of rotations. The end value should also be zero, since it goes forward and backwards the same amount. However our end value never quite reached zero. I think this is because of the time it takes for the motor to speed up and slow down; it is not instantaneous either way.

Touch Switch

The touch switch is a small switch on the front of our sciborg that senses when the sciborg hits something, as it gets pressed. We first wrote a sample code onto Arduino and connected an LED light to test the switch. We had a little trouble getting the LED to react with the touch switch at first. The first time we tried our LED lit automatically and turned off once we pressed the touch switch. Then, as expected, after we modified the code our LED started in the off position and stayed lit after pressing the touch sensor. To fix this, we added a second part to our code that said that if the button was "HIGH" or released, then the LED would be "LOW" or off.

Ultrasonic Sensor

The ultrasonic sensor senses objects in the area within a specific range. This range can be tested and viewed on the Arduino software. We first wrote a sample code for our sciborg and tested its range to get a feel for the numbers. Then we got to do some fun tasks using all three of these sensors!

Feedback and Control Activities

Earlier we tried to make our sciborg go ten feet and then stop using the Millis function. Now we'll use an encoder to do the same thing. My partner and I ran into a few problems with this task, namely we never know where to put the stop function so that our sciborg will actually stop. Also, we had the delay in the wrong place, so the encoder would only count to about 2 after going ten feet. This is what our first try looked like:
After we corrected our mistakes, our sciborg was able to easily go ten feet and then stop. At first we measured the encoder to be 10300 at ten feet, which is what is shown in the picture below. But after trying to implement it into proportional control, we went back and found the correct number to be 11200. Here is our (almost) final code:



With this code we got almost exactly to the 10 foot mark, witth the front of our sciborg being maybe an inch or two from the white line. This method was more precise than using Millis, where our closest was a few inches past the 10 foot mark.

Now we wanted to get our sciborg to stop when it hit an obstacle. To do this we used our touch switch. We wrote a code that indicated that when the switch is released the car will move at the speed specified. Then, when the sensor is pressed because of hitting the obstacle, the motors will shut off. This code worked on our first trial and we were very excited.
Slightly more challenging was using the ultrasonic sensor to stop the sciborg when it sensed it was close to an obstacle. To do this, we took note of the numbers when something got really close to the sensor, then we wrote it onto our sample code as a starting point. Slowly we were able to lower this number, which started out at 20, to 15, which got our sciborg a few inches away from the wall before stopping.

Now, our challenge was to make our sciborg run ten feet and then stop using proportional control, not bang bang. I'd like to note that the proportional part of this task only shows up at the very end of the program because we set a maximum speed value of 255. So while the output would normally be much greater than 255 for most of the run, and decreasing proportionally according to the error value, this does not show up until the very end of running our program when the output value drops below 255.
To implement proportional control, we calculated a reasonable gain factor using the fact that output=gain*error where error is your goal position minus your current position. We settled on a gain factor of 2, which stopped us about a foot and a half away from the line. This is to be expected, even with the correct goal value of 11200, because proportional control is asymptotic and at some point the output will be too low to continue. To cover the extra distance we implemented a function called "nudge" which runs the motors at a low speed for a short period of time. Originally, the regular sketch and nudge would run once and then nudge would repeat after every ten seconds, our original delay. This is because we didn't set a maximum current position in our if statement about nudge. once we did that, our nudge ran once for two seconds, and then the program stopped. We managed to get within 2 inches of our goal. We also used this function to not just run the motors for a long stretch of time until it reaches the goal, but to inch forward slowly. Here is what the code for that looks like:



This was able to get us about an inch closer to our goal, although we did find that it ran more crooked when it was inching forward.

Finally, we were asked to make our sciborg run in a "conga line" or to basically be able to follow a moving object in front of it for a short period of time. To do this, we used our ultrasonic sensor and make specifications through if and if else statements to tell it when to slow down, speed up, run at it's normal set speed, or stop. In order to do this, we measured the values of the ultrasonic sensor at different distances from a box top to get a correct range. Here is our code:
Here are videos of our sciborg running nudge without inching forward, and with inching forward.



No comments:

Post a Comment