-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample07_DistanceThresholdSettings.ino
216 lines (179 loc) · 5.74 KB
/
Example07_DistanceThresholdSettings.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Example 7: Distance Threshold Settings
Using the Acconeer XM125 A121 60GHz Pulsed Coherent Radar Sensor.
This example shows how operate the XM125 when the device is in Distance Reading Mode.
The sensor is initialized, then the distance amplitude, and strength , fixed
amplitude, and sensitivity thresholds are set.
By: Madison Chodikov
SparkFun Electronics
Date: 2024/1/22
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Hardware Connections:
QWIIC --> QWIIC
Serial.print it out at 115200 baud to serial monitor.
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/ - Qwiic XM125 Breakout
*/
#include "SparkFun_Qwiic_XM125_Arduino_Library.h"
#include <Arduino.h>
SparkFunXM125Distance radarSensor;
// I2C default address
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
// Setup Variables
uint32_t startVal = 0;
uint32_t endVal = 0;
uint32_t numDistances = 9;
uint32_t calibrateNeeded = 0;
uint32_t measDistErr = 0;
// Error statuses
uint32_t errorStatus = 0;
// Distance Variables
int32_t distancePeakStrength0 = 0;
uint32_t distancePeak0 = 0;
int32_t distancePeakStrength1 = 0;
uint32_t distancePeak1 = 0;
void setup()
{
// Start serial
Serial.begin(115200);
Serial.println("XM125 Example 7: Distance Threshold Settings");
Serial.println("");
Wire.begin();
// If begin is successful (0), then start example
if (radarSensor.begin(i2cAddress, Wire) == 1)
{
Serial.println("Begin");
}
else // Otherwise, infinite loop
{
Serial.println("Device failed to setup - Freezing code.");
while (1)
; // Runs forever
}
// Distance Sensor Setup
// Reset sensor configuration to reapply configuration registers
radarSensor.setCommand(SFE_XM125_DISTANCE_RESET_MODULE);
radarSensor.busyWait();
// Check error and busy bits
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
delay(100);
// Set Start register
if (radarSensor.setStart(100) != 0)
{
Serial.println("Distance Start Error");
}
radarSensor.getStart(startVal);
Serial.print("Start Val: ");
Serial.println(startVal);
delay(100);
// Set End register
if (radarSensor.setEnd(4000) != 0)
{
Serial.println("Distance End Error");
}
radarSensor.getEnd(endVal);
Serial.print("End Val: ");
Serial.println(endVal);
delay(100);
// Set Distance Threshold Settings - Threshold Sensitivity (range: 0 to 1000)
radarSensor.setThresholdSensitivity(100);
// Set Fixed Amplitude Threshold
radarSensor.setFixedAmpThreshold(100);
// Apply configuration
if (radarSensor.setCommand(SFE_XM125_DISTANCE_APPLY_CONFIGURATION) != 0)
{
// Check for errors
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
Serial.println("Configuration application error");
}
// Poll detector status until busy bit is cleared
if (radarSensor.busyWait() != 0)
{
Serial.print("Busy wait error");
}
// Check detector status
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
Serial.println();
delay(1000);
}
void loop()
{
// Check error bits
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
// Start detector
if (radarSensor.setCommand(SFE_XM125_DISTANCE_START_DETECTOR) != 0)
{
Serial.println("Start detector error");
}
// Poll detector status until busy bit is cleared - CHECK ON THIS!
if (radarSensor.busyWait() != 0)
{
Serial.println("Busy wait error");
}
// Verify that no error bits are set in the detector status register
radarSensor.getDetectorErrorStatus(errorStatus);
if (errorStatus != 0)
{
Serial.print("Detector status error: ");
Serial.println(errorStatus);
}
// Check MEASURE_DISTANCE_ERROR for measurement failed
radarSensor.getMeasureDistanceError(measDistErr);
if (measDistErr == 1)
{
Serial.println("Measure Distance Error");
}
// Recalibrate device if calibration error is triggered
radarSensor.getCalibrationNeeded(calibrateNeeded);
if (calibrateNeeded == 1)
{
Serial.println("Calibration Needed - Recalibrating.. ");
// Calibrate device (write RECALIBRATE command)
radarSensor.setCommand(SFE_XM125_DISTANCE_RECALIBRATE);
}
// Read PeakX Distance and PeakX Strength registers for the number of distances detected
radarSensor.getPeak0Distance(distancePeak0);
radarSensor.getPeak0Strength(distancePeakStrength0);
radarSensor.getPeak1Distance(distancePeak1);
radarSensor.getPeak1Strength(distancePeakStrength1);
// Read out 2 distances and peaks with threshold settings adjusted
if (distancePeak0 != 0)
{
Serial.print("Peak0 Distance, Strength: ");
Serial.print(distancePeak0);
Serial.print("mm, ");
Serial.println(distancePeakStrength0);
Serial.println();
}
if (distancePeak1 != 0)
{
Serial.print("Peak1 Distance, Strength: ");
Serial.print(distancePeak1);
Serial.print("mm, ");
Serial.println(distancePeakStrength1);
Serial.println();
}
// Half a second delay for easier readings
delay(500);
}