Our challenge was to
Encoders:
The encoder is a sensor which is located inside of the Lego motor. It measures the number of times the shaft has rotated. This can be very useful in describing the position of the sciborg when trying communicate that to the code. Below is the base code that runs the encoder. When the arduino software reads the code it begins at 0 because the shaft has not rotated. As the shaft rotates the number read by the encoder becomes increasingly negative. The magnitude of the number shows the number of times it has rotated since the beginning position marked by 0.
The version below has better comments
Touch Switch:
The touch switch sensor is located on the arm of the sciborg. It is similar to the Lego button sensor explored in the the previous post, but due to its design it is generally more effective in stopping when it encounters an object. This is because the Lego button sensor is very small and is often not positioned at the appropriate angle to ensure that the obstacle pushes the button. The arm covers a larger vertical space in front of the sciborg. A sample code for this sensor is also pictured below.
In this example code, the LED light stays on forever after the button is pushed because the code never resets itself to receive any other input after the first time. We altered the code so that it would only turn on when the button was being pressed. The video and code are shown below.
Ultrasonic Sensor:
In my opinion, the ultrasonic sensor is the coolest sensor we have used thus far. It works in the same way that a bat uses echolocation to project a small sound wave that is then bounced back to the sensor. The distance that the sensor reads is dependent on how quickly the wave returns to the sensor. The sample code is shown below. We found that it's range is from about 6 to 280 as measured by our tests; that translates to about a distance of 2.5 - 3 meters. The sensor is most accurate to large flat objects such as a wall or board, although it is capable of picking up other objects as you will see later on in this post. The accuracy of the sensor is also affected by the periphery. We found during tests that after calibrating the sensor by pointing it into open space, that sometimes it would pick up us or the walls when we were on the outskirts of its field.
Feedback & control Activities:
Encoders: Fixed distance, bang bang control
In this activity we had to use the encoder sensors in the motor to tell the arduino to stop after 10 ft. At first we misinterpreted the challenge by assuming that the number of turns of the wheel corresponded directly to the number of the turns of the shaft. This however is not the case. Once that confusion was sorted out though, we were able to write a code that accomplished what we wanted it to. Our original 10 ft code is directly below and the improved encoder bang bang code is under that.
Button Sensor: Fixed distance, bang bang control
Next we used the button sensor to make Frank travel 10 ft then stop. We just altered the code we wrote earlier for the button sensor slightly. In order for this code to work though there needs to be an obstacle to at the 10 ft mark to push the button sensor.
Next we wrote a code that would make Frank travel forward until he sensed something close enough in his path as measured by the ultrasonic sensor. We had a lot of trouble initially writing this code because the ultrasonic sensor wouldn't run while the motor was running. We found that the solution was simply to change the while to an if statement.
We then used this code to create a konga line code that relied on bang bang control
Encoder: Fixed Distance, Proportional Control
Our next challenge was to alter the codes so that they were proportional control rather than bang bang. proportional control allows us to gradually slow down the motor based on the input from the various sensor rather than just turning off the motor entirely. In order to do this we had to understand that proportional control must be written in terms of output=error*gain where output=(goal - measurement)*gain. Once this was explained to us we were able to write a code using proportional control.
Ultrasonic: Fixed Distance, Proportional Control...KONGA LINE!!!
The ultrasonic proportional code is written so that if the object in front of it is within a certain range picked up by the ultrasonic sensor then the motor's speed increases, but if it it is too close then a bang bang control back up is implemented to make sure the car does not run into the car in front of it a konga line is.
Reflection:
After this project I feel that I have gained a better understanding of the coding language which I think will serve me well as I begin to consider the problems related to my final project. That being said, there is so much that I have yet to understand fully about the coding language, so it will continue to be a challenge to work with it.
I really like that your blog has at the brown background! It makes it much easier to read the coding due to the color contrast.
ReplyDelete