class Rplidar::Driver

Ruby implementation of driver of the SLAMTEC RPLIDAR A2.

Constants

COMMANDS_WITH_RESPONSE
COMMAND_GET_HEALTH

Commands

COMMAND_GET_INFO
COMMAND_MOTOR_PWM
COMMAND_RESET
COMMAND_SCAN
COMMAND_STOP
GET_INFO_RESPONSE_LENGTH
RESPONSE_DESCRIPTOR_LENGTH

Default length of responses

SCAN_DATA_RESPONSE_LENGTH
UART_BAUD_RATE

Public Class Methods

new(port_address) click to toggle source
# File lib/rplidar/driver.rb, line 32
def initialize(port_address)
  @port_address = port_address
end

Public Instance Methods

clear_port() click to toggle source
# File lib/rplidar/driver.rb, line 127
def clear_port
  while port.getbyte
  end
end
close() click to toggle source
# File lib/rplidar/driver.rb, line 132
def close
  @port.close if @port
end
collect_scan_data_responses(iterations) click to toggle source
# File lib/rplidar/driver.rb, line 64
def collect_scan_data_responses(iterations)
  responses = []
  iteration = -1
  while iteration < iterations
    response = scan_data_response
    iteration += 1 if response[:start]
    responses << response if iteration.between?(0, iterations - 1)
  end
  responses
end
command(command) click to toggle source
# File lib/rplidar/driver.rb, line 84
def command(command)
  request(command)
  response_descriptor if COMMANDS_WITH_RESPONSE.include?(command)
end
current_state() click to toggle source
# File lib/rplidar/driver.rb, line 36
def current_state
  descriptor = command(COMMAND_GET_HEALTH)
  raw_response = read_response(descriptor[:data_response_length])
  Rplidar::CurrentStateDataResponse.new(raw_response).response
end
device_info() click to toggle source
# File lib/rplidar/driver.rb, line 42
def device_info
  descriptor = command(COMMAND_GET_INFO)
  raw_response = read_response(descriptor[:data_response_length])
  Rplidar::DeviceInfoDataResponse.new(raw_response).response
end
port() click to toggle source
# File lib/rplidar/driver.rb, line 136
def port
  @port ||= Serial.new(@port_address, UART_BAUD_RATE, 8, :none, 1)
end
read_response(length) click to toggle source
# File lib/rplidar/driver.rb, line 116
def read_response(length)
  t = Time.now
  response = []
  while response.size < length
    byte = port.getbyte
    response << byte if byte
    raise 'Timeout while reading a byte from the port' if Time.now - t > 2
  end
  response
end
request(command) click to toggle source
# File lib/rplidar/driver.rb, line 89
def request(command)
  params = [0xA5, command]
  port.write(ints_to_binary(params))
  sleep 0.5
end
request_with_payload(command, payload) click to toggle source
# File lib/rplidar/driver.rb, line 95
def request_with_payload(command, payload)
  payload_string = ints_to_binary(payload, 'S<*')
  payload_size = payload_string.size

  string = ints_to_binary([0xA5, command, payload_size])
  string += payload_string
  string += ints_to_binary(checksum(string))

  port.write(string)
end
reset() click to toggle source
# File lib/rplidar/driver.rb, line 80
def reset
  command(COMMAND_RESET)
end
response_descriptor() click to toggle source
# File lib/rplidar/driver.rb, line 106
def response_descriptor
  raw_response = read_response(RESPONSE_DESCRIPTOR_LENGTH)
  Rplidar::ResponseDescriptor.new(raw_response).response
end
scan(iterations = 1) click to toggle source
# File lib/rplidar/driver.rb, line 56
def scan(iterations = 1)
  command(COMMAND_SCAN)
  responses = collect_scan_data_responses(iterations)
  stop

  responses
end
scan_data_response() click to toggle source
# File lib/rplidar/driver.rb, line 111
def scan_data_response
  raw_response = read_response(SCAN_DATA_RESPONSE_LENGTH)
  Rplidar::ScanDataResponse.new(raw_response).response
end
start_motor(pwm = 660) click to toggle source
# File lib/rplidar/driver.rb, line 48
def start_motor(pwm = 660)
  request_with_payload(COMMAND_MOTOR_PWM, pwm)
end
stop() click to toggle source
# File lib/rplidar/driver.rb, line 75
def stop
  command(COMMAND_STOP)
  clear_port
end
stop_motor() click to toggle source
# File lib/rplidar/driver.rb, line 52
def stop_motor
  request_with_payload(COMMAND_MOTOR_PWM, 0)
end