{"id":5342,"date":"2021-05-06T09:22:18","date_gmt":"2021-05-06T13:22:18","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/?p=5342"},"modified":"2021-05-06T09:48:28","modified_gmt":"2021-05-06T13:48:28","slug":"assignment-24-masks-off","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/5342\/assignment-24-masks-off\/","title":{"rendered":"Assignment 24: Masks Off"},"content":{"rendered":"\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-content\/uploads\/2021\/05\/IMG_68121.mp4\"><\/video><\/figure>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-content\/uploads\/2021\/05\/IMG_6810.mp4\"><\/video><figcaption>Motor Running Even Though Duty Cycle is 0<\/figcaption><\/figure>\n\n\n\n<p>Anishwar~ I&#8217;m still struggling to solve the problems from class the other day. The motor pump is still constantly pumping air; additionally, I am having trouble with getting the non-blocking version of the sound part of the code working. However, one success was getting the program to run only when there isn&#8217;t any light. While, the motor pump was still running because of the aforementioned issue the duty cycle was 0 and no sound played.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"IMG 5156\" width=\"620\" height=\"349\" src=\"https:\/\/www.youtube.com\/embed\/mI5Vx2m-PUU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><figcaption>Motor pump under new fabric<\/figcaption><\/figure>\n\n\n\n<p>Elise~ I found new fabric that stretches well with the pneumatic. I had two versions that worked but I think a bigger inflatable makes more movement. Also tested code: It turned green when in brighter light and back to blue when in darker light. The tones changed with the light but no motor pump output.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"IMG 5152\" width=\"620\" height=\"349\" src=\"https:\/\/www.youtube.com\/embed\/qSc6DiqKNLA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><figcaption>Code tested on my end<\/figcaption><\/figure>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# cpb_pump_control.py\n\n# Demonstrate control of an air pump based on a DC motor driven via a MOSFET\n# transistor driver circuit *or* a DRV8833 motor driver in a single-ended mode.\n#\n#  1. The driver PWM input           connects to A2\n#  2. The battery ground line        connects to GND\n\n# Related documentation:\n# https:\/\/circuitpython.readthedocs.io\/en\/latest\/shared-bindings\/pwmio\/index.html#module-pwmio\n# https:\/\/circuitpython.readthedocs.io\/projects\/motor\/en\/latest\/api.html#module-adafruit_motor.servo\n\n# ----------------------------------------------------------------\n# Import standard Python modules.\nimport time, math\n\n# Import the board-specific input\/output library.\nfrom adafruit_circuitplayground import cp\n\n# Import the low-level hardware libraries.\nimport board\nimport pwmio\n\n# ----------------------------------------------------------------\n# Initialize hardware.\n\n# Create a PWMOut object on pad A2 to generate control signals.\npwm = pwmio.PWMOut(board.A3, duty_cycle=0, frequency=20000)\n\n# ----------------------------------------------------------------\n# Initialize global variables for the main loop.\n\nphase_angle = 0.0\ncycle_duration = 12                       # seconds per cycle\nphase_rate  = 2*math.pi \/ cycle_duration  # radians\/second\nnext_pwm_update = time.monotonic_ns()\nnext_status_update = time.monotonic_ns()\ntoneCheck = True\nnextSound = time.monotonic_ns()\n\n# ----------------------------------------------------------------\n# Enter the main event loop.\nwhile True:\n    # Read the current integer clock.\n    now = time.monotonic_ns()\n    soundNow = time.monotonic_ns()\n\n\n    # If the time has arrived to update the servo command signal:\n    if cp.light &lt; 81:\n        if now &amp;gt;= next_pwm_update:\n            next_pwm_update += 20000000  # 20 msec in nanoseconds (50 Hz update)\n            pwm_level = 0.5 + 0.5 * math.sin(phase_angle)\n\n            # convert a unit-range (0 to 1) pwm_level to a 16-bit integer representing a fraction\n            new_duty_cycle = min(max(int(pwm_level * 2**16), 0), 2**16-1)\n\n            # If the new value is less than a reasonable minimum, clamp to zero.\n            # The pump motor will stall if the PWM fraction is too low; this turns\n            # it off instead.\n            if new_duty_cycle &lt; 48000:\n                pwm.duty_cycle = 0\n            else:\n                pwm.duty_cycle = new_duty_cycle\n\n            phase_angle = (phase_angle + phase_rate * 0.020) % (2 * math.pi)\n\n        # If either button is pressed, override the generated servo signal and run the motor.\n        if cp.button_a or cp.button_b:\n            pwm.duty_cycle = 2**16-1\n\n        # If the time has arrived to display the status:\n        if now &amp;gt;= next_status_update:\n            next_status_update += 500000000  # 0.5 sec in nanoseconds (2 Hz update)\n            print(pwm.duty_cycle)\n\n        if cp.light &lt; 81 and cp.light &amp;gt;= 36:\n            cp.play_tone(329,.5)\n            cp.play_tone(493,.5)\n            cp.pixels.fill((0,255,0))\n            \n        elif cp.light &lt; 36:\n            cp.play_tone(293,.5)\n            cp.play_tone(466,.5)\n            cp.pixels.fill((0,0,255))\n    else:\n        pwm.duty_cycle = 0\n        print(&quot;ok&quot;)\n\n\n\n<\/pre>\n\n\n<p>Below is my attempt to try making the sounds play using a non-blocking code; however, it doesn&#8217;t seem to be working and I need to continue messing with it.  <\/p>\n\n\n\n<p>Elise~ tested and needed A2 to change to A3 like previous code. Motor and light combination has some issues moving smoothly.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"IMG 5154\" width=\"620\" height=\"349\" src=\"https:\/\/www.youtube.com\/embed\/q9-NndNXe8U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><figcaption>Testing second code set<\/figcaption><\/figure>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# cpb_pump_control.py\n\n# Demonstrate control of an air pump based on a DC motor driven via a MOSFET\n# transistor driver circuit *or* a DRV8833 motor driver in a single-ended mode.\n#\n#  1. The driver PWM input           connects to A2\n#  2. The battery ground line        connects to GND\n\n# Related documentation:\n# https:\/\/circuitpython.readthedocs.io\/en\/latest\/shared-bindings\/pwmio\/index.html#module-pwmio\n# https:\/\/circuitpython.readthedocs.io\/projects\/motor\/en\/latest\/api.html#module-adafruit_motor.servo\n\n# ----------------------------------------------------------------\n# Import standard Python modules.\nimport time, math\n\n# Import the board-specific input\/output library.\nfrom adafruit_circuitplayground import cp\n\n# Import the low-level hardware libraries.\nimport board\nimport pwmio\n\n# ----------------------------------------------------------------\n# Initialize hardware.\n\n# Create a PWMOut object on pad A2 to generate control signals.\npwm = pwmio.PWMOut(board.A2, duty_cycle=0, frequency=20000)\n\n# ----------------------------------------------------------------\n# Initialize global variables for the main loop.\n\nphase_angle = 0.0\ncycle_duration = 12                       # seconds per cycle\nphase_rate  = 2*math.pi \/ cycle_duration  # radians\/second\nnext_pwm_update = time.monotonic_ns()\nnext_status_update = time.monotonic_ns()\ntoneCheck = True\nnextSound = time.monotonic_ns()\n\n# ----------------------------------------------------------------\n# Enter the main event loop.\nwhile True:\n    # Read the current integer clock.\n    now = time.monotonic_ns()\n    soundNow = time.monotonic_ns()\n\n\n    # If the time has arrived to update the servo command signal:\n    if now &amp;gt;= next_pwm_update:\n        next_pwm_update += 20000000  # 20 msec in nanoseconds (50 Hz update)\n        pwm_level = 0.5 + 0.5 * math.sin(phase_angle)\n\n        # convert a unit-range (0 to 1) pwm_level to a 16-bit integer representing a fraction\n        new_duty_cycle = min(max(int(pwm_level * 2**16), 0), 2**16-1)\n\n        # If the new value is less than a reasonable minimum, clamp to zero.\n        # The pump motor will stall if the PWM fraction is too low; this turns\n        # it off instead.\n        if new_duty_cycle &lt; 48000:\n            pwm.duty_cycle = 0\n        else:\n            pwm.duty_cycle = new_duty_cycle\n\n        phase_angle = (phase_angle + phase_rate * 0.020) % (2 * math.pi)\n\n    # If either button is pressed, override the generated servo signal and run the motor.\n    if cp.button_a or cp.button_b:\n        pwm.duty_cycle = 2**16-1\n\n    # If the time has arrived to display the status:\n    if now &amp;gt;= next_status_update:\n        next_status_update += 500000000  # 0.5 sec in nanoseconds (2 Hz update)\n        print(pwm.duty_cycle)\n\n    if cp.light &lt; 81 and cp.light &amp;gt;= 36:\n        print(soundNow)\n        print(nextSound)\n        if soundNow &amp;gt;= nextSound:\n            print(&quot;something&quot;)\n            cp.start_tone(329)\n            nextSound = soundNow + 500000000\n            cp.stop_tone()\n        else:\n            print(&quot;cool&quot;)\n            cp.start_tone(493)\n            cp.stop_tone()\n    elif cp.light &lt; 36:\n        cp.play_tone(293,.5)\n        cp.play_tone(466,.5)\n    else:\n        cp.stop_tone()\n\n\n\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Anishwar~ I&#8217;m still struggling to solve the problems from class the other day. The motor pump is still constantly pumping air; additionally, I am having trouble with getting&#8230;<\/p>\n","protected":false},"author":82,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/posts\/5342"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/comments?post=5342"}],"version-history":[{"count":6,"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/posts\/5342\/revisions"}],"predecessor-version":[{"id":5371,"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/posts\/5342\/revisions\/5371"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/media?parent=5342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/categories?post=5342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/16-376\/s2021\/wp-json\/wp\/v2\/tags?post=5342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}