Presentation is loading. Please wait.

Presentation is loading. Please wait.

Robotics Week 6 line follower

Similar presentations


Presentation on theme: "Robotics Week 6 line follower"— Presentation transcript:

1 Robotics Week 6 line follower
Compiled from various Internet sources Presented by Mr. Hatfield Robotics Week 6 line follower

2 Unceremoniously Lifted
I used the code from this site to help teach this site

3 Let’s learn how to follow a line
In the simplest form with one sensor if the sensor senses a line turn in the direction of the line. Otherwise turn away from the line. The problem is that if the sensor passes the line before senses it then what?

4 Code For a Simple LF Take a cue from This is where I got the code…

5 Line Follower (Basic) //simple but effective, the only thing to worry about is turning too hard. //If you turn to hard then you will over shoot the line, that is bad if the //line turns to sharp. //I like to call this the wiggle waggle line follower, you’ll see… task main() { int pwr = 50; while(1==1){//This is just TRUE, it goes without stopping if(getColorReflected(color) < 45) {//45 is a decent half way value setMotorSync(left, right, 25, pwr); //Turn to the line } else { setMotorSync(left, right, -25, pwr); //Turn away }

6 What Happens at Power = 100 //simple but effective, the only thing to worry about is turning too hard. //If you turn to hard then you will over shoot the line, that is bad if the //line turns to sharp. //I like to call this the wiggle waggle line follower, you’ll see… task main() { int pwr = 50; while(1==1){//This is just TRUE, it goes without stopping if(getColorReflected(color) < 45) {//45 is a decent half way value setMotorSync(left, right, 100, pwr); //Turn to the line } else { setMotorSync(left, right, -100, pwr); //Turn away }

7 If You Gave It a Try You found that too much power caused the robot to overshoot the line on an inside curve with a .75 inch line. Smaller lines pose a more difficult challenge. Limitations are very real in this code. Either way too much energy was wasted in the “Wiggle-Waggle.”

8 What To Do Lower the power, but this is a race… Make it wiggle less…
But how??? The color sensor senses more than BLACK and WHITE, can I use a gray-scale value? Information on ROBOTC.net, Check it out for more information. It is for NXT but still good and applicable.

9 Calibrating the Color Sensor
If we don’t have to guess at the gray value that could be better. Pass the sensor over the line and see what the darkest and lightest values are. This gives a good approximation of gray that we can use in the code.

10 Specifically… Modify the code from turnLeft and turnRight
First, turn left 30 degrees and setLightDark Then turn right 60 degrees and setLightDark Then turn left 30 degrees to return to the original position. Then Calculate Gray based on the average This assumes a pivot style turn.

11 Can We Adjust Turn For Dark
//simple but effective, the only thing to worry about is turning too hard. //If you turn to hard then you will over shoot the line, that is bad if the //line turns to sharp. //I like to call this the wiggle waggle line follower, you’ll see… task main() { int pwr = 50; while(1==1){//This is just TRUE, it goes without stopping if(getColorReflected(color) < 45) {//45 is a decent half way value setMotorSync(left, right, 25, pwr); //Turn to the line } else { setMotorSync(left, right, -25S, pwr); //Turn away }

12 Calibrate Light/Dark/Gray
Guessing the values of light, dark and gray is a bad idea. If the room is dark… If the room is light… What if the room changes lighting conditions? All these types of concerns mean that we need to calibrate our sensors for the conditions at hand.

13 How to Calibrate the Sensor
Again, easy in theory… Move the sensor over the line and back again While you do that find the maximum value the sensor reads and keep it Also, find the minimum value the sensor reads and keep it. The first is the dark value, the second the light value, Gray is the average

14 Sample Code Set Light and Dark function, is necessary before we can calibrate the sensor. Code is self explanatory void setLightDark(){ int cr = getColorReflected(color); if (cr > light) {light = cr;} if (cr < dark ) {dark = cr;} }

15 Sample Code void calibrateColorSensor(int deg = 15) {
while(abs(getGyroDegrees(gyro)) < deg) { setMotorSync(right, left, 100, 25); setLightDark(); } resetGyro(gyro); while(abs(getGyroDegrees(gyro)) < 2 * deg) { setMotorSync(right, left, -100, 25); gray = (light + dark) / 2;

16 // Adding the calibration code gives…
int gray; int light; int dark; void setLightDark() {…} void calibrateSensor(){…} task main() { int pwr = 50; calibrateColorSensor(); gray = (light + dark) / 2; while(true){ if(getColorReflected(color) < gray) { setMotorSync(left, right, 25, pwr); } else { setMotorSync(left, right, -25S, pwr); }

17 Where to go from here Look into PID programming for a line follower
Look at an algorithm for using two color sensors. Try it out for yourself, then improve

18 Congratulations!!! I am assuming that you have a functioning EV3 with ROBOTC I am assuming that you were able to use the sensors to follow a line.


Download ppt "Robotics Week 6 line follower"

Similar presentations


Ads by Google