I am trying to calculate position from accelerometer values. To do this I am appending each acceleration output into a nested list along with a time stamp, I am then using those to calculate velocity and position. I am running into an issue where every value I appended in my velocity list is overwritten with the final velocity output in the while loop. Is there a way to keep a list of the velocity's value from each iteration of the loop without their value changing?
import time
from random import randint
acceleration_list = []
velocity_list = []
velocity = [[0,0,0],0]
index = 0
while index < 10:
# Get current acceleration and append to list
accel = [randint(-5,5),randint(-5,5),randint(-5,5)] # Accelerometer Values
accel[1] -= 1 # Subtract 1g from Y-value
time_stamp = time.time() # Time stamp
curr_acceleration = [accel, time_stamp] # List to store accelerometer values + time stamp
acceleration_list.append(curr_acceleration) # Add to total acceleration list
# print(f"Acceleration values are: {acceleration_list}")
time.sleep(0.025)
# Calculate velocity using acceleration
for i in range(3):
if len(acceleration_list) > 1 and index > 0: # Wait until we've done one iteration
if abs(acceleration_list[index][0][i]) < 0.5: # Filter out small values from the accelerometer
acceleration_list[index][0][i] = 0.0
# Previous Velocity + (Current Acceleration + Previous Acceleration / 2 ) * (Current Time - Previous Time)
velocity[0][i] = velocity_list[index-1][0][i] + ((acceleration_list[index][0][i] + acceleration_list[index-1][0][i])/2)*(acceleration_list[index][1] - acceleration_list[index-1][1])
velocity[1] = acceleration_list[index][1]
velocity_list.append(velocity)
print(velocity)
index += 1
print(velocity_list)