1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-02 10:53:01 +02:00

Improvements to logging, including

* Use client_write so data goes to all clients, not just the Uart
* Added unsigned int to SimpleOutputStream so uints can be logged
* Added MSG: prefix on all log messages for Grbl protocol compatibility
* Use log_warn in PWMSpindle to test it
This commit is contained in:
Mitch Bradley
2021-05-21 13:51:14 -10:00
parent 02c141edad
commit fe41a4d516
7 changed files with 108 additions and 100 deletions

View File

@@ -160,45 +160,6 @@ const int DEFAULT_RADIO_MODE = ESP_RADIO_OFF;
# endif # endif
#endif #endif
// Define realtime command special characters. These characters are 'picked-off' directly from the
// serial read data stream and are not passed to the grbl line execution parser. Select characters
// that do not and must not exist in the streamed GCode program. ASCII control characters may be
// used, if they are available per user setup. Also, extended ASCII codes (>127), which are never in
// GCode programs, maybe selected for interface programs.
// NOTE: If changed, manually update help message in report.c.
// NOTE: All override realtime commands must be in the extended ASCII character set, starting
// at character value 128 (0x80) and up to 255 (0xFF). If the normal set of realtime commands,
// such as status reports, feed hold, reset, and cycle start, are moved to the extended set
// space, serial.c's RX ISR will need to be modified to accommodate the change.
enum class Cmd : uint8_t {
Reset = 0x18, // Ctrl-X
StatusReport = '?',
CycleStart = '~',
FeedHold = '!',
SafetyDoor = 0x84,
JogCancel = 0x85,
DebugReport = 0x86, // Only when DEBUG enabled, sends debug report in '{}' braces.
FeedOvrReset = 0x90, // Restores feed override value to 100%.
FeedOvrCoarsePlus = 0x91,
FeedOvrCoarseMinus = 0x92,
FeedOvrFinePlus = 0x93,
FeedOvrFineMinus = 0x94,
RapidOvrReset = 0x95, // Restores rapid override value to 100%.
RapidOvrMedium = 0x96,
RapidOvrLow = 0x97,
RapidOvrExtraLow = 0x98, // *NOT SUPPORTED*
SpindleOvrReset = 0x99, // Restores spindle override value to 100%.
SpindleOvrCoarsePlus = 0x9A, // 154
SpindleOvrCoarseMinus = 0x9B,
SpindleOvrFinePlus = 0x9C,
SpindleOvrFineMinus = 0x9D,
SpindleOvrStop = 0x9E,
CoolantFloodOvrToggle = 0xA0,
CoolantMistOvrToggle = 0xA1,
};
// If homing is enabled, homing init lock sets Grbl into an alarm state upon power up. This forces // If homing is enabled, homing init lock sets Grbl into an alarm state upon power up. This forces
// the user to perform the homing cycle (or override the locks) before doing anything else. This is // the user to perform the homing cycle (or override the locks) before doing anything else. This is
// mainly a safety feature to remind the user to home, since position is unknown to Grbl. // mainly a safety feature to remind the user to home, since position is unknown to Grbl.

View File

