Skip to content

Commit 30fe729

Browse files
Functional Distance Sensor Code
Examples need to be completed, but the basic readings is now functional
1 parent ebac59d commit 30fe729

File tree

6 files changed

+335
-162
lines changed

6 files changed

+335
-162
lines changed
1.07 MB
Binary file not shown.

examples/Example01_DistanceBasicReadings/Example01_DistanceBasicReadings.ino

Lines changed: 140 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,24 @@ SfeXM125 radarSensor;
2929
// I2C default address
3030
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
3131

32+
// Distance Values
3233
uint32_t distanceVal = 0;
34+
uint32_t startVal = 0;
35+
uint32_t endVal = 0;
36+
uint32_t numDistances = 9;
37+
uint32_t calibrateNeeded = 0;
38+
uint32_t measDistErr = 0;
39+
40+
// Error statuses
41+
uint32_t errorStatus = 0;
42+
uint32_t busyError = 0;
43+
44+
45+
// TEST Values
46+
uint32_t distanceRegVal = 0;
47+
uint32_t distancePeak0 = 0;
48+
uint32_t distancePeakStrength0 = 0;
49+
3350

3451
void setup()
3552
{
@@ -51,50 +68,147 @@ void setup()
5168
while(1); // Runs forever
5269
}
5370

54-
// Default start = 1000; Default stop = 5000
55-
if(radarSensor.distanceBegin() != 0)
56-
{
57-
Serial.println("Sensor started successfully");
58-
}
59-
else
71+
// Distance Sensor Setup
72+
// Reset sensor configuration to reapply configuration registers
73+
radarSensor.setDistanceCommand(SFE_XM125_DISTANCE_RESET_MODULE);
74+
75+
radarSensor.distanceBusyWait();
76+
77+
// Check error and busy bits
78+
radarSensor.getDistanceDetectorErrorStatus(errorStatus);
79+
if(errorStatus != 0)
6080
{
61-
Serial.println("Sensor not initialized correctly - Freezing code.");
62-
while(1);
81+
Serial.print("Detector status error: ");
82+
Serial.println(errorStatus);
6383
}
6484

65-
66-
// Test code below - delete once complete
67-
radarSensor.setDistanceStart(1000);
68-
radarSensor.setDistanceEnd(5000);
6985
delay(100);
70-
71-
uint32_t startVal = 0;
72-
uint32_t endVal = 0;
86+
87+
// Set Start register
88+
if(radarSensor.setDistanceStart(300) != 0)
89+
{
90+
Serial.println("Distance Start Error");
91+
}
7392
radarSensor.getDistanceStart(startVal);
74-
radarSensor.getDistanceEnd(endVal);
7593
Serial.print("Start Val: ");
7694
Serial.println(startVal);
95+
96+
delay(100);
97+
// Set End register
98+
if(radarSensor.setDistanceEnd(4000) != 0)
99+
{
100+
Serial.println("Distance End Error");
101+
}
102+
radarSensor.getDistanceEnd(endVal);
77103
Serial.print("End Val: ");
78104
Serial.println(endVal);
105+
delay(100);
79106

80-
delay(1000);
107+
// Apply configuration
108+
if(radarSensor.setDistanceCommand(SFE_XM125_DISTANCE_APPLY_CONFIGURATION) != 0)
109+
{
110+
// Check for errors
111+
radarSensor.getDistanceDetectorErrorStatus(errorStatus);
112+
if(errorStatus != 0)
113+
{
114+
Serial.print("Detector status error: ");
115+
Serial.println(errorStatus);
116+
}
117+
118+
Serial.println("Configuration application error");
119+
}
120+
121+
// Poll detector status until busy bit is cleared
122+
if(radarSensor.distanceBusyWait() != 0)
123+
{
124+
Serial.print("Busy wait error");
125+
}
126+
127+
// Check detector status
128+
radarSensor.getDistanceDetectorErrorStatus(errorStatus);
129+
if(errorStatus != 0)
130+
{
131+
Serial.print("Detector status error: ");
132+
Serial.println(errorStatus);
133+
}
81134

