Forum Rule: Always post complete source code & details to reproduce any issue!
Page 6 of 6 FirstFirst ... 4 5 6
Results 126 to 141 of 141

Thread: Circular_Buffer

  1. #126
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,549
    Interesting - that was shown as used in post #107 on a CB >> Circular_Buffer<uint16_t, 2048> hz; :: hz.pop_front(HzArray, HzSamples);

    When the note says the flush wasn't working.

  2. #127
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    you are correct, flush is not used on CBA, only effective on CB
    post 107 code shows he is in fact using CB

    sorry, re-read, okay yeah the command can be called on CB, but wont do anything really, its for CBA only, he must remove the overload or switch to CBA, depending on his setup

    looks like he only needs CB, so he has to do a for loop and read/pop/pop_front/pop_back into a local array as needed

  3. #128
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,549
    But that was a CB then? .flush was giving dupe stats and using the line in #126 worked?

    Flush and clear are shown in ReadMe under : Methods for circular array buffer … same as for Methods for circular ring buffer

    So would this work to clear the data from a CB in a CBA? :: hz.front()[ tIC ].clear();

  4. #129
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    yes that will work

  5. #130
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    i could add an array feature to CB like what turtler tried to do using overload, internally the library would create a temp buffer to fill it with the circular queue and feed that to the user

  6. #131
    Senior Member
    Join Date
    Feb 2013
    Posts
    181
    Starting to get a little lost, however could be because of jetlag.
    The issue with my system is that HzSamples is different value for every 10 deg segment and can vary from 2048 samples to 10 samples, depending how fast the people are cycling. If I am using push.back, should I just use pop.back to get the samples that filled the CB during that 10 degree segment? My current method seems to work, because I flush it every single time, but only after I transfer it to another array (HzArray).
    What do you mean by overload?

  7. #132
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    if you only want to sample last 10 entries you should use 16 instead of 2048, the oldest entries will be overwritten when added to queue, im on my phone ill be home to help out better soon

  8. #133
    Senior Member
    Join Date
    Feb 2013
    Posts
    181
    No, I sometimes have 2048 values, this happens when the cyclists do a standing start and are pedaling slow, but producing a lot of torque (high Hz values). However as the cyclists speed up, they are producing less torque (lower Hz), which means I might only have 10 Hz samples for every 10 degree segment of the pedal revolution. Trust me is confusing and annoying, since I also have to write this data to an SD card every 10 degree segment, which makes it hard because getting 512 byte chunks to write to sd card took a bit of time to figure out.
    Thanks again, learning a lot.

  9. #134
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    in CB you can use the ::peek(index) feature, where you could peek in a for loop for example to read entries in the circular buffer queue.

    Since you are using CB and not CBA, front() wont be used, peek() will be used. note that this doesn’t remove the entries, but allows you to peek into the buffer to see what is in there.

    Lets say you create a CB of 15 entries, to read from 9-14 (if it’s full), you can for loop ::peek(9) to peek(14)
    if your buffer is only half full, peek will wrap around and start reading the the front. If this isn’t obvious you should also check size before requesting “too many” from peek, else you’ll read out of bounds im sure :)

  10. #135
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    i was re looking over post 107 and
    Code:
    hz.pop_front(HzArray, HzSamples);
          hz.flush();//was getting repeat hz
    the reason you got repeat values was because you never read every byte inside the queue which dequeues it. flush did indeed work as intended, but if you have 2000 entries for example, you must pop it via pop/read/pop_front 2000 times, or simply flush/clear to clear it. pop_front(buf,size) is ONLY used for CBA, so when you ran that it actually did nothing, basically ignored your request, thus making you think it had repeat values, which it did, because they were still there
    if you pop_front() (without any overload), a single byte will be removed from the front of the queue, so you’ll be left with 1999 items. hope all is clear now

    is your intention to copy the buffer to an array to feed it to the SD? you’ll need a for loop to read()/pop()/pop_front() the data into the array then so it will both populate your array and also clear out the circular buffer at same time.

  11. #136
    Senior Member
    Join Date
    Feb 2013
    Posts
    181
    Now I am getting confused. The system I am using now seems to be working great and don't get repeat values. From your github readme you have this listed under Circular Buffers:
    T pop_front(T *buffer, uint16_t length) { return readBytes(buffer,length); } // read the front of the queue into a buffer and remove it
    I just want to make sure my program is running as quick as possible so no data is missed, so adding in a loop to read out 2048 values seems like it would take longer than using memmove, which I think pop_front uses.
    My exact intention is to read the buffer to an array (but only as many values as I had for that 10 degree segment), and repeat over and over.
    I just looked at the data I have been collecting and I don't have repeat values any more and I use pop_front(buffer, length).

  12. #137
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    yes you are correct i completely forgot, pop_front in CB works differently than in CBA, but they do pretty much same thing.

    the CBA (array version) uses memmove completely as the arrays are fixed.
    the CB (circular buffer version) uses memmove ONLY if the data doesn’t wrap around, if it does, it’ll fallback to a for loop instead. So it’s working as expected for you
    Last edited by tonton81; 08-01-2018 at 11:25 PM.

  13. #138
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    This will also work if you want to index a bunch of circular buffers directly, without pointing to them using the CBA's front() method

    Code:
      Circular_Buffer<uint16_t, 2048>xx[2];
    
    
      xx[0].push_back(0x06);
      xx[0].push_back(0x07);
      xx[0].push_back(0x08);
    
      xx[1].push_back(0x02);
      xx[1].push_back(0x03);
      xx[1].push_back(0x04);
      xx[1].push_back(0x05);
    
      Serial.println("\tIndex [0]");
      Serial.print("Size: "); Serial.println(xx[0].size());
      Serial.println(xx[0].read());
      Serial.println(xx[0].read());
      Serial.println(xx[0].read());
    
      Serial.println("\tIndex [1]");
      Serial.print("Size: "); Serial.println(xx[1].size());
      Serial.println(xx[1].read());
      Serial.println(xx[1].read());
      Serial.println(xx[1].read());
      Serial.println(xx[1].read());
    Output:
    Code:
    	Index [0]
    Size: 3
    6
    7
    8
    	Index [1]
    Size: 4
    2
    3
    4
    5
    Last edited by tonton81; 08-05-2018 at 12:23 AM.

  14. #139
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    Added new feature for Circular_Buffer (CBA): isEqual

    This will confirm if your queues already contain the buffer your passing in. bool 1 is returned if a match is found, otherwise 0 is returned for not found.

  15. #140
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,009
    New feature added to Circular Buffer Arrays: findRemove.

    This has the same capability as find and replace methods, however, instead of finding or replacing, this actually removes the item from the queue. This method shifts all the consumer entries upwards via memmove afterwards and doesn't touch the producer endpoint.

    An example was added to demonstrate the use:

    https://github.com/tonton81/Circular...ve_Example.ino

    CANquitto is the first to use it for an active node listing, removing inactive nodes.
    Last edited by tonton81; 09-29-2018 at 09:33 PM.

  16. #141
    Junior Member
    Join Date
    Apr 2019
    Posts
    1
    all c++ features, you check size before you , this removes overhead, and is very known, user should know this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •