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 / 27111
27110  |  27112
Subject: 
Re: circular pointer
Newsgroups: 
lugnet.robotics
Date: 
Sun, 13 May 2007 00:08:45 GMT
Viewed: 
11805 times
  
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 '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"

    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)

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

// compute moving average (comment from book)

while (true) //** Do it forever
{
    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.

    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)

    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 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



Message has 1 Reply:
  Re: circular pointer
 
I've inserted some comments, not all are questions, some are simply 'thinking aloud'. (...) So by defining SIZE here I can easily change the size of the macro without having to alter anything else in the code. Nice! (...) Right you are, I should (...) (19 years ago, 13-May-07, to lugnet.robotics)

Message is in Reply To:
  circular pointer
 
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 (...) (19 years ago, 12-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
    

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