Implement rest of the methods.
Still no error handling yet, gonna test what works first.
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
"repositoryURL": "https://vato.ddns.net/gitlab/vato007/SwiftSerial.git",
|
"repositoryURL": "https://vato.ddns.net/gitlab/vato007/SwiftSerial.git",
|
||||||
"state": {
|
"state": {
|
||||||
"branch": "dtr_support",
|
"branch": "dtr_support",
|
||||||
"revision": "9799f402e77e5d69b0811b6c714b172d9ccdd768",
|
"revision": "34e59a7d8766f7097eb68779ec039e77a1eec78a",
|
||||||
"version": null
|
"version": null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ class RPLidar{
|
|||||||
|
|
||||||
init(serialPort: SerialPort) throws {
|
init(serialPort: SerialPort) throws {
|
||||||
self.serialPort = serialPort
|
self.serialPort = serialPort
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@@ -145,8 +146,8 @@ class RPLidar{
|
|||||||
return (status, errorCode)
|
return (status, errorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func clearInput(){
|
public func clearInput() throws{
|
||||||
|
try serialPort?.readData(ofLength: (serialPort?.inWaiting)!)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func stop() throws{
|
public func stop() throws{
|
||||||
@@ -170,7 +171,7 @@ class RPLidar{
|
|||||||
|
|
||||||
try sendCommand(Constants.SCAN.asData())
|
try sendCommand(Constants.SCAN.asData())
|
||||||
let (dataSize, isSingle, dataType) = try readDescriptor()!
|
let (dataSize, isSingle, dataType) = try readDescriptor()!
|
||||||
if (dataSize != 5){
|
if dataSize != 5 {
|
||||||
|
|
||||||
}
|
}
|
||||||
if isSingle {
|
if isSingle {
|
||||||
@@ -179,13 +180,35 @@ class RPLidar{
|
|||||||
if dataType != Constants.SCAN_TYPE {
|
if dataType != Constants.SCAN_TYPE {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Need to check in waiting or something...
|
||||||
for _ in 0..<500{
|
for _ in 0..<500{
|
||||||
let raw = try readResponse(Int(dataSize))
|
let raw = try readResponse(Int(dataSize))
|
||||||
|
if maxBufferMeasurements > 0 {
|
||||||
|
let dataInWaiting = serialPort?.inWaiting
|
||||||
|
if dataInWaiting! > maxBufferMeasurements {
|
||||||
|
print("Too many measurements in the input buffer. Clearing Buffer")
|
||||||
|
try serialPort?.readData(ofLength: dataInWaiting! / Int(dataSize) * Int(dataSize))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: Support cancelling of measurements. Would it already work though?
|
||||||
|
try onMeasure(processScan(raw: raw))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func iterScans(maxBufferMeasurements: Int = 500, minLength: Int = 5, _ onScan: ScanHandler){
|
public func iterScans(maxBufferMeasurements: Int = 500, minLength: Int = 5, _ onScan: ScanHandler) throws {
|
||||||
|
var scan: [LidarScan] = []
|
||||||
|
try iterMeasurements{ measurement in
|
||||||
|
if measurement.newScan {
|
||||||
|
if scan.count > minLength {
|
||||||
|
onScan(scan)
|
||||||
|
}
|
||||||
|
scan = []
|
||||||
|
}
|
||||||
|
if measurement.quality > 0 && measurement.distance > 0 {
|
||||||
|
scan.append(measurement)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,10 +243,10 @@ class RPLidar{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if (descriptor.count != Constants.DESCRIPTOR_LEN){
|
if (descriptor.count != Constants.DESCRIPTOR_LEN){
|
||||||
|
return
|
||||||
}
|
}
|
||||||
else if (descriptor[0...1] != Data([Constants.SYNC, Constants.SYNC2])){
|
else if (descriptor[0...1] != Data([Constants.SYNC, Constants.SYNC2])){
|
||||||
|
return
|
||||||
}
|
}
|
||||||
let isSingle = descriptor[descriptor.count - 2] == 0
|
let isSingle = descriptor[descriptor.count - 2] == 0
|
||||||
return (descriptor[2], isSingle, descriptor[descriptor.count - 1])
|
return (descriptor[2], isSingle, descriptor[descriptor.count - 1])
|
||||||
@@ -234,7 +257,7 @@ class RPLidar{
|
|||||||
return Data()
|
return Data()
|
||||||
}
|
}
|
||||||
if(data.count != dataSize){
|
if(data.count != dataSize){
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user