135+
Serial.println();
136+
137+
delay(1000);
82138

83139
}
84140

85141
void loop()
86142
{
87-
// Request Distance Data from the device
88-
radarSensor.setDistanceCommand(SFE_XM125_DISTANCE_START_DETECTOR);
89-
radarSensor.distanceBusyWait();
90-
radarSensor.getDistancePeak0Distance(distanceVal);
143+
// Check error bits
144+
radarSensor.getDistanceDetectorErrorStatus(errorStatus);
145+
if(errorStatus != 0)
146+
{
147+
Serial.print("Detector status error: ");
148+
Serial.println(errorStatus);
149+
}
150+
151+
152+
// Start detector
153+
if(radarSensor.setDistanceCommand(SFE_XM125_DISTANCE_START_DETECTOR) != 0)
154+
{
155+
Serial.println("Start detector error");
156+
}
157+
158+
// Poll detector status until busy bit is cleared - CHECK ON THIS!
159+
if(radarSensor.distanceBusyWait() != 0)
160+
{
161+
Serial.println("Busy wait error");
162+
}
163+
164+
// Verify that no error bits are set in the detector status register
165+
radarSensor.getDistanceDetectorErrorStatus(errorStatus);
166+
if(errorStatus != 0)
167+
{
168+
Serial.print("Detector status error: ");
169+
Serial.println(errorStatus);
170+
}
91171

92-
if(distanceVal != 0)
172+
// Read Detector Result register
173+
radarSensor.getDistanceReg(distanceRegVal);
174+
// DELETE THIS AFTER TESTING
175+
Serial.print("Distance Error Register: 0b");
176+
Serial.println(distanceRegVal, BIN);
177+
178+
// Check MEASURE_DISTANCE_ERROR for measurement failed
179+
radarSensor.getDistanceMeasureDistanceError(measDistErr);
180+
if(measDistErr == 1)
93181
{
94-
Serial.print("Peak Distance Found: ");
95-
Serial.print(distanceVal);
96-
Serial.println(" mm");
182+
Serial.println("Measure Distance Error");
97183
}
98184

99-
delay(100);
185+
// If CALIBRATION_NEEDED is set, then write RECALIBRATE command
186+
radarSensor.getDistanceCalibrationNeeded(calibrateNeeded);
187+
if(calibrateNeeded == 1)
188+
{
189+
Serial.println("Calibration Needed - Recalibrating.. ");
190+
// Calibrate device (write RECALIBRATE command)
191+
radarSensor.setDistanceCommand(SFE_XM125_DISTANCE_RECALIBRATE);
192+
}
193+
194+
// Read how many peak distances can be detected
195+
196+
radarSensor.getDistancePeak0Distance(distancePeak0);
197+
radarSensor.getDistancePeak0Strength(distancePeakStrength0);
198+
199+
Serial.print("Peak 0 Distance: ");
200+
Serial.println(distancePeak0);
201+
Serial.print("Peak 0 Strength: ");
202+
Serial.println(distancePeakStrength0);
203+
// Read PeakX Distance and PeakX Strength registers for the number of distances detected
204+
// for(int i = 0; i <= numDistances; i++)
205+
// {
206+
// // Check thru each peak distance detected
207+
// }
208+
209+
// Half a second delay for easier readings
210+
delay(500);
211+
212+
// Empty space for easier readings
213+
Serial.println();
100214
}

examples/Example04_PresenceBasicReadings/Example04_PresenceBasicReadings.ino

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,15 @@ SfeXM125 radarSensor;
2929
// I2C default address
3030
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
3131

32-
// Value to fill presence distance (in mm)
32+
// Presence distance values
3333
uint32_t distance = 0;
3434
uint32_t presenceDetected = 0;
3535
uint32_t presenceDetectedSticky = 0;
36-
uint32_t errorStatus = 0;
37-
uint32_t regErr = 0;
38-
39-
// DELETE TEST VARIABLES ONCE COMPLETE
4036
uint32_t startVal = 0;
4137
uint32_t endVal = 0;
42-
uint32_t counter = 0;
43-
uint32_t intra = 0;
44-
uint32_t regVal = 0;
45-
uint32_t busyBit = 0;
38+
39+
// Error statuses
40+
uint32_t errorStatus = 0;
4641
uint32_t busyError = 0;
4742

