-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.rb
executable file
·49 lines (45 loc) · 1.43 KB
/
demo.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
include FSM
puts "RunTime scheduling" if Raspberry.set_priority 99
# STEP 1
# Create a new FSM with a list of names for state parameters:
m = Machine.new(:count, :clock, :dt)
# set values for parameters:
m.params.count = 0
m.params.clock = []
m.params.dt = []
# STEP 2
# Create states, giving a name (String)
idle_state = State.new "Idle"
# define actions for on_enter, in_loop, and on_exit (using a DSL):
idle_state.on_enter { puts "> Entering #{self.name}"}
idle_state.in_loop do
params.clock << Time.now.to_f - START
params.dt << params.clock[-1] - (params.clock[-2] || 0)
params.count += 1
if params.count > 10
transition_to 'Stop'
end
end
idle_state.on_exit { puts "< Exiting #{self.name}"}
# If needed, define a timer for the state (in seconds):
idle_state.timing = 0.05
# finally add the state to the FSM instance:
m.add idle_state
# Repeat for other states:
stop_state = State.new "Stop"
stop_state.on_enter { puts "> Entering #{self.name}"}
stop_state.in_loop { stop_machine }
stop_state.on_exit { puts "< Exiting #{self.name}"}
m.add stop_state
# STEP 3
# set $debug to true for enabling warn(message)
# $debug = true
# run the FSM:
START = Time.now.to_f
m.run "Idle"
puts "Loop times:"
timings = m.params.dt
mean = timings.inject(0.0) {|s,v| s + v } / timings.count.to_f
var = timings.inject(0.0) {|s,v| s + (v - mean) ** 2 } / (timings.count.to_f - 1)
puts "Mean: #{mean} s, Standard Deviation: #{var ** 0.5} s"
Raspberry.set_priority 0