1
+
2
+ /*
3
+ Scanse Sweep Arduino Library Examples
4
+
5
+ MegaSerialPrinter:
6
+ - 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
8
+ - Assumes Sweep sensor is physically connected to Serial #1 (RX1 & TX1)
9
+ - For the sweep's power, ground, RX & TX pins, follow the connector
10
+ pinouts in the sweep user manual located here:
11
+ http://scanse.io/downloads
12
+ - Be sure to connect RX_device -> TX_arduino & TX_device -> RX_arduino
13
+ - If the arduino is running off USB power alone, the quantity and
14
+ quality of sensor readings will drop
15
+
16
+ Created by David C. Young, February 21, 2017.
17
+ Released into the public domain.
18
+ */
19
+
20
+ #include < Sweep.h>
21
+
22
+ // Create a Sweep device using Serial #1 (RX1 & TX1)
23
+ Sweep device (Serial1);
24
+ // Scan packet struct, used to store info for a single reading
25
+ ScanPacket reading;
26
+
27
+ // keeps track of how many scans have been collected
28
+ uint8_t scanCount = 0 ;
29
+ // keeps track of how many samples have been collected
30
+ uint16_t sampleCount = 0 ;
31
+
32
+ // Arrays to store attributes of collected scans
33
+ bool syncValues[500 ]; // 1 -> first reading of new scan, 0 otherwise
34
+ float angles[500 ]; // in degrees (accurate to the millidegree)
35
+ uint16_t distances[500 ]; // in cm
36
+ uint8_t signalStrengths[500 ]; // 0:255, higher is better
37
+
38
+ // Finite States for the program sequence
39
+ const uint8_t STATE_WAIT_FOR_USER_INPUT = 0 ;
40
+ const uint8_t STATE_ADJUST_DEVICE_SETTINGS = 1 ;
41
+ const uint8_t STATE_VERIFY_CURRENT_DEVICE_SETTINGS = 2 ;
42
+ const uint8_t STATE_BEGIN_DATA_ACQUISITION = 3 ;
43
+ const uint8_t STATE_GATHER_DATA = 4 ;
44
+ const uint8_t STATE_STOP_DATA_ACQUISITION = 5 ;
45
+ const uint8_t STATE_REPORT_COLLECTED_DATA = 6 ;
46
+ const uint8_t STATE_RESET = 7 ;
47
+ const uint8_t STATE_ERROR = 8 ;
48
+
49
+ // Current state in the program sequence
50
+ uint8_t currentState;
51
+
52
+ // String to collect user input over serial
53
+ String userInput = " " ;
54
+
55
+ void setup ()
56
+ {
57
+ // Initialize serial
58
+ Serial.begin (9600 ); // serial terminal on the computer
59
+ Serial1.begin (115200 ); // sweep device
60
+
61
+ // reserve space to accumulate user message
62
+ userInput.reserve (50 );
63
+
64
+ // initialize counter variables and reset the current state
65
+ reset ();
66
+ }
67
+
68
+ // Loop functions as an FSM (finite state machine)
69
+ void loop ()
70
+ {
71
+ switch (currentState)
72
+ {
73
+ case STATE_WAIT_FOR_USER_INPUT:
74
+ if (listenForUserInput ())
75
+ currentState++;
76
+ break ;
77
+ case STATE_ADJUST_DEVICE_SETTINGS:
78
+ currentState = adjustDeviceSettings () ? currentState + 1 : STATE_ERROR;
79
+ break ;
80
+ case STATE_VERIFY_CURRENT_DEVICE_SETTINGS:
81
+ currentState = verifyCurrentDeviceSettings () ? currentState + 1 : STATE_ERROR;
82
+ break ;
83
+ case STATE_BEGIN_DATA_ACQUISITION:
84
+ currentState = beginDataCollectionPhase () ? currentState + 1 : STATE_ERROR;
85
+ break ;
86
+ case STATE_GATHER_DATA:
87
+ gatherSensorReading ();
88
+ if (scanCount > 3 )
89
+ currentState++;
90
+ break ;
91
+ case STATE_STOP_DATA_ACQUISITION:
92
+ currentState = stopDataCollectionPhase () ? currentState + 1 : STATE_ERROR;
93
+ break ;
94
+ case STATE_REPORT_COLLECTED_DATA:
95
+ printCollectedData ();
96
+ currentState++;
97
+ break ;
98
+ case STATE_RESET:
99
+ reset ();
100
+ currentState = 0 ;
101
+ break ;
102
+ default : // there was some error
103
+ // DO NOTHING
104
+ break ;
105
+ }
106
+ }
107
+
108
+ // checks if the user has communicated anything over serial
109
+ // looks for the user to send "start"
110
+ bool listenForUserInput ()
111
+ {
112
+ while (Serial.available ())
113
+ {
114
+ userInput += (char )Serial.read ();
115
+ }
116
+ if (userInput.indexOf (" start" ) != -1 )
117
+ {
118
+ Serial.println (" Registered user start." );
119
+ return true ;
120
+ }
121
+ return false ;
122
+ }
123
+
124
+ // Adjusts the device settings
125
+ bool adjustDeviceSettings ()
126
+ {
127
+ // Set the motor speed to 5HZ (codes available from 1->10 HZ)
128
+ bool bSuccess = device.setMotorSpeed (MOTOR_SPEED_CODE_5_HZ);
129
+ Serial.println (bSuccess ? " \n Successfully set motor speed." : " \n Failed to set motor speed" );
130
+
131
+ /*
132
+ // Device will always default to 500HZ scan rate when it is powered on.
133
+ // Snippet below is left for reference.
134
+ // Set the sample rate to 500HZ (codes available for 500, 750 and 1000 HZ)
135
+ bool bSuccess = device.setSampleRate(SAMPLE_RATE_CODE_500_HZ);
136
+ Serial.println(bSuccess ? "\nSuccessfully set sample rate." : "\nFailed to set sample rate.");
137
+ */
138
+ return bSuccess;
139
+ }
140
+
141
+ // Querries the current device settings (motor speed and sample rate)
142
+ // and prints them to the console
143
+ bool verifyCurrentDeviceSettings ()
144
+ {
145
+ // Read the current motor speed and sample rate
146
+ int32_t currentMotorSpeed = device.getMotorSpeed ();
147
+ if (currentMotorSpeed < 0 )
148
+ {
149
+ Serial.println (" \n Failed to get current motor speed" );
150
+ return false ;
151
+ }
152
+ int32_t currentSampleRate = device.getSampleRate ();
153
+ if (currentSampleRate < 0 )
154
+ {
155
+ Serial.println (" \n Failed to get current sample rate" );
156
+ return false ;
157
+ }
158
+
159
+ // Report the motor speed and sample rate to the computer terminal
160
+ Serial.println (" \n Motor Speed: " + String (currentMotorSpeed) + " HZ" );
161
+ Serial.println (" Sample Rate: " + String (currentSampleRate) + " HZ" );
162
+
163
+ return true ;
164
+ }
165
+
166
+ // Initiates the data collection phase (begins scanning)
167
+ bool beginDataCollectionPhase ()
168
+ {
169
+ // Attempt to start scanning
170
+ bool bSuccess = device.startScanning ();
171
+ Serial.println (bSuccess ? " \n Successfully initiated scanning..." : " \n Failed to start scanning." );
172
+ return bSuccess;
173
+ }
174
+
175
+ // Gathers individual sensor readings until 3 complete scans have been collected
176
+ void gatherSensorReading ()
177
+ {
178
+ // attempt to get the next scan packet
179
+ // Note: getReading() will write values into the "reading" variable
180
+ if (device.getReading (reading))
181
+ {
182
+ // check if this reading was the very first reading of a new 360 degree scan
183
+ if (reading.bIsSync )
184
+ scanCount++;
185
+
186
+ // don't collect more than 3 scans
187
+ if (scanCount > 3 )
188
+ return ;
189
+
190
+ // store the info for this sample
191
+ syncValues[sampleCount] = reading.bIsSync ;
192
+ angles[sampleCount] = reading.angle ;
193
+ distances[sampleCount] = reading.distance ;
194
+ signalStrengths[sampleCount] = reading.signalStrength ;
195
+
196
+ // increment sample count
197
+ sampleCount++;
198
+ }
199
+ }
200
+
201
+ // Terminates the data collection phase (stops scanning)
202
+ bool stopDataCollectionPhase ()
203
+ {
204
+ // Attempt to stop scanning
205
+ bool bSuccess = device.stopScanning ();
206
+
207
+ Serial.println (bSuccess ? " \n Successfully stopped scanning." : " \n Failed to stop scanning." );
208
+ return bSuccess;
209
+ }
210
+
211
+ // Prints the collected data to the console
212
+ // (only prints the complete scans, ignores the first partial)
213
+ void printCollectedData ()
214
+ {
215
+ Serial.println (" \n Printing info for the collected scans (NOT REAL-TIME):" );
216
+
217
+ int indexOfFirstSyncReading = 0 ;
218
+ // don't print the trailing readings from the first partial scan
219
+ while (!syncValues[indexOfFirstSyncReading])
220
+ {
221
+ indexOfFirstSyncReading++;
222
+ }
223
+ // print the readings for all the complete scans
224
+ for (int i = indexOfFirstSyncReading; i < sampleCount; i++)
225
+ {
226
+ if (syncValues[i])
227
+ {
228
+ Serial.println (" \n ----------------------NEW SCAN----------------------" );
229
+ }
230
+ Serial.println (" Angle: " + String (angles[i], 3 ) + " , Distance: " + String (distances[i]) + " , Signal Strength: " + String (signalStrengths[i]));
231
+ }
232
+ }
233
+
234
+ // Resets the variables and state so the sequence can be repeated
235
+ void reset ()
236
+ {
237
+ scanCount = 0 ;
238
+ sampleCount = 0 ;
239
+ Serial.flush ();
240
+ userInput = " " ;
241
+ Serial.println (" \n\n Whenever you are ready, type \" start\" to to begin the sequence..." );
242
+ currentState = 0 ;
243
+ }
0 commit comments