No announcement yet.

How do you calibrate the light sensor?

  • Filter
  • Time
  • Show
Clear All
new posts

  • How do you calibrate the light sensor?

    This is our first year with FLL. I am confused about how to calibrate the robot. In fact I’m not even sure what that means. For now our team is trying to build a line follower program and it’s going beserk so I’m wondering if I should start with trying to understand the concept of calibration.

    For now we go into the viewing sensor of the Mindstorms brick and choose the reflected light and look at the number that pops up whenever the light sensor is on top of something white or black. This seems to work ok, but I’ve been through the nxt tutorial on and there it gives you a program to run in order to calibrate your robot and seems to say not to use the program on the brick. What’s the difference? The tutorial mentions something about the viewing sensor giving the absolute light value. I’m not sure I understand what’s wrong with that.

    Then I also thought I read somewhere that once you calibrated your light sensor with a program, you couldn’t use the viewing sensor anymore to get your numbers. What does this all mean?
    3rd Year Coach
    Mindstorms Maniacs #1023
    Colorado Springs, CO

  • #2
    Re: How do you calibrate the light sensor?

    If your robot responds to passing over the line it is unlikely that the light sensor or calibration is the problem.

    Phase 1: Verify that the light sensor is attached and that the program is using the correct sensor port. Write a program to display the light sensor value (just a read sensor and display block inside a loop does the trick). Verify that your tracking threshold is between the black line reading and the white mat reading. If the black line reading and white mat reading are really close together you'll have problems tracking. Check that the sensor block is set to Reflected light and that the red LED on the sensor turns on. If you still have a problem you may have some crazy calibration installed. Either perform a new calibration or restore the default calibration (described in help files).

    Phase 2: Verify that the program logic is correct. When testing a line follower problem the first thing I do is take of the wheels. It is a lot harder to see what's happening if the robot is racing around all over the place. Position the light sensor over the edge of the line. What are the motors doing? Move the robot to the left. Is the robot making the proper corrective action (if the wheels were attached would it be turning toward the line?) Move the robot to the right. Is the response of the robot correct? If it doesn't pass this test it will never work. A common problem I see is using the same corrective action for both tracking to the left and tracking to the right. Probably a cut and paste error. Another common problem is leaving the duration for the Move or Motor blocks at 1 rotation or something like that. Believe it or not the only reasonable duration for line following is Unlimited. When the duration is set to Unlimited the line following loop runs quicker (more times each second) and can make more frequent (and smaller) corrections.

    Phase 3: So you fixed any logic problems. The robot now makes the proper corrective actions, but it still loses the line. What is happening? Is the robot swinging wildly back and forth and sometimes whips around 180 degrees and starts driving in the opposite direction? You are overcorrecting or turning too much. Does the robot start following the line but get lost when the line curves? You are undercorrecting or turning too little. What about speed? Does the robot sometimes shoot over the line without trying to correct? Maybe you are goint too fast. Try again at a lower speed.

    Oops! I almost forgot robot design. Though it is possible to track a line with the light sensor behind or in line with the wheels it is very difficult and requires a sophisticated program that involves pattern matching and making predictions. What you want to do is put the light sensor in front of the wheels so the program gets immediate feedback about how the corrections (turning) worked. The farther the sensor is in front of the wheels the smoother the robot will track. I'd shoot for at least 2 inches.

    Here's the answer to your question about how calibration messes with using view to look at the light sensor:

    When you calibrate the light sensor (either using the programming block or the built in program) you grab a reading over a dark area and a bright area and calculate an offset and a gain that is applied to all future light sensor readings. This is best explained by an example:

    You run the calibration and the dark reading was 50 and the bright reading 75. NXT sees the range is 75 - 50 = 25 and that the darkest reading is 50. NXT wants the range to be 100 and the darkest reading to be zero, so it sets a gain value to 100 / 25 = 4, and an offset to 50. So if we place the light sensor back over the dark area it reads 0.

    output = (reading - offset) x gain = (50 - 50) x 4 = 0.

    And the value back over at the bright area is now 100.

    output = (75 - 50) x 4 = 100.

    I suppose that I should mention that the NXT does not use the 0-100 intensity numbers for calibration, but instead uses the raw values that range 0 to 1024. The intensity numbers you see using the View program are really these raw values divided by 10.24 (or maybe just 10). For the example above the raw dark reading was probably around 512 and the raw bright reading around 768. To repeat the math:

    Gain = 100 / (range) = 100 / (768 - 512) = 0.39. Offset = 512

    Dark Calibrated value = (reading - offset) x gain = (512 - 512) x 0.39 = 0
    Bright Calibrated value = (768 - 512) x 0.39 = 100

    If you write your own calibration routines you should use the uncalibrated raw values. That way you don't have to worry about someone using the built in NXT calibration routine and messing up your results (you don't want to apply a calibration to a calibrated value).

    Cool, now if I want to track the edge of a line right down the center of my light sensor I can use a tracking threshold of 50. Half way between light and dark. It is OK to hard code a threshold if you also control the range of the sensor.

    The built in calibration has a problem in that the view routine doesn't know about it. View shows you an UN-calibrated value (raw value / 10.24). The light sensor block uses the calibrated value, a wait for light sensor block uses the calibrated value, a loop end block uses the calibrated value, but view DOES NOT display the calibrated value. So you may be using the view routine to get a tracking threshold and see the number is 48 (you found a really dark spot). But using he calibration numbers above, the calibrated intensity value from the light sensor block is zero.

    View reading = 48, Raw reading = View reading * 10.24 = 491.
    Calibrated Intensity = (Raw reading - Offset) x gain = (491 - 521) x 0.39 = -11.7 which is trimmed to 0.

    See the problem? View says 48, Sensor block says 0. Great potential for confusion.

    The best way to view the calibrated value is to write a small program that reads the sensor and displays the value on the screen. Two blocks inside a loop. That way you see the same value the program sees.
    Last edited by Dean Hystad; 09-30-2010, 06:23 PM.


    • #3
      Re: How do you calibrate the light sensor?

      Without calibration, a light sensor might read something like 60 over white and 40 over black, but these numbers will depend on various factors such as the amount of ambient light in the room. With calibration, you can tell the sensor to report black as 0 and white as 100, by sampling these two colors on the actual mat ahead of time, then the light sensor block internally does the math to give you (approximately) 0 for black instead of the "uncalibrated" value of 40 or whatever.

      So calibration gives you more consistent results since you area sampling the actual conditions of the mat before running, and also you can "hard code" a test like "< 50" as being a confident way to tell black from white.

      The built-in NXT calibration is confusing to use for several reasons, due to poor design (e.g. numbers being shown in different scalings in different places). Worse, the View Reflected Light feature on the brick ignores the calibration and shows scaled but uncalibrated numbers. This is the cause of those warnings are such you have heard about.

      A discussion of the basics of calibration (there are many ways to go about it, but here are a couple) and how it can apply to line following can be found at:
      Also here are some alternative calibration utility programs that are more consistent and less confusing than the built-in ones, if you want to try those.

      Dave Parker


      • #4
        Re: How do you calibrate the light sensor?

        Thank you both so much for your explanations. I'm so impressed with the level of knowledge on this forum and the willingness to share with such detail. I wonder why the forums don't have "sticky" notes b/c I'm thinking you get these newbie questions every year.

        I think our problem started out with robot design. We put the light sensor under the robot between the wheels. So glad Dean mentioned that could be an issue. I know the program the kids wrote was not very complicated so I'll suggest they move the light sensor in front again.

        I'll have to study the notes on calibration some more. I don't get it all w/o actually doing it so I'll check back in if I have questions.

        Thanks for all your help.
        3rd Year Coach
        Mindstorms Maniacs #1023
        Colorado Springs, CO


        • #5
          Re: How do you calibrate the light sensor?

          Here is something that you may want to watch out for. My boys noticed that you the values returned for the "black level" would vary over a range of up to 5 depending on where they were taken. The full-scale value is 100 in Robolab and they use the reflected light mode to minimize the effect of outside light. They feel the variation is due to variations in the angle of the mat relative to the sensor (bumps), the local reflectiveness of the mat and variations in the intensity of the external light reaching the sensor. The last is influenced by the intensity of the external light sources as well as how much gets blocked by the robot (this is a big hint to you rookies). They saw similar variations in the "white level" too.

          The differences between representative values for the white level and the black level that they get is typically about 10 times greater than the variations so they used the average of the two as the threshold for detecting the lines (this is a pretty common practice in industrial controllers). They found that the values changed pretty rapidly as they moved the sensor slowly, by hand, between the white and black areas and that the exact value of the threshold used was not particularly critical. What was more critical was the speed at which the sensor traversed the boundary between the white and the black. In line-following, this would be influenced by how far the sensors are from the center of rotation of the robot. They did not use line-following last year because they wanted a shorter travel time and they had some "alternative" ideas for how to accomplish the tasks. They did use line detection quite extensively and were able to get very consistent results using the average value as the threshold.

          I believe that someone has already mentioned that two light sensors will probably not return the same values for the same "white" or "black". My boys actually calculated separate thresholds for each of the two light sensors they installed last year.

          It may not be so significant this year, but it would be good to test all the various colours on the mat to see what values they return. My boys noticed that many of the colors (yellow and sand) on the Smart Move mat gave the same range of values as the white areas.


          • #6
            Re: How do you calibrate the light sensor?

            Originally posted by philso View Post
            It may not be so significant this year, but it would be good to test all the various colours on the mat to see what values they return. My boys noticed that many of the colors (yellow and sand) on the Smart Move mat gave the same range of values as the white areas.
            I know it is getting a bit old, but "Building LEGO Robots for FIRST LEGO League" (google it) has a pretty good section on how the RCX light sensor worked. That information is still applicable.


            • #7
              Re: How do you calibrate the light sensor?

              Bringing up resources like "Building LEGO Robots for FIRST LEGO League" does not get old since there are always new people here and some of us could use a bit of a refresher.

              The issue at hand is that the light sensors have a monochromatic response. What we were trying to establish is what light intensity level each color on the mat would correspond to. It made sense to us that the best instrument available to measure the light intensity returned by the different areas on the mat at the wavelength/colour of the light sensor was the light sensor itself.