Hi all,
I am trying to count the steps of the stepper motor in a certain weight value that I measure from the balance weight. But the problem is that at first the motor runs and then the balance will show the weights. Is there any way that run these two functions at the same time and count the steps of the stepper motor at the certain value of the weight? I also uploaded my written code. The problem of the code is that at first the motor runs and each time that motor runs the weight is zero. I also can't change the sequence because the motor should run to change the weights.
#definition of librarys
import RPi.GPIO as GPIO
import time
import serial
import matplotlib.pyplot as plt
import numpy as np
import re
#definition of GPIO pins
step_pins=21 #step pin
dir_pins=20 #direction pin
CW=1
CCW=0
ms_pins=(14,15,18) #microstepping pins
mode='1/32'
microstep_sequence=(1, 0, 1)
delay=0.5/32
SPR=6400
#set up GPIO mode
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#set up the GPIO pins for step,direction and microstepping mode
GPIO.setup(step_pins,GPIO.OUT)
GPIO.setup(dir_pins,GPIO.OUT)
GPIO.setup(ms_pins,GPIO.OUT)
GPIO.output(ms_pins,microstep_sequence)
d=np.linspace(5,10,5)
steps=int((5*SPR)/4)
print(steps)
#definition serial port for the balance
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600)
time_values=[]
weights=[]
step_count=0
#motor movement function
def move_motor_and_read_weights(direction):
global step_count
GPIO.output(dir_pins,direction)
start_time=time.time()
for x in range(steps):
GPIO.output(step_pins, GPIO.HIGH)
time.sleep(delay)
GPIO.output(step_pins, GPIO.LOW)
#counting the steps when weight is not zero
if weights and weights[-1] != 0:
step_count+=1
end_time=time.time()
#calculation of the linear velocity
actual_time=end_time-start_time
print('time:',actual_time)
linear_velocity=4/(actual_time)
print('linear_velocity(mm/s)=',linear_velocity)
start_bal_time=time.time()
end_bal_time=time.time()+5
while time.time()<end_bal_time:
response=ser.readline()
weight=response.decode().strip()
match=re.search("\d+(\.\d+)?",weight)
if match:
current_time=time.time()-start_bal_time
time_values.append(current_time)
weights.append(float(match.group()))
#print(weights)
else:
print("No match found")
#move upward
move_motor_and_read_weights(CW)
print('total number of steps from the touch point:',step_count)
time.sleep(5)
#move downward
move_motor_and_read_weights(CCW)
#plot
plt.figure(figsize=(10,6))
plt.plot(time_values,weights)
plt.xlabel('time(s)')
plt.ylabel('weights(g)')
plt.show()
GPIO.cleanup()
I am trying to count the steps of the stepper motor in a certain weight value that I measure from the balance weight. But the problem is that at first the motor runs and then the balance will show the weights. Is there any way that run these two functions at the same time and count the steps of the stepper motor at the certain value of the weight? I also uploaded my written code. The problem of the code is that at first the motor runs and each time that motor runs the weight is zero. I also can't change the sequence because the motor should run to change the weights.
#definition of librarys
import RPi.GPIO as GPIO
import time
import serial
import matplotlib.pyplot as plt
import numpy as np
import re
#definition of GPIO pins
step_pins=21 #step pin
dir_pins=20 #direction pin
CW=1
CCW=0
ms_pins=(14,15,18) #microstepping pins
mode='1/32'
microstep_sequence=(1, 0, 1)
delay=0.5/32
SPR=6400
#set up GPIO mode
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#set up the GPIO pins for step,direction and microstepping mode
GPIO.setup(step_pins,GPIO.OUT)
GPIO.setup(dir_pins,GPIO.OUT)
GPIO.setup(ms_pins,GPIO.OUT)
GPIO.output(ms_pins,microstep_sequence)
d=np.linspace(5,10,5)
steps=int((5*SPR)/4)
print(steps)
#definition serial port for the balance
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600)
time_values=[]
weights=[]
step_count=0
#motor movement function
def move_motor_and_read_weights(direction):
global step_count
GPIO.output(dir_pins,direction)
start_time=time.time()
for x in range(steps):
GPIO.output(step_pins, GPIO.HIGH)
time.sleep(delay)
GPIO.output(step_pins, GPIO.LOW)
#counting the steps when weight is not zero
if weights and weights[-1] != 0:
step_count+=1
end_time=time.time()
#calculation of the linear velocity
actual_time=end_time-start_time
print('time:',actual_time)
linear_velocity=4/(actual_time)
print('linear_velocity(mm/s)=',linear_velocity)
start_bal_time=time.time()
end_bal_time=time.time()+5
while time.time()<end_bal_time:
response=ser.readline()
weight=response.decode().strip()
match=re.search("\d+(\.\d+)?",weight)
if match:
current_time=time.time()-start_bal_time
time_values.append(current_time)
weights.append(float(match.group()))
#print(weights)
else:
print("No match found")
#move upward
move_motor_and_read_weights(CW)
print('total number of steps from the touch point:',step_count)
time.sleep(5)
#move downward
move_motor_and_read_weights(CCW)
#plot
plt.figure(figsize=(10,6))
plt.plot(time_values,weights)
plt.xlabel('time(s)')
plt.ylabel('weights(g)')
plt.show()
GPIO.cleanup()
Statistics: Posted by Maya 1995 — Tue Jan 09, 2024 3:03 pm