Subject:
|
circular pointer
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Sat, 12 May 2007 16:46:15 GMT
|
Original-From:
|
linmix <linmix@gmailNOSPAM.com>
|
Viewed:
|
4719 times
|
| |
| |
Some time ago some of you offered to help out if I (and I suppose anyone
else) had any questions about programming.... well, here goes
I'm reading "Building Robots with Lego Mindstorms" and in Chapter 12,
there is some code to create a circular pointer for an array, with the
aim of smoothing averages. I've tried to understand the code as
explained in the same chapter, but I can't seem to understand exactly
what is happening.
I'll reproduce part of the code and comment it so you know if I'm
thinking right.
#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 'i' to zero, as long as 'i' is smaller than
SIZE add one to 'i' after every loop
{
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
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?
// compute moving average (comment from book)
while (true)
{
sum -= v[i];
//since 'i' now equals 2, and v[2] equals zero, this operations doesn't
appear to make sense
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?!
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?)
}
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
|
|
Message has 3 Replies: | | Re: circular pointer
|
| (...) The line above is the line that cycles the array - each time through the loop it increments i and resets it to zero if it's >= SIZE (% is modulus operator). But I would agree with you about the v[i]=0 line - seems to me it would make the (...) (18 years ago, 12-May-07, to lugnet.robotics)
| | | 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)
| | | Re: circular pointer
|
| (...) Another way to accomplish the same result with less code and less memory is to use exponential smoothing. (see Wikipedia) The method uses coefficients between 0 and 1, but if you want to use integers you can always use the integer numerator (...) (18 years ago, 16-May-07, to lugnet.robotics)
|
8 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
Active threads in Robotics
|
|
|
|