Last active
April 2, 2024 20:00
-
-
Save happyjake/51805b16ca35d8b3fda82e54afb5ee16 to your computer and use it in GitHub Desktop.
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
import math | |
import matplotlib.pyplot as plt | |
timestamps = [] | |
xs = [] | |
ys = [] | |
sample_rate = 4 # 10ms | |
x_begin = x_end = 0x285 | |
# | |
# y_begin = 0x900 | |
# y_end = 0x60 | |
# | |
y_begin = 0x1C9 | |
y_end = y_begin + (0x900 - 0x60) | |
flag = 2 | |
ts = 0 | |
total_ts = int(400 / sample_rate) + 1 | |
amplitude = 50 # adjust this to change the "width" of the snake movement | |
n = 0 # number of last values that should be the same | |
ss_begin = 2 # initial skip step | |
m = 30 # last m percent where ss decreases to 0 | |
skip_counter = 0 # counter for skip step | |
for ts in range(0, total_ts): # 300ms | |
if ts >= total_ts - n: | |
y = y_end | |
else: | |
# Calculate y as a cubic easing out function of ts, then scale and shift it to the range [y_begin, y_end] | |
t = ts / (total_ts - n) # normalize t to the range [0, 1] | |
y = int((y_end - y_begin) * ((t - 1) ** 3 + 1) + y_begin) | |
# Calculate x as a cosine function of ts, then scale and shift it to the range [x_begin - amplitude, x_begin + amplitude] | |
x = int(x_begin + amplitude * (math.cos(2 * math.pi * ts / total_ts) - 1)) | |
if ts == 0: | |
flag = 0 | |
elif ts == total_ts - 1: | |
flag = 1 | |
else: | |
flag = 2 | |
# Calculate ss as a linear interpolation from ss_begin to 0 over the last m percent of total_ts | |
if ts >= total_ts * (1 - m / 100): | |
ss = int( | |
ss_begin * (1 - (ts - total_ts * (1 - m / 100)) / (total_ts * m / 100)) | |
) | |
else: | |
ss = ss_begin | |
# Ensure ss is never zero | |
if ss == 0: | |
ss = 1 | |
# Only print and append data if skip_counter is 0 | |
if skip_counter == 0: | |
print( | |
f"{hex(ts*sample_rate)[2:].upper()} {flag} 1 0 {hex(x)[2:].upper()} {hex(y)[2:].upper()}" | |
) | |
timestamps.append(ts * sample_rate) | |
xs.append(x) | |
ys.append(y) | |
# Increment skip_counter and reset it to 0 if it reaches ss | |
skip_counter = (skip_counter + 1) % ss | |
print() | |
# Plot the data | |
plt.plot(timestamps, xs, label="X values") | |
plt.plot(timestamps, ys, label="Y values") | |
plt.xlabel("Timestamps") | |
plt.ylabel("Values") | |
plt.title("Generated Data") | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment