Karen Abruzzo and Jason Perez
Statement
Our initial intent was to create music by having marbles drop on differently shaped wooden pieces. The user would have control over the rate at which the marbles would drop through a spinning gate. The final outcome was realized through a spinning gate controlled by the user with a set of instructions. At the console, the user can define a sequence of six values that determine the speed and direction of the gate. These controls allow the user to create a rhythmic pattern with the motor and sound effects with the falling marbles.
Files
https://drive.google.com/file/d/1uyl0Dj6D769WWmYuC_1VhlOsYWAuSpW-/view?usp=sharing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | # musical_rotator.py # # This assumes a Pololu DRV8833 dual motor driver has been wired up to the Pico as follows: # Pico pin 24, GPIO18 -> AIN1 # Pico pin 25, GPIO19 -> AIN2 # any Pico GND -> GND # DRV8833 carrier board: https://www.pololu.com/product/2130 ################################################################ # CircuitPython module documentation: ################################################################################ # load standard Python modules import math, time # load the CircuitPython hardware definition module for pin definitions import board # load the CircuitPython pulse-width-modulation module for driving hardware import pwmio #-------------------------------------------------------------------------------- # 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, pwm_rate = 20000 ): # Create a pair of PWMOut objects for each motor channel. self .ain1 = pwmio.PWMOut(AIN1, duty_cycle = 0 , frequency = pwm_rate) self .ain2 = pwmio.PWMOut(AIN2, duty_cycle = 0 , frequency = pwm_rate) self .bin1 = pwmio.PWMOut(BIN1, duty_cycle = 0 , frequency = pwm_rate) self .bin2 = pwmio.PWMOut(BIN2, 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 else : if rate < 0 : self .bin1.duty_cycle = pwm self .bin2.duty_cycle = 0 else : self .bin1.duty_cycle = 0 self .bin2.duty_cycle = pwm #-------------------------------------------------------------------------------- # Create an object to represent a dual motor driver. print ( "Creating driver object." ) driver = DRV8833() #-------------------------------------------------------------------------------- # Begin the main processing loop. This is structured as a looping script, since # each movement primitive 'blocks', i.e. doesn't return until the action is # finished. #--------------------------------------------------------------- class Logic: def __init__( self ): """Application state machine.""" self .update_interval = 100000000 # period of 2 Hz in nanoseconds self .update_timer = 0 # initialize the state machine self .state = 'init' self .state_changed = False self .state_timer = 0 self .intList = [] def poll( self , elapsed): """Polling function to be called as frequently as possible from the event loop with the nanoseconds elapsed since the last cycle.""" self .update_timer - = elapsed if self .update_timer < 0 : self .update_timer + = self .update_interval self .tick() # evaluate the state machine def transition( self , new_state): """Set the state machine to enter a new state on the next tick.""" self .state = new_state self .state_changed = True self .state_timer = 0 print ( "Entering state:" , new_state) def tick( self ): """Evaluate the state machine rules.""" # advance elapsed time in seconds self .state_timer + = 1e - 9 * self .update_interval #print(self.state_timer) # set up a flag to process transitions within a state clause entering_state = self .state_changed self .state_changed = False # select the state clause to evaluate if self .state = = 'init' : self .transition( 'idle' ) elif self .state = = 'idle' : if entering_state: self .intList = [] driver.write( 0 , . 7 ) print ( "Please enter 6 numbers 0-5, negative numbers reverse motor" ) print ( "Example: 0 1 2 -3 -4 -5" ) userInput = input ( "Enter: " ) listTemp = list (userInput.split()) self .intList = list ( map ( int ,listTemp)) #print(self.intList) self .transition( 'userState' ) elif self .state = = 'userState' : if self .intList[ 0 ] > 0 : driver.write( 0 , . 55 + . 05 * self .intList[ 0 ]) else : driver.write( 0 , - . 55 + . 05 * self .intList[ 0 ]) time.sleep( 2.0 ) if self .intList[ 1 ] > 0 : driver.write( 0 , . 55 + . 05 * self .intList[ 1 ]) else : driver.write( 0 , - . 55 + . 05 * self .intList[ 1 ]) time.sleep( 2.0 ) if self .intList[ 2 ] > 0 : driver.write( 0 , . 55 + . 05 * self .intList[ 2 ]) else : driver.write( 0 , - . 55 + . 05 * self .intList[ 2 ]) time.sleep( 2.0 ) if self .intList[ 3 ] > 0 : driver.write( 0 , . 55 + . 05 * self .intList[ 3 ]) else : driver.write( 0 , - . 55 + . 05 * self .intList[ 3 ]) time.sleep( 2.0 ) if self .intList[ 4 ] > 0 : driver.write( 0 , . 55 + . 05 * self .intList[ 4 ]) else : driver.write( 0 , - . 55 + . 05 * self .intList[ 4 ]) time.sleep( 2.0 ) if self .intList[ 5 ] > 0 : driver.write( 0 , . 55 + . 05 * self .intList[ 5 ]) else : driver.write( 0 , - . 55 + . 05 * self .intList[ 5 ]) time.sleep( 2.0 ) self .transition( 'idle' ) #--------------------------------------------------------------- print ( "Starting main script." ) last_clock = time.monotonic_ns() logic = Logic() while True : now = time.monotonic_ns() elapsed = now - last_clock last_clock = now logic.poll(elapsed) |
Leave a Reply
You must be logged in to post a comment.