View unanswered posts | View active topics It is currently Fri Aug 01, 2014 7:31 pm






Reply to topic  [ 8 posts ] 
line following and turning on a color 
Author Message
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 184
Post line following and turning on a color
I would like to create a program that does this:http://nxt-wiki.rjmcnamara.com/index.php?title=NXTLineLeader

I completed my first PID program this weekend with for smooth line following. As shown in the video, I'd also like to have the robot turn on a color that is different from the white and black colors used for the line following. I am just beginning to learn about arrays, so I am not sure if I can create my own array to accomplish what I described, or if I need the sensor used in the video. If I don't need the sensor, and I can just create a program, please let me know how I can get started. That can at least get me started and I can then ask more questions (if needed) as I continue. Below is the code I used for the PID line following. Thanks for your help,

Code:
#pragma config(Sensor, S1,     light1,         sensorLightActive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

float Turn=0;
float threshold=53;
float Kp=1.8;
float Tp=30.5;
float error=0;
float powerB=0;
float powerC=0;
float lasterror=0;
float integral=0;
float derivative=0;
float Ki=.000873;
float Kd=927.835052;
int countvalue=0;


task main()
{


   while(true)
   {

      error=SensorValue(light1)-threshold;
      integral=integral+error;
      derivative=error-lasterror;
      Turn=Kp*error + Ki*integral + Kd*derivative;
      powerB=Tp-Turn;
      powerC=Tp+Turn;

      motor[motorB]=powerB;
      motor[motorC]=powerC;
      lasterror=error;

   }

}


Mon Feb 18, 2013 7:52 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3170
Location: Rotterdam, The Netherlands
Post Re: line following and turning on a color
I am not sure the LEGO Colour Sensor can be used in both light sensor mode and colour sensing mode. It takes a bit of time to switch between the two modes and that would mess up the smooth line following. The program would need a few modifications to make it work with the LEGO Colour Sensor as well.
It may be possible to achieve it using some low-level techniques, that may be a bit beyond the scope of teaching simple robotics to children (I am assuming you are keen to use this for a class, since you're a teacher). If you /are/ interested in doing this, let me know and I'll check if the functionality required to achieve this has been fixed in the latest beta that I have.

Alternatively, you could use both a LEGO Light Sensor and Colour Sensor and simply use the Light Sensor for following and the Colour Sensor for detecting colour.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Feb 19, 2013 12:41 am
Profile WWW
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 184
Post Re: line following and turning on a color
Thanks for the reply.

Honestly, I don't even have a color sensor. All the kits I have are the educational kits, so I've never used a color sensor.

You are correct, I am using it for class. I am interested in doing this, if for no other reason then to expand my own knowledge base. But, as I mentioned, I just have the basic sensors that come with the educational kit.

I was thinking of using multiple light sensors?


Tue Feb 19, 2013 7:24 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3170
Location: Rotterdam, The Netherlands
Post Re: line following and turning on a color
parkway wrote:
Honestly, I don't even have a color sensor. All the kits I have are the educational kits, so I've never used a color sensor.

That will make detecting colours a little tricky. You'll have to judge by a grey value equivalent.
Quote:
I was thinking of using multiple light sensors?

That would work. Just change the code to continuously check the value of the second light sensor and do something when the desired value is detected. You could call a function or something to that effect. Have a go at modifying the above code and let me know if you get stuck. I won't do your homework for you :P I am sure as a teacher you would expect me to say the same to your students if they were to come to these forums :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Feb 19, 2013 1:22 pm
Profile WWW
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 184
Post Re: line following and turning on a color
No, as their teacher I tell them that since I am an adult, I have all kinds of privileges that they do not. Such as asking questions on a forum and getting them answered. :-) Just kidding.

Seriously, that was the mistake I made when I first started. The curriculum is so well done, I just followed it. I never really got involved with writing the code myself. Moreover, coming from a humanities background, I was very nervous about not knowing the answer. The idea of engaging the students and myself with a project and learning with them was foreign.