@@ -5,7 +5,7 @@
# include <iostream> # include <iostream>
DebugStream::DebugStream(const char* name) { DebugStream::DebugStream(const char* name) {
std::cout << '[' << name << ": "; std::cout << '[MSG:' << name << ": ";
} }
void DebugStream::add(char c) { void DebugStream::add(char c) {
std::cout << c; std::cout << c;
@@ -17,20 +17,24 @@ DebugStream::~DebugStream() {
#else #else
# include "Uart.h" # include "Serial.h"
DebugStream::DebugStream(const char* name) { DebugStream::DebugStream(const char* name) {
Uart0.print("["); client_write(CLIENT_ALL, "[MSG:");
Uart0.print(name); client_write(CLIENT_ALL, name);
Uart0.print(": "); client_write(CLIENT_ALL, ": ");
} }
void DebugStream::add(char c) { void DebugStream::add(char c) {
Uart0.print(c); char txt[2];
txt[0] = c;
txt[1] = '\0';
client_write(CLIENT_ALL, txt);
} }
DebugStream::~DebugStream() { DebugStream::~DebugStream() {
Uart0.println("]"); client_write(CLIENT_ALL, "]");
client_write(CLIENT_ALL, "\r\n");
} }
#endif #endif

View File

@@ -46,14 +46,6 @@ enum class Message : uint8_t {
SdFileQuit = 60, // mc_reset was called during an SD job SdFileQuit = 60, // mc_reset was called during an SD job
}; };
#define CLIENT_SERIAL 0
#define CLIENT_BT 1
#define CLIENT_WEBUI 2
#define CLIENT_TELNET 3
#define CLIENT_INPUT 4
#define CLIENT_ALL 0xFF
#define CLIENT_COUNT 5 // total number of client types regardless if they are used
enum class MsgLevel : int8_t { // Use $Message/Level enum class MsgLevel : int8_t { // Use $Message/Level
None = 0, None = 0,
Error = 1, Error = 1,

View File

@@ -22,16 +22,13 @@
#include "stdint.h" #include "stdint.h"
#ifndef RX_BUFFER_SIZE #define CLIENT_SERIAL 0
# define RX_BUFFER_SIZE 256 #define CLIENT_BT 1
#endif #define CLIENT_WEBUI 2
#ifndef TX_BUFFER_SIZE #define CLIENT_TELNET 3
# ifdef USE_LINE_NUMBERS #define CLIENT_INPUT 4
# define TX_BUFFER_SIZE 112 #define CLIENT_ALL 0xFF
# else #define CLIENT_COUNT 5 // total number of client types regardless if they are used
# define TX_BUFFER_SIZE 104
# endif
#endif
// a task to read for incoming data from serial port // a task to read for incoming data from serial port
void clientCheckTask(void* pvParameters); void clientCheckTask(void* pvParameters);
@@ -50,5 +47,44 @@ void client_reset_read_buffer(uint8_t client);
// Returns the number of bytes available in the RX serial buffer. // Returns the number of bytes available in the RX serial buffer.
uint8_t client_get_rx_buffer_available(uint8_t client); uint8_t client_get_rx_buffer_available(uint8_t client);
// Define realtime command special characters. These characters are 'picked-off' directly from the
// serial read data stream and are not passed to the grbl line execution parser. Select characters
// that do not and must not exist in the streamed GCode program. ASCII control characters may be
// used, if they are available per user setup. Also, extended ASCII codes (>127), which are never in
// GCode programs, maybe selected for interface programs.
// NOTE: If changed, manually update help message in report.c.
// NOTE: All override realtime commands must be in the extended ASCII character set, starting
// at character value 128 (0x80) and up to 255 (0xFF). If the normal set of realtime commands,
// such as status reports, feed hold, reset, and cycle start, are moved to the extended set
// space, serial.c's RX ISR will need to be modified to accommodate the change.
enum class Cmd : uint8_t {
Reset = 0x18, // Ctrl-X
StatusReport = '?',
CycleStart = '~',
FeedHold = '!',
SafetyDoor = 0x84,
JogCancel = 0x85,
DebugReport = 0x86, // Only when DEBUG enabled, sends debug report in '{}' braces.
FeedOvrReset = 0x90, // Restores feed override value to 100%.
FeedOvrCoarsePlus = 0x91,
FeedOvrCoarseMinus = 0x92,
FeedOvrFinePlus = 0x93,
FeedOvrFineMinus = 0x94,
RapidOvrReset = 0x95, // Restores rapid override value to 100%.
RapidOvrMedium = 0x96,
RapidOvrLow = 0x97,
RapidOvrExtraLow = 0x98, // *NOT SUPPORTED*
SpindleOvrReset = 0x99, // Restores spindle override value to 100%.
SpindleOvrCoarsePlus = 0x9A, // 154
SpindleOvrCoarseMinus = 0x9B,
SpindleOvrFinePlus = 0x9C,
SpindleOvrFineMinus = 0x9D,
SpindleOvrStop = 0x9E,
CoolantFloodOvrToggle = 0xA0,
CoolantMistOvrToggle = 0xA1,
};
void execute_realtime_command(Cmd command, uint8_t client); void execute_realtime_command(Cmd command, uint8_t client);
bool is_realtime_command(uint8_t data); bool is_realtime_command(uint8_t data);

View File

@@ -2,54 +2,69 @@
#include <cstring> #include <cstring>
char* SimpleOutputStream::intToBuf(int value, char* dst) char* SimpleOutputStream::intToBuf(int value, char* dst) {
{
#ifdef ESP32 #ifdef ESP32
return itoa(value, dst, 10); return itoa(value, dst, 10);
#else #else
_itoa_s(value, dst, 10, 10); _itoa_s(value, dst, 10, 10);
return dst + strlen(dst); return dst + strlen(dst);
#endif #endif
} }
void SimpleOutputStream::add(const char* s) { void SimpleOutputStream::add(const char* s) {
for (; *s; ++s) { add(*s); } for (; *s; ++s) {
add(*s);
}
} }
void SimpleOutputStream::add(int value) { void SimpleOutputStream::add(int value) {
char buf[10]; char buf[12]; // Up to 10 digits, possibly a -, plus a null
intToBuf(value, buf); intToBuf(value, buf);
add(buf); add(buf);
} }
void SimpleOutputStream::add(double value, int numberDigits, int precision) void SimpleOutputStream::add(unsigned int value) {
{ if (value == 0) {
add('0');
return;
}
char reverse[11]; // Up to 10 digits plus a null
char* p = reverse;
while (value) {
*p++ = value % 10 + '0';
value /= 10;
}
while (p > reverse) {
add(*--p);
}
}
void SimpleOutputStream::add(double value, int numberDigits, int precision) {
if (isnan(value)) { if (isnan(value)) {
add("NaN"); add("NaN");
} } else if (isinf(value)) {
else if (isinf(value)) {
add("Inf"); add("Inf");
} }
char buf[30]; // that's already quite big char buf[30]; // that's already quite big
char fmt[10]; char fmt[10];
fmt[0] = '%'; fmt[0] = '%';
fmt[1] = '0'; fmt[1] = '0';
char* next = intToBuf(numberDigits, fmt + 2); char* next = intToBuf(numberDigits, fmt + 2);
*next++ = '.'; *next++ = '.';
intToBuf(precision, next); intToBuf(precision, next);
snprintf(buf, sizeof(buf) - 1, fmt, value); snprintf(buf, sizeof(buf) - 1, fmt, value);
add(buf); add(buf);
} }
void SimpleOutputStream::add(StringRange range) void SimpleOutputStream::add(StringRange range) {
{ for (auto ch : range) {
for (auto ch : range) { add(ch); } add(ch);
}
} }
void SimpleOutputStream::add(const Pin& pin) void SimpleOutputStream::add(const Pin& pin) {
{
add(pin.name()); add(pin.name());
} }

View File

@@ -5,15 +5,15 @@
#include "StringRange.h" #include "StringRange.h"
#include "Pin.h" #include "Pin.h"
class SimpleOutputStream class SimpleOutputStream {
{
static char* intToBuf(int value, char* dst); static char* intToBuf(int value, char* dst);
static char* uintToBuf(unsigned int value, char* dst);
public: public:
SimpleOutputStream() = default; SimpleOutputStream() = default;
SimpleOutputStream(const SimpleOutputStream& o) = delete; SimpleOutputStream(const SimpleOutputStream& o) = delete;
SimpleOutputStream(SimpleOutputStream&& o) = delete; SimpleOutputStream(SimpleOutputStream&& o) = delete;
SimpleOutputStream& operator=(const SimpleOutputStream& o) = delete; SimpleOutputStream& operator=(const SimpleOutputStream& o) = delete;
SimpleOutputStream& operator=(SimpleOutputStream&& o) = delete; SimpleOutputStream& operator=(SimpleOutputStream&& o) = delete;
@@ -23,12 +23,12 @@ public:
void add(const char* s); void add(const char* s);
void add(int value); void add(int value);
void add(unsigned int value);
void add(double value, int numberDigits, int precision); void add(double value, int numberDigits, int precision);
void add(StringRange range); void add(StringRange range);
void add(const Pin& pin); void add(const Pin& pin);
virtual ~SimpleOutputStream() { virtual ~SimpleOutputStream() {}
}
}; };
inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, char c) { inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, char c) {
@@ -46,6 +46,11 @@ inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, int v) {
return lhs; return lhs;
} }
inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, unsigned int v) {
lhs.add(v);
return lhs;
}
inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, double v) { inline SimpleOutputStream& operator<<(SimpleOutputStream& lhs, double v) {
lhs.add(v, 4, 3); lhs.add(v, 4, 3);
return lhs; return lhs;

View File

@@ -21,6 +21,7 @@
*/ */
#include "PWMSpindle.h" #include "PWMSpindle.h"
#include "soc/ledc_struct.h" #include "soc/ledc_struct.h"
#include "../Logging.h"
// ======================= PWM ============================== // ======================= PWM ==============================
/* /*
@@ -35,12 +36,12 @@ namespace Spindles {
get_pins_and_settings(); get_pins_and_settings();
if (_output_pin.undefined()) { if (_output_pin.undefined()) {
grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Spindle output pin not defined"); log_warn("Spindle output pin not defined");
return; // We cannot continue without the output pin return; // We cannot continue without the output pin
} }
if (!_output_pin.capabilities().has(Pin::Capabilities::PWM)) { if (!_output_pin.capabilities().has(Pin::Capabilities::PWM)) {
grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Spindle output pin %s cannot do PWM", _output_pin.name().c_str()); log_warn("Spindle output pin %s cannot do PWM" << _output_pin.name().c_str());
return; return;
} }
@@ -67,11 +68,11 @@ namespace Spindles {
is_reversable = _direction_pin.defined(); is_reversable = _direction_pin.defined();
_pwm_precision = calc_pwm_precision(_pwm_freq); // detewrmine the best precision _pwm_precision = calc_pwm_precision(_pwm_freq); // determine the best precision
_pwm_period = (1 << _pwm_precision); _pwm_period = (1 << _pwm_precision);
if (_pwm_min_value > _pwm_max_value) { if (_pwm_min_value > _pwm_max_value) {
grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Spindle min pwm is greater than max. Check $35 and $36"); log_warn("Spindle min pwm is greater than max.");
} }
// pre-calculate some PWM count values // pre-calculate some PWM count values
@@ -96,8 +97,6 @@ namespace Spindles {
return rpm; return rpm;
} }
//grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "set_rpm(%d)", rpm);
// apply override // apply override
rpm = rpm * sys.spindle_speed_ovr / 100; // Scale by spindle speed override value (uint8_t percent) rpm = rpm * sys.spindle_speed_ovr / 100; // Scale by spindle speed override value (uint8_t percent)
@@ -113,7 +112,7 @@ namespace Spindles {
if (_piecewise_linear) { if (_piecewise_linear) {
//pwm_value = piecewise_linear_fit(rpm); TODO //pwm_value = piecewise_linear_fit(rpm); TODO
pwm_value = 0; pwm_value = 0;
grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Warning: Linear fit not implemented yet."); log_warn("Linear fit not implemented yet.");
} else { } else {
if (rpm == 0) { if (rpm == 0) {
@@ -172,14 +171,10 @@ namespace Spindles {
// prints the startup message of the spindle config // prints the startup message of the spindle config
void PWM::config_message() { void PWM::config_message() {
grbl_msg_sendf(CLIENT_ALL, log_info("PWM spindle Output:" << _output_pin.name().c_str() << ", Enbl:" << _enable_pin.name().c_str() << ", Dir:"
MsgLevel::Info, << _direction_pin.name().c_str() << ", Freq:" << _pwm_freq << "Hz, Res:" << _pwm_precision << "bits"
"PWM spindle Output:%s, Enbl:%s, Dir:%s, Freq:%dHz, Res:%dbits",
_output_pin.name().c_str(), );
_enable_pin.name().c_str(),
_direction_pin.name().c_str(),
_pwm_freq,
_pwm_precision);
} }
void PWM::set_output(uint32_t duty) { void PWM::set_output(uint32_t duty) {