This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
171 lines (121 loc) · 4.82 KB
/
debug.py
File metadata and controls
171 lines (121 loc) · 4.82 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile, Font
import time
# This file contains all of the inbuilt functions for the robot wrapped in the test_decorator.
# Create your objects here.
ev3 = EV3Brick()
# Initialize the motors.
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)
center_motor = Motor(Port.D)
# If you want to use more motors, add them here.
#arm_motor = Motor(Port.A)
# Initialize the drive base.
# MIGHT WANT TO CHECK TO MAKE SURE THIS IS RIGHT
# axle_track is distance between the two wheels
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104)
# settings(straight_speed, straight_acceleration, turn_rate, turn_acceleration
robot.settings(250, 250, 360, 720)
tiny_font = Font(size=16)
ev3.screen.set_font(tiny_font)
# Initialize the motors.
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)
center_motor = Motor(Port.D)
# If you want to use more motors, add them here.
#arm_motor = Motor(Port.A)
# Initialize the drive base.
# MIGHT WANT TO CHECK TO MAKE SURE THIS IS RIGHT
# axle_track is distance between the two wheels
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104)
# settings(straight_speed, straight_acceleration, turn_rate, turn_acceleration
robot.settings(250, 250, 360, 720)
# data = DataLog("Time", "Line", "Distance", "Angle", extension="txt")
# Initialize any sensors you want here by uncommenting and changing the port.
#touch_sensor = TouchSensor(Port.S1)
# To use touch sensor, pass it in as a parameter in the run function for whichever mission needs it.
# Example usage:
# def run(robot, touch_sensor):
# # drive forward until the touch sensor is pressed
# while !touch_sensor.pressed():
# robot.drive(100, 0)
# robot.stop()
#color_sensor = ColorSensor(Port.S2)
# To use color sensor, pass it in as a parameter in the run function for whichever mission needs it.
# Example usage:
# def run(robot, color_sensor):
# # drive forward until the color sensor sees the color you want
# while color_sensor.color() != Color.BLACK:
# robot.drive(100, 0)
# robot.stop()
#ultrasonic_sensor = UltrasonicSensor(Port.S3)
# To use ultrasonic sensor, pass it in as a parameter in the run function for whichever mission needs it.
# Example usage:
# def run(robot, ultrasonic_sensor):
# # drive forward until the ultrasonic sensor sees the distance you want
# while ultrasonic_sensor.distance() > 10:
# robot.drive(100, 0)
# robot.stop()
#gyro_sensor = GyroSensor(Port.S4)
# To use gyro sensor, pass it in as a parameter in the run function for whichever mission needs it.
# Example usage:
# def run(robot, gyro_sensor):
# # turn until the gyro sensor sees the angle you want
# while gyro_sensor.angle() < 10:
# robot.drive(0, 100)
# robot.stop()
def format_tuple(value):
return "(" + ",".join(repr(v) for v in value) + ")"
# Decorator that prints the currently running function to the screen.
def test_decorator(func):
def inner1(*args, **kwargs):
name = func.__name__
# Showing user which function is currently executing with what arguments on which line
ev3.screen.print("%s %s Start" % (name, format_tuple(args)))
# data.log(time.time(),"%s %s %d %d" % (name, format_tuple(args), robot.distance(),robot.angle()))
# getting the returned value
returned_value = func(*args, **kwargs)
# Showing user that the function has been executed and the arguments that were passed into it
ev3.screen.print("%s %s Done" % (name, format_tuple(args)))
# returning the value to the original frame
return returned_value
return inner1
@test_decorator
def ev3_print(printable):
ev3.screen.print(printable)
@test_decorator
def ev3_beep():
ev3.speaker.beep()
@test_decorator
def straight(distance):
robot.straight(distance)
@test_decorator
def turn(angle):
robot.turn(angle)
@test_decorator
def drive(speed, angle):
robot.drive(speed, angle)
@test_decorator
def run_target(speed, target_angle):
center_motor.run_target(speed, target_angle)
@test_decorator
def reset_angle(angle):
center_motor.reset_angle(angle)
@test_decorator
def motor_stop():
center_motor.stop()
@test_decorator
def run_until_stalled(speed):
center_motor.run_until_stalled(speed)
@test_decorator
def hold():
center_motor.hold()
@test_decorator
def run_time(speed, milliseconds):
center_motor.run_time(speed, milliseconds)