Anyways, the issue I'm having is the fact that the green threshold is in between the light and the black thresholds. Therefore, I'm having a hard time writing the condition in my while loop. I'll try some more (my homework :mrgreen: ) and let you know how it goes.


Tue Feb 19, 2013 5:44 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3170
Location: Rotterdam, The Netherlands
Post Re: line following and turning on a color
Have you considered calibrating your sensor before each run? Calibration is where you try to find the minimum and the maximum values you get when you're on the line (which is black) and when you're off the line (when it's white). Then you calculate a threshold, based on the value that sits between these two values.
How would you do that? Well, you start off with the robot's light sensor pointed either on the track or next to the track, but not half way. Then you make the robot slowly turn from side to side and you adjust the minimum and maximum values according to what you're seeing. So if a detected value is LESS than what you're current minimum is, you change that value. The same, of course, applies to the maximum value. Then when you're satisfied you've covered both ends of the spectrum, you calculate your threshold by subtracting the minimum from the maximum and dividing that value by two. Then you slowly move your robot *back* towards the line until your light sensor sees the threshold value. Congratulations, you now have a calibrated line following robot!
This doesn't solve your green vs black issue, but it's something to keep in mind. You could also calibrate your other sensor for the value of green, red or whatever, before a run.

It is nice to see that you're trying to get more involved and understand the materials behind the curriculum. When I was a kid, I was always fascinated by chemistry and biology and always admired my teachers when they could not answer my question straight away but would say "You know what, I have no idea, but I will try to find out for you." This was before the Internet, mind you. I would ask about stuff I read in the American Scientist and New Scientist magazines. Don't be afraid to not know, just tell them you'll find out and give them the answer next time.

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Wed Feb 20, 2013 12:49 am
Profile WWW
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 184
Post Re: line following and turning on a color
Well, I sort of finished my homework. I got the robot to solve the maze. I just had the robot follow the line on the same side as the green squares. About halfway through the maze, the green squares were on the other side, so I just had the robot switch sides and line follow the other side. I did make one change to the PID algorithm that I wanted to show you. I was having a hard time finding a way to explain to my students how fine-tuning the K values would optimize their line following. I didn't just want to say that increasing the Kd value would help with overshoot, I was/am hoping that they would understand why. So, I simplified the ID parts of the PID algorithm.

Code:
#pragma config(Sensor, S1,     lightSensor,    sensorLightActive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()

//light value is 70.  Dark value is 40.  Threshold is 55
{
   float Kp=1.5;//difference in light value readings/difference in motot speed
   int offset = 55;
   int Tp=25;//max speed
   int error;
   int turn;
   int powerB;
   int powerC;
   while(true)
   {
      error = offset - SensorValue(lightSensor);
      turn = Kp * error;
      powerC = Tp - turn;//following the left side of the line
      powerB = Tp + turn;
      if(powerC < 10)//when the robot sees dark
      {
         powerC = -15;
      }
      if(powerC > 70)
      {
         powerC = 50;//too high = overshoot
      }
      if(powerB < 10)
      {
         powerB = -10;
      }
      if(powerB > 80)
      {
         powerB = 75;//too high = overshoot
      }
      motor[motorB]=powerB;
      motor[motorC]=powerC;
   }

}


Hopefully, what this will do is allow the students to see a little clearer what is happening. On the sharp turns, if the robot is overshooting the black line, we need to decrease the power given to motor C and increase the power given to motorB (when the light sensor sees dark). Or, put the robot a certain point on the maze where it is losing the line. Have the students figure out what is the actual power levels for motorB and motorC at this point. What do we have to do to manipulate those power levels so the robot does not lose the line or overshoot the line?

That's the plan anyways. We start tomorrow. Let me know what you think. Thanks,


Mon Feb 25, 2013 10:02 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3170
Location: Rotterdam, The Netherlands
Post Re: line following and turning on a color
To be honest, I have never really had to use the ID part in a PID line follower. The P is usually good enough, unless you start making super fast ones. The simplification is good, it can still be used to show the influence of a regulator on a robot's response to change.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Feb 26, 2013 2:49 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.