#include "ClearCore.h"
// Specifies which motor to move.
// Options are: ConnectorM0, ConnectorM1, ConnectorM2, or ConnectorM3.
#define S1 ConnectorM0
#define S2 ConnectorM1
#define S3 ConnectorM2
#define S4 ConnectorM3
//Define constants
const int R1 = DI8; //Recipe 1
const int R2 = A9; //Recipe 2
const int R3 = A10; //Recipe 3
const int R4 = A11; //Recipe 4
const int R5 = A12; //Recipe 5
const int TF = IO0; //IO0 Output Table full light
const int AL = IO1; //IO1 Alarm for motor move fail
const int GL = IO2; //IO2 Output for greenlight
const int RFP = IO3; //IO3 output to robot signaling "Ready For Parts"
const int PC = IO4; //IO4 output to robot for parts clear
const int DSO = IO5; //IO5 output to robot from door switch
const int DSI = DI6; //DI6 Input connected to door switch
const int PB = DI7; //DI7 Input connected to Button
int state = 0; //HIGH / LOW
int lastState = HIGH; //HIGH / LOW
int PBCounter = 0;
// Declares our user-defined helper function, which is used to command moves to
// the motor. The definition/implementation of this function is at the bottom
// of the example
bool MoveAbsolutePosition(int32_t position);
// Select the baud rate to match the target serial device
#define baudRate 9600
// Define the velocity and acceleration limits to be used for each move
int velocityLimit = 10000; // pulses per sec
int accelerationLimit = 100000; // pulses per sec^2
// Declares our user-defined helper function, which is used to command moves to
// the motor. The definition/implementation of this function is at the bottom
// of the example
bool MoveDistance(int distance);
void setup() {
// Put your setup code here, it will only run once:
pinMode(IO0, OUTPUT);
pinMode(IO1, OUTPUT);
pinMode(IO2, OUTPUT);
pinMode(IO3, OUTPUT);
pinMode(IO4, OUTPUT);
pinMode(IO5, OUTPUT);
pinMode(DI6, INPUT);
pinMode(DI7, INPUT);
pinMode(DI8, INPUT);
pinMode(A9, INPUT);
pinMode(A10, INPUT);
pinMode(A11, INPUT);
pinMode(A12, INPUT);
// Sets the input clocking rate. This normal rate is ideal for ClearPath
// step and direction applications.
MotorMgr.MotorInputClocking(MotorManager::CLOCK_RATE_NORMAL);
// Sets all motor connectors into step and direction mode.
MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,
Connector::CPM_MODE_STEP_AND_DIR);
// Set the motor's HLFB mode to bipolar PWM
S1.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
S2.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
S3.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
S4.HlfbMode(MotorDriver::HLFB_MODE_HAS_BIPOLAR_PWM);
// Set the HFLB carrier frequency to 482 Hz
S1.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
S2.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
S3.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
S4.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
// Sets the maximum velocity for each move
S1.VelMax(velocityLimit);
S2.VelMax(velocityLimit);
S3.VelMax(velocityLimit);
S4.VelMax(velocityLimit);
// Set the maximum acceleration for each move
S1.AccelMax(accelerationLimit);
S2.AccelMax(accelerationLimit);
S3.AccelMax(accelerationLimit);
S4.AccelMax(accelerationLimit);
// Sets up serial communication and waits up to 5 seconds for a port to open.
// Serial communication is not required for this example to run.
Serial.begin(baudRate);
uint32_t timeout = 5000;
uint32_t startTime = millis();
while (!Serial && millis() - startTime < timeout) {
continue;
}
// Enables the motor; homing will begin automatically if enabled
S1.EnableRequest(true);
Serial.println("S1 Enabled");
S2.EnableRequest(true);
Serial.println("S2 Enabled");
S3.EnableRequest(true);
Serial.println("S3 Enabled");
S4.EnableRequest(true);
Serial.println("S4 Enabled");
// Waits for HLFB to assert (waits for homing to complete if applicable)
Serial.println("Waiting for HLFB...");
while (S1.HlfbState() != MotorDriver::HLFB_ASSERTED)
Serial.println("Waiting for HLFB...");
while (S2.HlfbState() != MotorDriver::HLFB_ASSERTED)
Serial.println("Waiting for HLFB...");
while (S3.HlfbState() != MotorDriver::HLFB_ASSERTED)
Serial.println("Waiting for HLFB...");
while (S4.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Motors Ready");
}
void loop() {
// Put your main code here, it will run repeatedly:
// Read the state of the input connector.
state = digitalRead(R1);
// Display the state of the input connector.
Serial.print("R1 Input state: ");
if (state = LOW) {
Serial.println("ON");
// Move to +10000 counts (positive direction), then wait 1000ms
MoveDistance (1000);
}
else {
Serial.println("OFF");
}
delay(200);
// Read the state of the input connector.
state = digitalRead(R2);
// Display the state of the input connector.
// Display the state of the input connector.
Serial.print("R2 Input state: ");
if (state = LOW) {
Serial.println("ON");
// Move to +2000 counts (positive direction), then wait 2000ms
MoveDistance (2000);
}
else {
Serial.println("OFF");
}
delay(200);
// Read the state of the input connector.
state = digitalRead(R3);
// Display the state of the input connector.
// Display the state of the input connector.
Serial.print(" R3 Input state: ");
if (LOW) {
Serial.println("ON");
// Move to +3000 counts (positive direction), then wait 2000ms
MoveDistance (3000);
}
else {
Serial.println("OFF");
}
delay(200);
// Read the state of the input connector.
state = digitalRead(R4);
// Display the state of the input connector.
// Display the state of the input connector.
Serial.print("R4 Input state: ");
if (LOW) {
Serial.println("ON");
// Move to +4000 counts (positive direction), then wait 2000ms
MoveDistance (4000);
}
else {
Serial.println("OFF");
}
delay(200);
// Read the state of the input connector.
state = digitalRead(R5);
// Display the state of the input connector.
// Display the state of the input connector.
Serial.print("R5 Input state: ");
if (LOW) {
Serial.println("ON");
// Move to +5000 counts (positive direction), then wait 2000ms
MoveDistance (5000);
}
else {
Serial.println("OFF");
}
delay(200); {
state = digitalRead(PB);
if (state != lastState) {
if (state == LOW) {
PBCounter++;
Serial.print("Button Press Count = ");
Serial.println(PBCounter);
}
}
if (PBCounter == 1);
// Interrupt any on-going move and move to the absolute position 100
S1.Move(100, StepGenerator::MOVE_TARGET_ABSOLUTE);
if (PBCounter == 2);
// Interrupt any on-going move and move to the absolute position 100
S2.Move(100, StepGenerator::MOVE_TARGET_ABSOLUTE);
if (PBCounter == 3);
// Interrupt any on-going move and move to the absolute position 100
S3.Move(100, StepGenerator::MOVE_TARGET_ABSOLUTE);
if (PBCounter == 4);
// Interrupt any on-going move and move to the absolute position 100
S4.Move(100, StepGenerator::MOVE_TARGET_ABSOLUTE);
}
lastState = state;
if (PBCounter >= 4) {
Serial.println(" ");
Serial.println("Resettng the Counter...");
PBCounter = 0;
}
delay(500);
}
/*------------------------------------------------------------------------------
MoveAbsolutePosition
Command step pulses to move the motor's current position to the absolute
position specified by "position"
Prints the move status to the USB serial port
Returns when HLFB asserts (indicating the motor has reached the commanded
position)
Parameters:
int position - The absolute position, in step pulses, to move to
Returns: True/False depending on whether the move was successfully triggered.
*/
bool MoveAbsolutePosition(int position) {
// Check if an alert is currently preventing motion
if (S1.StatusReg().bit.AlertsPresent) {
Serial.println("S1 status: 'In Alert'. Move Canceled.");
return false;
}
Serial.print("Moving to absolute position: ");
Serial.println(position);
// Command the move of absolute distance
S1.Move(position, MotorDriver::MOVE_TARGET_ABSOLUTE);
// Waits for HLFB to assert (signaling the move has successfully completed)
Serial.println("Moving.. Waiting for HLFB");
while (!S1.StepsComplete() || S1.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Move Done");
return true;
// Check if an alert is currently preventing motion
if (S2.StatusReg().bit.AlertsPresent) {
Serial.println("S2 status: 'In Alert'. Move Canceled.");
return false;
}
Serial.print("Moving to absolute position: ");
Serial.println(position);
// Command the move of absolute distance
S2.Move(position, MotorDriver::MOVE_TARGET_ABSOLUTE);
// Waits for HLFB to assert (signaling the move has successfully completed)
Serial.println("Moving.. Waiting for HLFB");
while (!S2.StepsComplete() || S2.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Move Done");
return true;
// Check if an alert is currently preventing motion
if (S3.StatusReg().bit.AlertsPresent) {
Serial.println("S1 status: 'In Alert'. Move Canceled.");
return false;
}
Serial.print("Moving to absolute position: ");
Serial.println(position);
// Command the move of absolute distance
S3.Move(position, MotorDriver::MOVE_TARGET_ABSOLUTE);
// Waits for HLFB to assert (signaling the move has successfully completed)
Serial.println("Moving.. Waiting for HLFB");
while (!S3.StepsComplete() || S3.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Move Done");
return true;
// Check if an alert is currently preventing motion
if (S4.StatusReg().bit.AlertsPresent) {
Serial.println("S1 status: 'In Alert'. Move Canceled.");
return false;
}
Serial.print("Moving to absolute position: ");
Serial.println(position);
// Command the move of absolute distance
S4.Move(position, MotorDriver::MOVE_TARGET_ABSOLUTE);
// Waits for HLFB to assert (signaling the move has successfully completed)
Serial.println("Moving.. Waiting for HLFB");
while (!S4.StepsComplete() || S4.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Move Done");
return true;
}
//------------------------------------------------------------------------------
/*------------------------------------------------------------------------------
MoveDistance
Command "distance" number of step pulses away from the current position
Prints the move status to the USB serial port
Returns when HLFB asserts (indicating the motor has reached the commanded
position)
Parameters:
int distance - The distance, in step pulses, to move
Returns: True/False depending on whether the move was successfully triggered.
*/
bool MoveDistance(int distance) {
// Check if an alert is currently preventing motion
if (S1.StatusReg().bit.AlertsPresent) {
Serial.println("S1 status: 'In Alert'. Move Canceled.");
return false;
}
if (S2.StatusReg().bit.AlertsPresent) {
Serial.println("S2 status: 'In Alert'. Move Canceled.");
return false;
}
if (S3.StatusReg().bit.AlertsPresent) {
Serial.println("S3 status: 'In Alert'. Move Canceled.");
return false;
}
if (S4.StatusReg().bit.AlertsPresent) {
Serial.println("S4 status: 'In Alert'. Move Canceled.");
return false;
}
Serial.print("Moving distance: ");
Serial.println(distance);
// Command the move of incremental distance
S1.Move(distance);
S2.Move(distance);
S3.Move(distance);
S4.Move(distance);
// Waits for HLFB to assert (signaling the move has successfully completed)
Serial.println("Moving.. Waiting for HLFB");
while (!S1.StepsComplete() || S1.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Moving.. Waiting for HLFB");
while (!S2.StepsComplete() || S2.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Moving.. Waiting for HLFB");
while (!S3.StepsComplete() || S3.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Moving.. Waiting for HLFB");
while (!S4.StepsComplete() || S4.HlfbState() != MotorDriver::HLFB_ASSERTED) {
continue;
}
Serial.println("Move Done");
return true;
}
//------------------------------------------------------------------------------
What sort of advice are you looking for ?
Which board are you using ?
What peripherals are connected to it ?
How is the project powered ?
Does the code compile ?
What is the ClearCore.h file and where did you get it from ?
More descriptive variable names would make the code easier to follow. Variable names are not part of the code in memory. Using greenLight insead of GL or motorFailAlarm instead of AL are less ambiguous, add some self commenting and take up no more memory space. And if those are pin names, add Pin to the name, greenLightPin for instance, is more self commenting.
You print quite a few string literals. You would save some SRAM if you were to use the F macro.
Serial.println(F("S1 Enabled"));
= for assignment == for comparison. You want comparison there.
From the if reference:
Beware of accidentally using the single equal sign (e.g.
if (x = 10)
). The single equal sign is the assignment operator, and setsx
to 10 (puts the value 10 into the variablex
). Instead use the double equal sign (e.g.if (x == 10)
), which is the comparison operator, and tests whetherx
is equal to 10 or not. The latter statement is only true ifx
equals 10, but the former statement will always be true.
If you enable compiler warnings in the IDE, File, Preferences, that will trigger a warning.
Is something missing there. If not, that is a pretty useless statement. It will never be true.
looks like you wrote a lot of code without testing each part as it was added. it's much more difficult to debug now.
certainly true in this case
looks like the above block of code is repeated for several inputs. it could be captured in a sub-function with an arguments for each input
the bodies of the above if statement, the part that is conditionally executed, are the ";" following the ")" which mean these if statements have no effect
bool MoveAbsolutePosition(int position) {
...
Serial.println("Move Done");
return true;
this "return true" means the remained of the sub-function will never be executed
assume there are other syntax error and architectural improvements
Clear core is the controller I’m using. It is a one off controller from Teknic. They are also the manufacturer of the clear path servos that are connected. It compiles with no errors as it is. It has a dedicated 24vdc power supply for the controller and a separate 75vdc ps for the servos. I have not tested it on the controller yet. I’m am trying to merge a relative move as well as an absolute move in the same program. With in the program, I have a single push button the triggers the absolute move to position 100. 1 push move servo 1, push again move servo 2, etc. has door switch so it won’t move if door is open for a safety. I am C and C++ illiterate. I am a native Allen Bradley plc guy. Just looking for more of a proofreader type advice. I am very new and learning what feels like very slowly.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.