Tuesday, May 18, 2010

Fire Fighting Robot [Coding]

[Fire Fighting Robot: Coding - Auxiliary codes]



There are 12 auxiliary methods. I call them auxiliary because they do not really control the motion of our robot, but either start or help the mechanism:

* brake, forward, backward = Those methods make the robot behave like their names
* lslow, rslow, sback, sfor = These methods are abbreviation for "left slow," "right slow," "slow backward" and "slow forward" (They are controlled by the low power settings from input)
* nudge = Nudge allow the car to march forward for 100 msec each time invoked
* settee = If the reading from sensor 1 is greater than the given value of the variable largest, then it sets the largest to whatever value read from sensor 1.
* getlarge = This method helps the settee method. It allows the car to turn left one full rotation and help settee method set the value of the variable largest to be the reading from sensor 1.
* startpos = This method determines the starting position and is invoked after the getlarge method. By the time this method is invoked, the variable largest will be set to the values read from the sensor 1 during the one full rotation. The robot turns left until the sensor 2 detects a number 100 less than the given largest value. (100 was a good number for it to stop facing the candle. If the number was too small, then the value of largest was often bigger than the reading from sensor 2, causing the robot to just spin and spin)
* FINAL = This method is what we double click to give all the instructions to our robot.

[Fire Fighting Robot: Coding - Main code "go-d-2" & "hello"]



This method called "go-d-2" is the method that commands the robot to travel a certain distance in the beginning. Once it gets close to the target line (10 feet away from the start position), it starts to check for the light. Generally, our readings on sensors were pretty consistent. They would always range in between 993-996 if the candle was in the desired position (in between the two sensors and under the top shell). Therefore, we set our if condition to be "if sensor 1 reading if less than 990," which means that the candle is not in our desired position. If the if statement is true, then the snud2 method gets invoked (snud2 method basically commands the robot to approach the candle so that it is in the desired position. It is explained in greater detail below). Otherwise, it would perceive the condition as "yes, the candle is in the right place" and would brake and then execute the method called hello.

The hello method is the one that controls the mechanism of the servo motor and, therefore, the mechanism of the top sea shell. In the beginning, we only had the servo motor turn thisway, wait for a while, and then turn thatway, causing the top sea shell to drop down, squiz the candle, and the go back up. However, many of the times, the light on candle would not go out completely and would light up again after the sea shell goes up. Therefore, we implemented some more mechanism to our hello method to really make sure that the candle light goes out. We added sfor, lslow, rslow, and sback methods, which were invoked after the top sea shell has dropped, having the candle light in between the two sea shells. With it in between, the robot would move slightly forward, left, right and then back. In this way, the robot completely put out the fire without a question. (This is also shown in one of the video clips in the next blog entry)

[Fire Fighting Robot: Coding - Main code "snud2"]




This method is our really long method. It gets invoked from the go-d-2 method to begin with. Under the condition that the sensor 1 reading is less than 990, which means that the robot still has to search for the candle, this method commands the robot to move towards the candle based on the difference between sensor1 and sensor2 (difference = sensor 1 - sensor 2). A positive or a value that is greater than 5 for the difference means that the candle is placed to the left of the robot since sensor 1 is on the left side of it. So, it would be commanded to execute lslow method or left slow method. We also wanted to have another if condition before executing lslow method because the candle may be really close to the robot and so commanding it to turn left can be suicidal for the robot. Therefore, in the method above includes another if statement that checks if the sensor 1 reading is greater than 990. If it is true, then we surely want to back up and then execute lslow method. If the difference is a negative number or a number that is less than -5, then we want to do the opposite and the same logic was applied to the coding. If the difference is fairly small, which means that the candle is pretty much in between the two sensors, then the robot is to be commanded to nudge forward.

In the beginning, we refrained ourselves from using the other two sensors, sensors 3 and 4, because we believed that they were making the coding even more complicated than it already was. However, because we had our sensors 1 and 2 angled slightly inward, the sensors were reading numbers very unexpectedly as it got closer to the candle light. When the light was placed in front of sensor 2 (on the right), sensor 1 (on the left) was reading a greater number than sensor 2 because of the shadow created from the black cover we put on the sensors. And this caused our motor to turn the other direction, again, setting itself on fire. Therefore, we decided to use the readings from sensors 3 and 4 in the beginning of the method. If the back sensors were reading larger number than the front sensors, then we had the robot turn to the side of the sensors. In this way, the robot no longer turned to the other direction.

After this method, the system will return to the forever loop in the go-d-2 method and repeat the whole process again.

No comments:

Post a Comment