Skip to content

Commit

Permalink
Moving average logic and neopixel logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
slabua committed Mar 24, 2024
1 parent affc74c commit 6bdbff6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
14 changes: 8 additions & 6 deletions OLED/picomotodash_neopx.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ def set_np(self, i, rgb_tuple):
self.np.write()

def set_np_rpm(self, rpm):
self.off()

upto = rpm // 1000
for i in range(24, upto + 24):
self.np[i] = (2, 2, 0)
for i in range(24, self.n):
if i < upto + 24:
self.np[i] = (2, 2, 0)
else:
self.np[i] = (0, 0, 0)

self.np.write()

def set_np_compass(self, heading):
# PLACEHOLDER
# TODO Update logic

self.off()
for i in range(24, self.n):
self.np[i] = (0, 0, 0)

i = int(self.map_range(heading, (0, 360), (34, 24)))
self.np[i] = (0, 0, 5)
Expand Down
74 changes: 56 additions & 18 deletions OLED/picomotodash_oled_ws10d.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
display.fill(0)
display.show()

# RPM setup
RPM_ESTIMATE = 0
PWM2RPM_FACTOR = 10

# GPS setup
gps = pmdGPS(local_offset=9, location_formatting="dd")

Expand All @@ -53,6 +49,29 @@
HEADING = 0
labels = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"]

# Neopixel setup
PIN_NUM = 3
NUM_LEDS = 37
neopixel_ring = pdmNEOPX(pin=PIN_NUM, n=NUM_LEDS)

# RPM setup
RPM_ESTIMATE = 0
PWM2RPM_FACTOR = 10


# Utility functions
log_file = open("log.csv", "a")


def _log(data):
log_file.write(str(data) + "\n")
log_file.flush()


def map_range(value, in_range, out_range):
(a, b), (c, d) = in_range, out_range
return (value - a) / (b - a) * (d - c) + c


def moving_avg(n):
global headings
Expand All @@ -62,12 +81,14 @@ def moving_avg(n):

headings.append(HEADING)

return sum(headings) / len(headings)


def normalise_avg(avg):
# Check whether the array contains both positive and negative numbers
has_positive = any(num > 270 for num in headings)
has_negative = any(num < 90 for num in headings)

avg = sum(headings) / len(headings)

if avg > 180:
np_val = 360 - avg
else:
Expand All @@ -90,24 +111,40 @@ def moving_avg(n):
return avg


# Neopixel setup
PIN_NUM = 3
NUM_LEDS = 37
neopixel_ring = pdmNEOPX(pin=PIN_NUM, n=NUM_LEDS)
def moving_avg_backup(n):
global headings

if len(headings) >= n:
headings.pop(0)

# Utility functions
log_file = open("log.csv", "a")
headings.append(HEADING)

# Check whether the array contains both positive and negative numbers
has_positive = any(num > 270 for num in headings)
has_negative = any(num < 90 for num in headings)

def _log(data):
log_file.write(str(data) + "\n")
log_file.flush()
avg = sum(headings) / len(headings)

if avg > 180:
np_val = 360 - avg
else:
np_val = avg

def map_range(value, in_range, out_range):
(a, b), (c, d) = in_range, out_range
return (value - a) / (b - a) * (d - c) + c
if has_positive and has_negative:
neopixel_ring.set_np(0, (0, 0, 32))

tmp_headings = []
for h in headings:
if h > 270:
h = h - 360
tmp_headings.append(h)
avg = ((sum(tmp_headings) / len(tmp_headings)) + 360) % 360

return avg

neopixel_ring.set_np(0, (0, round(map_range(np_val, (180, 0), (0, 32))), 0))

return avg


def show_logo():
Expand Down Expand Up @@ -446,6 +483,7 @@ def thread1(PWM2RPM_FACTOR):
read_mpu()
HEADING = mpu.heading
HEADING = moving_avg(5) # 9
HEADING = normalise_avg(HEADING)

# neopixel_ring.update(RPM_ESTIMATE, HEADING)

Expand Down

0 comments on commit 6bdbff6

Please sign in to comment.