Welcome, Guest. Please login or register.
May 25, 2013, 02:06:04 PM
<< Back to The NXT Step Home Page    Home Help Search Login Register

+  The NXT STEP Forum
|-+  Programming Software
| |-+  NXT-G
| | |-+  Programming Light Sensor
« previous next »
Pages: [1] 2 Print
Author Topic: Programming Light Sensor  (Read 8273 times)
Husky
Jr. Member
**
Posts: 21


View Profile
« on: September 10, 2009, 08:39:56 AM »

Thank you all for the earlier advice on light sensor activities.  The kids are really enjoying the light sensor and are committed to using it in this years FLL challenge.....gulp!  I am a bit nervous and trying to teach them about this sensor.  They want to use 2 light sensors, one on each side to see if the robot can stay on track follwoing the curving black line.  Will this be faster than follow the black line? So here are the questions:

1. How do you get both sensors to work at the same time detecting something that may or maynot appear?  Do you use a crowbar to get both light sensors to work at the same time, so it will detect something on the right or left side?  (They saw the Legoguards robot and have been Very inspired by this)

2.  We have only programmed  it using a move block - unlimited followed by a wait light block with greater than or less than value keyed in per our measurment on the table, white vs dark and then whatever the next action is, say a move block. We have not tried the switch yet any suggestions for using a switch.

I have ordered the NXT-G programming guide and I am praying there will be alot of "HOW to program light sensors, beyond the NXT tutorial."  The kids are meeting today and tomorrow to experiment more and I do not think the book will arrive by then so ANY advice would be wonderful.  I wish I had had this tool when I was in school!  It has even captivated us  adults,  who are left feeling rather perplexed at times like these and we are learning right along with the kids!
Thanks so much.
Tracy
Report to moderator   Logged
jimmy101
Full Member
***
Posts: 71


View Profile
« Reply #1 on: September 11, 2009, 11:48:17 AM »

Here's a few things to get your started. There are a number of ways to do what you want. There are a lot of buzzes and whistles that can be added ...

LOOP forever (later you'll need to figure out how to stop the loop when you reached your destination)

   Inside the LOOP put a SWITCH block set to look at your right hand light   
   sensor. If the sensor is "seeing black" is
   TRUE
      turn right
     (I prefer to do the turns as two sequential MOTOR blocks using
     different power levels and set to run forever)
   FALSE
      Another switch block that looks at the left hand sensor. If the sensor
      is "seeing black" is
         TRUE
            turn left
         FALSE
            go straight
      end inner switch
   end outer switch
end loop forever

So you have a SWITCH inside a LOOP and inside the FALSE branch of the SWITCH is another SWITCH. The outer SWITCH handles one light sensor and the inner SWITCH handles the other.

This approach doesn't look at both sensors at the same time. They are looked at sequentially. Usually that won't matter. But if both sensors ever "see black" the first SWITCH defines the robot's behavior (it'll turn to the right).

"See black" means the sensor reading is below whatever you are using as the light level for black. I assume you know how to get a value for "sees black" from your sensor.

If "sees black" is say 40, and goes up the more white it sees, you might want to add a couple to your "black level". It'll trigger a bit early but it keeps the robot from never "seeing black" if the light conditions (or mat) changes a bit. Often for this type of black / white triggering people will just use the average of the black and white light values. That'll give a fair amount of tolerance to changes in the room lighting conditions.

Buzzes and whistles and details ...
* How sharp should the robot turn?
* Maybe slow down when turning and speed up when going straight?
* Have the program measure the black and white light values from the line before trying to follow the line (great place to use variables and the SUITCASE). And calculate a "grey" level.
* How do you stop the outer loop? What conditions should stop the outer loop?  How do you do the condition in the code?
Report to moderator   Logged
drusch100
Full Member
***
Posts: 50


Bwa Ha Ha!


View Profile
« Reply #2 on: September 11, 2009, 01:11:24 PM »

This is how the robot should work (see picture).

If the left sensor sees black, it should turn left (drive right wheel forward).  If the right sensor sees black, turn right (drive left wheel forward).  If both sensors see black, turn a direction of your choice (you can detemine which way to turn by turning the opposite direction that you turned last). If neither sensor sees black, drive forward (both wheels going forward).

I hope this helps!


drusch100


* linefollowing.jpg (25.18 KB, 975x684 - viewed 237 times.)
Report to moderator   Logged
Dino_Martino
Hero Member
*****
Posts: 411


AC-130: my other passion, the sky!


View Profile WWW
« Reply #3 on: September 12, 2009, 12:30:02 AM »

you could also use a single light sensor,
with a black line and a grey line next to eachother.
if the sensor sees grey, it is driving perfect (because grey should always be the centre) if it sees black, it should turn left (if the black line is on the right) and if it sees white (at the left)
it should turn right. I once did this succesfully.
the great advantage vs a single black line is that this version immediatly knows wich way to turn.
I made an entire circuit, with turns in both dirrections but unfortunatly, I deleted it...
sorry!
but ms paint will do just fine.
I added a small exemple.


