Skip to content

Commit 00a94c3

Browse files
author
dcyoung
committed
Support for sweep firmware v1.1
1 parent c97e933 commit 00a94c3

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Arduino Library for Scanse Sweep LiDAR
55
### Arduino
66
Currently the library has only been tested with an `Arduino Mega 2560`.
77
### Sweep Firmware
8-
Currently the library only supports sweep firmware `v1.0`. Firmware `v1.1` introduced a calibration routine and new commands that change the device's behavior. Firmware `v1.1` is significantly more robust for arduino applications, so support for `v1.1` will be coming soon.
8+
Currently the library only supports Sweep firmware `v1.1`, available [here](http://scanse.io/downloads). Earlier verisons (<=`v1.0`) did not support the communication protocol used by the latest firmware (`v1.1`) and will not work properly with this library.
99

1010
# Installation
1111
Copy the entire `Sweep/` folder to your `.../Arduino/libraries/` directory.
@@ -98,15 +98,17 @@ True if the device is currently scanning.
9898
```c++
9999
bool startScanning()
100100
```
101-
Signals the sweep device to start scanning. Initiates an indefinite stream of individual sensor readings until `stopScanning()` is called.
101+
Blocks until the sweep device is ready, then signals the device to start scanning. Initiates an indefinite stream of individual sensor readings until `stopScanning()` is called.
102102
During an active data stream, any attempt to communicate with the device other than `stopScanning()` will fail.
103103

104+
To avoid blocking, you can use the `getMotorReady()` command to query the current state of the device , and do work in the meantime if it isn't ready.
105+
104106

105107
```c++
106108
bool stopScanning()
107109
```
108110

109-
Signals the sweep device to stop scanning.
111+
Signals the sweep device to stop scanning. Will block for ~500ms to flush leftover data stream from the buffer and validate a second response from the sensor.
110112

111113
```c++
112114
bool getReading(ScanPacket &reading)
@@ -126,6 +128,19 @@ struct ScanPacket
126128

127129
Structure representing a single sensor reading (ranging). ie: a full 360deg scan would be composed of many such readings.
128130

131+
```c++
132+
bool getMotorReady()
133+
```
134+
135+
Returns true if the device is ready. A device is ready when the motor speed has stabilized to the current setting and the calibration routine is complete. If a device was just powered on, or the motor speed was just changed, it can take up to 6 seconds for the device to get ready. For visual confirmation, the blue LED on the face of the sweep will blink until the device is ready.
136+
137+
```c++
138+
bool waitUntilMotorReady()
139+
```
140+
141+
Blocks until the device is ready. Returns true if the device is ready, and false if the check timed out (max 8 seconds).
142+
143+
129144
```c++
130145
int32_t getMotorSpeed()
131146
```
@@ -148,7 +163,9 @@ MOTOR_SPEED_CODE_9_HZ
148163
MOTOR_SPEED_CODE_10_HZ
149164
```
150165
151-
Adjusts the motor speed setting to the provided code. Recommend users pass one of the const codes defined by library:
166+
Blocks until the device is ready, then adjusts the motor speed setting to the provided code. Recommend users pass one of the const codes defined by library.
167+
168+
To avoid blocking, you can use the `getMotorReady()` command to query the current state of the device , and do work in the meantime if it isn't ready.
152169
153170
```c++
154171
int32_t getSampleRate()
@@ -165,4 +182,4 @@ SAMPLE_RATE_CODE_750_HZ
165182
SAMPLE_RATE_CODE_1000_HZ
166183
```
167184
168-
Adjusts the sample rate setting to the provided code. Recommend users pass one of the const codes defined by library:
185+
Adjusts the sample rate setting to the provided code. Recommend users pass one of the const codes defined by library.

Sweep/Examples/MegaSerialPrinter/MegaSerialPrinter.ino

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@
44
55
MegaSerialPrinter:
66
- Example sketch for using the Scanse Sweep with the Arduino Mega 2560.
7-
Collects at least 3 complete scans, and then prints the sensor readings
7+
Collects 3 complete scans, and then prints the sensor readings
88
- Assumes Sweep sensor is physically connected to Serial #1 (RX1 & TX1)
99
- For the sweep's power, ground, RX & TX pins, follow the connector
1010
pinouts in the sweep user manual located here:
1111
http://scanse.io/downloads
1212
- Be sure to connect RX_device -> TX_arduino & TX_device -> RX_arduino
13-
- The sketch WILL work with the arduino running off USB power alone,
14-
however, the quantity and quality of sensor readings will drop.
15-
- The behavior of the sketch can be unreliable with both USB & powerjack
16-
powering the arduino. If the sketch doesn't seem to to work with both
17-
connected, try following sequence:
18-
- upload the sketch
19-
- unplug the USB connector
20-
- unplug/remove power to the power jack
21-
- plug in the USB connector
22-
- open the serial terminal from the arduino IDE
23-
- plug in the power to the power jack
24-
- send "start" over serial
25-
26-
Created by David C. Young, February 21, 2017.
13+
- You should run the sketch off USB power alone! Using both power sources
14+
(USB + power jack) can cause issues.
15+
- Note that running off of USB power is not entirely adequate for the sweep,
16+
so the quantity and qaulity of sensor readings will drop. This is OK for
17+
this example , as it is only meant to provide some visual feedback over
18+
the serial monitor.
19+
- In your own projects that do not involve the serial monitor, be sure to use
20+
dedicated power instead of the USB.
21+
22+
Created by Scanse LLC, February 21, 2017.
2723
Released into the public domain.
2824
*/
2925

@@ -167,8 +163,8 @@ bool verifyCurrentDeviceSettings()
167163
}
168164

