Class: Rplidar::ResponseDescriptor

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

Overview

Incapsulates Response Descriptor processing. Format of Response Descriptor:

Start Flag 1 Start Flag 2 Data Response Length Send Mode Data Type 1 byte (0xA5) 1 bytes (0x5A) 30 bits 2 bits 1 byte

Instance Attribute Summary

Attributes inherited from Response

#raw_response

Instance Method Summary collapse

Methods inherited from Response

#initialize

Constructor Details

This class inherits a constructor from Rplidar::Response

Instance Method Details

#check_headerObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rplidar/response_descriptor.rb', line 19

def check_header
  unless correct_first_byte?
    raise 'Wrong first byte of the response descriptor: ' \
      "'#{int_to_hex(raw_response[0])}'"
  end

  unless correct_second_byte?
    raise 'Wrong second byte of the response descriptor: ' \
      "'#{int_to_hex(raw_response[1])}'"
  end
end

#check_payloadObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rplidar/response_descriptor.rb', line 31

def check_payload
  unless correct_send_mode?
    raise 'Wrong send mode value of the response descriptor: ' \
      "'#{int_to_hex(send_mode)}'"
  end

  unless correct_data_type?
    raise 'Wrong data type value of the response descriptor: ' \
      "'#{int_to_hex(data_type)}'"
  end
end

#check_responseObject



14
15
16
17
# File 'lib/rplidar/response_descriptor.rb', line 14

def check_response
  check_header
  check_payload
end

#correct_data_type?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/rplidar/response_descriptor.rb', line 58

def correct_data_type?
  [
    DATA_TYPE_DEVICE_INFO, DATA_TYPE_CURRENT_STATE, DATA_TYPE_SCAN
  ].include?(data_type)
end

#correct_first_byte?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rplidar/response_descriptor.rb', line 43

def correct_first_byte?
  raw_response[0] == 0xA5
end

#correct_second_byte?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rplidar/response_descriptor.rb', line 47

def correct_second_byte?
  raw_response[1] == 0x5A
end

#correct_send_mode?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/rplidar/response_descriptor.rb', line 51

def correct_send_mode?
  [
    SEND_MODE_SINGLE_REQUEST_SINGLE_RESPONSE,
    SEND_MODE_SINGLE_REQUEST_MULTIPLE_RESPONSE
  ].include?(send_mode)
end

#data_response_lengthObject



64
65
66
# File 'lib/rplidar/response_descriptor.rb', line 64

def data_response_length
  (raw_response[4] << 16) + (raw_response[3] << 8) + raw_response[2]
end

#data_typeObject

The 1byte Data Type describes the type of the incoming data response packets.



82
83
84
# File 'lib/rplidar/response_descriptor.rb', line 82

def data_type
  raw_response[6]
end

#responseObject



86
87
88
89
90
91
92
# File 'lib/rplidar/response_descriptor.rb', line 86

def response
  {
    data_response_length: data_response_length,
    send_mode: send_mode,
    data_type: data_type
  }
end

#send_modeObject

The 2 bits Send Mode field describes the request/response mode of the current session. Values:

  • 0x0 - Single Request - Single Response mode, RPLIDAR will send

    only one data response packet in the current session.
    
  • 0x1 - Single Request - Multiple Response mode, RPLIDAR will

    continuously send out data response packets with the same format
    in the current session.
    
  • 0x2 and 0x3 are reserved for future use



76
77
78
# File 'lib/rplidar/response_descriptor.rb', line 76

def send_mode
  raw_response[5] >> 6
end