import pyb  # import the pyboard module
import time # import the time module (remove if not using)

# We'll use the machine i2c implementation. 
#
import machine 

# We also need to import the DC motor code from the library
import motor

# Initialize communication with the motor driver
i2c = machine.I2C(scl=machine.Pin("X9"), sda=machine.Pin("X10"))

# And, then initialize the DC motor control object
motors = motor.DCMotors(i2c)

# Now, we can initialize the DC motor object. The number should match the
# motor number on the motor driver board
MOTOR_NUMBER = 1 # DC1

# We'll define a variable to hold a running average of our prior speed commands
last_speed = 0

# Define a start time
start_time = time.ticks_ms() 

# The variable alpha defines how quickly we track changes. A higher value slows
# our response by favoring previous inputs more heavily. Our speed will be:
#
#  speed = alpha * last_speed + beta * desired_speed
Beispiel #2
0
import machine, motor, bot, time

print('creating i2c and motors ...')
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
motors = motor.DCMotors(i2c)  #creates motors object

LEFT = 0  #M0 - left motor
RIGHT = 3  #M4 - right motor
print('creating robot ...')
robot = bot.Robot(motors, LEFT, RIGHT)  # creates robot

dt = 3  # duration in seconds
print('robot moves ...')
robot.left(2000, dt)  #turn left
time.sleep(0.3)
robot.right(2000, dt)  # turn right
time.sleep(0.3)
robot.forward(2000, dt)  #forward
time.sleep(0.3)
robot.backward(2000, dt)  #backwards
time.sleep(0.3)

print('robot demo ...')
speed = 3000  #motorspeed
for i in range(3):
    robot.left(speed, dt)
    time.sleep(0.3)
    robot.right(speed, dt)
    time.sleep(0.3)
    robot.forward(speed, dt)
    time.sleep(0.3)