def Run(self, duration, newPosition, speed, direction): #TODO https://www.google.com/search?q=pass+object+to+python+function&rlz=1C1GCEA_enUS892US892&oq=pass+object+to+python+function&aqs=chrome..69i57.5686j0j7&sourceid=chrome&ie=UTF-8 #TODO https://stackoverflow.com/questions/20725699/how-do-i-pass-instance-of-an-object-as-an-argument-in-a-function-in-python """ Run an actuator for a given number of milliseconds to a given position at percentage of max speed in FORWARD or BACKWARDS direction Key arguments: duration - Time actuator is in motion, for Servo() objects this can be used to control speed of movement newPosition - New position between -1 and 1 that actuator should move to speed - Speed at which actuator moves at, for Servo() objects this parameter is NOT used direction - Set counter-clockwise (CCW or LINEAR_IN) or clockwise (CW or LINEAR_OUT) as the forward direction Return Value: NOTHING """ print("Actuator.py Run() function started!") if(type == "S"): currentPosition = self.actuatorObject.value() if(currentPosition < (newPosition - Actuator.SERVO_SLACK)): self.actuatorObject.max() #TODO THIS MAY NOT STOP AND GO ALL THE WAY TO MAX POS elif(currentPosition > (newPosition - Actuator.SERVO_SLACK)): self.actuatorObject.min() #TODO THIS MAY NOT STOP AND GO ALL THE WAY TO MIN POS else: # NEAR to new position DO NOTHING self.actuatotObject.dettach() elif(type == "M"): #TODO Write motor control code Motor.enable() #TODO CHANGE TO self.acutatorObject currentPosition = actuatorObject.value while(currentPosition != newPosition): if(actuatorObject.forwardDirection == Actuator.CW): Motor.forward(speed) else: Motor.reverse(speed) currentPosition = self.actuatorObject.value sleep(duration) #TODO signal.pause(duration) Motor.disable() elif(type == "R"): relay.on() sleep(duration) #TODO signal.pause(duration) relay.off() else: self.DebugObect.Dprint("INVALID Actutator Type sent to Run method, please use S, M, R as first parameter to Actuator() Object") self.DebugObject.Dprint("Run function completed!")
from gpiozero import Motor import pygame from pygame.locals import * motor = Motor(2, 3) motor.enable() done = false while not done: event = pygame.event.poll() if event.type == pygame.QUIT: done = true elif event.type == pygame.KEYUP: if event.key == pygame.K_w: motor.value = 0 if event.key == pygame.K_s: motor.value = 0 else: keys = pygame.key.get_pressed() if keys[pygame.K_w]: motor.forward() elif keys[pygame.K_s]: motor.backwards() elif keys[pygame.K_SPACE]: motor.stop()