-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive-examples.py
91 lines (72 loc) · 2.67 KB
/
drive-examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Initial code pulled from the installation verification code and API documents
from XRPLib.defaults import *
import time
"""
By the end of this file students will learn how to control the drivetrain,
both by setting effort values directly to the motors and by using go_straight and go_turn
"""
# drive straight for a set time period (defualt 1 second)
def drive_straight(drive_time: float = 1):
drivetrain.set_effort(0.8, 0.8)
time.sleep(drive_time)
drivetrain.stop()
## end of function / subroutine ##
# drive at a slight counter clockwise arc for a set time period (default 1 second)
def arc_turn(turn_time: float = 1):
drivetrain.set_effort(0.5, 0.8)
time.sleep(turn_time)
drivetrain.stop()
## end of function / subroutine ##
# turn CCW at a point for a set time period (default 1 second)
def point_turn(turn_time: float = 1):
drivetrain.set_effort(-0.8, 0.8)
time.sleep(turn_time)
drivetrain.stop()
## end of function / subroutine ##
# pivot turn around the left wheel for a set time period (default 1 second)
def swing_turn(turn_time: float = 1):
drivetrain.set_effort(0, 0.8)
time.sleep(turn_time)
drivetrain.stop()
## end of function / subroutine ##
# Driving in a circle by setting a difference in motor efforts
def circle():
while True:
drivetrain.set_effort(0.8, 1)
## end of function / subroutine ##
# Follow the perimeter of a square with variable sidelength
def square(sidelength):
for sides in range(4):
drivetrain.straight(sidelength, 0.8)
drivetrain.turn(90)
# Alternatively:
# polygon(sidelength, 4)
## end of function / subroutine ##
# Follow the perimeter of an arbitrary polygon with variable side length and number of sides
# Side length in centimeters
def polygon(side_length, number_of_sides):
for s in range(number_of_sides):
drivetrain.straight(side_length)
drivetrain.turn(360/number_of_sides)
## end of function / subroutine ##
# A slightly longer example program showing how a robot may follow a simple path
def test_drive():
# blink and and wait for button press
# Flash LED at 5Hz
board.led_blink(5)
board.wait_for_button()
time.sleep(1)
print("Driving forward 25cm")
drivetrain.straight(25, 0.8)
time.sleep(1)
print("turn 90 degrees right")
drivetrain.turn(90,0.8)
time.sleep(1)
print("turn 90 degrees left by setting speed negative")
drivetrain.turn(90, -0.8)
time.sleep(1)
print("drive backwards 25 cm by setting distance negative")
# There is no difference between setting speed or distance negative, both work
drivetrain.straight(-25,0.8)
## end of function / subroutine ##
test_drive()