Ejemplo n.º 1
0
	def __init__(self, pwm0, pwm1, pwm2, pwm3):
		"""
		"""
		self.mux = MCP230XX(0x20, 16, 1)

		for pin in range(0, 15):
			self.mux.config(pin, OUT)

		# this can be:
		# BOARD -> Board numbering scheme. The pin numbers follow the pin numbers on header P1.
		# BCM -> Broadcom chip-specific pin numbers.
# 		GPIO.setmode(GPIO.BCM)
		setmode(BCM)  # Pi cover uses BCM pin numbers, GPIO.BCM = 11
		setup([pwm0, pwm1, pwm2, pwm3], OUT)  # GPIO.OUT = 0

		freq = 100.0  # Hz
		self.motor0 = PWM(pwm0, freq)
		self.motor1 = PWM(pwm1, freq)
		self.motor2 = PWM(pwm2, freq)
		self.motor3 = PWM(pwm3, freq)

		self.motor0.start(0)
		self.motor1.start(0)
		self.motor2.start(0)
		self.motor3.start(0)
 def __init__(self):
     # Initialize lock servo and button.
     self.servo = PWM.Servo()
     RPIO.setup(config.BUTTON_PIN, RPIO.IN)
     # Set initial box state.
     self.button_state = RPIO.input(config.BUTTON_PIN)
     self.is_locked = None
Ejemplo n.º 3
0
 def play_negado(self):
     # Inicia buzzer
     #GPIO.setup(self.gpio, GPIO.OUT)
     buzz = PWM(self.porta, 131)  # initial frequency.
     buzz.start(50)  # Start buzzer pin with 50% duty ration
     time.sleep(0.2)  # delay a note for beat * 0.5s
     buzz.ChangeFrequency(101)  # Change the frequency along the song note
     time.sleep(0.7)  # delay a note for beat * 0.5s
     buzz.stop()  # Stop the buzzer
     super().ligar_porta()  # Set buzzer pin to High
Ejemplo n.º 4
0
def led_breathe(pin_no):
    """
	Fn to vary the duty cycle to   
	dim/brighten the leds
	"""
    pwm = PWM(pin_no, 100)  # create object for PWM on port pin_no at 100 Hertz
    pwm.start(0)  # start led on 0 percent duty cycle (off)
    for i in range(101):
        pwm.ChangeDutyCycle(
            i)  # Increase duty cycle from 0% to 100% (step by 1)
        time.sleep(0.03)
    for i in range(100, -1, -1):
        pwm.ChangeDutyCycle(
            i)  # Decrease duty cycle from 0% to 100% (step by 1)
        time.sleep(0.03)
    time.sleep(1)
    pwm.stop()  # stop the PWM output
Ejemplo n.º 5
0
    def __init__(self, pwm0, pwm1, pwm2, pwm3, a0, b0, a1, b1):
        """
		"""
        # self.mux = MCP230XX(0x20, 16, 1)
        #
        # for pin in range(0, 15):
        # 	self.mux.config(pin, GPIO.OUT)

        # this can be:
        # BOARD -> Board numbering scheme. The pin numbers follow the pin numbers on header P1.
        # BCM -> Broadcom chip-specific pin numbers.
        GPIO.setmode(GPIO.BCM)  # Pi cover uses BCM pin numbers, GPIO.BCM = 11
        GPIO.setup([pwm0, pwm1, pwm2, pwm3], GPIO.OUT)  # GPIO.OUT = 0

        freq = 100.0  # Hz
        self.motor0 = PWM(pwm0, freq)
        self.motor1 = PWM(pwm1, freq)
        self.motor2 = PWM(pwm2, freq)
        self.motor3 = PWM(pwm3, freq)

        self.motor0.start(0)
        self.motor1.start(0)
        self.motor2.start(0)
        self.motor3.start(0)

        self.ctl = [a0, b0, a1, b1]

        GPIO.setup([a0, b0, a1, b1], GPIO.OUT)  # GPIO.OUT = 0
        # self.a0 = a0
        # self.b0 = b0
        # self.a1 = a1
        # self.b1 = b1

        # GPIO.output(self.a0, GPIO.LOW)
        # GPIO.output(self.b0, GPIO.LOW)
        # GPIO.output(self.a1, GPIO.LOW)
        # GPIO.output(self.a1, GPIO.LOW)

        for pin in self.ctl:
            GPIO.output(pin, GPIO.LOW)
Ejemplo n.º 6
0

def stop(duty=0):
    straight()
    output(20, LOW)
    output(22, LOW)
    return default_response()


def default(duty=0):
    print "Command not found"

stop(0)
straight(0)

p = PWM(17, 90)
p2 = PWM(13, 90)
p.start(50)
p2.start(100)

commands_dict = {"b": backward,
                 "f": forward,
                 "s": stop,
                 "r": right,
                 "l": left,
                 "st": straight}


def move(request):
    commands_dict.get(request.matchdict['direction'], default)()
    return default_response()
Ejemplo n.º 7
0
 def __stop(pwm: GPIO.PWM):
     pwm.ChangeDutyCycle(0)
Ejemplo n.º 8
0
 def turn(self, pwm_turn: GPIO.PWM):
     self.__resetTurnPWMS()
     pwm_turn.ChangeDutyCycle(turn_duty)
     time.sleep(turn_slp_interval)
     pwm_turn.ChangeDutyCycle(0)
Ejemplo n.º 9
0
class MotorDriver(object):
	"""
	This uses RPI.GPIO
	"""
	STOP    = 0
	FORWARD = 1
	REVERSE = 2
	COAST   = 3

	def __init__(self, pwm0, pwm1, pwm2, pwm3):
		"""
		"""
		self.mux = MCP230XX(0x20, 16, 1)

		for pin in range(0, 15):
			self.mux.config(pin, OUT)

		# this can be:
		# BOARD -> Board numbering scheme. The pin numbers follow the pin numbers on header P1.
		# BCM -> Broadcom chip-specific pin numbers.
# 		GPIO.setmode(GPIO.BCM)
		setmode(BCM)  # Pi cover uses BCM pin numbers, GPIO.BCM = 11
		setup([pwm0, pwm1, pwm2, pwm3], OUT)  # GPIO.OUT = 0

		freq = 100.0  # Hz
		self.motor0 = PWM(pwm0, freq)
		self.motor1 = PWM(pwm1, freq)
		self.motor2 = PWM(pwm2, freq)
		self.motor3 = PWM(pwm3, freq)

		self.motor0.start(0)
		self.motor1.start(0)
		self.motor2.start(0)
		self.motor3.start(0)

	def __del__(self):
		print('motor drive ... bye')
		self.allStop()
		self.motor0.stop()
		self.motor1.stop()
		self.motor2.stop()
		self.motor3.stop()
		cleanup()

	def clamp(self, x):
		"""
		Clamps a PWM from 0-100 and puts it into the right servo usec timing.
		"""
		minimum = 0
		maximum = 100
		if x == 0: return 0  # really stop motor
		return max(minimum, min(x, maximum))

	def setMotors(self, m0, m1, m2, m3):
		"""
		Takes 4 dicts (shown below) for each motor.

		dict: {'dir': x, 'duty': 0-100}

		MotorDriver.STOP    = 0
		MotorDriver.FORWARD = 1
		MotorDriver.REVERSE = 2
		MotorDriver.COAST   = 3
		"""
		# if not 'dir' in m0 or not 'duty' in m0: return
		# low = 0
		# high = 100

		print(m0, m1, m2, m3)

		# set mux
		val = m3['dir'] << 6 | m2['dir'] << 4 | m1['dir'] << 2 | m0['dir']
		self.mux.write8(val)

		# set pwm
		pwm = self.clamp(m0['duty'])
		self.motor0.ChangeDutyCycle(pwm)

		pwm = self.clamp(m1['duty'])
		self.motor1.ChangeDutyCycle(pwm)

		pwm = self.clamp(m2['duty'])
		self.motor2.ChangeDutyCycle(pwm)

		pwm = self.clamp(m3['duty'])
		self.motor3.ChangeDutyCycle(pwm)

	def allStop(self):
		"""
		"""
		self.mux.write8(0)

		stop = {'dir': 0, 'duty': 0}
		self.setMotors(stop, stop, stop, stop)
