Class: Rplidar::ScanDataResponse

Inherits:
Response
  • Object
show all
Defined in:
lib/rplidar/scan_data_response.rb

Overview

Data response for one scan measurement

Instance Attribute Summary

Attributes inherited from Response

#raw_response

Instance Method Summary collapse

Methods inherited from Response

#check_payload, #check_response, #initialize

Constructor Details

This class inherits a constructor from Rplidar::Response

Instance Method Details

#angleObject



35
36
37
# File 'lib/rplidar/scan_data_response.rb', line 35

def angle
  ((raw_response[2] << 7) + (raw_response[1] >> 1)) / 64.0
end

#check_headerObject



4
5
6
7
8
9
10
11
12
# File 'lib/rplidar/scan_data_response.rb', line 4

def check_header
  unless correct_start_bit?
    raise 'Inversed start bit of the data response ' \
      'is not inverse of the start bit'
  end

  raise 'Check bit of the data response is not equal to 1' \
    unless correct_check_bit?
end

#correct_check_bit?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rplidar/scan_data_response.rb', line 23

def correct_check_bit?
  raw_response[1][0] == 1
end

#correct_start_bit?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/rplidar/scan_data_response.rb', line 14

def correct_start_bit?
  # start bit
  start = raw_response[0][0]
  # inversed start bit
  inversed = raw_response[0][1]

  (start == 1 && inversed.zero?) || (start.zero? && inversed == 1)
end

#distanceObject



39
40
41
# File 'lib/rplidar/scan_data_response.rb', line 39

def distance
  ((raw_response[4] << 8) + raw_response[3]) / 4.0
end

#qualityObject



31
32
33
# File 'lib/rplidar/scan_data_response.rb', line 31

def quality
  raw_response[0] >> 2
end

#responseObject



43
44
45
46
47
48
49
50
# File 'lib/rplidar/scan_data_response.rb', line 43

def response
  {
    start: start?,
    quality: quality,
    angle: angle,
    distance: distance
  }
end

#start?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rplidar/scan_data_response.rb', line 27

def start?
  raw_response[0][0] == 1
end