Apr-10-2019, 09:55 AM
Hei, we have a project going on at moment where we have to use a flowmeter sensor and a servo motor. The main objective of the servo motor is to open or close a valve, and once it opens the flowmeter will measure the flow. Both of them works perfectly fine separately, but once we merge those two together, the flowmeter works fine, but servo motor wont move, but only vibrate, we found out that we can use threading, but the same issue still appears,
We decided to use an S3003(180 degrees) Servo Motor and an YF-S201 Water Flow Sensor
We have split our coding into three different tabs:
Init:
We decided to use an S3003(180 degrees) Servo Motor and an YF-S201 Water Flow Sensor
We have split our coding into three different tabs:
Init:
from threading import Thread import Servo_Motor # Import Servo_Motor.py function import Flowmeter # Import Flowmeter.py function import time if __name__=='__main__': water = Flowmeter.flow() on = Servo_Motor.on(1) off = Servo_Motor.off(0) while True: Thread(target = water).start() #GPIO.cleanup()Flowmeter:
import RPi.GPIO as GPIO # Used for sending repetitive signals at even intervals by importing a class to control the GPIO on a Raspberry Pi import time # Used for slowing down our Python scipt by delaying import sys # Used for sys.exit() so we can exit from Python GPIO.setmode(GPIO.BCM) # Activate the Broadcom-chip specific pin numbers flowPIN = 17 # Assign GPIO17 Pin 11 to Flow Meter GPIO.setup(flowPIN, GPIO.IN) #Sets Flow meter PIN = 17 as INPUT def flow(): rate_cnt = 0 # Revolution counts (r/min) tot_cnt = 0 # Total counts time_zero = 0.0 # System start up time time_start = 0.0 #Keep measurement beging time time_end = 0.0 # Keep measurement end time gpio_last = 0 # Was last state 0 or 1 or other? pulses = 0 # 0-5 pulses from YF-S201 constant = 1.79 # Water meter calibration factor print('Water flow - Approximate') time_zero = time.time() while True: rate_cnt = 0 # Reset rate counter pulses = 0 # 0-5 pulses from YF-S201 time_start = time.time() # Keep start time while pulses <= 5: # 6 pulses per revolution gpio_cur = GPIO.input(flowPIN) # Poll input if gpio_cur != 0 and gpio_cur != gpio_last: # If the rotor has not stopped pulses += 1 # Count pulses upwards ''' try: None except KeyboardInterrupt: # Look for exit command print("\nCTR + C - Exiting") GPIO.cleanup() # Clean up GPIO print("Done") # Print 'Done' sys.exit() # Exit ''' gpio_last = gpio_cur # Keep last input state rate_cnt += 1 # Revolution / time tot_cnt += 1 # Total revolutions since start time_end = time.time() # End of measurement time print("\nLiters / min ", round((rate_cnt * constant)/(time_end - time_start),2),"approximated") #print("Total liters ", round(tot_cnt*constant,1)) #print("Time (min & clock) ", round((time.time() - time_zero)/60,2), "\t", time.asctime(time.localtime(time.time())),"\n")Servo Motor:
import RPi.GPIO as GPIO # Used for sending repetitive signals at even intervals by importing a class to control the GPIO on a Raspberry Pi import time # Used for slowing down our Python scipt by delaying GPIO.setmode(GPIO.BCM) # Activate the Broadcom-chip specific pin numbers servoPIN = 27 # Assign GPIO17 Pin 11 to servomotor GPIO.setup(servoPIN, GPIO.OUT) # Sets ServoPIN = 17 as OUTPUT p = GPIO.PWM(servoPIN, 50) # Sets servoPIN up with a frequency of 50Hz p.start(2.5) # Set the output to a 25% Duty Cycle def on(x): # Function to open the valve if(x == 1): p.ChangeDutyCycle(7.5) # Set the output to a 75% Duty Cycle time.sleep(0.5) # Delay by 500 milliseconds else: print("Cant open") def off(x): # Function to close the valve if(x == 0): p.ChangeDutyCycle(2.5) # Set the output to a 25% Duty Cycle time.sleep(0.5) # Delay by 500 milliseconds else: print("Cant close") ''' def keyboard_interrupt(): p.stop() # Stop PWM GPIO.cleanup() # Gargbage collecting (Release any resources our script uses) '''Our question is, why can`t we run those two at the same time?