forked from commaai/openpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tesla_alpha' of https://github.com/boggyver/openpilot i…
…nto tesla_alpha
- Loading branch information
Showing
5 changed files
with
30 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,20 @@ | ||
import queue | ||
|
||
class MovingAverage(): | ||
def __init__(self, length): | ||
self.position = 0 | ||
self.length = length | ||
self.sum = 0. | ||
self.no_items = 0 | ||
self.values = [0.] * length | ||
|
||
self.reset() | ||
|
||
def reset(self): | ||
self.position = 0 | ||
self.sum = 0. | ||
self.no_items = 0 | ||
self.values = [0.] * self.length | ||
self.queue = queue.Queue(maxsize=self.length) | ||
self.sum = 0 | ||
|
||
def add(self,element): | ||
if self.no_items == self.length: | ||
self.no_items -= 1 | ||
self.sum -= self.values[self.position] | ||
self.values[self.position] = element | ||
self.sum += self.values[self.position] | ||
self.no_items += 1 | ||
self.position += 1 | ||
if self.sum == 0.: | ||
#all empty so initialize | ||
self.position = 0 | ||
self.sum = 0. | ||
self.no_items = 0 | ||
return 0. | ||
self.position = self.position % self.length | ||
return self.sum/self.no_items | ||
def add(self, sample): | ||
if self.queue.full(): | ||
self.sum -= self.queue.get_nowait() | ||
self.queue.put_nowait(sample) | ||
self.sum += sample | ||
return self.sum / self.queue.qsize() | ||
|
||
def dele(self): | ||
if self.no_items == 0: | ||
return 0. | ||
if self.no_items > 0: | ||
self.no_items -= 1 | ||
self.sum -= self.values[self.position] | ||
self.values[self.position] = 0. | ||
self.position -= 1 | ||
if self.position < 0: | ||
self.position = self.length-1 | ||
if self.sum == 0. or self.no_items == 0.: | ||
#all empty so initialize | ||
self.position = 0 | ||
self.sum = 0. | ||
self.no_items = 0 | ||
return 0. | ||
self.position = self.position % self.length | ||
return self.sum/self.no_items | ||
def full(self): | ||
return self.queue.full() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters