print("Starting dual_spin script.")
import math, time
import board
import pwmio
from digitalio import DigitalInOut, Direction, Pull
#--------------------------------------------------------------------------------
# Class to represent a single dual H-bridge driver.
class DRV8833():
def __init__(self, AIN1=board.GP18, AIN2=board.GP19, BIN2=board.GP20, BIN1=board.GP21, CIN1=board.GP6, CIN2=board.GP11, pwm_rate=20000):
# right paddle
self.ain1 = pwmio.PWMOut(AIN1, duty_cycle=0, frequency=pwm_rate)
self.ain2 = pwmio.PWMOut(AIN2, duty_cycle=0, frequency=pwm_rate)
# middle gear
self.bin1 = pwmio.PWMOut(BIN1, duty_cycle=0, frequency=pwm_rate)
self.bin2 = pwmio.PWMOut(BIN2, duty_cycle=0, frequency=pwm_rate)
# left paddle
self.cin1 = pwmio.PWMOut(CIN1, duty_cycle=0, frequency=pwm_rate)
self.cin2 = pwmio.PWMOut(CIN2, duty_cycle=0, frequency=pwm_rate)
def write(self, channel, rate):
"""Set the speed and direction on a single motor channel.
:param channel: 0 for motor A, 1 for motor B
:param rate: modulation value between -1.0 and 1.0, full reverse to full forward."""
# convert the rate into a 16-bit fixed point integer
pwm = min(max(int(2**16 * abs(rate)), 0), 65535)
if channel == 0:
if rate < 0:
self.ain1.duty_cycle = pwm
self.ain2.duty_cycle = 0
else:
self.ain1.duty_cycle = 0
self.ain2.duty_cycle = pwm
elif channel == 1:
if rate < 0:
self.bin1.duty_cycle = pwm
self.bin2.duty_cycle = 0
else:
self.bin1.duty_cycle = 0
self.bin2.duty_cycle = pwm
else:
if rate < 0:
self.cin1.duty_cycle = pwm
self.cin2.duty_cycle = 0
else:
self.cin1.duty_cycle = 0
self.cin2.duty_cycle = pwm
#--------------------------------------------------------------------------------
# Create an object to represent a dual motor driver.
print("Creating driver object.")
driver = DRV8833()
led = DigitalInOut(board.LED) # GP25
led.direction = Direction.OUTPUT
# for LEFT sensor:
left_sensor_switch = DigitalInOut(board.GP15) # left sensor
left_sensor_switch.direction = Direction.INPUT
left_sensor_last_value = False
# for RIGHT sensor:
right_sensor_switch = DigitalInOut(board.GP14) # left sensor
right_sensor_switch.direction = Direction.INPUT
right_sensor_last_value = False
ball_sensor_1 = DigitalInOut(board.GP13)
ball_sensor_1.direction = Direction.INPUT
ball_sensor_1_last_value = True
ball_sensor_2 = DigitalInOut(board.GP12)
ball_sensor_2.direction = Direction.INPUT
ball_sensor_2_last_value = True
#--------------------------------------------------------------------------------
def move_gear():
# channel == 1
driver.write(1, 1)
time.sleep(1.0)
driver.write(1, 0.0)
time.sleep(1.0)
driver.write(1, -1)
time.sleep(1.0)
driver.write(1, 0.0)
time.sleep(1.0)
print("Starting main script.")
right_paddle=1 #right paddle direction
left_paddle=1 #left paddle direction
middle = 1 #middle gear direction
speed = 0.70
counter = 0
balls_detected = 0;
while True:
balls_detected = 0;
driver.write(1, middle*speed) #spin gear
driver.write(0, right_paddle*speed) #spin right paddle
driver.write(2, left_paddle*speed) #spin left paddle
if right_sensor_switch.value is True:
if right_sensor_last_value is False:
right_sensor_last_value = True
else:
if right_sensor_last_value is True:
right_sensor_last_value = False
right_paddle= right_paddle * -1 #switch direction of right paddle
if left_sensor_switch.value is True:
if left_sensor_last_value is False:
left_sensor_last_value = True
else:
if left_sensor_last_value is True:
left_sensor_last_value = False
left_paddle= left_paddle * -1 #switch direction of left paddle
counter = counter + 1
if counter is 5000:
counter = 0
middle = middle * -1 #switch direction of gear
if ball_sensor_1.value is False:
balls_detected = balls_detected + 1
if ball_sensor_2.value is False:
balls_detected = balls_detected + 1
if balls_detected is 0:
speed = 0.75
if balls_detected is 1:
speed = 0.85
if balls_detected is 2:
speed = 1
Karen Abruzzo, Emilie Zhou
Description
Our intent was to create a game where users can drop marbles from the top of the tile and earn a different number of points based on where the marble lands on the bottom of the tile. There are seven different dips of varying sizes where marbles can fall into. The dip in the middle is the largest and the sizes of the dips are smaller the farther they are from the center. Other components, such as a spinning gear and rotating paddles, will affect the way the marble falls as well. The toothed gear is constantly spinning but changes directions every few seconds. The two paddles rotate left and right and have sensors that detect when a marble has landed on it which then causes the paddles to change the direction that they’re rotating in. There are also two other sensors above the end dips that cause the paddles to rotate faster if a marble lands in either of those dips. The final outcome is a game where users have to time when they drop the marbles in order to try and get marbles in all the holes on the bottom while accounting for the other various moving components that the marbles will come into contact with.
Leave a Reply
You must be logged in to post a comment.