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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: FollowDigitalVelocity
3 *
4 * Objective:
5 * This example demonstrates control of the ClearPath-MC operational mode
6 * Follow Digital Velocity Command, Unipolar PWM Command.
7 *
8 * Description:
9 * This example enables a ClearPath motor and executes velocity moves based
10 * on the state of an analog input sensor. During operation, various move
11 * statuses are written to the USB serial port.
12 * Consider using Manual Velocity Control mode instead if you do not wish to
13 * use an analog sensor to command velocity, if you require greater velocity
14 * command resolution (i.e. more commandable positions), or if HLFB is needed
15 * for "move done/at speed" status feedback.
16 *
17 * Requirements:
18 * 1. A ClearPath motor must be connected to Connector M-0.
19 * 2. The connected ClearPath motor must be configured through the MSP software
20 * for Follow Digital Velocity Command, Unipolar PWM Command mode (In MSP
21 * select Mode>>Velocity>>Follow Digital Velocity Command, then with
22 * "Unipolar PWM Command" selected hit the OK button).
23 * 3. The ClearPath must have a defined Max Speed configured through the MSP
24 * software (On the main MSP window fill in the "Max Speed (RPM)" box with
25 * your desired maximum speed). Ensure the value of maxSpeed below matches
26 * this Max Speed.
27 * 4. Ensure the "Invert PWM Input" checkbox found on the MSP's main window is
28 * unchecked.
29 * 5. Ensure the Input A filter in MSP is set to 20ms, (In MSP
30 * select Advanced>>Input A, B Filtering... then in the Settings box fill in
31 * the textbox labeled "Input A Filter Time Constant (msec)" then hit the OK
32 * button).
33 * 6. An analog input source (0-10V) connected to ConnectorA9 to control
34 * motor velocity.
35 *
36 *
37 * Links:
38 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
39 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
40 * ** ClearPath Manual (DC Power): https://www.teknic.com/files/downloads/clearpath_user_manual.pdf
41 * ** ClearPath Manual (AC Power): https://www.teknic.com/files/downloads/ac_clearpath-mc-sd_manual.pdf
42 * ** ClearPath Mode Informational Video: https://www.teknic.com/watch-video/#OpMode8
43 *
44 *
45 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
46 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
47 */
48
49#include "ClearCore.h"
50
51// Defines the motor's connector as ConnectorM0
52#define motor ConnectorM0
53
54// Defines the analog input to control commanded velocity
55#define AnalogSensor ConnectorA9
56
57// The INPUT_A_FILTER must match the Input A filter setting in MSP
58// (Advanced >> Input A, B Filtering...)
59#define INPUT_A_FILTER 20
60
61// Select the baud rate to match the target device.
62#define baudRate 9600
63
64// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
65#define SerialPort ConnectorUsb
66
67// This is the commanded speed limit in RPM (must match the MSP value). This speed
68// cannot actually be commanded, so use something slightly higher than your real
69// max speed here and in MSP
70double maxSpeed = 510;
71
72// Declares our user-defined helper function, which is used to command speed and
73// direction. The definition/implementation of this function is at the bottom of
74// the sketch.
75bool CommandVelocity(int32_t commandedVelocity);
76
77int main() {
78 // Set up an analog sensor to control commanded velocity.
79 AnalogSensor.Mode(Connector::INPUT_ANALOG);
80
81 // Sets all motor connectors to the correct mode for Follow Digital
82 // Velocity, Unipolar PWM mode.
83 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
84 Connector::CPM_MODE_A_DIRECT_B_PWM);
85
86 // Sets up serial communication and waits up to 5 seconds for a port to open.
87 // Serial communication is not required for this example to run.
88 SerialPort.Mode(Connector::USB_CDC);
89 SerialPort.Speed(baudRate);
90 uint32_t timeout = 5000;
91 uint32_t startTime = Milliseconds();
92 SerialPort.PortOpen();
93 while (!SerialPort && Milliseconds() - startTime < timeout) {
94 continue;
95 }
96
97 // Enables the motor
98 motor.EnableRequest(true);
99 SerialPort.SendLine("Motor Enabled");
100
101 while (true) {
102 // Read the voltage on the analog sensor (0-10V).
103 float analogVoltage = AnalogSensor.AnalogVoltage();
104 // Convert the voltage measured to a velocity within the valid range.
105 int32_t commandedVelocity =
106 static_cast<int32_t>(round(analogVoltage / 10 * maxSpeed));
107
108 // Move at the commanded velocity.
109 CommandVelocity(commandedVelocity); // See below for the detailed function definition.
110 }
111}
112
113/*------------------------------------------------------------------------------
114 * CommandVelocity
115 *
116 * Command the motor to move using a velocity of commandedVelocity
117 * Prints the move status to the USB serial port
118 *
119 * Parameters:
120 * int commandedVelocity - The velocity to command
121 *
122 * Returns: True/False depending on whether the velocity was successfully
123 * commanded.
124 */
125bool CommandVelocity(int32_t commandedVelocity) {
126 if (abs(commandedVelocity) >= abs(maxSpeed)) {
127 SerialPort.SendLine("Move rejected, requested velocity at or over the limit.");
128 return false;
129 }
130
131 // Check if an alert is currently preventing motion
132 if (motor.StatusReg().bit.AlertsPresent) {
133 SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
134 return false;
135 }
136
137 SerialPort.Send("Commanding velocity: ");
138 SerialPort.SendLine(commandedVelocity);
139
140 // Change ClearPath's Input A state to change direction.
141 // Note: this section of code was included so this commandVelocity function
142 // could be used to command negative (opposite direction) velocity. However the
143 // analog signal used by this example only commands positive velocities.
144 if (commandedVelocity >= 0) {
145 motor.MotorInAState(false);
146 }
147 else {
148 motor.MotorInAState(true);
149 }
150
151 // Delays to send the correct filtered direction.
152 Delay_ms(20 + INPUT_A_FILTER);
153
154 // Find the scaling factor of our velocity range mapped to the PWM duty
155 // cycle range (255 is the max duty cycle).
156 double scaleFactor = 255 / maxSpeed;
157
158 // Scale the velocity command to our duty cycle range.
159 uint8_t dutyRequest = abs(commandedVelocity) * scaleFactor;
160
161 // Command the move.
162 motor.MotorInBDuty(dutyRequest);
163
164 return true;
165}
166//------------------------------------------------------------------------------
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.
bool MotorModeSet(MotorPair motorPair, Connector::ConnectorModes newMode)
Sets the operational mode for the specified MotorDriver connectors.