To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 26902
26901  |  26903
Subject: 
Re: FindLight
Newsgroups: 
lugnet.robotics
Date: 
Mon, 26 Feb 2007 06:34:52 GMT
Viewed: 
3935 times
  
In lugnet.robotics, linmix <linmix@gmail.com> wrote:
I've declared turnfind at the beginning and set it to 0 (just in case)
According to BricxCC's 'Watching the brick' the values are ok, but no
matter what the values the robot will only spin back for a fraction of a
second.


//findlight.nqc

#define A OUT_A
#define C OUT_C

int maxlight;
int light;
int time = 0;
#define TURNTIME 500; //change to accomodate 1 turn
#define ROTATE_CLOCKWISE OnFwd(A); OnRev(C);
#define ROTATE_COUNTERCLOCKWISE OnFwd(C); OnRev(A);

task main()
{
   SetSensor (SENSOR_2, SENSOR_LIGHT);
   // measure light level at start of program
   maxlight = SENSOR_2;
   // turn slowly by lowering the motor power level
   SetPower(A+C, 3);
   // start rotating
   ROTATE_CLOCKWISE;
   // reset timer to zero
   ClearTimer (0);
   // rotate for 5 seconds
   while (Timer (0) < TURNTIME)
   {
     // measure current light sensor reading
     // use a variable so the value doesn't get re-read from
     // from the sensor inside the if statement
     light = SENSOR_2;
     if (light > maxlight)
     {
          // if it exceeds previous max then remember the new reading
          // and the time when we found it
          maxlight = light;  // set maxlight to new max value
          time = Timer(0);  // remember when maxlight was found
     }
   }
   // after rotating clockwise for 5 seconds now rotate back the other way
   ROTATE_COUNTERCLOCKWISE;
   // wait until we are back to the time when we found the maximum light
   Wait (TURNTIME - time);
   // stop turning
   Off (A+C);
}

What value do you see for time in the Watch window after your program runs?
Does the value correspond to the number of 1/100s of a second from the start of
the program where the maximum light should have been found?

You are better off using a constant for TURNTIME since it never changes.  I
defined macros for clockwise and counter clockwise rotation just to make it
better describe what you are trying to tell the robot to do.  You should adjust
the definition of these macros so that they actually match your robot's
configuration.

John Hansen



Message has 1 Reply:
  Re: FindLight
 
(...) I accidentally put a semicolon on the #define line above which is wrong. The program I posted will not compile unless the semicolon is removed. My apologies. It's what I get for posting late at night. John Hansen (18 years ago, 26-Feb-07, to lugnet.robotics)

Message is in Reply To:
  Re: FindLight
 
Thanks. I thought setting maxlight before the 'if' statement would be enough, but moving it closer to the front (before the 'while' statement') did the trick. The bit after the while loop still gives me pain though. I've changed it a bit to be able (...) (18 years ago, 25-Feb-07, to lugnet.robotics)

10 Messages in This Thread:


Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR