I'm programming a stepper motor with a SparkFun Thing Plus rp2040 along with a LIS3DH accelerometer and the goal of the project is the move two stepper motor independently in CW/CCW direction when the accelerometer is reading a tilt value within a positive range and the other direction when it is reading a value in the negative range. One stepper motor is responsible for the x axis and the other is responsible for the y axis.
In order for me to remove some of the noise produced by the LIS3DH, I implemented a moving average calculation within the loop and converted average to degrees, but as the sample window gets bigger (ex: 75 and above) the code also runs slower, which causes my stepper motors to move only one step per loop iteration.
Is there a way to speed up the loop? or make the stepper motor move without regards to the loop iteration?
I'm I doing something wrong? Kinda new to code as well btw.
Code: Select all
import time
import math
import board
import digitalio
import adafruit_lis3dh
# Initialize the I2C bus
i2c = board.STEMMA_I2C()
# Initialize the LSM6DSOX accelerometer
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
# Initialize the stepper motor pins
x_step = digitalio.DigitalInOut(board.D26)
x_step.direction = digitalio.Direction.OUTPUT
x_dir = digitalio.DigitalInOut(board.D27)
x_dir.direction = digitalio.Direction.OUTPUT
x_ms1 = digitalio.DigitalInOut(board.D18)
x_ms1.direction = digitalio.Direction.OUTPUT
x_ms2 = digitalio.DigitalInOut(board.D17)
x_ms2.direction = digitalio.Direction.OUTPUT
x_ms3 = digitalio.DigitalInOut(board.D16)
x_ms3.direction = digitalio.Direction.OUTPUT
y_step = digitalio.DigitalInOut(board.D28)
y_step.direction = digitalio.Direction.OUTPUT
y_dir = digitalio.DigitalInOut(board.D29)
y_dir.direction = digitalio.Direction.OUTPUT
y_ms1 = digitalio.DigitalInOut(board.D22)
y_ms1.direction = digitalio.Direction.OUTPUT
y_ms2 = digitalio.DigitalInOut(board.D21)
y_ms2.direction = digitalio.Direction.OUTPUT
y_ms3 = digitalio.DigitalInOut(board.D20)
y_ms3.direction = digitalio.Direction.OUTPUT
#Function setup for x and y axis
def x_spin_function():
x_step.value = True
time.sleep(0.001)
x_step.value = False
time.sleep(0.001)
def y_spin_function():
y_step.value = True
time.sleep(0.001)
y_step.value = False
time.sleep(0.001)
# Set up a few global variables & list
G_CONST = 9.81
sum_x_value = 0
sum_y_value = 0
avg_x_value_offset = 0
avg_y_value_offset = 0
degrees_list_x = [0]
degrees_list_y = [0]
sample_counter = 0
sample_window = 100
while True:
raw_x_value = lis3dh.acceleration[0] # "0" represents the x axis
raw_y_value = lis3dh.acceleration[1] # "1" represents the y axis
sum_x_value += raw_x_value
sum_y_value += raw_y_value
sample_counter += 1
if sample_counter == sample_window:
avg_x_value = ((sum_x_value)/(sample_window)) + (avg_x_value_offset)
avg_y_value = ((sum_y_value)/(sample_window)) + (avg_y_value_offset)
degrees_x = math.asin((avg_x_value)/(G_CONST)) * (180/math.pi)
degrees_y = math.asin((avg_y_value)/(G_CONST)) * (180/math.pi)
degrees_list_x[0] = degrees_x
degrees_list_y[0] = degrees_y
sum_x_value = 0 # x Reset
sum_y_value = 0 # y Reset
sample_counter = 0
print(degrees_list_x, degrees_list_y)
if 1 < degrees_list_x[0] and degrees_list_y[0] < 90:
x_dir.value = True
y_dir.value = True
x_spin_function()
y_spin_function()
continue
if -90 < degrees_list_x[0] and degrees_list_y[0] < -1:
x_dir.value = False
y_dir.value = False
x_spin_function()
y_spin_function()
continue