Bang-Bang Control
Last time when we used time to make the Sciborg to go for 10 ft, the Sciborg would stop +/- .5 ft. This week we are using the encoder, which is a sensor in the motor that records how many turns the shaft has made. After a few trials, we decided that we will make it close to 10ft using a number around 12500.![]() |
| The delay allows us to put the Sciborg on the floor and straighten it up before it starts running |
Our next task is to use bang-bang control with the ultrasonic sensor. We used the number 15 here, which is the smallest number that showed up in our previous reading with the ultrasonic code.
We were able to get the Sciborg stop wherever we wanted after a few test runs, seeing what the reading of 15 translates to distance in real life. Note that we had to make sure the whole track was clear so that it would not stop because of other objects in the way. Note that we must put the Serial.println in the loop so that the value would be updated each half second. We first tried the code without Serial.println in the loop. The Sciborg ended up going at the same speed that correspond to the initial reading.
Proportional Control
Using encoder again, we are now doing proportional control. Our goal is to have the speed of the Sciborg proportional to the number of turns in the shaft. This means that the closer we get to the target goal, the slower the Sciborg would be.Note that the number for target position is very different from the one we used previously with an encoder, because the Sciborg would actually stop before it reaches target position, once the velocity becomes too small. So this actually never reaches the target position written in the code. However, it got us very close to the finishing line. Also, like we did with ultrasonic bang-bang control, the value for speeds must be written in the loop, so that it will be updated each 10 milliseconds.
We then added another function toward the end, called nudge, which would help the Sciborg to over come the last bit of friction and reach target position.
Note that we used a different value for target position, and that is because we used a different surface. We moved to the rug outside with a bigger friction, which in fact helped the Sciborg to go straight. The bigger friction, however, would require more turns to reach the target line in real life. I cannot figure out why exactly, but I imagine that it has something to do with slipping of the wheels.
When we were writing this code, we also had a question regarding the difference between the "if()" and "while()" command. It turned out that in the loop, "while()" command would not end until the statement in the parentheses are not true, but "if()" would only run once. As a result, we put delay in the while() command to run it repeatedly, asking Arduino to check its distance from target position.
Our next task is to use ultrasonic sensor to have the Sciborg follow something in front of it.
Before we started writing the code, we first tested how the reading correspond to distance. It turned out that for objects <20cm away from the sensor, the reading was 11. At 30cm, the reading was 20. 30cm --> 25, 40cm -->30.
This is in fact a bang-bang control, even though there are three options, as opposed to two. We then wrote another code, so that if the Sciborg gets too close to the leading object, it would back off a little.
Here is when we encountered a problem, where the reading would become very random. After a while we realized what happened. Earlier, our ground wire broke off from the sensor, and we soldered it on. And then the middle wire broke off, which we soldered on again. We probably did not do the best job soldering the wire. We tried the sensor on basic code that simply shows the reading, but same thing would happen. Since we could not fix the sensor, we were not able to run the proportional code.

Here is the loop part of the proportional control code:
while (analogRead(A0) < 100) {
int v1 = analogRead(A0) * 12.75;
int v2 = analogRead(A0) * 11.75;
m1.set_speed (v1);
m2.set_speed (v2);
delay (100);
}







I like how you explained your code to the reader as you were explaining your design and testing process.
ReplyDeleteWe ran into the same problem with the encoder where it would stop because the velocity was too small to reach the target. Nice job addressing the problem:)
ReplyDelete