Subject:
|
Re: circular pointer
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Sun, 13 May 2007 14:34:47 GMT
|
Original-From:
|
linmix <linmix@gmail.comIHATESPAM>
|
Viewed:
|
5074 times
|
| |
| |
I've inserted some comments, not all are questions, some are simply
'thinking aloud'.
Dennis Williamson wrote:
> I've added my comments to the code you posted (they start with **). I've also
> added some indenting.:
>
> #define SIZE 3
So by defining SIZE here I can easily change the size of the macro
without having to alter anything else in the code. Nice!
> int v[SIZE],i,sum,ave;
> // initialize the array and some other variables
>
> sum = 0;
> for (i=0;i<SIZE-1;i++)
> {
> //I think this means set 'i' to zero, as long as 'i' is smaller than
> SIZE add one to 'i' after every loop
> //** It means "as long as 'i' is smaller than SIZE MINUS ONE"
Right you are, I should wear my glasses!
> v[i] = SENSOR_1;
> sum += v[i];
> }
>
> //what would happen is this, using arbitrary numbers to represent sensor
> reading:
> //v[0] = 50
> //sum = 0+50 =50
> //v[1] =52
> //sum = 50+52 = 102
> //v[2] =55
> //sum = 102+55 = 157
> //** But the loop stops at v[1] because one is less than SIZE-1 (which is two)
So the final sum is only 102
> i=SIZE-1;
> v[i]=0;
> //so 'i' becomes 2 and I set v[2] to zero. Why would I want to eliminate
> //my last reading?
> //** Yes, 'i' NOW becomes two and you initialize v[2] so it's not undefined
Is it necessary to initialise v[2] or simply 'cleaner' coding?
> // compute moving average (comment from book)
>
> while (true) //** Do it forever
Thanks, I overlooked that
> {
> sum -= v[i];
> //since 'i' now equals 2, and v[2] equals zero, this operations doesn't
> //appear to make sense
> //** This statement appears here because it makes sense INSIDE the while
> //** loop. Basically, this some and the following sensor reading are
> //** saying "replace the oldest reading with a new one".
> //** At first (before the 'while'), v[2] is the "oldest" reading.
Not really the oldest, but because it hasn't been used yet the place to
start the loop.
> v[i] = SENSOR_1;
> //I read a new value into v[2], e.g. 60
>
> sum += v[i];
> //since I haven't subtracted anything but 0 from the SUM, the sum now
> //holds the total value of 4 readings?!
> //** There's not really four readings, because (a) you can interpret the
> //** zero as if it meant 'empty' (but only during the initialization,
> //** of course), (b) the array only has three elements (SIZE), and
> //** (c) you're averaging by dividing by SIZE (three)
When I overlooked the SIZE-1 statement 4 reading had been added to sum
(50,52,60 and 0), but after your correction this makes sense again.
> ave = sum / SIZE;
> //average value, but for the last comment it makes perfect sense.
>
> i = (i+1) % SIZE;
> //you lost me there...
> // other instructions... (comment from book: what other instructions
> //might I want to include here?)
> //** As someone else stated, it's the modulus operator, which gives the
> //** remainder after integer division. When 'i' is 3, this statement
> //** evaluates to '(3 + 1) % 3' so three divided by three is one with
> //** a remainder of zero. So the new value of 'i' is zero. As the while
> //** loop loops, 'i' goes 2, 3, 0, 1, 2, 3, 0...
> }
It took a bit of pen and paper, but I think I've finally understood how
modulo works - at least in this particular case.
> > It looks like only the last value is recycled, when what should happen
> > is that the first value is eliminated and the second becomes first, the
> > third second and the new one the last.
> >
> > If I'm not making myself clear, please help me improve my question.
> > Thanks in advance for your efforts.
> >
> > linmix
>
>
> You don't have to move the values around, because 'i' changes and points to the
> oldest value, which then becomes the newest value, hence "circular pointer". A
> fun thing to do with them is the "snake" program. The elements of the array hold
> the x, y coordinates of each segment of the snake's body. The program clears a
> spot on the screen at the oldest position (the end of the snake's tail) and
> paints a spot at the new position (the head). This causes the snake to move
> around the screen. Some directional control selects which direction it moves
> (increment or decrement x or y or both).
>
> Dennis
Nice application. In this case the idea was to use it in a line
following program, in which case I suppose I'd need another variable to
compare the present average with the previous average and observe the
trend. Or maybe not, and link the steering of the robot to the value of
the average instead of individual readings.
linmix
|
|
Message has 1 Reply: | | Re: circular pointer
|
| In lugnet.robotics, linmix <linmix@gmail.com> wrote: [snip] (...) [snip] I would say defining SIZE changes the size of the array and thus the period of the moving average. I'm not sure how you mean "macro" in this context. [snip] (...) Yes, both (...) (18 years ago, 13-May-07, to lugnet.robotics)
|
Message is in Reply To:
| | Re: circular pointer
|
| I've added my comments to the code you posted (they start with **). I've also added some indenting.: #define SIZE 3 int v[SIZE],i,sum,ave; // initialize the array and some other variables sum = 0; for (i=0;i<SIZE-1;i++) { //I think this means set (...) (18 years ago, 13-May-07, to lugnet.robotics)
|
8 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
|
|
|
|