Class: Sensor

Inherits:
Object show all
Defined in:
lib/sensors/mock_sensor.rb,
lib/sensors/network_sensor.rb,
lib/sensors/basic_message_arduino_sensor.rb,
lib/sensors/advanced_message_arduino_sensor.rb

Overview

Arduino: a sensor written for the arduino open source hardware.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Sensor

Returns a new instance of Sensor


4
5
# File 'lib/sensors/mock_sensor.rb', line 4

def initialize(filename=nil)
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue


3
4
5
# File 'lib/sensors/network_sensor.rb', line 3

def queue
  @queue
end

#rObject

Returns the value of attribute r


3
4
5
# File 'lib/sensors/mock_sensor.rb', line 3

def r
  @r
end

#versionObject

Returns the value of attribute version


7
8
9
# File 'lib/sensors/mock_sensor.rb', line 7

def version
  "mock-1"
end

Instance Method Details

#d_n_fObject


63
64
65
66
67
68
69
# File 'lib/sensors/mock_sensor.rb', line 63

def d_n_f
  if Time.now-@last_random>0.25 && Time.now-@start < 2.0
    @last_random = Time.now
    @random += [(Time.now - @start)*1000] * 20
  end
  @random
end

#false_startObject


71
72
# File 'lib/sensors/mock_sensor.rb', line 71

def false_start
end

#fastObject


39
40
41
42
43
44
45
# File 'lib/sensors/mock_sensor.rb', line 39

def fast
  if Time.now-@last_fast>0.25
    @last_fast = Time.now
    @fast += [(@last_fast - @start)*1000] * 20
  end
  @fast
end

#finish_timesObject

I don't like this but it's just legacy until I truly deprecate the basic_msg


27
28
29
# File 'lib/sensors/mock_sensor.rb', line 27

def finish_times
  @t["racers"].map{|r| r[$RACE_DISTANCE / $ROLLER_CIRCUMFERENCE] }
end

#get_versionObject


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 268

def get_version
  begin
    Timeout.timeout(2){
      sleep(1)
      @f.flush
      @f.puts "!v"
      @f.flush
      puts "getting version"
      version = @f.readline
      puts "version: #{version}"
      if version =~ /^V:/
        @version = version.sub(/^V:/,'').to_f
        extend NewFirmware
      else
        extend LegacyFirmware
      end
    }
  rescue Timeout::Error
    puts "Timeout getting version"
    extend LegacyFirmware
  end
end

#racersObject


31
32
33
# File 'lib/sensors/mock_sensor.rb', line 31

def racers
  @t["racers"]
end

#randomObject


55
56
57
58
59
60
61
# File 'lib/sensors/mock_sensor.rb', line 55

def random
  if Time.now-@last_random>0.25
    @last_random = Time.now
    @random += [(Time.now - @start)*1000] * random(20)+1
  end
  @random
end

#slowObject


47
48
49
50
51
52
53
# File 'lib/sensors/mock_sensor.rb', line 47

def slow
  if Time.now-@last_slow>0.25
    @last_slow = Time.now
    @slow += [(Time.now - @start)*1000] * 18
  end
  @slow
end

#startObject


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sensors/mock_sensor.rb', line 11

def start
  @fast,@slow,@random = [[]]*3

  @start = Time.now+4.0
  @last_fast,@last_slow,@last_random = [Time.now+4.0]*3
  @t = Thread.new do
    loop do
      fake = fast
      Thread.current["racers"] = [fake, slow, fake, fake]
      Thread.current["time"] = (Time.now - @start)*1000
    end
  end
end

#stopObject


74
75
76
# File 'lib/sensors/mock_sensor.rb', line 74

def stop
  @t.kill
end

#timeObject


35
36
37
# File 'lib/sensors/mock_sensor.rb', line 35

def time
  @t["time"]
end