@@ -29,7 +29,24 @@ SfeXM125 radarSensor;
2929// I2C default address
3030uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
3131
32+ // Distance Values
3233uint32_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
3451void 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
85141void 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}
0 commit comments