Chapter 5 review

This commit is contained in:
Danny Staple
2022-01-09 23:34:55 +00:00
parent f1e0e2d007
commit 13640d0dad
2 changed files with 13 additions and 6 deletions
+8 -4
View File
@@ -6,15 +6,17 @@ motor_A2 = pwmio.PWMOut(board.GP16)
motor_B1 = pwmio.PWMOut(board.GP18) motor_B1 = pwmio.PWMOut(board.GP18)
motor_B2 = pwmio.PWMOut(board.GP19) 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(): def stop():
motor_A1.duty_cycle = 0 motor_A1.duty_cycle = 0
motor_A2.duty_cycle = 0 motor_A2.duty_cycle = 0
motor_B1.duty_cycle = 0 motor_B1.duty_cycle = 0
motor_B2.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): def set_speed(motor, speed):
# Swap motor pins if we reverse the 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[0].duty_cycle = int(max_speed * speed)
direction[1].duty_cycle = 0 direction[1].duty_cycle = 0
def set_left(speed): def set_left(speed):
set_speed(left_motor, speed) set_speed(left_motor, speed)
def set_right(speed): def set_right(speed):
set_speed(right_motor, speed) set_speed(right_motor, speed)
+5 -2
View File
@@ -8,6 +8,8 @@ motor_B2 = pwmio.PWMOut(board.GP19)
right_motor = motor_A1, motor_A2 right_motor = motor_A1, motor_A2
left_motor = motor_B1, motor_B2 left_motor = motor_B1, motor_B2
max_speed = 2**16-1
def stop(): def stop():
motor_A1.duty_cycle = 0 motor_A1.duty_cycle = 0
@@ -15,6 +17,7 @@ def stop():
motor_B1.duty_cycle = 0 motor_B1.duty_cycle = 0
motor_B2.duty_cycle = 0 motor_B2.duty_cycle = 0
def set_speed(motor, speed): def set_speed(motor, speed):
# Swap motor pins if we reverse the speed # Swap motor pins if we reverse the speed
if speed < 0: if speed < 0:
@@ -23,14 +26,14 @@ def set_speed(motor, speed):
else: else:
direction = motor direction = motor
speed = min(speed, 1) # limit to 1.0 speed = min(speed, 1) # limit to 1.0
max_speed = 2**16-1
direction[0].duty_cycle = int(max_speed * speed) direction[0].duty_cycle = int(max_speed * speed)
direction[1].duty_cycle = 0 direction[1].duty_cycle = 0
def set_left(speed): def set_left(speed):
set_speed(left_motor, speed) set_speed(left_motor, speed)
def set_right(speed): def set_right(speed):
set_speed(right_motor, speed) set_speed(right_motor, speed)