* linefollower.bmp (115.25 KB, 154x383 - viewed 213 times.)
Report to moderator   Logged

jimmy101
Full Member
***
Posts: 71


View Profile
« Reply #4 on: September 12, 2009, 09:05:01 AM »

you could also use a single light sensor,
with a black line and a grey line next to each other.
Or, omit the gray and just use the black line on the white background (or vice versa).

Try to keep the light sensor on the edge of the line. When the sensor is on the edge it "sees" half white and half black which is the same as gray (as far as the sensor is concerened).

You have to pick which side of the line your are trying to follow, left or right.
To follow the left side:
If sensor sees (less than gray) turn right
If sensor sees (more than gray) turn left
If sensor sees (gray) go straight

Just nest a SWITCH in a SWITCH in a LOOP FOREVER.

You'll need to know what values the sensor returns for black and white. Or, run the light sensor calibration and gray will generally be very close 50, black~0 and white~100.
Report to moderator   Logged
Dino_Martino
Hero Member
*****
Posts: 411


AC-130: my other passion, the sky!


View Profile WWW
« Reply #5 on: September 13, 2009, 12:02:27 AM »

you could also use a single light sensor,
with a black line and a grey line next to each other.
Or, omit the gray and just use the black line on the white background (or vice versa).

Try to keep the light sensor on the edge of the line. When the sensor is on the edge it "sees" half white and half black which is the same as gray (as far as the sensor is concerened).

You have to pick which side of the line your are trying to follow, left or right.
To follow the left side:
If sensor sees (less than gray) turn right
If sensor sees (more than gray) turn left
If sensor sees (gray) go straight

Just nest a SWITCH in a SWITCH in a LOOP FOREVER.

You'll need to know what values the sensor returns for black and white. Or, run the light sensor calibration and gray will generally be very close 50, black~0 and white~100.


that is endeed interesting... nice idea! but this system can (not shure) have some speed limitations. if yo make the black&grey strokes wide enough, you can travel at a descent speed.
Report to moderator   Logged

brdavis
Nxt Step Authors
Hero Member
*****
Posts: 547


View Profile
« Reply #6 on: September 13, 2009, 06:33:44 AM »

There are always speed issues... and sharpness of turn issues. The idea (edge following, not pure "line" following), is very common and useful. It will fail on very narrow lines... but so will lots of other techniques. Try it.

--
Brian Davis
Report to moderator   Logged
alan4cast
Jr. Member
**
Posts: 24


View Profile
« Reply #7 on: September 15, 2009, 06:17:03 PM »

They want to use 2 light sensors, one on each side to see if the robot can stay on track following the curving black line.  Will this be faster than follow the black line?

If you mean a two light follow (the sensors close together) then it is more accurate, but not necessarily faster.

If you mean following the outside edges of the obvious path on the FLL mat this year, you might have difficulty because the lines are so thin.


1. How do you get both sensors to work at the same time detecting something that may or maynot appear?  Do you use a crowbar to get both light sensors to work at the same time, so it will detect something on the right or left side?  (They saw the Legoguards robot and have been Very inspired by this)

Thanks for the compliment!

In general, to answer your question for what my team (the Lego Guards that you mentioned) did, is have a loop that sets the line follow motor using one sensor (actually, as Brian said it was an edge follow) - checks the other to see if the loop should end.

The Lego Guards line (edge) follow used one motor (the one on the side furthest from the line) that ran at a specific speed. The other side was controlled by a loop that read the light value and sent that value to the motor. Actually, in order to make the edge follow work consistently their algorithm had a tuning parameter that was used to determine the amount of correction on the trailing motor. At low speeds with the correction turned up, they could turn very tight corners. At higher speeds, or with the correction turned down, they would just blow past intersections that occurred under the controlling sensor.  If you watch their video of the Power Puzzle runs on youtube, and watch the robot going to the house, you'll see it drive right through intersections of black lines, and you'll also see it make a quick sharp turn to line up with the house. That sharp turn was the line-follow actually trying to follow the curve of the road.

Hope that helps!

Alan LeVezu
Coach - Lego Guards - Power Puzzle, NanoQuest
Asst Coach - Lego Guards - Climate Connections , Smart Moves
Coach - Techno Guards FTC 2848 - Face Off, Hot Shot
Report to moderator   Logged
Jimbo
Newbie
*
Posts: 7


View Profile
« Reply #8 on: October 07, 2009, 10:31:07 AM »

This is some great information! However can someone include a sample program using the NXT blocks. I'm very new in this and not sure how to turn your great suggestions into a functioning program. Can anyone recomend a book?
Report to moderator   Logged
jimmy101
Full Member
***
Posts: 71


View Profile
« Reply #9 on: October 07, 2009, 06:19:48 PM »

Jimbo

Below is a screen shot of a basic line (edge) follower that uses a single light sensor.

To get it to work on your robot you need to know what the light sensor reads for "half-black-half-white". Take a reading of white and one of black. Average those two values and that is the "half-black-half-white" value. Put that value in the SWITCH statement as the test. (In the screen shot the test is set at 50.) If you have calibrated your light sensor for your line and mat then the value should be pretty close to 50.

