Chapter 5 examples

This commit is contained in:
Danny Staple
2021-12-07 21:34:30 +00:00
parent 4cdabd2403
commit 1f9e6b8f91
20 changed files with 311 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
import time
import robot
max_speed = 2**16-1
robot.motor_A1.duty_cycle = int(0.8 * max_speed)
time.sleep(0.3)
robot.stop()
+11
View File
@@ -0,0 +1,11 @@
import time
import board
import pwmio
A1_PWM = pwmio.PWMOut(board.GP17)
A1_PWM.duty_cycle = 2**16-1
time.sleep(0.3)
A1_PWM.duty_cycle = 2**15
time.sleep(0.3)
A1_PWM.duty_cycle = 0
+9
View File
@@ -0,0 +1,9 @@
import time
import robot
try:
robot.set_left(1.0)
robot.set_right(0.5)
time.sleep(1)
finally:
robot.stop()
+10
View File
@@ -0,0 +1,10 @@
import time
import robot
try:
for speed in range(5, 10):
robot.set_left(speed/10)
robot.set_right(speed/10)
time.sleep(0.3)
finally:
robot.stop()
+35
View File
@@ -0,0 +1,35 @@
import board
import pwmio
motor_A1 = pwmio.PWMOut(board.GP17)
motor_A2 = pwmio.PWMOut(board.GP16)
motor_B1 = pwmio.PWMOut(board.GP18)
motor_B2 = pwmio.PWMOut(board.GP19)
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
if speed < 0:
direction = motor[1], motor[0]
speed = -speed
else:
direction = motor
speed = min(speed, 1) # limit to 1.0
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)
+14
View File
@@ -0,0 +1,14 @@
import board
import pwmio
motor_A1 = pwmio.PWMOut(board.GP17)
motor_A2 = pwmio.PWMOut(board.GP16)
motor_B1 = pwmio.PWMOut(board.GP18)
motor_B2 = pwmio.PWMOut(board.GP19)
def stop():
motor_A1.duty_cycle = 0
motor_A2.duty_cycle = 0
motor_B1.duty_cycle = 0
motor_B2.duty_cycle = 0