-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer_test.rb
More file actions
75 lines (62 loc) · 1.66 KB
/
Copy pathplayer_test.rb
File metadata and controls
75 lines (62 loc) · 1.66 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
class PlayerTest2
COLOR_SENSOR = "2"
DISTANCE_SENSOR = "3"
LEFT_MOTOR = "C"
RIGHT_MOTOR = "B"
MOTOR_SPEED = 10
attr_reader :last_color, :last_distance
def initialize(port = "COM3")
@motors = [LEFT_MOTOR, RIGHT_MOTOR]
@brick = EV3::Brick.new(EV3::Connections::Bluetooth.new(port))
@brick.connect
@brick.reset(*@motors)
@wait_cnt = 0
@last_color = 0.0
@last_distance = 0.0
update_sensor_value
end
def move_forward(sec, speed = MOTOR_SPEED)
@brick.start(speed, *@motors)
sleep sec
@brick.stop(true, *@motors)
end
def move_back(sec, speed = MOTOR_SPEED)
@brick.reverse_polarity(*@motors)
@brick.start(speed, *@motors)
sleep sec
@brick.stop(true, *@motors)
@brick.reverse_polarity(*@motors)
end
def right_torun(sec, speed = MOTOR_SPEED)
@brick.reverse_polarity(RIGHT_MOTOR)
@brick.start(speed, *@motors)
sleep sec
@brick.stop(true, *@motors)
@brick.run_forward(*@motors)
@brick.start(speed, *@motors)
sleep sec + 1
@brick.stop(true, *@motors)
end
def left_torun(sec, speed = MOTOR_SPEED)
@brick.reverse_polarity(LEFT_MOTOR)
@brick.start(speed, *@motors)
sleep sec
@brick.stop(true, *@motors)
@brick.run_forward(*@motors)
@brick.start(speed, *@motors)
sleep sec + 1
@brick.stop(true, *@motors)
end
def update_sensor_value
@wait_cnt += 1
return unless @wait_cnt % 30 == 0
@last_color = @brick.get_sensor(COLOR_SENSOR, 2)
@last_distance = @brick.get_sensor(DISTANCE_SENSOR, 0)
end
def close()
@brick.stop(true, *@motors)
@brick.clear_all
@brick.disconnect
end
end
right_torun(10,speed=MOTOR_SPEED)