169165
// Report the motor speed and sample rate to the computer terminal
170-
Serial.println("\nMotor Speed: " + String(currentMotorSpeed) + " HZ");
171-
Serial.println("Sample Rate: " + String(currentSampleRate) + " HZ");
166+
Serial.println("\nMotor Speed Setting: " + String(currentMotorSpeed) + " HZ");
167+
Serial.println("Sample Rate Setting: " + String(currentSampleRate) + " HZ");
172168

173169
return true;
174170
}
@@ -177,6 +173,7 @@ bool verifyCurrentDeviceSettings()
177173
bool beginDataCollectionPhase()
178174
{
179175
// Attempt to start scanning
176+
Serial.println("\nWaiting for motor speed to stabilize and calibration routine to complete...");
180177
bool bSuccess = device.startScanning();
181178
Serial.println(bSuccess ? "\nSuccessfully initiated scanning..." : "\nFailed to start scanning.");
182179
return bSuccess;

Sweep/Sweep.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ bool Sweep::startScanning()
1414
if (bIsScanning)
1515
return false;
1616

17+
// wait until the device is ready (calibration complete + motor stabilized)
18+
if (!waitUntilMotorReady())
19+
return false;
20+
1721
_writeCommand(_DATA_ACQUISITION_START);
1822
// wait for the receipt (possible timeout)
1923
if (_readResponseHeader())
@@ -66,6 +70,35 @@ bool Sweep::getReading(ScanPacket &reading)
6670
return false;
6771
}
6872

73+
bool Sweep::getMotorReady()
74+
{
75+
if (bIsScanning)
76+
return false;
77+
_writeCommand(_MOTOR_READY);
78+
if (_readResponseInfoSetting())
79+
{
80+
// TODO: validate receipt (hold off until performance hit is determined)
81+
const uint8_t readyCode[2] = {_responseInfoSetting[2], _responseInfoSetting[3]};
82+
// readyCode == 0 indicates device is ready
83+
return _ascii_bytes_to_integer(readyCode) == 0;
84+
}
85+
}
86+
87+
bool Sweep::waitUntilMotorReady()
88+
{
89+
if (bIsScanning)
90+
return false;
91+
// only check for 8 seconds (16 iterations with 500ms pause)
92+
for (uint8_t i = 0; i < 16; ++i)
93+
{
94+
if (getMotorReady())
95+
return true;
96+
delay(500);
97+
}
98+
// timeout after 8 seconds
99+
return false;
100+
}
101+
69102
int32_t Sweep::getMotorSpeed()
70103
{
71104
if (bIsScanning)
@@ -86,6 +119,10 @@ bool Sweep::setMotorSpeed(const uint8_t motorSpeedCode[2])
86119
if (bIsScanning)
87120
return false;
88121

122+
// wait until the device is ready (calibration complete + motor stabilized)
123+
if (!waitUntilMotorReady())
124+
return false;
125+
89126
_writeCommandWithArgument(_MOTOR_SPEED_ADJUST, motorSpeedCode);
90127
// wait for the receipt (possible timeout)
91128
if (_readResponseParam())

Sweep/Sweep.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Sweep
5656
// Destructor
5757
~Sweep();
5858

59-
// true if the device is currently scanning
59+
// True if the device is currently scanning
6060
bool bIsScanning;
6161

6262
/**
@@ -65,14 +65,14 @@ class Sweep
6565
* stream. This data stream continues indefinitely until
6666
* "stopScanning()" is called.
6767
*
68-
* @return True is start was successful, false otherwise.
68+
* @return True if start was successful
6969
*/
7070
bool startScanning();
7171

7272
/**
7373
* Terminates an active data stream.
7474
*
75-
* @return True is stop was successful, false otherwise.
75+
* @return True if stop was successful
7676
*/
7777
bool stopScanning();
7878

@@ -86,6 +86,23 @@ class Sweep
8686
*/
8787
bool getReading(ScanPacket &reading);
8888

89+
/**
90+
* Check if the device is ready. A device is ready if the
91+
* calibration routine is complete and the motor speed has
92+
* stabilized to the current speed setting.
93+
*
94+
* @return True if the device is ready
95+
*/
96+
bool getMotorReady();
97+
98+
/**
99+
* Waits (blocks) until the device is ready, or the wait
100+
* times out after 8 seconds. See getMotorReady for details.
101+
*
102+
* @return True if motor is ready, false if the wait timed out
103+
*/
104+
bool waitUntilMotorReady();
105+
89106
// Returns the current motor speed setting in HZ
90107
int32_t getMotorSpeed();
91108

@@ -118,6 +135,7 @@ class Sweep
118135
const uint8_t _COMMAND_TERMINATION = '\n';
119136
const uint8_t _DATA_ACQUISITION_START[2] = {'D', 'S'};
120137
const uint8_t _DATA_ACQUISITION_STOP[2] = {'D', 'X'};
138+
const uint8_t _MOTOR_READY[2] = {'M', 'Z'};
121139
const uint8_t _MOTOR_SPEED_ADJUST[2] = {'M', 'S'};
122140
const uint8_t _MOTOR_INFORMATION[2] = {'M', 'I'};
123141
const uint8_t _SAMPLE_RATE_ADJUST[2] = {'L', 'R'};

Sweep/keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ bIsScanning KEYWORD2
1515
startScanning KEYWORD2
1616
stopScanning KEYWORD2
1717
getReading KEYWORD2
18+
getMotorReady KEYWORD2
19+
waitUntilMotorReady KEYWORD2
1820
getMotorSpeed KEYWORD2
1921
setMotorSpeed KEYWORD2
2022
getSampleRate KEYWORD2

0 commit comments

Comments
 (0)