Skip to content

Commit cec3406

Browse files
committed
Added New/Modified Examples
1 parent d0ad410 commit cec3406

File tree

25 files changed

+2883
-263
lines changed

25 files changed

+2883
-263
lines changed

libraries/ClearPathModeExamples/ClearPath-MC Series/FollowDigitalPosition/FollowDigitalPosition.ino

+60-8
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,26 @@
6767
// Select the baud rate to match the target device.
6868
#define baudRate 9600
6969

70+
// This example has built-in functionality to automatically clear motor faults.
71+
// Any uncleared fault will cancel and disallow motion.
72+
// WARNING: enabling automatic fault handling will clear faults immediately when
73+
// encountered and return a motor to a state in which motion is allowed. Before
74+
// enabling this functionality, be sure to understand this behavior and ensure
75+
// your system will not enter an unsafe state.
76+
// To enable automatic fault handling, #define HANDLE_MOTOR_FAULTS (1)
77+
// To disable automatic fault handling, #define HANDLE_MOTOR_FAULTS (0)
78+
#define HANDLE_MOTOR_FAULTS (0)
79+
7080
// Defines the bounds for our position range. These positions cannot actually be
7181
// commanded, only values inside this range.
7282
double positionZeroPWM = 0;
7383
double positionMaxPWM = 10000;
7484

75-
// Declares our user-defined functions, which are used to pass the Command Lock
76-
// sensor state and send position commands to the motor. The implementations of
77-
// these functions are at the bottom of the sketch.
85+
// Declares user-defined helper functions.
86+
// The definition/implementations of these functions are at the bottom of the sketch.
7887
void LockSensorCallback();
7988
bool CommandPosition(int commandedPosition);
89+
void HandleMotorFaults();
8090

