-
Notifications
You must be signed in to change notification settings - Fork 0
/
Move.py
98 lines (78 loc) · 3.19 KB
/
Move.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
92
93
94
95
96
97
98
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase
left_motor = Motor( Port.B )
right_motor = Motor( Port.C )
def move( steering, power, inches, brake ):
angle = 36*Inches
left_motor.run_target( power, angle, brake, False)
right_motor.run_target( power, angle, brake, True)
def stop( brake ):
left_motor.stop( brake )
right_motor.stop( brake )
def move_forever( power ):
left_motor.run( power )
right_motor.run( power )
def move_to_black( power, port, brake ):
move_forever( power )
color_sensor=ColorSensor( port )
print( color_sensor.reflection() )
while color_sensor.reflection()>15:
print(color_sensor.reflection())
wait(0.1)
stop(brake)
brick.sound.beep()
def move_to_black_for_square( power, port1, port2, brake ):
move_forever( power )
light_sensor1=ColorSensor( port1 )
light_sensor2=ColorSensor( port2 )
print(str(light_sensor1.reflection())+ " " + str(light_sensor2.reflection()))
while light_sensor1.reflection() > 15 and light_sensor2.reflection() > 15:
print(str(light_sensor1.reflection())+ " " + str(light_sensor2.reflection()))
wait(0.1)
print(str(light_sensor1.reflection())+ " " + str(light_sensor2.reflection()))
stop( brake )
brick.sound.beep()
def move_to_white( power, port, brake ):
move_forever( power )
color_sensor=ColorSensor( port )
print(color_sensor.reflection())
while color_sensor.reflection()<90:
print(color_sensor.reflection())
wait(0.1)
stop( brake )
brick.sound.beep()
def square():
left_sensor=ColorSensor( Port.S1 )
right_sensor=ColorSensor( Port.S4 )
move_to_black_for_square( 50, Port.S1, Port.S4, Stop.BRAKE )
def follow_line( power, port, brake ):
#move, then check the color underneath the sensor.
#If the color underneath the sensor is black, move forwards.
#If the color is not black, turn right and check the color again.
#Do the same for the left side of the robot.
#If there is no black on either sides of the robot, then beep and stop.
#Move, then check the color underneath the sensor.
move_forever( power )
while color_reflection <15:
#If the color underneath the sensor is black, move forwards.
move_forever( power )
#print color_reflection
#If the color is not black, turn right and check the color again.
pivotright( power, degrees, brake )
while color_reflection < 15:
move_forever( power)
#print color_reflection
#Do the same for the left side of the robot.
pivotleft( power, degrees, brake )
while color_reflection < 15:
move_forever( power )
#print color_reflection
#If there is no black on either sides of the robot, then beep and stop.
brick.sound.beep()
stop( brake )