@@ -29,7 +29,24 @@ SfeXM125 radarSensor;
29
29
// I2C default address
30
30
uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS;
31
31
32
+ // Distance Values
32
33
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
+
33
50
34
51
void setup ()
35
52
{
@@ -51,50 +68,147 @@ void setup()
51
68
while (1 ); // Runs forever
52
69
}
53
70
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 )
60
80
{
61
- Serial.println ( " Sensor not initialized correctly - Freezing code. " );
62
- while ( 1 );
81
+ Serial.print ( " Detector status error: " );
82
+ Serial. println (errorStatus );
63
83
}
64
84
65
-
66
- // Test code below - delete once complete
67
- radarSensor.setDistanceStart (1000 );
68
- radarSensor.setDistanceEnd (5000 );
69
85
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
+ }
73
92
radarSensor.getDistanceStart (startVal);
74
- radarSensor.getDistanceEnd (endVal);
75
93
Serial.print (" Start Val: " );
76
94
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);
77
103
Serial.print (" End Val: " );
78
104
Serial.println (endVal);
105
+ delay (100 );
79
106
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
+ }
81
134
135
+ Serial.println ();
136
+
137
+ delay (1000 );
82
138
83
139
}
84
140
85
141
void loop ()
86
142
{
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
+ }
91
171
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 )
93
181
{
94
- Serial.print (" Peak Distance Found: " );
95
- Serial.print (distanceVal);
96
- Serial.println (" mm" );
182
+ Serial.println (" Measure Distance Error" );
97
183
}
98
184
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 ();
100
214
}
0 commit comments