55
55
// Select the baud rate to match the target device.
56
56
#define baudRate 9600
57
57
58
+ // This example has built-in functionality to automatically clear motor faults.
59
+ // Any uncleared fault will cancel and disallow motion.
60
+ // WARNING: enabling automatic fault handling will clear faults immediately when
61
+ // encountered and return a motor to a state in which motion is allowed. Before
62
+ // enabling this functionality, be sure to understand this behavior and ensure
63
+ // your system will not enter an unsafe state.
64
+ // To enable automatic fault handling, #define HANDLE_MOTOR_FAULTS (1)
65
+ // To disable automatic fault handling, #define HANDLE_MOTOR_FAULTS (0)
66
+ #define HANDLE_MOTOR_FAULTS (0 )
67
+
58
68
// This is the variable used to keep track of the current commanded velocity
59
69
double commandedVelocity = 0 ;
60
70
@@ -68,10 +78,10 @@ int maxVelocityCCW = 1000;
68
78
// command velocity with a finer resolution
69
79
double velocityResolution = 2.0 ;
70
80
71
- // Declares our user-defined helper function, which is used to send a velocity
72
- // command. The definition/implementation of this function is at the bottom of
73
- // the sketch.
81
+ // Declares user-defined helper functions.
82
+ // The definition/implementations of these functions are at the bottom of the sketch.
74
83
bool MoveAtVelocity (double velocity);
84
+ void HandleMotorFaults ();
75
85
76
86
void setup () {
77
87
// Put your setup code here, it will run once:
@@ -104,12 +114,26 @@ void setup() {
104
114
motor.EnableRequest (true );
105
115
Serial.println (" Motor Enabled" );
106
116
107
- // Waits for HLFB to assert.
117
+ // Waits for HLFB to assert
108
118
Serial.println (" Waiting for HLFB..." );
109
- while (motor.HlfbState () != MotorDriver::HLFB_ASSERTED) {
119
+ while (motor.HlfbState () != MotorDriver::HLFB_ASSERTED &&
120
+ !motor.StatusReg ().bit .MotorInFault ) {
110
121
continue ;
111
122
}
112
- Serial.println (" Motor Ready" );
123
+ // Check if a motor faulted during enabling
124
+ // Clear fault if configured to do so
125
+ if (motor.StatusReg ().bit .MotorInFault ) {
126
+ Serial.println (" Motor fault detected." );
127
+ if (HANDLE_MOTOR_FAULTS){
128
+ HandleMotorFaults ();
129
+ } else {
130
+ Serial.println (" Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1." );
131
+ }
132
+ Serial.println (" Enabling may not have completed as expected. Proceed with caution." );
133
+ Serial.println ();
134
+ } else {
135
+ Serial.println (" Motor Ready" );
136
+ }
113
137
}
114
138
115
139
void loop () {
@@ -164,9 +188,15 @@ bool MoveAtVelocity(double velocity) {
164
188
return false ;
165
189
}
166
190
167
- // Check if an alert is currently preventing motion
168
- if (motor.StatusReg ().bit .AlertsPresent ) {
169
- Serial.println (" Motor status: 'In Alert'. Move Canceled." );
191
+ // Check if a motor fault is currently preventing motion
192
+ // Clear fault if configured to do so
193
+ if (motor.StatusReg ().bit .MotorInFault ) {
194
+ if (HANDLE_MOTOR_FAULTS){
195
+ Serial.println (" Motor fault detected. Move canceled." );
196
+ HandleMotorFaults ();
197
+ } else {
198
+ Serial.println (" Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1." );
199
+ }
170
200
return false ;
171
201
}
172
202
@@ -214,11 +244,47 @@ bool MoveAtVelocity(double velocity) {
214
244
Serial.println (" Ramping Speed... Waiting for HLFB" );
215
245
// Wait for some time so HLFB has time to transition.
216
246
delay (1 );
217
- while (motor.HlfbState () != MotorDriver::HLFB_ASSERTED) {
247
+ while (motor.HlfbState () != MotorDriver::HLFB_ASSERTED &&
248
+ !motor.StatusReg ().bit .MotorInFault ) {
218
249
continue ;
219
250
}
220
-
221
- Serial.println (" Target Velocity Reached" );
222
- return true ;
251
+ // Check if a motor faulted during move
252
+ // Clear fault if configured to do so
253
+ if (motor.StatusReg ().bit .MotorInFault ) {
254
+ Serial.println (" Motor fault detected." );
255
+ if (HANDLE_MOTOR_FAULTS){
256
+ HandleMotorFaults ();
257
+ } else {
258
+ Serial.println (" Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1." );
259
+ }
260
+ Serial.println (" Motion may not have completed as expected. Proceed with caution." );
261
+ Serial.println ();
262
+ return false ;
263
+ } else {
264
+ Serial.println (" Move Done" );
265
+ return true ;
266
+ }
267
+ }
268
+ // ------------------------------------------------------------------------------
269
+
270
+ /* ------------------------------------------------------------------------------
271
+ * HandleMotorFaults
272
+ *
273
+ * Clears motor faults by cycling enable to the motor.
274
+ * Assumes motor is in fault
275
+ * (this function is called when motor.StatusReg.MotorInFault == true)
276
+ *
277
+ * Parameters:
278
+ * requires "motor" to be defined as a ClearCore motor connector
279
+ *
280
+ * Returns:
281
+ * none
282
+ */
283
+ void HandleMotorFaults (){
284
+ Serial.println (" Handling fault: clearing faults by cycling enable signal to motor." );
285
+ motor.EnableRequest (false );
286
+ Delay_ms (10 );
287
+ motor.EnableRequest (true );
288
+ Delay_ms (100 );
223
289
}
224
290
// ------------------------------------------------------------------------------
0 commit comments