def __init__(self, *args): util.traceCall() if len(args) == 1 and isinstance(args[0], dict): self.__dict__ = args[0] elif len(args) == 3: name, pinF, pinB = args self.name = name self._pinForward = pinF self._pinBackward = pinB # Set the GPIO modes util.trace("set the GPIO mode and the pins " + str(self._pinForward) + " and " + str(self._pinBackward)) if self._nb_instances == 0: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(self._pinForward, GPIO.OUT) GPIO.setup(self._pinBackward, GPIO.OUT) # Set the GPIO to software PWM at 'Frequency' Hertz self._pwmMotorForward = GPIO.PWM(self._pinForward, self._frequency) self._pwmMotorBackward = GPIO.PWM(self._pinBackward, self._frequency) # Start the software PWM with a duty cycle of 0 (i.e. not moving) self._pwmMotorForward.start(self._stopCycle) self._pwmMotorBackward.start(self._stopCycle) # end of instance initialization self._nb_instances += 1
def motorStop(self): util.traceCall() if util.getDebug() > 0: return util.trace("_pwmMotorForward.ChangeDutyCycle to stop") util.trace("_pwmMotorBackward.ChangeDutyCycle to stop") self._pwmMotorForward.ChangeDutyCycle(self._stopCycle) self._pwmMotorBackward.ChangeDutyCycle(self._stopCycle)
def isOverBlack(): # If the sensor is Low (=0), it's above the black line if GPIO.input(pinLineFollower) == 0: util.trace("The sensor is seeing a black surface") return True # If not (else), print the following else: util.trace("The sensor is seeing a white surface") return False
def init(): util.trace("lib_motors_base.init") # Set the GPIO modes GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Set the GPIO Pin mode for x in range(pinMotorLeftBackwards, pinMotorRightForwards + 1): util.trace("set pin " + str(x)) GPIO.setup(x, GPIO.OUT)
def setSpeed(self, v): util.traceCall() if v <= self.getMaxSpeed(): if self._curSpeed != v: # apply the new speed self._curSpeed = v self.motorUpdate() else: util.trace("The speed was already " + v) else: util.trace("The speed must be lower than " + self.getMaxSpeed())
def setWay(self, way): util.traceCall() if way in self._way: if self._curWay != way: # apply the new way self._curWay = way self.motorStop() self.motorUpdate() else: util.trace("The way was already " + way) else: util.trace("The way must be in " + str(self._way))
def leftMotorSpeed(speed_f, speed_b): util.trace("lib_motors_evol.leftMotorSpeed with speed_f=" + str(speed_f) + " and speed_b=" + str(speed_b)) if util.getDebug() > 0: return if 1 <= speed_f <= speed_max: pwmMotorLeftForwards.ChangeDutyCycle(DutyCycleLeft * speed_f) else: pwmMotorLeftForwards.ChangeDutyCycle(Stop) if 1 <= speed_b <= speed_max: pwmMotorLeftBackwards.ChangeDutyCycle(DutyCycleLeft * speed_b) else: pwmMotorLeftBackwards.ChangeDutyCycle(Stop)
def startRightMotor(speed): util.trace("lib_motors_evol.startRightMotor with speed=" + str(speed)) s = speed if speed == 0: rightMotorSpeed(0, 0) elif speed > 0: if speed > speed_max: s = speed_max rightMotorSpeed(s, 0) else: if speed < -speed_max: s = -speed_max rightMotorSpeed(0, s)
def __init__(self, *args): util.traceCall() if len(args) == 1 and isinstance(args[0], dict): self.__dict__ = args[0] elif len(args) == 3: name = args self.name = name # Define the 2 wheels util.trace("Define the 2 wheels") self._wheel_left = motor.Motor("left_wheel", 8, 7) self._wheel_right = motor.Motor("right_wheel", 10, 9) # end of instance initialization self._nb_instances += 1
def init(): util.trace("lib_motors_evol.init") base.init() # Declare the PWM variables global pwmMotorLeftForwards global pwmMotorLeftBackwards global pwmMotorRightForwards global pwmMotorRightBackwards # Set the GPIO to software PWM at 'Frequency' Hertz pwmMotorLeftForwards = GPIO.PWM(base.pinMotorLeftForwards, Frequency) pwmMotorLeftBackwards = GPIO.PWM(base.pinMotorLeftBackwards, Frequency) pwmMotorRightForwards = GPIO.PWM(base.pinMotorRightForwards, Frequency) pwmMotorRightBackwards = GPIO.PWM(base.pinMotorRightBackwards, Frequency) # Start the software PWM with a duty cycle of 0 (i.e. not moving) pwmMotorLeftForwards.start(Stop) pwmMotorLeftBackwards.start(Stop) pwmMotorRightForwards.start(Stop) pwmMotorRightBackwards.start(Stop)
def motorUpdate(self): util.traceCall() if util.getDebug() > 0: return if self._curWay == 0: # forward util.trace("_pwmMotorForward.ChangeDutyCycle to " + str(self._dutyCycle * self._curSpeed)) util.trace("_pwmMotorBackward.ChangeDutyCycle to stop") self._pwmMotorForward.ChangeDutyCycle(self._dutyCycle * self._curSpeed) self._pwmMotorBackward.ChangeDutyCycle(self._stopCycle) else: # backward util.trace("_pwmMotorForward.ChangeDutyCycle to stop") util.trace("_pwmMotorBackward.ChangeDutyCycle to " + str(self._dutyCycle * self._curSpeed)) self._pwmMotorForward.ChangeDutyCycle(self._stopCycle) self._pwmMotorBackward.ChangeDutyCycle(self._dutyCycle * self._curSpeed)
def left(): util.trace("lib_motors_base.left") stopLeftMotor() forwardsRightMotor()
def backwards(): util.trace("lib_motors_base.backwards") backwardsRightMotor() backwardsLeftMotor()
def forwards(): util.trace("lib_motors_base.forwards") forwardsRightMotor() forwardsLeftMotor()
def backwardsLeftMotor(): util.trace("lib_motors_base.backwardsLeftMotor") GPIO.output(pinMotorLeftForwards, 0) if util.getDebug == 0: GPIO.output(pinMotorLeftBackwards, 1)
def forwardsRightMotor(): util.trace("lib_motors_base.forwardsRightMotor") if util.getDebug == 0: GPIO.output(pinMotorRightForwards, 1) GPIO.output(pinMotorRightBackwards, 0)
def end(): util.trace("lib_motors_evol.end") stopMotors() base.end()
def backwards(): util.trace("lib_motors_evol.backwards") leftMotorBackwards() rightMotorBackwards()
def right(): util.trace("lib_motors_evol.right") leftMotorForwards() stopRightMotor()
def end(): util.trace("lib_motors_base.end") stopMotors() # Reset the GPIO pins (turn off motors too) GPIO.cleanup()
def rightStay(): util.trace("lib_motors_base.rightStay") forwardsLeftMotor() backwardsRightMotor()
def right(): util.trace("lib_motors_base.right") forwardsLeftMotor() stopRightMotor()
def stopRightMotor(): util.trace("lib_motors_evol.stopRightMotor") if util.getDebug() > 0: return pwmMotorRightForwards.ChangeDutyCycle(Stop) pwmMotorRightBackwards.ChangeDutyCycle(Stop)
def setSpeed(v): if v < -speed_max or v > speed_max: util.trace("The speed must be in range " + str(-speed_max) + ".." + str(speed_max)) else: my_speed = v
def left(): util.trace("lib_motors_evol.left") stopLeftMotor() rightMotorForwards()
def stopRightMotor(): util.trace("lib_motors_base.stopRightMotor") GPIO.output(pinMotorRightForwards, 0) GPIO.output(pinMotorRightBackwards, 0)
def move(speed_l, speed_r): util.trace("lib_motors_evol.move with speed_l=" + str(speed_l) + " and speed_r=" + str(speed_r)) startLeftMotor(speed_l) startRightMotor(speed_r)
def stopLeftMotor(): util.trace("lib_motors_base.stopLeftMotor") GPIO.output(pinMotorLeftForwards, 0) GPIO.output(pinMotorLeftBackwards, 0)
def stopMotors(): util.trace("lib_motors_base.stopMotors") stopRightMotor() stopLeftMotor()
def rightStay(): util.trace("lib_motors_evol.rightStay") leftMotorForwards() rightMotorBackwards()