Ejemplo n.º 10
0
class MotorDriver(object):
    """
	This uses RPI.GPIO
	"""
    STOP = 0
    FORWARD = 1
    REVERSE = 2
    COAST = 3

    def __init__(self, pwm0, pwm1, pwm2, pwm3, a0, b0, a1, b1):
        """
		"""
        # self.mux = MCP230XX(0x20, 16, 1)
        #
        # for pin in range(0, 15):
        # 	self.mux.config(pin, GPIO.OUT)

        # this can be:
        # BOARD -> Board numbering scheme. The pin numbers follow the pin numbers on header P1.
        # BCM -> Broadcom chip-specific pin numbers.
        GPIO.setmode(GPIO.BCM)  # Pi cover uses BCM pin numbers, GPIO.BCM = 11
        GPIO.setup([pwm0, pwm1, pwm2, pwm3], GPIO.OUT)  # GPIO.OUT = 0

        freq = 100.0  # Hz
        self.motor0 = PWM(pwm0, freq)
        self.motor1 = PWM(pwm1, freq)
        self.motor2 = PWM(pwm2, freq)
        self.motor3 = PWM(pwm3, freq)

        self.motor0.start(0)
        self.motor1.start(0)
        self.motor2.start(0)
        self.motor3.start(0)

        self.ctl = [a0, b0, a1, b1]

        GPIO.setup([a0, b0, a1, b1], GPIO.OUT)  # GPIO.OUT = 0
        # self.a0 = a0
        # self.b0 = b0
        # self.a1 = a1
        # self.b1 = b1

        # GPIO.output(self.a0, GPIO.LOW)
        # GPIO.output(self.b0, GPIO.LOW)
        # GPIO.output(self.a1, GPIO.LOW)
        # GPIO.output(self.a1, GPIO.LOW)

        for pin in self.ctl:
            GPIO.output(pin, GPIO.LOW)

    def __del__(self):
        print('motor drive ... bye')
        self.allStop()
        self.motor0.stop()
        self.motor1.stop()
        self.motor2.stop()
        self.motor3.stop()
        GPIO.cleanup()

    def clamp(self, x, minimum=0, maximum=100):
        """
		Clamps a PWM from 0-100 and puts it into the right servo usec timing.
		"""
        # minimum = 0
        # maximum = 100
        return 0 if x == 0 else max(minimum, min(x, maximum))

    def setMotors(self, m0, m1, m2, m3):
        """
		Takes 4 tuples (shown below) for each motor.

		tuple: (dir, duty)

		MotorDriver.STOP    = 0
		MotorDriver.FORWARD = 1
		MotorDriver.REVERSE = 2
		MotorDriver.COAST   = 3
		"""
        # if not 'dir' in m0 or not 'duty' in m0: return
        # low = 0
        # high = 100

        # print(m0, m1, m2, m3)

        # set mux
        # val = m3['dir'] << 6 | m2['dir'] << 4 | m1['dir'] << 2 | m0['dir']
        # val = m3[0] << 6 | m2[0] << 4 | m1[0] << 2 | m0[0]
        # self.mux.write8(val)
        # GPIO.output(self.a0, m0[0])
        # GPIO.output(self.b0, m1[0])
        # GPIO.output(self.a1, m2[0])
        # GPIO.output(self.b1, m3[0])
        #
        # # set pwm
        # pwm = self.clamp(m0[1])
        # self.motor0.ChangeDutyCycle(pwm)
        #
        # pwm = self.clamp(m1[1])
        # self.motor1.ChangeDutyCycle(pwm)
        #
        # pwm = self.clamp(m2[1])
        # self.motor2.ChangeDutyCycle(pwm)
        #
        # pwm = self.clamp(m3[1])
        # self.motor3.ChangeDutyCycle(pwm)

        for pin, cmd, motor in zip(
                self.ctl, [m0, m1, m2, m3],
            [self.motor0, self.motor1, self.motor2, self.motor3]):
            GPIO.output(pin, cmd[0])
            pwm = self.clamp(cmd[1])
            motor.ChangeDutyCycle(pwm)

    def allStop(self):
        """
		"""
        # self.mux.write8(0)
        # GPIO.output(self.a0, GPIO.LOW)
        # GPIO.output(self.b0, GPIO.LOW)
        # GPIO.output(self.a1, GPIO.LOW)
        # GPIO.output(self.a1, GPIO.LOW)
        for pin in self.ctl:
            GPIO.output(pin, GPIO.LOW)

        stop = (0, 0)
        self.setMotors(stop, stop, stop, stop)