4843
void setup()
@@ -70,8 +65,7 @@ void setup()
7065

7166
delay(200);
7267

73-
74-
// Test code setup below:
68+
// Presence Sensor Setup
7569
// Reset sensor configuration to reapply configuration registers
7670
radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE);
7771

@@ -96,7 +90,7 @@ void setup()
9690

9791
delay(100);
9892
// Set End register
99-
if(radarSensor.setPresenceEnd(5200) != 0)
93+
if(radarSensor.setPresenceEnd(7000) != 0)
10094
{
10195
Serial.println("Presence End Error");
10296
}
@@ -119,11 +113,10 @@ void setup()
119113
Serial.println("Configuration application error");
120114
}
121115

122-
// Poll detector status until busy bit is cleared - CHECK ON THIS!
116+
// Poll detector status until busy bit is cleared
123117
if(radarSensor.presenceBusyWait() != 0)
124118
{
125-
Serial.print("Busy wait error: ");
126-
Serial.println(radarSensor.getPresenceRegisterVal(busyError));
119+
Serial.print("Busy wait error");
127120
}
128121

129122
// Check detector status
@@ -148,10 +141,8 @@ void setup()
148141
// Serial.println("Sensor not initialized correctly - Freezing code.");
149142
// while(1); // Runs forever
150143
// }
151-
152144

153145
delay(1000);
154-
//while(1);
155146
}
156147

157148
void loop()
@@ -182,21 +173,13 @@ void loop()
182173
radarSensor.getPresenceDetectorErrorStatus(errorStatus);
183174
if(errorStatus != 0)
184175
{
185-
Serial.println("Detector status error");
176+
Serial.print("Detector status error: ");
177+
Serial.println(errorStatus);
186178
}
187179

188-
189-
// Read detector result register and determine
180+
// Read detector result register and determine detection status
190181
radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
191182
radarSensor.getPresenceDetectorPresenceStickyDetected(presenceDetectedSticky);
192-
radarSensor.getPresenceDetectorRegError(regErr);
193-
radarSensor.getPresenceRegisterVal(regVal);
194-
195-
196-
// Serial.print("Presence Detected: ");
197-
// Serial.println(presenceDetected);
198-
// Serial.print("Presence Detected Sticky: ");
199-
// Serial.println(presenceDetectedSticky);
200183

201184
if((presenceDetected == 1) | (presenceDetectedSticky == 1))
202185
{
@@ -206,35 +189,6 @@ void loop()
206189
Serial.println("mm");
207190
}
208191

209-
//radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE);
210-
211-
Serial.println();
212-
192+
// Delay 1 second between readings
213193
delay(1000);
214-
215-
216-
// ***** PREVIOUS CODE BELOW *****
217-
218-
// radarSensor.setPresenceCommand(XM125_PRESENCE_APPLY_CONFIGURATION);
219-
// // If Presence is detected, then print out distance from device
220-
// radarSensor.presenceBusyWait();
221-
// radarSensor.getPresenceDetectorPresenceDetected(presenceDetected);
222-
// Serial.print("Presence Detector flag: ");
223-
// Serial.println(presenceDetected);
224-
225-
// radarSensor.getPresenceDistance(distance);
226-
// Serial.print("Presence detected at ");
227-
// Serial.print(distance);
228-
// Serial.println("mm");
229-
230-
// if(presenceDetected == 1)
231-
// {
232-
// radarSensor.getPresenceDistance(distance);
233-
// Serial.print("Presence detected at ");
234-
// Serial.print(distance);
235-
// Serial.println("mm");
236-
// }
237-
238-
// Read out at a slower rate for easier reading (0.5 second delay)
239-
// delay(500);
240194
}

0 commit comments

Comments
 (0)