I want to program robot to line track a dark line on a light surface. I have 2 motors 1 for 2 left wheels and one for right 2 wheels. 1 line Tracker. The line I want to track goes straight then makes 90 degree turns. What code Do I use???
Which programming language are you using? RobotC has some sample programs in their samples folder that might help you get started. I don’t know about EasyC or other programming languages.
I’m using robotc I can’t find a good example program can you give me code?
In your RobotC, go to File, then you will see a file called** Sample Files** or Sample Programs. If you click on that, you should see some folders, one of which should contain a variety of codes for Line Following. I’m away from my RobotC computer, so I can’t just cut and paste it.
You can have a look at some simple code on the Wiki here:
http://www.vexrobotics.com/wiki/Line_Follower#ROBOTC_Sample_Code
You might also go here and click on Sensing near the top of the page, and then click on Line Tracking in the box to the left, then click on the various examples shown:
http://www.education.rec.ri.cmu.edu/products/teaching_robotc_cortex/index.html
Ya I tried all of those nothing has worked but thanks.
This sounds more like a physical issue at this point, would you mind posting a picture of your wiring and the position of the sensors?
Also make sure that the motor and sensor setup is correct and you are referencing the right ports.
The second link that FullMetalMentor shared is really good, try watching the videos in there again, and also look into watching more of the videos from that website, they help a lot.
One of the troubles with using a line sensor is you tend to turn to find the line again. You tend to do this over and over again as you follow the line wiggling back and forth.
Positioning the line sensors further in front or behind the turning wheels gives you a smaller range of where the line is under the robot. If the line followers are right under the center of the turn, then you turn a massive amount to find the line again.
At the end of your following part, you could be 20 degrees off from straight. :eek:
Using a gyro plus a line follower helps in many ways. At the end of your line travel distance, you can use the current angle to see if you have to go more or less than 90 degrees to your next way point.
Yes I did have my line tracker in front of my wheels ill move it back to the middle. I did check the ports they are all right. I will try the videos again. If anyone can give me code I would love that thanks.
What is a Gyro? I have a optical shaft encoder?
Front is better in theory but you tend to overreact and lose the line more the further out front you are.
Assuming you reset encoders to 0 at the start of the line following. Otherwise it is a starting offset versus 0. (Just set them to 0 for now)
If you are turned, the left and right encoder should not be the same and they should be really close to being the same when you are straight.
You can see what the delta of the two encoders is when you are done your turn and adjust how much you need to turn based upon how different left/right encoders are at the end of the line following.
Or you could turn yourself until encoders read the same again which would be straight position (or the original delta). Then perform your turn of +turn_distance for the one encoder and -turn_distance for the other.
Getting them to be exactly the same can be a trick though and that is where an acceptable range comes into play. while (abs(left_right_error)> allowable_slop))
From our Vexmen class last spring…
I am supposed to be using Encoders??? The videos links you posted are exactly want I want my robot to do. Just Dark on light surface.
In order to control the turn to be where you want to point, you have to use some sensor - a gyro and/or encoders.
The line follower tells you you are over the line currently. Due to left right movement varying, timing can lead to different lengths down the line and different ending direction for your robot pointing 10-20 degrees off the line direction.
Getting to a specific direction at the end of the line segment to do a turn is where you need some feedback to say what angle am I pointing? Gyros tell the angle directly, encoders try and back into it.
How many line follower sensors are you using?
I am using 1 but I have a total of 3. I can put 2 more on if needed.
The three line trackers let you know you’re close and which side was seen last. Having 1 you only know you’re on the line or not.
So keeping track of which side you saw last lets you know which way to turn to find the line again. Seeing the line under the outer line follower can also be a slight turn case versus no line which is a harder turn to go find the line again. Seeing under both the middle and an outer one can mean an even gentler turn. Seeing under all three means you are either perpendicular to the line or you hit a cross of two lines.
Supersonic sparks (Legomindsotrmmaniac) had some code on the forums I think from a few years ago but with the wiping out of some categories all those code examples are now lost. Maybe he could re-post.
Ok I found a good sample program for triple line trackers ill try that. Should I put line tracker in front middle or back???
You usually want to have the line trackers near the front. That way, with the three-sensor rig, you just have to program the bot to turn towards whichever sensors are seeing a line. Basically:
if( left sensor >= line brightness constant ) //Left sensor sees a line
{
[slow down left drive motors] //Initiates left swing turn
}
Repeat for the right sensor and wheels.
The middle sensor is there to confirm that the robot is actually on the line (making three sensors much better than two). If all three sensors are not seeing a line, then you can have the robot go into a search mode where it tries to reacquire the line it lost. I would suggest telling it to do a point turn in the same direction it turned last.
You can get the robot to follow lines very smoothly once you get the correction turn speed (when the line is on the left or right sensors) to be at the right pace. Too fast and your bot will jerk about and shimmy along the line as it over-corrects, and too low of a turn speed will cause you to lose the line frequently and either trigger the search mode or simply get lost.
You may also want to take a look through the ‘Sensing → Line Tracking’ section of our VEX Cortex Video Trainer using ROBOTC; this will walk through the process of line tracking, how it operates (you are actually tracking the edge of the line in most cases; it’s the transition from a light surface to a dark surface that you want the robot to react to), and how to program a line tracking sensor in ROBOTC.
Once you have a firm understanding of the principles of line tracking and how to use the code in ROBOTC, then I would suggest taking a look at the sample programs again to make sure they make sense. Once they do, you can either make your own line tracking algorithm to fit your robot (best solution) or modify existing code to fit your robot.
VEX Cortex Video Trainer using ROBOTC: http://www.education.rec.ri.cmu.edu/products/teaching_robotc_cortex/index.html
You can also review the other videos in the curriculum trainer in order (starting with the ‘Fundamentals’ section) as a good refresher of the different sensors and how they are programmed in ROBOTC.
I used this code and the robot keeps going in circles. It was a sample program.
Code–
#pragma config(Sensor, in1, lineFollowerLEFT, sensorLineFollower)
#pragma config(Sensor, in2, lineFollowerCENTER, sensorLineFollower)
#pragma config(Sensor, in3, lineFollowerRIGHT, sensorLineFollower)
#pragma config(Motor, port3, rightmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, leftmotor, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//
/----------------------------------------------------------------------------------------------------
|* - Triple Sensor Line Tracking - |
| ROBOTC on VEX 2.0 CORTEX |
| |
| This program uses 3 VEX Line Follower Sensors to track a black line on a light(er) surface. |
| There is a two second pause at the beginning of the program. |
| |
| ROBOT CONFIGURATION |
| NOTES: |
| 1) Reversing ‘rightMotor’ (port 2) in the “Motors and Sensors Setup” is needed with the |
| “Squarebot” mode, but may not be needed for all robot configurations. |
| 2) Lighting conditions, line darkness, and surface lightness change from place to place, |
| so the value of ‘threshold’ may need to be changed to better suit your environment. |
| |
| MOTORS & SENSORS: |
| * [Name] [Type] [Description] |
| Motor - Port 2 rightMotor VEX 3-wire module Right side motor |
| Motor - Port 3 leftMotor VEX 3-wire module Left side motor |
| Analog - Port 1 lineFollowerRIGHT VEX Light Sensor Front-right, facing down |
| Analog - Port 2 lineFollowerCENTER VEX Light Sensor Front-center, facing down |
| Analog - Port 3 lineFollowerLEFT VEX Light Sensor Front-left, facing down |
*-----------------------------------------------------------------------------------------------4246-/
//+++++++++++++++++++++++++++++++++++++++++++++| MAIN |+++++++++++++++++++++++++++++++++++++++++++++++
task main()
{
wait1Msec(2000); // The program waits for 2000 milliseconds before continuing.
int threshold = 505; /* found by taking a reading on both DARK and LIGHT /
/ surfaces, adding them together, then dividing by 2. */
while(true)
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
displayLCDCenteredString(0, “LEFT CNTR RGHT”); // Display |
displayLCDPos(1,0); // Sensor |
displayNextLCDNumber(SensorValue(lineFollowerLEFT)); // Readings |
displayLCDPos(1,6); // to LCD. |
displayNextLCDNumber(SensorValue(lineFollowerCENTER)); // |
displayLCDPos(1,12); // L C R |
displayNextLCDNumber(SensorValue(lineFollowerRIGHT)); // x x x |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
// RIGHT sensor sees dark:
if(SensorValue(lineFollowerRIGHT) > threshold)
{
// counter-steer right:
motor[leftMotor] = 63;
motor[rightMotor] = 0;
}
// CENTER sensor sees dark:
if(SensorValue(lineFollowerCENTER) > threshold)
{
// go straight
motor[leftMotor] = 63;
motor[rightMotor] = 63;
}
// LEFT sensor sees dark:
if(SensorValue(lineFollowerLEFT) > threshold)
{
// counter-steer left:
motor[leftMotor] = 0;
motor[rightMotor] = 63;
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
Did you make the light and dark measurements to set the threshold to your particular field?
int threshold = 505; /* found by taking a reading on both DARK and LIGHT */
/* surfaces, adding them together, then dividing by 2. */
The sample code says this is for a dark line on a white surface. Is that what you have? Have you confirmed that the sensors can actually see your dark line?
What sort of readings is your LCD giving you?
Are your motors configured so that the motions caused by the program are actually the proper response to the sensor readings? In other words, if the motors are wired up the wrong way or the robot moves in the wrong direction, then the program might never respond to the sensor inputs the way you hope they do.
I don’t have a LCD I have a cortex an no it is not on a white surface. It is on a lighter blue and black electrical tape on it. I want to track the Black electrical tape. I think the motors are wired right.