Subject:
|
Re: FindLight
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Mon, 26 Feb 2007 23:20:18 GMT
|
Original-From:
|
linmix <linmix@gmail.&Spamless&com>
|
Viewed:
|
4053 times
|
| |
| |
Thank you for your well commented adaptation.
I'm still experiencing the same problem even though the values for the
different variables as shown by 'watch the brick' check out.
The problem is in a different place. I wrongly assumed wait 'ticks' and
timer 'ticks' to be the same, but wait uses 1/100 and timer 1/10. In
other words, the value calculated with the timer needs to be multiplied
by ten to correspond to the motor movement!
For this same reason TURNTIME needs to be a factor 10 smaller (it takes
about 5 seconds for my bot to turn around
The following code works:
//findlight.nqc
#define A OUT_A
#define C OUT_C
int maxlight;
int light;
int time = 0;
#define TURNTIME 50 //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)*10); // stop turning
Off (A+C);
}
Thank you all for your help
regards
linmix
John Hansen wrote:
> In lugnet.robotics, John Hansen wrote:
>
> > In lugnet.robotics, linmix <linmix@gmail.com> wrote:
> > int time = 0;
> > #define TURNTIME 500; //change to accomodate 1 turn
>
>
> 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
>
>
|
|
Message has 1 Reply: | | Re: FindLight
|
| (...) Use FastTimer instead of Timer for 10ms resolution. I should have remembered that last night. John Hansen (18 years ago, 27-Feb-07, to lugnet.robotics)
|
Message is in Reply To:
| | 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)
|
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
|
|
|
|