8191
void setup() {
8292
// Put your setup code here, it will only run once:
@@ -116,10 +126,24 @@ void setup() {
116126

117127
// Waits for HLFB to assert (waits for homing to complete if applicable)
118128
Serial.println("Waiting for HLFB...");
119-
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
129+
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
130+
!motor.StatusReg().bit.MotorInFault) {
120131
continue;
121132
}
122-
Serial.println("Motor Ready");
133+
// Check if a motor faulted during enabling
134+
// Clear fault if configured to do so
135+
if (motor.StatusReg().bit.MotorInFault) {
136+
Serial.println("Motor fault detected.");
137+
if(HANDLE_MOTOR_FAULTS){
138+
HandleMotorFaults();
139+
} else {
140+
Serial.println("Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
141+
}
142+
Serial.println("Enabling may not have completed as expected. Proceed with caution.");
143+
Serial.println();
144+
} else {
145+
Serial.println("Motor Ready");
146+
}
123147
}
124148

125149

@@ -152,9 +176,15 @@ bool CommandPosition(int commandedPosition) {
152176
return false;
153177
}
154178

155-
// Check if an alert is currently preventing motion
156-
if (motor.StatusReg().bit.AlertsPresent) {
157-
Serial.println("Motor status: 'In Alert'. Move Canceled.");
179+
// Check if a motor fault is currently preventing motion
180+
// Clear fault if configured to do so
181+
if (motor.StatusReg().bit.MotorInFault) {
182+
if(HANDLE_MOTOR_FAULTS){
183+
Serial.println("Motor fault detected. Move canceled.");
184+
HandleMotorFaults();
185+
} else {
186+
Serial.println("Motor fault detected. Move canceled. Enable automatic fault handling by setting HANDLE_MOTOR_FAULTS to 1.");
187+
}
158188
return false;
159189
}
160190

@@ -192,3 +222,25 @@ void LockSensorCallback() {
192222
motor.MotorInAState(digitalRead(LockSensor));
193223
}
194224
//------------------------------------------------------------------------------
225+
226+
/*------------------------------------------------------------------------------
227+
* HandleMotorFaults
228+
*
229+
* Clears motor faults by cycling enable to the motor.
230+
* Assumes motor is in fault
231+
* (this function is called when motor.StatusReg.MotorInFault == true)
232+
*
233+
* Parameters:
234+
* requires "motor" to be defined as a ClearCore motor connector
235+
*
236+
* Returns:
237+
* none
238+
*/
239+
void HandleMotorFaults(){
240+
Serial.println("Handling fault: clearing faults by cycling enable signal to motor.");
241+
motor.EnableRequest(false);
242+
Delay_ms(10);
243+
motor.EnableRequest(true);
244+
Delay_ms(100);
245+
}
246+
//------------------------------------------------------------------------------

libraries/ClearPathModeExamples/ClearPath-MC Series/FollowDigitalTorque/FollowDigitalTorque.ino

+2-8
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,7 @@ bool CommandTorque(int commandedTorque) {
135135
Serial.println("Move rejected, invalid torque requested");
136136
return false;
137137
}
138-
139-
// Check if an alert is currently preventing motion
140-
if (motor.StatusReg().bit.AlertsPresent) {
141-
Serial.println("Motor status: 'In Alert'. Move Canceled.");
142-
return false;
143-
}
144-
138+
145139
Serial.print("Commanding torque: ");
146140
Serial.println(commandedTorque);
147141

@@ -178,4 +172,4 @@ bool CommandTorque(int commandedTorque) {
178172
Serial.println("Move Done");
179173
return true;
180174
}
181-
//------------------------------------------------------------------------------
175+
//------------------------------------------------------------------------------

libraries/ClearPathModeExamples/ClearPath-MC Series/FollowDigitalVelocity/FollowDigitalVelocity.ino

+1-7
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ bool CommandVelocity(long commandedVelocity) {
124124
return false;
125125
}
126126

127-
// Check if an alert is currently preventing motion
128-
if (motor.StatusReg().bit.AlertsPresent) {
129-
Serial.println("Motor status: 'In Alert'. Move Canceled.");
130-
return false;
131-
}
132-
133127
Serial.print("Commanding Velocity: ");
134128
Serial.println(commandedVelocity);
135129

@@ -158,4 +152,4 @@ bool CommandVelocity(long commandedVelocity) {
158152

159153
return true;
160154
}
161-
//------------------------------------------------------------------------------
155+
//------------------------------------------------------------------------------

libraries/ClearPathModeExamples/ClearPath-MC Series/FollowDigitalVelocityWithVariableTorque/FollowDigitalVelocityWithVariableTorque.ino

-6
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ bool CommandVelocity(int commandedVelocity) {
151151
return false;
152152
}
153153

154-
// Check if an alert is currently preventing motion
155-
if (motor.StatusReg().bit.AlertsPresent) {
156-
Serial.println("Motor status: 'In Alert'. Move Canceled.");
157-
return false;
158-
}
159-
160154
Serial.print("Commanding velocity: ");
161155
Serial.println(commandedVelocity);
162156

libraries/ClearPathModeExamples/ClearPath-MC Series/ManualVelocity/ManualVelocity.ino

+79-13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
// Select the baud rate to match the target device.
5656
#define baudRate 9600
5757

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+
5868
// This is the variable used to keep track of the current commanded velocity
5969
double commandedVelocity = 0;
6070

@@ -68,10 +78,10 @@ int maxVelocityCCW = 1000;
6878
// command velocity with a finer resolution
6979
double velocityResolution = 2.0;
7080

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.
7483
bool MoveAtVelocity(double velocity);
84+
void HandleMotorFaults();
7585

7686
void setup() {
7787
// Put your setup code here, it will run once:
@@ -104,12 +114,26 @@ void setup() {
104114
motor.EnableRequest(true);
105115
Serial.println("Motor Enabled");
106116

107-
// Waits for HLFB to assert.
117+
// Waits for HLFB to assert
108118
Serial.println("Waiting for HLFB...");
109-
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
119+
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
120+
!motor.StatusReg().bit.MotorInFault) {
110121
continue;
111122
}
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+
}
113137
}
114138

115139
void loop() {
@@ -164,9 +188,15 @@ bool MoveAtVelocity(double velocity) {
164188
return false;
165189
}
166190

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+
}
170200
return false;
171201
}
172202

@@ -214,11 +244,47 @@ bool MoveAtVelocity(double velocity) {
214244
Serial.println("Ramping Speed... Waiting for HLFB");
215245
// Wait for some time so HLFB has time to transition.
216246
delay(1);
217-
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
247+
while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED &&
248+
!motor.StatusReg().bit.MotorInFault) {
218249
continue;
219250
}
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);
223289
}
224290
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)