Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for GetMaxRPM issues #4

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 50 additions & 37 deletions huanyang.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static spindle_state_t vfd_state = {0};
static spindle_data_t spindle_data = {0};
static settings_changed_ptr settings_changed;
static on_report_options_ptr on_report_options;
static driver_reset_ptr driver_reset;
static uint32_t rpm_max = 0;
#if SPINDLE_HUANYANG == 1
static float rpm_max50 = 3000;
Expand All @@ -76,6 +77,39 @@ static const modbus_callbacks_t callbacks = {
.on_rx_exception = rx_exception
};

// Read maximum configured RPM from spindle, value is used later for calculating current RPM
// In the case of the original Huanyang protocol, the value is the configured RPM at 50Hz
static void spindleGetMaxRPM (void)
{
#if SPINDLE_HUANYANG == 2
modbus_message_t cmd = {
.context = (void *)VFD_GetMaxRPM,
.adu[0] = VFD_ADDRESS,
.adu[1] = ModBus_ReadHoldingRegisters,
.adu[2] = 0xB0,
.adu[3] = 0x05,
.adu[4] = 0x00,
.adu[5] = 0x02,
.tx_length = 8,
.rx_length = 8
};
modbus_send(&cmd, &callbacks, true);
#else
modbus_message_t cmd = {
.context = (void *)VFD_GetMaxRPM50,
.adu[0] = VFD_ADDRESS,
.adu[1] = ModBus_ReadCoils,
.adu[2] = 0x03,
.adu[3] = 0x90, // PD144
.adu[4] = 0x00,
.adu[5] = 0x00,
.tx_length = 8,
.rx_length = 8
};
modbus_send(&cmd, &callbacks, true);
#endif
}

static void spindleSetRPM (float rpm, bool block)
{

Expand Down Expand Up @@ -255,7 +289,12 @@ static void raise_alarm (uint_fast16_t state)

static void rx_exception (uint8_t code, void *context)
{
protocol_enqueue_rt_command(raise_alarm);
// Alarm needs to be raised directly to correctly handle an error during reset (the rt command queue is
// emptied on a warm reset). Exception is during cold start, where alarms need to be queued.
if (!sys.cold_start)
raise_alarm(Alarm_Spindle);
else
protocol_enqueue_rt_command(raise_alarm);
}

static void onReportOptions (bool newopt)
Expand All @@ -271,6 +310,12 @@ static void onReportOptions (bool newopt)
}
}

static void huanyang_reset (void)
{
driver_reset();
spindleGetMaxRPM();
}

static void huanyang_settings_changed (settings_t *settings)
{
static bool init_ok = false;
Expand Down Expand Up @@ -311,42 +356,7 @@ static void huanyang_settings_changed (settings_t *settings)
hal.driver_cap.spindle_at_speed = On;
hal.driver_cap.spindle_dir = On;
}

#if SPINDLE_HUANYANG == 2
if(!init_ok) {

modbus_message_t cmd = {
.context = (void *)VFD_GetMaxRPM,
.crc_check = false,
.adu[0] = VFD_ADDRESS,
.adu[1] = ModBus_ReadHoldingRegisters,
.adu[2] = 0xB0,
.adu[3] = 0x05,
.adu[4] = 0x00,
.adu[5] = 0x02,
.tx_length = 8,
.rx_length = 8
};

modbus_send(&cmd, &callbacks, true);
}
#else
if(!init_ok) {

modbus_message_t cmd = {
.context = (void *)VFD_GetMaxRPM50,
.crc_check = false,
.adu[0] = VFD_ADDRESS,
.adu[1] = ModBus_ReadCoils,
.adu[2] = 0x03,
.adu[3] = 122,
.tx_length = 8,
.rx_length = 8
};

modbus_send(&cmd, &callbacks, true);
}
#endif
spindleGetMaxRPM();
}

init_ok = true;
Expand All @@ -361,6 +371,9 @@ void huanyang_init (void)
on_report_options = grbl.on_report_options;
grbl.on_report_options = onReportOptions;

driver_reset = hal.driver_reset;
hal.driver_reset = huanyang_reset;

if(!hal.driver_cap.dual_spindle)
huanyang_settings_changed(&settings);
}
Expand Down