The SWITCH selects one of the two sets of MOTOR blocks. The upper pair of blocks turns on the A motor and coasts the C motor. If A is the left and C the right motor, then this will make the robot turn right. The lower pair of blocks is the same except which motor is on and which is off is reversed.

This is a "left hand" follower since it is set to follow the left side of a black line. When the light sensor "sees" white, that is it reads greater than 50, then the robot turns right. If the sensor "sees" black, sensor reads less than 50, then the robot turns left.

You will probably have to fiddle with the power levels. You might try 50% power for one motor and coast the other motor. (The same levels are used for both turns, just which motor is which is switched.) If it can't follow your line then you probably should lower the power level. If it follows your line correctly then try to boost the power a bit to get it running faster.

This edge follower is sometimes called a "bang-bang" follower since it just bangs back and forth between turning left and right. It isn't very elegant and there is a lot that can be done to improve it.

The outer loops is set to cycle forever so this line follower will go "forever". To get it to stop when you want you'll need to change the outer loop.


* Image002.jpg (44.78 KB, 658x527 - viewed 565 times.)
Report to moderator   Logged
Jimbo
Newbie
*
Posts: 7


View Profile
« Reply #10 on: October 13, 2009, 11:35:22 AM »

Jimmy101,
Thanks for the sample. I used it and it works great. Now I have another question. I tried to set the number of times it "bangs" back and forth and to stop when it has cycled through 25 times. So I changed the sensor from "forever" to "count" and put in 25, however now it doesn't go back and forth. It just turns right for about 5 seconds and tunrs off. What am I doing wrong? Thanks for any help you could give.
Jimbo
 
Report to moderator   Logged
jimmy101
Full Member
***
Posts: 71


View Profile
« Reply #11 on: October 13, 2009, 01:18:11 PM »

Jimmy101,
Thanks for the sample. I used it and it works great. Now I have another question. I tried to set the number of times it "bangs" back and forth and to stop when it has cycled through 25 times. So I changed the sensor from "forever" to "count" and put in 25, however now it doesn't go back and forth. It just turns right for about 5 seconds and tunrs off. What am I doing wrong? Thanks for any help you could give.
Jimbo
 
The loop probably takes something like 0.01 seconds for a complete cycle. 25 times through the loop would be about one quarter of one second. So you followed the line for about 0.25 seconds then the loop ended. Once the loop ended the program ended and the motors continued to do what they were last told to do. It takes NXT-G a while (couple seconds) to shut things down once a program ends. So the motor(s) kept running even though the program was done.

To get things to end as you expect, always put a MOVE block set to stop/brake all motors as the last block in a program. If you do that with your current 25 loop limit the robot will hardly move at all.

1000 loops would be something like 10~15 seconds.
Report to moderator   Logged
Husky
Jr. Member
**
Posts: 21


View Profile
« Reply #12 on: October 22, 2009, 02:47:34 PM »

We have a new light sensor problem, after fixing the firmware problem!  The kids have a light sensor on the back of the robot as well as on the front.  For one task they spin the robot around in base and want the robot to drive out of base backwards unlimited, then they have a loop block with a swithc in it to follow the line. They do this same thing going forwards and it works great, it does nothing but sit in base looking for a line barely moving.  It seems like it is not picking up the 1st drive out of base unlimited block.  Any suggestions? 
Thanks,
Tracy
Report to moderator   Logged
thrashercharged
Newbie
*
Posts: 3


View Profile
« Reply #13 on: October 22, 2009, 04:40:15 PM »

Quote
We have a new light sensor problem, after fixing the firmware problem!

When you say fixed the firmware problem what do you mean?  Which version of firmware are you using?  Which NXT-G version?

Report to moderator   Logged
Husky
Jr. Member
**
Posts: 21


View Profile
« Reply #14 on: October 23, 2009, 05:26:52 AM »

We received the new edu kit 2.0 for this season.  When we loaded the software it instructed us to udate firmware on the 2 bricks, one new with kit and one we used last year with 1.1 software.  The brick kept locking up and would not run.  So we called tech support and they told us how to reset with a paperclip and the kids figured out when this happens to take battery out.  The kids NEW something was not right so they tested both bricks and when you asked it to run a program it had not run in a while it would happen( even ones without a light sensor).  Whenever we ran a calibrate light sensor program it locked up and our light sensor program worked for 1 program and when we tried to download the exact light sensor program but reverse it ( so the bot takes out of base backwards with light sensor on rear doing the detecting) it gets stuck in base barely moving and the light sensor is on but it is suppose to go backwards, 50 power, unlimited and pick up the loop when it sees the curve part of the black line?

Thanks to you and some others we have now been able to update our firmware to 1.28.  I am not sure the battery was completely full, maybe 7.9 level so we may need to try that again.
Is there anything else we need to do?
Report to moderator   Logged
Pages: [1] 2 Print 
« previous next »
Jump to:  


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!