Skip to content

Commit

Permalink
first release with homing - yahoo!
Browse files Browse the repository at this point in the history
  • Loading branch information
Strooom committed Sep 5, 2022
1 parent 329d702 commit da56d36
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 84 deletions.
44 changes: 28 additions & 16 deletions lib/maincontroller/homing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "motionctrl.h"
#include "motionstate.h"
#include "debouncedinput.h"
#include "Arduino.h"

extern motionCtrl theMotionController;
extern machineProperties theMachineProperties;
Expand All @@ -20,10 +19,10 @@ void homingController::start() {
goTo(homingState::stopping);
} else {
if (myInputs[theLimitSwitch].isClosed()) {
Serial.printf("limitSwitch %s is closed\n", toString(currentHomingAxis));
// Serial.printf("limitSwitch %s is closed\n", toString(currentHomingAxis));
goTo(homingState::opening);
} else {
Serial.printf("limitSwitch %s is open\n", toString(currentHomingAxis));
// Serial.printf("limitSwitch %s is open\n", toString(currentHomingAxis));
goTo(homingState::closing);
}
}
Expand Down Expand Up @@ -103,10 +102,10 @@ void homingController::handleEvents(event theEvent) {
switch (theEvent) {
case event::motionStopped:
if (myInputs[theLimitSwitch].isClosed()) {
Serial.printf("limitSwitch %s is closed\n", toString(currentHomingAxis));
// Serial.printf("limitSwitch %s is closed\n", toString(currentHomingAxis));
goTo(homingState::opening);
} else {
Serial.printf("limitSwitch %s is open\n", toString(currentHomingAxis));
// Serial.printf("limitSwitch %s is open\n", toString(currentHomingAxis));
goTo(homingState::closing);
}
break;
Expand Down Expand Up @@ -155,13 +154,13 @@ void homingController::handleEvents(event theEvent) {
case event::motionStopped: {
point currentPosition;
simplifiedMotion aMotion;
Serial.printf("axis %s : [%d], stopped\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
// Serial.printf("axis %s : [%d], stopped\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
if (nextAxis()) {
if (myInputs[theLimitSwitch].isClosed()) {
Serial.printf("limitSwitch %s is closed\n", toString(currentHomingAxis));
// Serial.printf("limitSwitch %s is closed\n", toString(currentHomingAxis));
goTo(homingState::opening);
} else {
Serial.printf("limitSwitch %s is open\n", toString(currentHomingAxis));
// Serial.printf("limitSwitch %s is open\n", toString(currentHomingAxis));
goTo(homingState::closing);
}
} else {
Expand Down Expand Up @@ -203,16 +202,21 @@ void homingController::enterState(homingState theNewState) {
point currentPosition;
simplifiedMotion aMotion;
theMotionController.getMachinePosition(currentPosition);
Serial.printf("axis %s : [%d], closing\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
aMotion.set(currentPosition, currentHomingAxis, theMachineProperties.motors.sMax[currentHomingAxisIndex], theMachineProperties.vHoming);
// Serial.printf("axis %s : [%d], closing\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
if (theMachineProperties.homingDirection[currentHomingAxisIndex]) {
aMotion.set(currentPosition, currentHomingAxis, theMachineProperties.motors.sMax[currentHomingAxisIndex], theMachineProperties.vHoming);
} else {
aMotion.set(currentPosition, currentHomingAxis, -1 * theMachineProperties.motors.sMax[currentHomingAxisIndex], theMachineProperties.vHoming);
}

theMotionController.append(aMotion);
theMotionController.start();
} break;

case homingState::closedWaitForStop: {
point currentPosition;
theMotionController.getMachinePosition(currentPosition);
Serial.printf("axis %s : [%d], closed, stopping\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
// Serial.printf("axis %s : [%d], closed, stopping\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
theMotionController.stop();
} break;

Expand All @@ -221,16 +225,20 @@ void homingController::enterState(homingState theNewState) {
point currentPosition;
simplifiedMotion aMotion;
theMotionController.getMachinePosition(currentPosition);
Serial.printf("axis %s : [%d], stopped, opening\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
aMotion.set(currentPosition, currentHomingAxis, -1 * theMachineProperties.motors.sMax[currentHomingAxisIndex], theMachineProperties.vHoming);
// Serial.printf("axis %s : [%d], stopped, opening\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
if (theMachineProperties.homingDirection[currentHomingAxisIndex]) {
aMotion.set(currentPosition, currentHomingAxis, -1 * theMachineProperties.motors.sMax[currentHomingAxisIndex], theMachineProperties.vHomingSlow);
} else {
aMotion.set(currentPosition, currentHomingAxis, theMachineProperties.motors.sMax[currentHomingAxisIndex], theMachineProperties.vHomingSlow);
}
theMotionController.append(aMotion);
theMotionController.start();
} break;

case homingState::openedWaitForStop: {
point currentPosition;
theMotionController.getMachinePosition(currentPosition);
Serial.printf("axis %s : [%d], opened, stopping\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
// Serial.printf("axis %s : [%d], opened, stopping\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
theMotionController.stop();
} break;

Expand All @@ -239,8 +247,12 @@ void homingController::enterState(homingState theNewState) {
point currentPosition;
simplifiedMotion aMotion;
theMotionController.getMachinePosition(currentPosition);
Serial.printf("axis %s : [%d], stopped, offsetting\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
aMotion.set(currentPosition, currentHomingAxis, theMachineProperties.homingOffset[currentHomingAxisIndex], theMachineProperties.vHoming);
// Serial.printf("axis %s : [%d], stopped, offsetting\n", toString(currentHomingAxis), currentPosition.inSteps[currentHomingAxisIndex]);
if (theMachineProperties.homingDirection[currentHomingAxisIndex]) {
aMotion.set(currentPosition, currentHomingAxis, -1 * theMachineProperties.homingOffset[currentHomingAxisIndex], theMachineProperties.vHoming);
} else {
aMotion.set(currentPosition, currentHomingAxis, theMachineProperties.homingOffset[currentHomingAxisIndex], theMachineProperties.vHoming);
}
theMotionController.append(aMotion);
theMotionController.start();
} break;
Expand Down
2 changes: 1 addition & 1 deletion lib/motion/motionctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class motionCtrl {
int32_t machinePositionInSteps[nmbrAxis]{0}; // TODO make this private again, currently public so we can print it..

#ifndef unitTesting
private:
//private:
#endif
motionState theMotionCtrlState = motionState::stopped; // eg running, stopping or stopped
overrides theOverrides; // override values for feedrate and spindlespeed
Expand Down
2 changes: 2 additions & 0 deletions lib/motion/motionspeedprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

void motionSpeedProfile::setSpeed(const simplifiedMotion &theMotion) {
vFeed = static_cast<float>(theMotion.speedProfile.vFeed);
done.duration = 0.0F;
done.length = 0.0F;
left.setMax(aMax, dMax, jMax);
left.setvStart(0.0F);
right.setMax(aMax, dMax, jMax);
Expand Down
2 changes: 1 addition & 1 deletion lib/motion/sampletime.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class sampleTime {
void setminStepPulseWidth(const float theMinStepPulseWidth); //

#ifndef unitTesting
private:
// private:
#endif
friend class motionCtrl;
float timeInMotion{0.0F}; // time [s] between beginning of the motion and the current sampling point
Expand Down
24 changes: 12 additions & 12 deletions lib_target/cnc3axis/general/machineproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ class machineProperties {
void save();

struct Motors {
float jMax{200.0F}; // [mm/s^3] // TODO should we not set jMax per axis ?
float aMax[nmbrAxis]{100.0F, 100.0F, 100.0F}; // [mm/s^2]
float dMax[nmbrAxis]{-100.0F, -100.0F, -100.0F}; // [mm/s^2]
float jMax{600.0F}; // [mm/s^3] // TODO should we not set jMax per axis ?
float aMax[nmbrAxis]{200.0F, 200.0F, 200.0F}; // [mm/s^2]
float dMax[nmbrAxis]{-200.0F, -200.0F, -200.0F}; // [mm/s^2]
// float vMax[nmbrAxis]{60.0F, 60.0F, 60.0F}; // [mm/s]
float vMax[nmbrAxis]{30.0F, 30.0F, 30.0F}; // [mm/s]
float sMax[nmbrAxis]{300.0F, 160.0F, 60.0F}; // [mm]
float stepsPerMm[nmbrAxis]{200.0F, 200.0F, 200.0F}; // [1/mm]
} motors;

struct Limits {
bool hasLimitsMax[nmbrAxis]{true, false, true}; // limit switches towards the positive direction of the Axis
uint32_t limitMaxIndex[nmbrAxis]{1, 2, 0}; // index into myInputs[] telling which input is the matching limit switch
bool hasLimitsMin[nmbrAxis]{false, false, false}; // limit switches towards the negative direction of the Axis
uint32_t limitMinIndex[nmbrAxis]{0, 2, 4}; // index into myInputs[] telling which input is the matching limit switch
float maxLimitswitchTravel{2.0F}; // [mm]
bool hasLimitsMax[nmbrAxis]{true, false, true}; // limit switches towards the positive direction of the Axis
uint32_t limitMaxIndex[nmbrAxis]{1, 2, 0}; // index into myInputs[] telling which input is the matching limit switch
bool hasLimitsMin[nmbrAxis]{false, true, false}; // limit switches towards the negative direction of the Axis
uint32_t limitMinIndex[nmbrAxis]{0, 2, 4}; // index into myInputs[] telling which input is the matching limit switch
float maxLimitswitchTravel{2.0F}; // [mm]
} limits;

struct Spindle {
Expand All @@ -68,12 +68,12 @@ class machineProperties {

double minLengthSProfile{0.0F}; // [mm] all motions with a length smaller will be 2nd order T-profile - larger will be 3rd order S-profile

float vHoming{5}; // faster homing speed, towards switch closing
float vHoming{10}; // faster homing speed, towards switch closing
float vHomingSlow{1}; // slower homing, towards opening limitswitch

axis homingSequence[nmbrAxis]{axis::Z, axis::X, axis::nmbrAxis}; // in which sequence do we want to home axis.
bool homingDirection[nmbrAxis]{true, true, true}; // in which direction do we want to home : true = positive, false = negative
double homingOffset[nmbrAxis]{-10.0, -10.0, -10.0}; // after homing, what distance from the limitswitches do we set the machine zero
axis homingSequence[nmbrAxis]{axis::Z, axis::X, axis::Y}; // in which sequence do we want to home axis.
bool homingDirection[nmbrAxis]{true, false, true}; // in which direction do we want to home : true = positive, false = negative
double homingOffset[nmbrAxis]{3.0, 3.0, 3.0}; // after homing, what distance from the limitswitches do we set the machine zero. Needs to be positive

// static constexpr float t = limits.maxLimitswitchTravel * 6.0 / motors.jMax;
};
28 changes: 14 additions & 14 deletions lib_target/native/general/machineproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
#include <stdint.h>
#include "axis.h"

static constexpr uint32_t busFrequency = 60'000'000U;

static constexpr uint32_t nmbrInputs = 9U;
static constexpr uint32_t inputSamplingInterval{10U}; // [ms] interval between sampling inputs like buttons and limitswitches

static constexpr uint32_t outputTimerFrequency = 60'000U; // Design parameter resulting in a certain maximum stepping frequency, as well as a minimum Step pulse width and Dir setup timeBefore
static constexpr uint32_t maxSteppingFrequency = outputTimerFrequency / 2U; //
static constexpr float minStepPulseWidth = 1.0F / outputTimerFrequency; // period of the PIT1 timer, this will automatically become the DIR setup timeBefore, as well as the STEP pulse width
static constexpr float minStepBufferTotalTime = 0.025F; // amount of totalTime [s] we want at least in the buffer
static constexpr uint32_t minStepBufferTotalTimeTicks = (uint32_t)(outputTimerFrequency * minStepBufferTotalTime); // amount of totalTime [PIT1 ticks] we want at least in the buffer
static constexpr uint32_t minBufferLevel = 4U; //
static constexpr uint32_t maxTicksSinceLastOutput = minStepBufferTotalTimeTicks / minBufferLevel; //
static constexpr float minSteppingFrequency = (float)maxSteppingFrequency / (float)std::numeric_limits<uint32_t>::max(); //
static constexpr float maxMotionDuration = (float)(std::numeric_limits<uint32_t>::max() / outputTimerFrequency); //
static constexpr uint32_t inputTimerFrequency = 100U; // Design parameter : 100 Hz = 10ms
static constexpr uint32_t inputSamplingRate = inputTimerFrequency; //
static constexpr float hysteresis = 0.1F; // hysteresis, to avoid setting steps forward and backwards due to floating point rounding errors. In fact the value put here is half of the hysteresis
static constexpr uint32_t outputTimerFrequency = 60'000U; // Design parameter resulting in a certain maximum stepping frequency, as well as a minimum Step pulse width and Dir setup timeBefore
static constexpr uint32_t maxSteppingFrequency = outputTimerFrequency / 2U; //
static constexpr float minStepPulseWidth = 1.0F / outputTimerFrequency; // period of the PIT1 timer, this will automatically become the DIR setup timeBefore, as well as the STEP pulse width
static constexpr float minStepBufferTotalTime = 0.025F; // amount of totalTime [s] we want at least in the buffer
static constexpr uint32_t minStepBufferTotalTimeMs = static_cast<uint32_t>(minStepBufferTotalTime * 1000.0F); // amount of totalTime [ms] we want at least in the buffer

static constexpr uint32_t minStepBufferTotalTimeTicks = static_cast<uint32_t>(outputTimerFrequency * minStepBufferTotalTime); // amount of totalTime [PIT1 ticks] we want at least in the buffer
static constexpr uint32_t minStepBufferLevel = 4U; //
static constexpr uint32_t maxTicksSinceLastOutput = minStepBufferTotalTimeTicks / minStepBufferLevel; //
static constexpr float minSteppingFrequency = static_cast<float>(maxSteppingFrequency) / static_cast<float>(std::numeric_limits<uint32_t>::max()); //
static constexpr float maxMotionDuration = static_cast<float>(std::numeric_limits<uint32_t>::max() / outputTimerFrequency); //
static constexpr float hysteresis = 0.05F; // hysteresis, to avoid setting steps forward and backwards due to floating point rounding errors. In fact the value put here is half of the hysteresis

static constexpr float oneSixth{1.0F / 6.0F}; // constant to avoid having to divide by 6, as division is slower than multiplication

Expand Down Expand Up @@ -73,6 +75,4 @@ class machineProperties {
axis homingSequence[nmbrAxis]{axis::Z, axis::X, axis::nmbrAxis}; // in which sequence do we want to home axis.
bool homingDirection[nmbrAxis]{true, true, true}; // in which direction do we want to home : true = positive, false = negative
double homingOffset[nmbrAxis]{-10.0, -10.0, -10.0}; // after homing, what distance from the limitswitches do we set the machine zero


};
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test_framework = unity
test_filter =
native/test_*

debug_test = native/test_motion_control
debug_test = native/test_maincontrol_homing

check_patterns =
lib/*
Expand Down
Loading

0 comments on commit da56d36

Please sign in to comment.