ClearCore Library
Loading...
Searching...
No Matches
ClearPathModeExamples/ClearPath-SD_Series/MotorStatusRegister/MotorStatusRegister.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: MotorStatusRegister
3 *
4 * Objective:
5 * This example demonstrates how to read and display bits in the ClearCore's
6 * MotorDriver status register.
7 *
8 * Description:
9 * This example gets a snapshot of the status register for each MotorDriver
10 * connector with an attached motor. Then, the state of the status register
11 * bits is printed to the USB serial port.
12 *
13 * Requirements:
14 * ** A ClearPath motor must be connected to Connector M-0.
15 *
16 * Links:
17 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
18 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
19 *
20 *
21 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
22 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
23 */
24
25#include "ClearCore.h"
26
27// Select the baud rate to match the target device.
28#define baudRate 9600
29
30// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
31#define SerialPort ConnectorUsb
32
33// The containers for our motor objects. If only some of the motor connectors
34// are being used, remove the unused entries from the following arrays and
35// reduce motorConnectorCount.
36MotorDriver *motorConnectors[] = {&ConnectorM0, &ConnectorM1,
37 &ConnectorM2, &ConnectorM3
38 };
39char motorConnectorNames[][4] = { "M-0", "M-1", "M-2", "M-3" };
40uint8_t motorConnectorCount = 4;
41
42// Hold a string representation of each motor's ready state.
43char *readyStateStr;
44
45// Declare our helper function so that it may be used before it is defined.
46char *ReadyStateString(MotorDriver::MotorReadyStates readyState);
47
48int main() {
49 // Set up serial communication at a baud rate of 9600 bps then wait up to
50 // 5 seconds for a port to open.
51 // Serial communication is not required for this example to run, however the
52 // example will appear to do nothing without serial output.
53 SerialPort.Mode(Connector::USB_CDC);
54 SerialPort.Speed(baudRate);
55 uint32_t timeout = 5000;
56 uint32_t startTime = Milliseconds();
57 SerialPort.PortOpen();
58 while (!SerialPort && Milliseconds() - startTime < timeout) {
59 continue;
60 }
61
62 while (true) {
63 // Get a copy of the motor status register for each motor connector.
64 for (uint8_t i = 0; i < motorConnectorCount; i++) {
65 MotorDriver *motor = motorConnectors[i];
66 volatile const MotorDriver::StatusRegMotor &statusReg = motor->StatusReg();
67 volatile const MotorDriver::AlertRegMotor &alertReg = motor->AlertReg();
68
69 SerialPort.Send("Motor status register for motor M");
70 SerialPort.Send(i);
71 SerialPort.Send(": ");
72 SerialPort.SendLine(statusReg.reg, 2); // prints the status register in binary
73
74 SerialPort.Send("AtTargetPosition: ");
75 SerialPort.SendLine(statusReg.bit.AtTargetPosition);
76
77 SerialPort.Send("StepsActive: ");
78 SerialPort.SendLine(statusReg.bit.StepsActive);
79
80 SerialPort.Send("AtTargetVelocity: ");
81 SerialPort.SendLine(statusReg.bit.AtTargetVelocity);
82
83 SerialPort.Send("MoveDirection: ");
84 SerialPort.SendLine(statusReg.bit.MoveDirection);
85
86 SerialPort.Send("MotorInFault: ");
87 SerialPort.SendLine(statusReg.bit.MotorInFault);
88
89 SerialPort.Send("Enabled: ");
90 SerialPort.SendLine(statusReg.bit.Enabled);
91
92 SerialPort.Send("PositionalMove: ");
93 SerialPort.SendLine(statusReg.bit.PositionalMove);
94
95 SerialPort.Send("HLFB State: ");
96 switch (statusReg.bit.HlfbState) {
97 case 0:
98 SerialPort.SendLine("HLFB_DEASSERTED");
99 break;
100 case 1:
101 SerialPort.SendLine("HLFB_ASSERTED");
102 break;
103 case 2:
104 SerialPort.SendLine("HLFB_HAS_MEASUREMENT");
105 break;
106 case 3:
107 SerialPort.SendLine("HLFB_UNKNOWN");
108 break;
109 default:
110 // something has gone wrong if this is printed
111 SerialPort.SendLine("???");
112 }
113
114 SerialPort.Send("AlertsPresent: ");
115 SerialPort.SendLine(statusReg.bit.AlertsPresent);
116
117 SerialPort.Send("Ready state: ");
118 switch (statusReg.bit.ReadyState) {
119 case MotorDriver::MOTOR_DISABLED:
120 SerialPort.SendLine("Disabled");
121 break;
122 case MotorDriver::MOTOR_ENABLING:
123 SerialPort.SendLine("Enabling");
124 break;
125 case MotorDriver::MOTOR_FAULTED:
126 SerialPort.SendLine("Faulted");
127 break;
128 case MotorDriver::MOTOR_READY:
129 SerialPort.SendLine("Ready");
130 break;
131 case MotorDriver::MOTOR_MOVING:
132 SerialPort.SendLine("Moving");
133 break;
134 default:
135 // something has gone wrong if this is printed
136 SerialPort.SendLine("???");
137 }
138
139 SerialPort.Send("Triggering: ");
140 SerialPort.SendLine(statusReg.bit.Triggering);
141
142 SerialPort.Send("InPositiveLimit: ");
143 SerialPort.SendLine(statusReg.bit.InPositiveLimit);
144
145 SerialPort.Send("InNegativeLimit: ");
146 SerialPort.SendLine(statusReg.bit.InNegativeLimit);
147
148 SerialPort.Send("InEStopSensor: ");
149 SerialPort.SendLine(statusReg.bit.InEStopSensor);
150
151 SerialPort.SendLine("--------------------------------");
152
153
154 if (statusReg.bit.AlertsPresent){
155 SerialPort.Send("Alert register: ");
156 SerialPort.SendLine(alertReg.reg, 2); // prints the alert register in binary
157
158 SerialPort.Send("MotionCanceledInAlert: ");
159 SerialPort.SendLine(alertReg.bit.MotionCanceledInAlert);
160
161 SerialPort.Send("MotionCanceledPositiveLimit: ");
162 SerialPort.SendLine(alertReg.bit.MotionCanceledPositiveLimit);
163
164 SerialPort.Send("MotionCanceledNegativeLimit: ");
165 SerialPort.SendLine(alertReg.bit.MotionCanceledNegativeLimit);
166
167 SerialPort.Send("MotionCanceledSensorEStop: ");
168 SerialPort.SendLine(alertReg.bit.MotionCanceledSensorEStop);
169
170 SerialPort.Send("MotionCanceledMotorDisabled: ");
171 SerialPort.SendLine(alertReg.bit.MotionCanceledMotorDisabled);
172
173 SerialPort.Send("MotorFaulted: ");
174 SerialPort.SendLine(alertReg.bit.MotorFaulted);
175
176 SerialPort.SendLine("--------------------------------");
177 }
178 }
179
180 // Wait a few seconds then repeat...
181 Delay_ms(5000);
182 }
183}
184
185/*------------------------------------------------------------------------------
186 * ReadyStateString
187 *
188 * Converts the state of a motor status register bit into a user-readable
189 * format so it may be printed to a serial port.
190 *
191 * Parameters:
192 * MotorReadyStates readyState - The current state of the ReadyState bit
193 *
194 * Returns: Text describing the state of the status bit.
195 */
196char *ReadyStateString(MotorDriver::MotorReadyStates readyState) {
197 switch (readyState) {
198 case MotorDriver::MOTOR_DISABLED:
199 return (char *)"Disabled";
200 case MotorDriver::MOTOR_ENABLING:
201 return (char *)"Enabling";
202 case MotorDriver::MOTOR_FAULTED:
203 return (char *)"Faulted";
204 case MotorDriver::MOTOR_READY:
205 return (char *)"Ready";
206 case MotorDriver::MOTOR_MOVING:
207 return (char *)"Moving";
208 default:
209 // Something has gone wrong if this is printed
210 return (char *)"???";
211 }
212}
213//------------------------------------------------------------------------------
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.