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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: AsgWithMeasuredTorque
3 *
4 * Objective:
5 * This example demonstrates how to configure and read the High-Level
6 * Feedback output mode "ASG-Position with Measured Torque" or "ASG-Velocity
7 * with Measured Torque" from a ClearPath motor.
8 *
9 * Note: There are two different versions of the ASG with Measured Torque
10 * HLFB mode, one for Position modes and one for Velocity modes. See the
11 * ClearPath MC/SD manual for a full description of all HLFB modes.
12 *
13 * Description:
14 * This example reads the state of an attached ClearPath motor's HLFB output
15 * in All Systems Go with Measured Torque mode. During operation, the state
16 * of the HLFB is written to the USB serial port.
17 *
18 * This example does not enable the motor or command any motion. Use the
19 * Motion Generator in MSP to easily exercise the full features of this
20 * example and see HLFB change state and give a torque measurement.
21 *
22 * Requirements:
23 * 1. A ClearPath motor must be connected to Connector M-0.
24 * 2. To command motion, the connected ClearPath motor must be configured for
25 * the Motion Generator mode through the MSP software (In MSP select Mode>>
26 * Motion Generator).
27 * 3. The connected ClearPath motor must have its HLFB mode set to ASG with
28 * measured torque through the MSP software (select Advanced>>High Level
29 * Feedback [Mode]... then choose "ASG-Position, w/Measured Torque" or
30 * "ASG-Velocity, w/ Measured Torque" and hit the OK button).
31 * Select a 482 Hz PWM Carrier Frequency in this menu.
32 *
33 * Links:
34 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
35 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
36 *
37 *
38 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
39 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
40 */
41
42#include "ClearCore.h"
43
44// Specify which motor to move.
45// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
46#define motor ConnectorM0
47
48// Select the baud rate to match the target serial device
49#define baudRate 9600
50
51// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
52#define SerialPort ConnectorUsb
53
54int main() {
55 // Put the motor connector into the HLFB mode to read bipolar PWM (the
56 // correct mode for ASG w/ Measured Torque)
57 motor.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
58 // Set the HFLB carrier frequency to 482 Hz
59 motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
60
61 // Set up serial communication and wait up to 5 seconds for a port to open
62 // HLFB states are written to the serial port
63 SerialPort.Mode(Connector::USB_CDC);
64 SerialPort.Speed(baudRate);
65 uint32_t timeout = 5000;
66 uint32_t startTime = Milliseconds();
67 SerialPort.PortOpen();
68 while (!SerialPort && Milliseconds() - startTime < timeout) {
69 continue;
70 }
71
72 while (true) {
73 SerialPort.Send("HLFB state: ");
74
75 // Check the current state of the ClearPath's HLFB.
76 MotorDriver::HlfbStates hlfbState = motor.HlfbState();
77
78 // Write the HLFB state to the serial port
79 if (hlfbState == MotorDriver::HLFB_HAS_MEASUREMENT) {
80 // Writes the torque measured, as a percent of motor peak torque rating
81 SerialPort.Send(int8_t(round(motor.HlfbPercent())));
82 SerialPort.SendLine("% torque");
83 }
84 else if (hlfbState == MotorDriver::HLFB_ASSERTED) {
85 // Asserted indicates either "Move Done" for position modes, or
86 // "At Target Velocity" for velocity moves
87 SerialPort.SendLine("ASSERTED");
88 }
89 else {
90 SerialPort.SendLine("DISABLED or SHUTDOWN");
91 }
92
93 // Wait before reading HLFB again
94 Delay_ms(500);
95 }
96}
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.