Repository navigation
Expand file tree
/
Copy pathhigh_level.jl
More file actions
60 lines (49 loc) · 1.43 KB
/
Copy pathhigh_level.jl
File metadata and controls
60 lines (49 loc) · 1.43 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
function drive(motor::Motor, speed::Int; direct::Bool = true)
drive(motor; direct = direct)
change_speed(motor, speed)
end
function drive(robot::Robot, speed = nothing, turning_rate = nothing; direct = true)
same = 0
if speed === nothing
speed = robot.current_speed
same += 1
else
if speed == robot.speed
same += 1
else
robot.speed = speed
end
end
if turning_rate === nothing
turning_rate = robot.turning_rate
same += 1
else
if turning_rate == robot.turning_rate
same += 1
else
robot.turning_rate = turning_rate
end
end
if same != 2
left_speed = speed
right_speed = speed
half_turn = round(Int, turning_rate / 2)
left_speed += half_turn
right_speed -= half_turn
left_speed > 100 && (left_speed = 100)
left_speed < -100 && (left_speed = -100)
right_speed > 100 && (right_speed = 100)
right_speed < -100 && (right_speed = -100)
drive(robot.left, left_speed; direct = direct)
drive(robot.right, right_speed; direct = direct)
end
end
function stop(robot::Robot, stop_action::Symbol = :coast)
stop(robot.left, stop_action)
stop(robot.right, stop_action)
end
function mode(mode, sensors::LightSensor...)
for sensor in sensors
command(sensor, sensor.modes[mode])
end
end