diff --git a/ch-5/4-pwm/with_helpers/robot.py b/ch-5/4-pwm/with_helpers/robot.py index fbe0836..f1a9a2b 100755 --- a/ch-5/4-pwm/with_helpers/robot.py +++ b/ch-5/4-pwm/with_helpers/robot.py @@ -6,15 +6,17 @@ motor_A2 = pwmio.PWMOut(board.GP16) motor_B1 = pwmio.PWMOut(board.GP18) motor_B2 = pwmio.PWMOut(board.GP19) +right_motor = motor_A1, motor_A2 +left_motor = motor_B1, motor_B2 +max_speed = 2**16-1 + + def stop(): motor_A1.duty_cycle = 0 motor_A2.duty_cycle = 0 motor_B1.duty_cycle = 0 motor_B2.duty_cycle = 0 -max_speed = 2**16-1 -right_motor = motor_A1, motor_A2 -left_motor = motor_B1, motor_B2 def set_speed(motor, speed): # Swap motor pins if we reverse the speed @@ -28,8 +30,10 @@ def set_speed(motor, speed): direction[0].duty_cycle = int(max_speed * speed) direction[1].duty_cycle = 0 + def set_left(speed): set_speed(left_motor, speed) + def set_right(speed): - set_speed(right_motor, speed) \ No newline at end of file + set_speed(right_motor, speed) diff --git a/ch-5/5-path/robot.py b/ch-5/5-path/robot.py index 6c657ab..f1a9a2b 100755 --- a/ch-5/5-path/robot.py +++ b/ch-5/5-path/robot.py @@ -8,6 +8,8 @@ motor_B2 = pwmio.PWMOut(board.GP19) right_motor = motor_A1, motor_A2 left_motor = motor_B1, motor_B2 +max_speed = 2**16-1 + def stop(): motor_A1.duty_cycle = 0 @@ -15,6 +17,7 @@ def stop(): motor_B1.duty_cycle = 0 motor_B2.duty_cycle = 0 + def set_speed(motor, speed): # Swap motor pins if we reverse the speed if speed < 0: @@ -23,14 +26,14 @@ def set_speed(motor, speed): else: direction = motor speed = min(speed, 1) # limit to 1.0 - max_speed = 2**16-1 - direction[0].duty_cycle = int(max_speed * speed) direction[1].duty_cycle = 0 + def set_left(speed): set_speed(left_motor, speed) + def set_right(speed): set_speed(right_motor, speed)