Motor Encoders
The following sample code for encoders measure the number of rotations of the motors. The encoder reads the current position and these values are printed. In the sample code, the wheel moves backward and forward, and the encoder reads the current position of the motor. The starting value for the encoder should be zero because we reset the encoder to zero in the void setup when we have "m.set_pos(0)". The end value is not zero because we don't reset the encoder to zero within the loop.
Touch Switch
There is a touch switch on the front of our sciborg. This can detect when the touch switch is activated and when it is not (e.g. the sciborg runs into an obstacle). The touch switch is wired to a digital pin on the Arduino and also to ground.
Wiring for Touch Switch and LED |
The following sample code includes the LED and the touch switch. When the touch switch is pressed, the LED turns on, but stays on forever and will not turn off when we let go of the touch switch. Our job was to fix this and make the LED change according to the touch switch. We wanted to modify the sample code to make the LED turn on when the touch switch is pressed down and turn off when the touch switch is not pressed down. All we have to do is add an else statement to the if statement.
Touch Switch with LED
At first, the LED did the opposite of what we wanted. It would turn off when the switch was activated and stay on when it was not activated. We weren't really sure what was happening, but played around with the code and wiring to see what was going on. The code below is what we had for our first attempt.
Then, we modified the code once we figured out what was wrong. The code in the loop is identical from the previous picture, but we think it had to do with the mode of the touch switch in the setup. The mode in the first attempt was just "Input" (we're not sure what happened there?) and in the following code for the second attempt, we changed the mode to back to "Input-Pullup". This switches the input. Additionally, for some reason it would not work when we wired the touch sensor to the LED, so as you can see in the comments, we took that wire out from our first attempt. In the video, we have the LED turning on and off with the touch switch.
Ultrasonic Sensor
The ultrasonic sensor measures the distance from objects surrounding around it. For example, if nothing is in front of the sensor, the printed values are large, and then as the sensor approaches a wall, the values become smaller, indicating there is something close to the sensor. As you might guess, this can be useful for helping our sciborg avoid obstacles. The following is the sample code for the ultrasonic sensor. The sample code here gives value in the serial monitor that corresponds to distances. A low number indicates something is near to the sensor and a large number indicates the sensor does not sense anything near it. The sensor can also pick up readings from the sides, not just straight in front of it.
Feedback and Control
A. Fixed Distance, bang-bang control
In Day 3 of Arduinos we made our sciborg travel 10 feet by determining the time it would take and setting out motors to stop after that given amount of time. In this bang-bang control task, we were able to get our sciborg within a few centimeters of 10 feet.
**Explain testing of values
Now we are using our new sensors to have the sciborg stop in different ways. We used the ultrasonic sensor to control the point at which our sciborg would stop in front of an obstacle. In our testing, we found that using the ultrasonic sensor, our sciborg would stop about 10cm short. We could lower the parameter of how far away it should stop, but the ultrasonic sensor also isn't very steady. A very lower value doesn't the sciborg to stop in time.
This code is a combination of the ultrasonic sensor code and basic motor sciborg code. We have the ultrasonic sensor reading values and the motors are controlled by those values. If the sensor does not sense anything in front, meaning the sensor values are greater than 30, the motors are set to forward at full speed and if the sensor senses an obstacle, meaning the values are less than 30, the motors set the speed to zero.
B. Fixed distance, proportional control
First we had our sciborg travel 10 feet using bang-bang control, and now we want to use proportional control. Proportional control means that, as the sciborg gets closer to its goal, its speed will be proportional to the remaining distance. Thus, it slows down as it gets closer to 10 feet.
Using proportional control means we need to do some calculations. Here in our code the equation we used was Speed = distance * gain, where distance is the distance remaining or error (goal - actual) and gain is a constant that we calculate. We find the gain constant by taking the maximum speed of the motors 255 and dividing by the total distance. In this case, we are using the distance 13000 because that is the number of rotations (measured by the encoder) needed to travel 10 feet. We determined this value by testing different encoder values for the sciborg. Our gain constant is 0.0196. Our encoders count down from 13000 to 0, so we take the current reading from the encoder and multiply this by the gain constant. So when the sciborg is at the beginning, it's position is 13000 and this multiplied by 0.0196 is the maximum speed 255. (This is because we divided 13000 by 255 to get the gain constant in the first place). Then as the sciborg continues, the current encoder value is decreasing and multiplying by the gain results in a smaller number for the speed. Therefore it slows down.
You might notice there is a function in our code called nudge(). In proportional control, one issue is that around 9 feet, the speed has been getting smaller and smaller, so the sciborg is moving very slow and at some point can no longer move. However, it hasn't reached 10 feet. As we found earlier, the sciborg has a minimum speed where the motors are no longer able to move the car. This value is around 50 for our sciborg. To solve the problem, we created a function that nudges the sciborg to the end when it can no longer move under the proportional control. If the speed of the sciborg gets lower than 70 and the sciborg has still not reached its goal, we call a function called nudge. This function turns the motors on and off and will continue to do so until 10 feet is reached.
C. Conga Line
Next implemented bang-bang control and proportional control in a different task. This is the conga line, where we want the sciborg to follow something in front of it.
For bang-bang control, we decided to have the sciborg turn left or right. Here, if the sensor doesn't sense anything the car turns left and if it senses something it turns right. This way, when we moved our sheet of Delrin in front of it, it moves forward and slightly right, and then when we moved back with the Delrin it moved forward and slightly left. This allows our sciborg to 'search' for objects in front of it to follow, while also (somewhat) traveling in a straight line.
At first, we didn't think about doing bang-bang by just having the sciborg move forward. We could do this by having he motors turn on when nothing is in front of the sensor (meaning the Delrin is further in front of it) and turn off when something is in front of the sensor (when it's really close to the Delrin).
When we coded for the the conga line with proportional control, we had our sciborg just traveling straight. Proportional control for the conga line involves having the sciborg move faster when the Delrin sheet is far away and slower when the Delrin sheet is close.
Our next step is using bang-bang and proportional control to have our sciborg follow a white line on the floor. We are going to use a new sensor that measures brightness!
No comments:
Post a Comment