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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: StepAndDirection
3 *
4 * Objective:
5 * This example demonstrates control of a third party Step and
6 * Direction motor using a ClearCore motor connector.
7 * This example is NOT intended to be used with ClearPath servos.
8 * There are other examples created specifically for ClearPath.
9 *
10 * Description:
11 * This example enables a motor then commands a series of repeating
12 * moves to the motor.
13 *
14 * Requirements:
15 * 1. A motor capable of step and direction must be connected to Connector M-0.
16 * 2. The motor may optionally be connected to the MotorDriver's HLFB line if
17 * the motor has a "servo on" type feedback feature.
18 *
19 * Links:
20 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
21 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
22 *
23 *
24 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
25 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
26 */
27
28#include "ClearCore.h"
29
30// Specifies which motor to move.
31// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
32#define motor ConnectorM0
33
34// Select the baud rate to match the target serial device
35#define baudRate 9600
36
37// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
38#define SerialPort ConnectorUsb
39
40// Define the velocity and acceleration limits to be used for each move
41int32_t velocityLimit = 10000; // pulses per sec
42int32_t accelerationLimit = 100000; // pulses per sec^2
43
44// Declares our user-defined helper function, which is used to command moves to
45// the motor. The definition/implementation of this function is at the bottom
46// of the example.
47void MoveDistance(int32_t distance);
48
49int main() {
50 // Sets the input clocking rate.
51 MotorMgr.MotorInputClocking(MotorManager::CLOCK_RATE_LOW);
52
53 // Sets all motor connectors into step and direction mode.
54 MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
55 Connector::CPM_MODE_STEP_AND_DIR);
56
57 // These lines may be uncommented to invert the output signals of the
58 // Enable, Direction, and HLFB lines. Some motors may have input polarities
59 // that are inverted from the ClearCore's polarity.
60 //motor.PolarityInvertSDEnable(true);
61 //motor.PolarityInvertSDDirection(true);
62 //motor.PolarityInvertSDHlfb(true);
63
64 // Sets the maximum velocity for each move
65 motor.VelMax(velocityLimit);
66
67 // Set the maximum acceleration for each move
68 motor.AccelMax(accelerationLimit);
69
70 // Sets up serial communication and waits up to 5 seconds for a port to open.
71 // Serial communication is not required for this example to run.
72 SerialPort.Mode(Connector::USB_CDC);
73 SerialPort.Speed(baudRate);
74 uint32_t timeout = 5000;
75 uint32_t startTime = Milliseconds();
76 SerialPort.PortOpen();
77 while (!SerialPort && Milliseconds() - startTime < timeout) {
78 continue;
79 }
80
81 // Enables the motor.
82 motor.EnableRequest(true);
83
84 // Waits for HLFB to assert. Uncomment these lines if your motor has a
85 // "servo on" feature and it is wired to the HLFB line on the connector.
86 //SerialPort.SendLine("Waiting for HLFB...");
87 //while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
88 // continue;
89 //}
90 SerialPort.SendLine("Motor Ready");
91
92 while (true) {
93 // Move 6400 counts (positive direction), then wait 2000ms
94 MoveDistance(6400);
95 Delay_ms(2000);
96 // Move 19200 counts farther positive, then wait 2000ms
97 MoveDistance(19200);
98 Delay_ms(2000);
99 // Move back 12800 counts (negative direction), then wait 2000ms
100 MoveDistance(-12800);
101 Delay_ms(2000);
102 // Move back 6400 counts (negative direction), then wait 2000ms
103 MoveDistance(-6400);
104 Delay_ms(2000);
105 // Move back to the start (negative 6400 pulses), then wait 2000ms
106 MoveDistance(-6400);
107 Delay_ms(2000);
108 }
109}
110
111/*------------------------------------------------------------------------------
112 * MoveDistance
113 *
114 * Command "distance" number of step pulses away from the current position
115 * Prints the move status to the USB serial port
116 * Returns when step pulses have completed
117 *
118 * Parameters:
119 * int distance - The distance, in step pulses, to move
120 *
121 * Returns: None
122 */
123void MoveDistance(int32_t distance) {
124 SerialPort.Send("Moving distance: ");
125 SerialPort.SendLine(distance);
126
127 // Command the move of incremental distance
128 motor.Move(distance);
129
130 // Waits for all step pulses to output
131 SerialPort.SendLine("Moving... Waiting for the step output to finish...");
132 while (!motor.StepsComplete()) {
133 continue;
134 }
135
136 SerialPort.SendLine("Steps Complete");
137}
138//------------------------------------------------------------------------------
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 MotorInputClocking(MotorClockRates newRate)
Sets the output step rate for the motor step generators.
bool MotorModeSet(MotorPair motorPair, Connector::ConnectorModes newMode)
Sets the operational mode for the specified MotorDriver connectors.