From 28dcf0bd5176c952524a37cf759dff9fb9f5385a Mon Sep 17 00:00:00 2001 From: me Date: Wed, 21 Apr 2021 06:02:17 -0700 Subject: [PATCH] need to override set_rpm --- .gitignore | 1 + Grbl_Esp32/src/Machine.h | 2 +- Grbl_Esp32/src/Spindles/Spindle.cpp | 5 + Grbl_Esp32/src/Spindles/Spindle.h | 1 + Grbl_Esp32/src/Spindles/TecoL510.cpp | 193 +++++++++++++++++++++++++++ Grbl_Esp32/src/Spindles/TecoL510.h | 45 +++++++ platformio.ini | 7 + 7 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 Grbl_Esp32/src/Spindles/TecoL510.cpp create mode 100644 Grbl_Esp32/src/Spindles/TecoL510.h diff --git a/.gitignore b/.gitignore index 44cc0076..d11902c0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ __vm/ *.suo Grbl_Esp32.ino.cpp /packages +Grbl_Esp32/src/Machine.h diff --git a/Grbl_Esp32/src/Machine.h b/Grbl_Esp32/src/Machine.h index 11ab8406..a684b000 100644 --- a/Grbl_Esp32/src/Machine.h +++ b/Grbl_Esp32/src/Machine.h @@ -8,7 +8,7 @@ // !!! For initial testing, start with test_drive.h which disables // all I/O pins // #include "Machines/atari_1020.h" -# include "Machines/test_drive.h" +# include "Machines/jesse_6pack.h" // !!! For actual use, change the line above to select a board // from Machines/, for example: diff --git a/Grbl_Esp32/src/Spindles/Spindle.cpp b/Grbl_Esp32/src/Spindles/Spindle.cpp index 7dfd8f6b..c783309b 100644 --- a/Grbl_Esp32/src/Spindles/Spindle.cpp +++ b/Grbl_Esp32/src/Spindles/Spindle.cpp @@ -39,6 +39,7 @@ #include "BESCSpindle.h" #include "10vSpindle.h" #include "YL620Spindle.h" +#include "TecoL510.h" namespace Spindles { // An instance of each type of spindle is created here. @@ -53,6 +54,7 @@ namespace Spindles { BESC besc; _10v _10v; YL620 yl620; + L510 l510; void Spindle::select() { switch (static_cast(spindle_type->get())) { @@ -83,6 +85,9 @@ namespace Spindles { case SpindleType::YL620: spindle = &yl620; break; + case SpindleType::L510: + spindle = &l510; + break; case SpindleType::NONE: default: spindle = &null; diff --git a/Grbl_Esp32/src/Spindles/Spindle.h b/Grbl_Esp32/src/Spindles/Spindle.h index 200e99cd..ff2ea5d4 100644 --- a/Grbl_Esp32/src/Spindles/Spindle.h +++ b/Grbl_Esp32/src/Spindles/Spindle.h @@ -39,6 +39,7 @@ enum class SpindleType : int8_t { _10V, H2A, YL620, + L510 }; #include "../Grbl.h" diff --git a/Grbl_Esp32/src/Spindles/TecoL510.cpp b/Grbl_Esp32/src/Spindles/TecoL510.cpp new file mode 100644 index 00000000..3dd6de6e --- /dev/null +++ b/Grbl_Esp32/src/Spindles/TecoL510.cpp @@ -0,0 +1,193 @@ +#include "TecoL510.h" + +/* + TecoL510.cpp + + Part of Grbl_ESP32 + 2021 - Jesse Schoch + + Grbl is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + Grbl is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with Grbl. If not, see . + + WARNING!!!! + VFDs are very dangerous. They have high voltages and are very powerful + Remove power before changing bits. + + See TecoL510.md for details +*/ + +namespace Spindles { + L510::L510() : VFD() { + _baudrate = 9600; + _parity = Uart::Parity::None; + _max_rpm = 24000; + _max_freq = 40000; + } + + void L510::direction_command(SpindleState mode, ModbusCommand& data) { + // NOTE: data length is excluding the CRC16 checksum. + data.tx_length = 6; + data.rx_length = 6; + + data.msg[1] = 0x06; // WRITE + data.msg[2] = 0x25; // Command ID 0x2501 + data.msg[3] = 0x01; + data.msg[4] = 0x01; + if(mode == SpindleState::Ccw){ + data.msg[5] = 1; + }else{ + data.msg[5] = 0; + } + grbl_msg_sendf(CLIENT_SERIAL,MsgLevel::Info,"sending dir: state: %d",mode; + } + + void L510::start_command(ModbusCommand& data){ + data.tx_length = 6; + data.rx_length = 6; + data.msg[1] = 0x06; + data.msg[2] = 0x25; + data.msg[3] = 0x01; + data.msg[4] = 0x00; + data.msg[5] = 0x01; + + } + + void L510::set_speed_command(uint32_t rpm, ModbusCommand& data) { + + // NOTE: data length is excluding the CRC16 checksum. + data.tx_length = 6; + data.rx_length = 6; + + // We have to know the max RPM before we can set the current RPM: + auto max_rpm = this->_max_rpm; + auto max_freq = this->_max_freq; + + // Assuming max frequncy is 400Hz + // Speed is in [0..40,000] + uint16_t speed = (uint16_t(rpm) * max_freq) / uint32_t(max_rpm); + if (speed < 0) { + speed = 0; + } + if (speed > 40000) { + speed = 40000; + } + + data.msg[1] = 0x06; // WRITE + data.msg[2] = 0x25; // Command ID 0x2502 + data.msg[3] = 0x02; + data.msg[4] = uint8_t(speed >> 8); // RPM + data.msg[5] = uint8_t(speed & 0xFF); + + grbl_msg_sendf(CLIENT_SERIAL,MsgLevel::Info,"setting speed to: %d",speed); + } + uint16_t L510::rpm_to_frequency(uint32_t rpm){ + auto max_rpm = this->_max_rpm; + uint16_t freq = (uint32_t(rpm) * 40000L) / uint32_t(max_rpm); + return freq; + } + + uint32_t L510::freq_to_rpm(uint16_t freq){ + auto max_rpm = this->_max_rpm; + auto max_freq = this->_max_freq; + uint32_t rpm = (freq/max_freq)/max_rpm; + } + + VFD::response_parser L510::initialization_sequence(int index, ModbusCommand& data) { + if (index == -1) { + // NOTE: data length is excluding the CRC16 checksum. + data.tx_length = 6; + data.rx_length = 5; + + + // Send: + data.msg[1] = 0x03; // READ + data.msg[2] = 0x02; // 0x0203 = Get RPM + data.msg[3] = 0x03; + data.msg[4] = 0x00; // Read 1 value + data.msg[5] = 0x01; + + /* + // Hack to see if a known working register can be read + data.msg[1] = 0x03; // READ + data.msg[2] = 0x25; // 0x2523 = Get RPM + data.msg[3] = 0x23; + data.msg[4] = 0x00; // Read 1 value + data.msg[5] = 0x01; + */ + + + // Recv: ?? + + return [](const uint8_t* response, Spindles::VFD* vfd) -> bool { + //uint16_t rpm = (uint16_t(response[4]) << 8) | uint16_t(response[5]); + //TODO remove hardcoding + //vfd->_max_rpm = rpm; + vfd->_max_rpm = 24000; + + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "L510 spindle hardcoded 24000 %d ", vfd->_max_rpm); + + return true; + }; + } else { + return nullptr; + } + } + + VFD::response_parser L510::get_current_rpm(ModbusCommand& data) { + // NOTE: data length is excluding the CRC16 checksum. + data.tx_length = 6; + data.rx_length = 5; + + // Send: 01 03 700C 0002 + data.msg[1] = 0x03; // READ + data.msg[2] = 0x25; // 0x2523 = Get RPM + data.msg[3] = 0x23; + data.msg[4] = 0x00; // Read 1 value + data.msg[5] = 0x01; + + return [](const uint8_t* response, Spindles::VFD* vfd) -> bool { + uint16_t freq = (response[3] << 8) | response[4]; + grbl_msg_sendf(CLIENT_SERIAL,MsgLevel::Info,"current frequency: %d",freq); + + // Set current RPM value? Somewhere? + //vfd->_sync_rpm = vfd->freq_to_rpm(freq); + + //TODO: how do get access to the object I was in before wandering into this lambda? + // undo hardcoded garbage + vfd->_sync_rpm = (freq/40000)*24000; + return true; + }; + } + + VFD::response_parser L510::get_current_direction(ModbusCommand& data) { + // NOTE: data length is excluding the CRC16 checksum. + data.tx_length = 6; + data.rx_length = 6; + + // Send: 01 03 30 00 00 01 + data.msg[1] = 0x03; // READ + data.msg[2] = 0x25; // 0x2520 + data.msg[3] = 0x20; + data.msg[4] = 0x00; // Message ID + data.msg[5] = 0x01; + + // Receive: 01 03 00 02 00 02 + // ----- status + + // TODO: this doesn't seem to do anything in H2A + return [](const uint8_t* response, Spindles::VFD* vfd) -> bool { + uint16_t got = (uint16_t(response[4]) << 8) | uint16_t(response[5]); + bool dir = bitRead(got,1); + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "L510 dir %d", int(dir)); + return true; + }; + } +} \ No newline at end of file diff --git a/Grbl_Esp32/src/Spindles/TecoL510.h b/Grbl_Esp32/src/Spindles/TecoL510.h new file mode 100644 index 00000000..743139ea --- /dev/null +++ b/Grbl_Esp32/src/Spindles/TecoL510.h @@ -0,0 +1,45 @@ +#pragma once + +#include "VFDSpindle.h" + +/* + TecoL510.h + + Part of Grbl_ESP32 + 2021 - Jesse Schoch + + Grbl is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + Grbl is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with Grbl. If not, see . + +*/ + +namespace Spindles { + class L510 : public VFD { + protected: + void direction_command(SpindleState mode, ModbusCommand& data) override; + void set_speed_command(uint32_t rpm, ModbusCommand& data) override; + + response_parser initialization_sequence(int index, ModbusCommand& data) override; + response_parser get_current_rpm(ModbusCommand& data) override; + response_parser get_current_direction(ModbusCommand& data) override; + response_parser get_status_ok(ModbusCommand& data) override { return nullptr; } + + // what is this, what should it do? + bool supports_actual_rpm() const override { return true; } + bool safety_polling() const override { return false; } + uint16_t rpm_to_frequency(uint32_t rpm); + uint32_t freq_to_rpm(uint16_t); + + public: + L510(); + uint16_t _max_freq; + }; +} diff --git a/platformio.ini b/platformio.ini index ddf3a854..a99cf2a3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -70,3 +70,10 @@ build_type = debug lib_deps = TMCStepper@>=0.7.0,<1.0.0 ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.0 + + +[env:ota] +upload_protocol = espota +upload_port = 192.168.1.61 +lib_deps = + TMCStepper@>=0.7.0,<1.0.0