ClearCore Library
Loading...
Searching...
No Matches
SpeedOutput.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: SpeedOutput
3 *
4 * Objective:
5 * This example demonstrates how to configure and read-in the High-Level
6 * Feedback mode "Speed Output" of a ClearPath motor.
7 *
8 * This HLFB mode is available in ClearPath-MC series servos, in every
9 * ClearPath-MC operational mode except Ramp Up/Down to Selected Velocity.
10 *
11 * Description:
12 * This example reads the state of an attached ClearPath motor's HLFB output
13 * when configured for "Speed Output". During operation, the state of HLFB
14 * and calculated measured speed are written to the USB serial port.
15 *
16 * This example does not enable the motor or command motion. Use the
17 * "Override Inputs" feature in MSP to command motion and see changes in the
18 * HLFB measurement.
19 *
20 * Requirements:
21 * 1. A ClearPath motor must be connected to Connector M-0.
22 * 2. The connected ClearPath motor must be configured through the MSP software
23 * for an operational mode compatible with Speed Output HLFB mode (see above)
24 * 3. The connected ClearPath motor must have its HLFB mode set to "Speed Output"
25 * (select Advanced>>High Level Feedback [Mode]... then choose "Speed Output"
26 * from the dropdown and hit the OK button).
27 * Select a 482 Hz PWM Carrier Frequency in this menu.
28 *
29 * Links:
30 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
31 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
32 *
33 *
34 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
35 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
36 */
37
38#include "ClearCore.h"
39
40// Specify which motor to move.
41// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
42#define motor ConnectorM0
43
44// Select the baud rate to match the target serial device
45#define baudRate 9600
46
47// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
48#define SerialPort ConnectorUsb
49
50int main() {
51 // Put the motor connector into the correct HLFB mode to read the Speed
52 // Output PWM signal and convert it to percent of Max Speed.
53 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_PWM);
54 // Set the HFLB carrier frequency to 482 Hz
55 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
56
57 // Set up serial communication at a baud rate of baudRate (9600 bps) then
58 // wait up to 5 seconds for a port to open.
59 SerialPort.Mode(Connector::USB_CDC);
60 SerialPort.Speed(baudRate);
61 uint32_t timeout = 5000;
62 uint32_t startTime = Milliseconds();
63 SerialPort.PortOpen();
64 while (!SerialPort && Milliseconds() - startTime < timeout) {
65 continue;
66 }
67
68 while (true) {
69 // Use the MSP application to enable and move the motor. The duty cycle of
70 // the HFLB output will be refreshed and displayed every 0.5 seconds.
71
72 // Check the state of the HLFB.
73 MotorDriver::HlfbStates hlfbState = motor.HlfbState();
74
75 // Print the HLFB state.
76 if (hlfbState == MotorDriver::HLFB_HAS_MEASUREMENT) {
77 // Get the measured speed as a percent of Max Speed.
78 float hlfbPercent = motor.HlfbPercent();
79
80 SerialPort.Send("Speed output: ");
81
82 if (hlfbPercent == MotorDriver::HLFB_DUTY_UNKNOWN) {
83 SerialPort.SendLine("UNKNOWN");
84 }
85 else {
86 char hlfbPercentStr[10];
87 // Convert the floating point duty cycle into a string representation.
88 snprintf(hlfbPercentStr, sizeof(hlfbPercentStr), "%.0f%%", hlfbPercent);
89 SerialPort.Send(hlfbPercentStr);
90 SerialPort.SendLine(" of maximum speed");
91 }
92 }
93 else if (hlfbState == MotorDriver::HLFB_DEASSERTED) {
94 SerialPort.SendLine("Disabled or Shutdown");
95 }
96
97 // Wait 0.5 secs before reading the HLFB again.
98 Delay_ms(500);
99 }
100}
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.