/ examples / motorkit_robot_test.py
motorkit_robot_test.py
 1  # Simple two DC motor robot class usage example.
 2  # Author: Tony DiCola, Chris Anderron
 3  # License: MIT License https://opensource.org/licenses/MIT
 4  import time
 5  
 6  # Import the Robot.py file (must be in the same directory as this file!).
 7  import Robot
 8  
 9  
10  # Set the trim offset for each motor (left and right).  This is a value that
11  # will offset the speed of movement of each motor in order to make them both
12  # move at the same desired speed.  Because there's no feedback the robot doesn't
13  # know how fast each motor is spinning and the robot can pull to a side if one
14  # motor spins faster than the other motor.  To determine the trim values move the
15  # robot forward slowly (around 100 speed) and watch if it veers to the left or
16  # right.  If it veers left then the _right_ motor is spinning faster so try
17  # setting RIGHT_TRIM to a small negative value, like -0.05, to slow down the right
18  # motor.  Likewise if it veers right then adjust the _left_ motor trim to a small
19  # negative value.  Increase or decrease the trim value until the bot moves
20  # straight forward/backward.
21  LEFT_TRIM = 0
22  RIGHT_TRIM = 0
23  
24  
25  # Create an instance of the robot with the specified trim values.
26  
27  robot = Robot.Robot(left_trim=LEFT_TRIM, right_trim=RIGHT_TRIM)
28  
29  # Now move the robot around!
30  # Each call below takes two parameters:
31  #  - speed: The speed of the movement, a value from -1.0 to +1.0.  The higher the value
32  #           the faster the movement.  You need to start with a value around 0.10
33  #           to get enough torque to move the robot.
34  #  - time (seconds):  Amount of time to perform the movement.  After moving for
35  #                     this amount of seconds the robot will stop.  This parameter
36  #                     is optional and if not specified the robot will start moving
37  #                     forever.
38  
39  robot.left(0.5, 1)
40  robot.right(0.5, 1)
41  robot.steer(0.5, 0.2)
42  time.sleep(3)
43  robot.stop()  # Stop the robot from moving.
44  
45  
46  # That's it!  Note that on exit the robot will automatically stop moving.