-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrone.rb
84 lines (64 loc) · 1.39 KB
/
drone.rb
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
require_relative 'engine'
require_relative 'gyroscope'
require_relative 'orientation_sensor'
class Drone
attr :engines, :gyroscope, :orientation_sensor, :status
OFF = 0
HOVERING = 1
MOVING = 2
def initialize
@engines = []
4.times do
@engines << Engine.new
end
@gyroscope = Gyroscope.new
@orientation_sensor = OrientationSensor.new
@status = OFF
end
def take_off(power)
if power==0
puts "FAILED TO TAKE OFF"
end
@engines.each do |e|
e.status_on
e.power = power
end
@status = MOVING
end
def move_forward(velocity)
@status = MOVING
@gyroscope.x_vector = velocity
end
def move_back(velocity)
@status = MOVING
@gyroscope.x_vector = velocity
end
def move_up(velocity)
@status = MOVING
@gyroscope.y_vector = velocity
end
def move_down(velocity)
@status = MOVING
@gyroscope.y_vector = velocity
end
def move_left
@status = MOVING
@gyroscope.z_vector = velocity
end
def move_right
@status = MOVING
@gyroscope.z_vector = velocity
end
def stabilize
@status = HOVERING
end
def land
@status = MOVING
end
def engine_broken
@engines.each_with_index {|e, i|
puts "ENGINE NUMBER #{i} has broken, starting to land" if e.status_off && status.include?(HOVERING, MOVING)
}
land
end
end