{"id":5059,"date":"2021-10-17T20:47:51","date_gmt":"2021-10-18T00:47:51","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/?p=5059"},"modified":"2021-10-18T10:19:18","modified_gmt":"2021-10-18T14:19:18","slug":"spinning-marble-game","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/2021\/10\/17\/spinning-marble-game\/","title":{"rendered":"Spinning Marble Game"},"content":{"rendered":"\n<div class=\"wp-block-buttons\"><\/div>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-content\/uploads\/2021\/10\/drive-download-20211018T002243Z-001.zip\">drive-download-20211018T002243Z-001<\/a><a href=\"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-content\/uploads\/2021\/10\/drive-download-20211018T002243Z-001.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-content\/uploads\/2021\/10\/IMG_6654-6.mov\"><\/video><\/figure>\n\n\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;Starting dual_spin script.&quot;)\nimport math, time\nimport board\nimport pwmio\nfrom digitalio import DigitalInOut, Direction, Pull\n#--------------------------------------------------------------------------------\n# Class to represent a single dual H-bridge driver.\n\nclass DRV8833():\n    def __init__(self, AIN1=board.GP18, AIN2=board.GP19, BIN2=board.GP20, BIN1=board.GP21, CIN1=board.GP6, CIN2=board.GP11, pwm_rate=20000):\n        # right paddle\n        self.ain1 = pwmio.PWMOut(AIN1, duty_cycle=0, frequency=pwm_rate)\n        self.ain2 = pwmio.PWMOut(AIN2, duty_cycle=0, frequency=pwm_rate)\n\n        # middle gear\n        self.bin1 = pwmio.PWMOut(BIN1, duty_cycle=0, frequency=pwm_rate)\n        self.bin2 = pwmio.PWMOut(BIN2, duty_cycle=0, frequency=pwm_rate)\n\n         # left paddle\n        self.cin1 = pwmio.PWMOut(CIN1, duty_cycle=0, frequency=pwm_rate)\n        self.cin2 = pwmio.PWMOut(CIN2, duty_cycle=0, frequency=pwm_rate)\n\n    def write(self, channel, rate):\n        &quot;&quot;&quot;Set the speed and direction on a single motor channel.\n\n        :param channel:  0 for motor A, 1 for motor B\n        :param rate: modulation value between -1.0 and 1.0, full reverse to full forward.&quot;&quot;&quot;\n\n        # convert the rate into a 16-bit fixed point integer\n        pwm = min(max(int(2**16 * abs(rate)), 0), 65535)\n\n        if channel == 0:\n            if rate &lt; 0:\n                self.ain1.duty_cycle = pwm\n                self.ain2.duty_cycle = 0\n            else:\n                self.ain1.duty_cycle = 0\n                self.ain2.duty_cycle = pwm\n        elif channel == 1:\n            if rate &lt; 0:\n                self.bin1.duty_cycle = pwm\n                self.bin2.duty_cycle = 0\n            else:\n                self.bin1.duty_cycle = 0\n                self.bin2.duty_cycle = pwm\n        else:\n            if rate &lt; 0:\n                self.cin1.duty_cycle = pwm\n                self.cin2.duty_cycle = 0\n            else:\n                self.cin1.duty_cycle = 0\n                self.cin2.duty_cycle = pwm\n\n#--------------------------------------------------------------------------------\n# Create an object to represent a dual motor driver.\nprint(&quot;Creating driver object.&quot;)\ndriver = DRV8833()\n\nled = DigitalInOut(board.LED)  # GP25\nled.direction = Direction.OUTPUT\n\n# for LEFT sensor:\nleft_sensor_switch = DigitalInOut(board.GP15) # left sensor\nleft_sensor_switch.direction = Direction.INPUT\nleft_sensor_last_value = False\n\n\n# for RIGHT sensor:\nright_sensor_switch = DigitalInOut(board.GP14) # left sensor\nright_sensor_switch.direction = Direction.INPUT\nright_sensor_last_value = False\n\nball_sensor_1  = DigitalInOut(board.GP13)\nball_sensor_1.direction = Direction.INPUT\nball_sensor_1_last_value = True\n\nball_sensor_2  = DigitalInOut(board.GP12)\nball_sensor_2.direction = Direction.INPUT\nball_sensor_2_last_value = True\n#--------------------------------------------------------------------------------\n\ndef move_gear():\n    # channel == 1\n    driver.write(1, 1)\n    time.sleep(1.0)\n\n    driver.write(1, 0.0)\n    time.sleep(1.0)\n\n    driver.write(1, -1)\n    time.sleep(1.0)\n\n    driver.write(1, 0.0)\n    time.sleep(1.0)\n\nprint(&quot;Starting main script.&quot;)\n\nright_paddle=1  #right paddle direction\nleft_paddle=1 #left paddle direction\nmiddle = 1 #middle gear direction\nspeed = 0.70\ncounter  = 0\nballs_detected  = 0;\nwhile True:\n    balls_detected  = 0;\n    driver.write(1, middle*speed) #spin gear\n    driver.write(0, right_paddle*speed) #spin right paddle\n    driver.write(2, left_paddle*speed) #spin left paddle\n    if right_sensor_switch.value is True:\n        if right_sensor_last_value is False:\n            right_sensor_last_value = True\n\n\n    else:\n        if right_sensor_last_value is True:\n            right_sensor_last_value = False\n            right_paddle=  right_paddle *  -1 #switch direction of right paddle\n\n    if left_sensor_switch.value is True:\n        if left_sensor_last_value is False:\n            left_sensor_last_value = True\n\n\n\n    else:\n        if left_sensor_last_value is True:\n            left_sensor_last_value = False\n            left_paddle=  left_paddle *  -1 #switch direction of  left paddle\n    counter = counter +  1\n    if counter is 5000:\n        counter = 0\n        middle = middle * -1 #switch direction of gear\n    if ball_sensor_1.value is  False:\n        balls_detected = balls_detected + 1\n    if ball_sensor_2.value is  False:\n        balls_detected = balls_detected + 1\n    if balls_detected is 0:\n        speed = 0.75\n    if balls_detected is 1:\n        speed = 0.85\n    if balls_detected is 2:\n        speed = 1\n\n<\/pre>\n\n\n<p>Karen Abruzzo, Emilie Zhou<\/p>\n\n\n\n<p><strong>Description<\/strong><\/p>\n\n\n\n<p>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\u2019re 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.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/posts\/5059"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/comments?post=5059"}],"version-history":[{"count":2,"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/posts\/5059\/revisions"}],"predecessor-version":[{"id":5087,"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/posts\/5059\/revisions\/5087"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/media?parent=5059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/categories?post=5059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-223\/f2021\/work\/wp-json\/wp\/v2\/tags?post=5059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}