Go Back   VEX Forum > Community > General Forum

General Forum Open Discussion of the VEX Robotics System that can be answered by anyone. VEX Robotics Engineers will not answer questions posted here; see Official VEX Technical Support below.

Reply
 
Thread Tools
  #1  
Old 05-04-2012, 02:26 PM
villas_118 villas_118 is offline
Junior Member
 
Join Date: Apr 2012
Posts: 3
Autonomous line follower

Hi, I'm new. I'm in my first project and I'm having problems with the code. I'm doing the code on MPlab and the objective is to follow a black line. Some words are in Portuguese, so: SENSORDIREITA= right sensor; SENSORESQUERDA= left sensor; SENSORFRONTAL= center sensor; CORINTERMEDIARIA= avg; Thanks (:



#include "API.h"
#include "BuiltIns.h"
int SENSORDIREITA; //Sensor da direita.
int SENSORCENTRAL; //Sensor central.
int SENSORESQUERDA; //Sensor da esquerda.
int SENSORFRONTAL; //Sensor da frente.

int CORINTERMEDIARIA = 300; //Nível de luminosidade dos sensores (0=PRETO e 1000=BRANCO).
int FWD = 192; //Velocidade máxima para tras.

int STP = 127; //Velocidade nula.
int REV = 67; //Velocidade máxima para frente.
int SLOW = 157; //Velocidade lenta para tras.
int REVSLOW = 97; //Velocidade lenta para frente.
void main (void)
{
SetPWM (1,STP);
SetPWM (2,STP);
SENSORDIREITA = GetAnalogInput(1);
SENSORCENTRAL = GetAnalogInput(2);
SENSORESQUERDA = GetAnalogInput(3);
Wait (1000); //Tempo para carregar sensores.
while (1) //Loop Infinito.
{
while (SENSORCENTRAL < CORINTERMEDIARIA) //Sensor central está sobre a linha.
{
SENSORCENTRAL = GetAnalogInput(2);
SetPWM (1,REV);
SetPWM (2,FWD);
}
while (SENSORDIREITA < CORINTERMEDIARIA) //Caso o valor for menor que CORINTERMEDIARIA, então SENSORDIREITA está sobre a linha.
{
SENSORDIREITA = GetAnalogInput(1);
SetPWM (1,FWD);
SetPWM (2,FWD);
}
while (SENSORESQUERDA < CORINTERMEDIARIA) //Caso o valor for menor que CORINTERMEDIARIA, então SENSORESQUERDA está sobre a linha.
{
SENSORESQUERDA = GetAnalogInput(3);
SetPWM (1,FWD);
SetPWM (2,FWD);
}
SENSORDIREITA = GetAnalogInput(1);
SENSORCENTRAL = GetAnalogInput(2);
SENSORESQUERDA = GetAnalogInput(3);
}
}
Reply With Quote
  #2  
Old 05-04-2012, 08:37 PM
MarkO's Avatar
MarkO MarkO is offline
Senior Member
 
Join Date: Aug 2006
Location: Albany, Oregon, USA, North Western Hemisphere, Planet Tera
Posts: 2,340
Images: 10
Re: Autonomous line follower

Quote:
Originally Posted by villas_118 View Post
Hi, I'm new. I'm in my first project and I'm having problems with the code. I'm doing the code on MPlab and the objective is to follow a black line. Some words are in Portuguese, so: SENSORDIREITA= right sensor; SENSORESQUERDA= left sensor; SENSORFRONTAL= center sensor; CORINTERMEDIARIA= avg; Thanks (:

Code:
#include "API.h"
#include "BuiltIns.h"
int SENSORDIREITA; //Sensor da direita.
int SENSORCENTRAL; //Sensor central.
int SENSORESQUERDA; //Sensor da esquerda.
int SENSORFRONTAL; //Sensor da frente.

int CORINTERMEDIARIA = 300; //Nível de luminosidade dos sensores (0=PRETO e 1000=BRANCO).
int FWD = 192; //Velocidade máxima para tras.

int STP = 127; //Velocidade nula.
int REV = 67; //Velocidade máxima para frente.
int SLOW = 157; //Velocidade lenta para tras.
int REVSLOW = 97; //Velocidade lenta para frente.

void main (void)
{
	SetPWM (1,STP);
	SetPWM (2,STP);
	SENSORDIREITA = GetAnalogInput(1);
	SENSORCENTRAL = GetAnalogInput(2);
	SENSORESQUERDA = GetAnalogInput(3);
	Wait (1000); //Tempo para carregar sensores.
	while (1) //Loop Infinito.
	{
		while (SENSORCENTRAL < CORINTERMEDIARIA) //Sensor central está sobre a linha.
		{
		SENSORCENTRAL = GetAnalogInput(2);
		SetPWM (1,REV);
		SetPWM (2,FWD);
		}
		while (SENSORDIREITA < CORINTERMEDIARIA) //Caso o valor for menor que CORINTERMEDIARIA, então SENSORDIREITA está sobre a linha.
		{
		SENSORDIREITA = GetAnalogInput(1);
		SetPWM (1,FWD);
		SetPWM (2,FWD);
		}
		while (SENSORESQUERDA < CORINTERMEDIARIA) //Caso o valor for menor que CORINTERMEDIARIA, então SENSORESQUERDA está sobre a linha.
		{
		SENSORESQUERDA = GetAnalogInput(3);
		SetPWM (1,FWD);
		SetPWM (2,FWD);
		}
		SENSORDIREITA = GetAnalogInput(1);
		SENSORCENTRAL = GetAnalogInput(2);
		SENSORESQUERDA = GetAnalogInput(3);
	}
}

First Off, this is EasyC Code, Version 1.x, which could be compiled in MPLAB if the the WPILIB is Included.

Over All, the Code has basically the Right Structure.. But you need to expand the Logic...

Download this EasyC 2.x Printed Code and look at the Track_Line function.

Found on Page: Line Tracker Code - Printed
Reply With Quote
  #3  
Old 05-04-2012, 08:56 PM
dontworryaboutit's Avatar
dontworryaboutit dontworryaboutit is offline
Senior Member
VEX # 254D
 
Join Date: Mar 2010
Posts: 621
Images: 1
Re: Autonomous line follower

Code:
while(e^(i*Pi) == -1) {
if(!OnLine)
  lineFollow();
else if(OnLine)
  cookie += 1;
}

Your structure looks good but I think the logic needs some work (as MarkO said).

What goes wrong when you run it?
__________________
254D = 254Doesn't work at competition
254D = redefining "18 inches" since 2009
First make it work, then make it work well.
Reply With Quote
  #4  
Old 05-07-2012, 08:12 AM
villas_118 villas_118 is offline
Junior Member
 
Join Date: Apr 2012
Posts: 3
Re: Autonomous line follower

Thanks MarkO, I did the download and I'll analyse what I can change in my code.
Reply With Quote
  #5  
Old 05-07-2012, 08:22 AM
villas_118 villas_118 is offline
Junior Member
 
Join Date: Apr 2012
Posts: 3
Re: Autonomous line follower

dontworryaboutit, nothing happens when I run the code. I tested some basic codes to check if the robot had any problem, but the robot was ok.
Reply With Quote
  #6  
Old 05-07-2012, 02:38 PM
jgraber jgraber is offline
Senior Member
 
Join Date: Nov 2006
Location: Dallas Texas metroplex
Posts: 1,109
Images: 13
Re: Autonomous line follower

Quote:
Originally Posted by villas_118 View Post
dontworryaboutit, nothing happens when I run the code. I tested some basic codes to check if the robot had any problem, but the robot was ok.
If all the analog values returned are greater than 300, your code will not do any SetPWM.

When saying "code doesn't work", it helps to list:
- What you did: "put robot on line, and turn it on"
- What you expected the robot to do: "follow the line forward"
- What the robot did : "robot did not move"

Even just writing such a post will help you debug it yourself.
For example, If your robot never moves, then the motors were never turned on.
What conditions turn on the motors? GetAnalog < 300.
Maybe GetAnalog is always returning a value >= 300?
Reply With Quote
  #7  
Old 05-07-2012, 03:46 PM
MarkO's Avatar
MarkO MarkO is offline
Senior Member
 
Join Date: Aug 2006
Location: Albany, Oregon, USA, North Western Hemisphere, Planet Tera
Posts: 2,340
Images: 10
Re: Autonomous line follower

You need to determine what the Threshold Value is..


Add the following Lines of code to your Project:

Code:
    PrintToScreen ( "===================================\n" ) ;
    PrintToScreen ( "SENSORDIREITA --> %d\n" , (int)SENSORDIREITA ) ;
    PrintToScreen ( "SENSORCENTRAL --> %d\n" , (int)SENSORCENTRAL ) ;
    PrintToScreen ( "SENSORESQUERDA --> %d\n" , (int)SENSORESQUERDA ) ;
    PrintToScreen ( "\n" ) ;
Place them like this:
Code:
    SENSORDIREITA = GetAnalogInput(1);
    SENSORCENTRAL = GetAnalogInput(2);
    SENSORESQUERDA = GetAnalogInput(3);

    PrintToScreen ( "===================================\n" ) ;
    PrintToScreen ( "SENSORDIREITA --> %d\n" , (int)SENSORDIREITA ) ;
    PrintToScreen ( "SENSORCENTRAL --> %d\n" , (int)SENSORCENTRAL ) ;
    PrintToScreen ( "SENSORESQUERDA --> %d\n" , (int)SENSORESQUERDA ) ;
    PrintToScreen ( "\n" ) ;

    Wait (1000); //Tempo para carregar sensores.
Now after you download your program, the Downloader Program should change to the Terminal.. You will see the values of each of the Line Sensors.

Disconnect the Motors from the Vex Controller, ( so it won't drive away ) and place the Robot over the Line.

Position it so the Center is over the line and view the Terminal, the Left and Right values should be about the same, the Center should be different.

Place the Left Sensor over the Line and view the Terminal, the Center and Right values should be about the same, the Left should be different.

Place the Right Sensor over the Line and view the Terminal, the Left and Center values should be about the same, the Right should be different.

Your Threshold Value, CORINTERMEDIARIA should be between the Line Value and the Non Line Value.

Last edited by MarkO; 05-08-2012 at 08:16 AM. Reason: Singular "Line" to Plural "Lines"
Reply With Quote
  #8  
Old 05-08-2012, 07:51 AM
fretless_kb fretless_kb is offline
Senior Member
VEX # 1166
 
Join Date: Jan 2010
Location: Parker CO
Posts: 282
Images: 6
Re: Autonomous line follower

Mark-O is spot on, you may also find as I did that each sensor has a different threshold, not a problem if they are close but can drive you to use sensor unique thresholds if they vary by a great deal (as mine do).

Cheers Kb
__________________
Kevin Barrett - Mentor - Team 1166
Reply With Quote
  #9  
Old 05-08-2012, 08:15 AM
MarkO's Avatar
MarkO MarkO is offline
Senior Member
 
Join Date: Aug 2006
Location: Albany, Oregon, USA, North Western Hemisphere, Planet Tera
Posts: 2,340
Images: 10
Re: Autonomous line follower

Quote:
Originally Posted by fretless_kb View Post
Mark-O is spot on, you may also find as I did that each sensor has a different threshold, not a problem if they are close but can drive you to use sensor unique thresholds if they vary by a great deal (as mine do).

Cheers Kb
Spot On to you too...


Or.... If your Sensors have a widely Varying Threshold, use an Upper Limit variably and a Lower Limit variable... Basically a Dead Band, where the Sensors are considered neither The Line or Not The Line.

Then, when you test for Below The Threshold, you use the Lower Value, and when you test for Above The Threshold you use the Higher Value.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 06:44 AM.


VEX and VEX Robotics are trademarks or service marks of Innovation First International, Inc.
Copyright © 2002-2013. All Rights Reserved. VEX Robotics, Inc. is a subsidiary of Innovation First International, Inc.
All other product names/marks of others are the property of their respective owners.