Skip to content

Commit

Permalink
gcode value words need to be cleared, debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dymk committed Sep 24, 2024
1 parent caebc4a commit 3eda31c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
43 changes: 28 additions & 15 deletions FluidNC/src/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,17 @@ Error gc_execute_line(char* line) {
uint32_t command_words = 0; // Tracks G and M command words. Also used for modal group violations.
uint32_t value_words = 0; // Tracks value words.

bool jogMotion = false;
bool checkMantissa = false;
bool clockwiseArc = false;
bool probeExplicit = false;
bool probeAway = false;
bool probeNoError = false;
bool syncLaser = false;
bool disableLaser = false;
bool laserIsMotion = false;
bool nonmodalG38 = false; // Used for G38.6-9
bool jogMotion = false;
bool checkMantissa = false;
bool clockwiseArc = false;
bool probeExplicit = false;
bool probeAway = false;
bool probeNoError = false;
bool syncLaser = false;
bool disableLaser = false;
bool laserIsMotion = false;
bool nonmodalG38 = false; // Used for G38.6-9
bool isWaitOnInputDigital = false;

auto n_axis = config->_axes->_numberAxis;
float coord_data[MAX_N_AXIS]; // Used by WCO-related commands
Expand Down Expand Up @@ -1022,13 +1023,21 @@ Error gc_execute_line(char* line) {
// need at most one of P or E
FAIL(Error::GcodeValueWordInvalid);
}
isWaitOnInputDigital = bitnum_is_true(value_words, GCodeWord::P);
clear_bitnum(value_words, GCodeWord::P);
clear_bitnum(value_words, GCodeWord::E);
if (bitnum_is_false(value_words, GCodeWord::L)) {
FAIL(Error::GcodeValueWordMissing);
}
clear_bitnum(value_words, GCodeWord::L);
auto const wait_mode = validate_wait_on_input_mode_value(gc_block.values.l);
if (!wait_mode) {
FAIL(Error::GcodeValueWordInvalid);
}
// Only Immediate mode is valid for analog input
if (!isWaitOnInputDigital && wait_mode != WaitOnInputMode::Immediate) {
FAIL(Error::GcodeValueWordInvalid);
}
// Q is the timeout in seconds (conditionally optional)
// - Ignored if L is 0 (Immediate).
// - Error if value 0 seconds, and L is not 0 (Immediate).
Expand All @@ -1045,6 +1054,7 @@ Error gc_execute_line(char* line) {
FAIL(Error::GcodeValueWordMissing);
}
}
clear_bitnum(value_words, GCodeWord::Q);
}
if (gc_block.modal.set_tool_number == SetToolNumber::Enable) {
if (bitnum_is_false(value_words, GCodeWord::Q)) {
Expand Down Expand Up @@ -1701,12 +1711,11 @@ Error gc_execute_line(char* line) {
}
}
if (gc_block.modal.io_control == IoControl::WaitOnInput) {
bool const is_digital = bitnum_is_true(value_words, GCodeWord::P); // E implies analog
auto const validate_input_number = [&](const float input_number) -> std::optional<uint8_t> {
if (input_number < 0) {
return std::nullopt;
}
if (is_digital) {
if (isWaitOnInputDigital) {
if (input_number > MaxUserDigitalPin) {
return std::nullopt;
} else if (input_number > MaxUserAnalogPin) {
Expand All @@ -1715,14 +1724,14 @@ Error gc_execute_line(char* line) {
}
return (uint8_t)input_number;
};
auto const maybe_input_number = validate_input_number(is_digital ? gc_block.values.p : gc_block.values.e);
auto const maybe_input_number = validate_input_number(isWaitOnInputDigital ? gc_block.values.p : gc_block.values.e);
if (!maybe_input_number.has_value()) {
FAIL(Error::PParamMaxExceeded);
}
auto const input_number = *maybe_input_number;
auto const wait_mode = *validate_wait_on_input_mode_value(gc_block.values.l);
auto const timeout = gc_block.values.q;
gc_wait_on_input(is_digital, input_number, wait_mode, timeout);
gc_wait_on_input(isWaitOnInputDigital, input_number, wait_mode, timeout);
}

// [9. Override control ]: NOT SUPPORTED. Always enabled, except for parking control.
Expand Down Expand Up @@ -2016,10 +2025,14 @@ static Error gc_wait_on_input(bool is_digital, uint8_t input_number, WaitOnInput
auto const result = is_digital ? config->_userInputs->readDigitalInput(input_number) :
config->_userInputs->readAnalogInput(input_number);
auto const on_ok = [&](bool result) {
log_debug("M66: " << (is_digital ? "digital" : "analog") << "_input" << input_number << " result=" << result);
set_numbered_param(5399, result ? 1.0 : 0.0);
return Error::Ok;
};
auto const on_error = [&](Error error) { return error; };
auto const on_error = [&](Error error) {
log_error("M66: " << (is_digital ? "digital" : "analog") << "_input" << input_number << " failed");
return error;
};
return std::visit(overloaded { on_ok, on_error }, result);
}

Expand Down
32 changes: 18 additions & 14 deletions FluidNC/src/Machine/UserInputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ namespace Machine {
char item_name[64];
for (size_t i = 0; i < MaxUserAnalogPin; i++) {
snprintf(item_name, sizeof(item_name), "analog%d_pin", i);
handler.item(item_name, _analogInput[i]);
handler.item(item_name, _analogInput[i].pin);
_analogInput[i].name = item_name;
}
for (size_t i = 0; i < MaxUserDigitalPin; i++) {
snprintf(item_name, sizeof(item_name), "digital%d_pin", i);
handler.item(item_name, _digitalInput[i]);
handler.item(item_name, _digitalInput[i].pin);
_digitalInput[i].name = item_name;
}
}

void UserInputs::init() {
for (auto& input : _digitalInput) {
if (input.defined()) {
input.setAttr(Pin::Attr::Input);
for (auto& input : _analogInput) {
if (input.pin.defined()) {
input.pin.setAttr(Pin::Attr::Input);
log_info("User Analog Input: " << input.name << " on Pin " << input.pin.name());
}
}
for (auto& input : _analogInput) {
if (input.defined()) {
input.setAttr(Pin::Attr::Input);
for (auto& input : _digitalInput) {
if (input.pin.defined()) {
input.pin.setAttr(Pin::Attr::Input);
log_info("User Digital Input: " << input.name << " on Pin " << input.pin.name());
}
}
}
Expand All @@ -36,23 +40,23 @@ namespace Machine {
if (input_number >= MaxUserDigitalPin) {
return Error::PParamMaxExceeded;
}
auto& pin = _digitalInput[input_number];
if (!pin.defined()) {
auto& input = _digitalInput[input_number];
if (!input.pin.defined()) {
return Error::InvalidValue;
}
return pin.read();
return input.pin.read();
}

UserInputs::ReadInputResult UserInputs::readAnalogInput(uint8_t input_number) {
// TODO - analog pins are read the same as digital.
if (input_number >= MaxUserAnalogPin) {
return Error::PParamMaxExceeded;
}
auto& pin = _analogInput[input_number];
if (!pin.defined()) {
auto& input = _analogInput[input_number];
if (!input.pin.defined()) {
return Error::InvalidValue;
}
return pin.read();
return input.pin.read();
}

} // namespace Machine
9 changes: 7 additions & 2 deletions FluidNC/src/Machine/UserInputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
namespace Machine {

class UserInputs : public Configuration::Configurable {
std::array<Pin, MaxUserDigitalPin> _digitalInput;
struct PinAndName {
std::string name;
Pin pin;
};

std::array<PinAndName, MaxUserDigitalPin> _digitalInput;

// TODO - analog pins are read the same as digital. The Pin
// API should either be extended to support analog reads, or
// a new AnalogPin class should be created.
std::array<Pin, MaxUserAnalogPin> _analogInput;
std::array<PinAndName, MaxUserAnalogPin> _analogInput;

public:
UserInputs();
Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Machine/UserOutputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Machine {
if (pin.defined()) {
pin.setAttr(Pin::Attr::Output);
pin.off();
log_info("User Digital Output:" << i << " on Pin:" << pin.name());
log_info("User Digital Output: " << i << " on Pin:" << pin.name());
}
}
// determine the highest resolution (number of precision bits) allowed by frequency
Expand All @@ -32,7 +32,7 @@ namespace Machine {
if (pin.defined()) {
_pwm[i] = new PwmPin(pin, _analogFrequency[i]);
_pwm[i]->setDuty(0);
log_info("User Analog Output " << i << " on Pin:" << pin.name() << " Freq:" << _pwm[i]->frequency() << "Hz");
log_info("User Analog Output: " << i << " on Pin:" << pin.name() << " Freq:" << _pwm[i]->frequency() << "Hz");
}
}
}
Expand Down

0 comments on commit 3eda31c

Please sign in to comment.