def gpio_set_stepper(cmd, message, recdata, stepper_data): msgarray = message.split(":") messagelen = len(msgarray) Interface = msgarray[1] Interface_Number = int( Interface) # this is the interface on same I2C board direction = msgarray[2] speed = int(msgarray[3]) steps = int(msgarray[4]) # following instructions have been removed as in case there is cuncurrency, then better not to wait, automation will eventually repeat the command #waitstep=0.1 #waittime=0 #maxwait=2.5 #while (read_status_data(stepper_data,Interface,"busyflag")==True)and(waittime<maxwait): # time.sleep(waitstep) # waittime=waittime+waitstep if read_status_data(stepper_data, Interface, "busyflag"): # check how long the busyflag has been True lasttime = read_status_data(stepper_data, Interface, "busyflagtime") deltat = datetime.datetime.utcnow() - lasttime if deltat.total_seconds() > 600: # 600 seconds = 10 minutes # someting wrong, try to reset the stepper controller logger.warning( "Stepper busy status Time exceeded, reset stepper controller: %s ************", Interface) write_status_data(stepper_data, Interface, "busyflag", False) #reset mh = Adafruit_MotorHAT() mh.reset() else: print("Stepper busy ") logger.warning("Stepper Busy, not proceeding with stepper: %s", Interface) return False write_status_data(stepper_data, Interface, "busyflag", True) write_status_data(stepper_data, Interface, "busyflagtime", datetime.datetime.utcnow()) # stepper is no busy, proceed # try: # create a default object, no changes to I2C address or frequency mh = Adafruit_MotorHAT() # set motor parameters myStepper = mh.getStepper( 200, Interface_Number) # 200 steps/rev, motor port #1 or #2 myStepper.setSpeed(speed) # 30 RPM #print "Double coil steps" if direction == "FORWARD": myStepper.step(steps, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE) elif direction == "BACKWARD": myStepper.step(steps, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.DOUBLE) # turn off motors mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE) del mh try: write_status_data(stepper_data, Interface, "busyflag", False) except: print("problem I2C stepper controller") logger.error("problem I2C stepper controller") write_status_data(stepper_data, Interface, "busyflag", False) return False #print "stepper: Interface", Interface_Number , " direction=", direction , " speed=", speed , " steps=", steps recdata.append(cmd) recdata.append(Interface_Number) return True
def gpio_set_stepper(cmd, message, recdata, stepper_data): msgarray = message.split(":") messagelen = len(msgarray) Interface = msgarray[1] Interface_Number = int( Interface) # this is the interface on same I2C board direction = msgarray[2] speed = int(msgarray[3]) steps = int(msgarray[4]) waitstep = 0.1 waittime = 0 maxwait = 2.5 while (read_status_data(stepper_data, Interface, "busyflag") == True) and (waittime < maxwait): time.sleep(waitstep) waittime = waittime + waitstep print "Stepper wait time -----> ", waittime if (waittime >= maxwait): #something wrog, wait too long, avoid initiate further processing # check how long the busyflag has been True lasttime = read_status_data(stepper_data, Interface, "busyflagtime") deltat = datetime.datetime.utcnow() - lasttime if deltat.total_seconds() > 600: # 600 seconds = 10 minutes # someting wrong, try to reset the stepper controller logger.warning( "Stepper busy status Time exceeded, reset stepper controller: %s ************", Interface) write_status_data(stepper_data, Interface, "busyflag", False) #reset mh = Adafruit_MotorHAT() mh.reset() else: print "Stepper wait time EXCEEDED " logger.warning( "Stepper Wait Time exceeded, not proceeding with stepper: %s", Interface) return False write_status_data(stepper_data, Interface, "busyflag", True) write_status_data(stepper_data, Interface, "busyflagtime", datetime.datetime.utcnow()) # stepper is no busy, proceed # create a default object, no changes to I2C address or frequency mh = Adafruit_MotorHAT() # set motor parameters myStepper = mh.getStepper( 200, Interface_Number) # 200 steps/rev, motor port #1 or #2 myStepper.setSpeed(speed) # 30 RPM print "Double coil steps" if direction == "FORWARD": myStepper.step(steps, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE) elif direction == "BACKWARD": myStepper.step(steps, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.DOUBLE) print "stepper: Interface", Interface_Number, " direction=", direction, " speed=", speed, " steps=", steps recdata.append(cmd) recdata.append(Interface_Number) # turn off motors mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE) del mh write_status_data(stepper_data, Interface, "busyflag", False) return True