Files
g2/g2core/coolant.cpp
2023-02-16 09:09:05 -06:00

223 lines
8.5 KiB
C++

/*
* coolant.cpp - canonical machine coolant driver
* This file is part of the g2core project
*
* Copyright (c) 2015 - 2019 Alden S. Hart, Jr.
*
* This file ("the software") is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by the
* Free Software Foundation. You should have received a copy of the GNU General Public
* License, version 2 along with the software. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, you may use this file as part of a software library without
* restriction. Specifically, if other files instantiate templates or use macros or
* inline functions from this file, or you compile this file and link it with other
* files to produce an executable, this file does not by itself cause the resulting
* executable to be covered by the GNU General Public License. This exception does not
* however invalidate any other reasons why the executable file might be covered by the
* GNU General Public License.
*
* THE SOFTWARE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "coolant.h"
#include "canonical_machine.h" // #3
#include "config.h" // #2
#include "g2core.h" // #1 dependency order
#include "gpio.h"
#include "hardware.h"
#include "planner.h"
#include "text_parser.h" // #4
#include "util.h"
/**** Allocate structures ****/
coCoolant_t coolant;
// gpioDigitalOutput *mist_enable_output = nullptr;
// gpioDigitalOutput *flood_enable_output = nullptr;
#ifndef MIST_ENABLE_OUTPUT_NUMBER
#warning MIST_ENABLE_OUTPUT_NUMBER is defaulted to 0 (off)!
#warning MIST_ENABLE_OUTPUT_NUMBER should be defined in settings or a board file!
#define MIST_ENABLE_OUTPUT_NUMBER 0
#endif
#ifndef FLOOD_ENABLE_OUTPUT_NUMBER
#warning FLOOD_ENABLE_OUTPUT_NUMBER is defaulted to 0 (off)!
#warning FLOOD_ENABLE_OUTPUT_NUMBER should be defined in settings or a board file!
#define FLOOD_ENABLE_OUTPUT_NUMBER 0
#endif
/**** Static functions ****/
static void _exec_coolant_control(float *value, bool *flag);
/****************************************************************************************
* coolant_init()
* coolant_reset()
*/
void coolant_init() {
if (MIST_ENABLE_OUTPUT_NUMBER > 0) {
coolant.mist.output = d_out[MIST_ENABLE_OUTPUT_NUMBER - 1];
coolant.mist.output->setEnabled(IO_ENABLED);
coolant.mist.output->setPolarity((ioPolarity)COOLANT_MIST_POLARITY);
}
if (FLOOD_ENABLE_OUTPUT_NUMBER > 0) {
coolant.flood.output = d_out[FLOOD_ENABLE_OUTPUT_NUMBER - 1];
coolant.flood.output->setEnabled(IO_ENABLED);
coolant.flood.output->setPolarity((ioPolarity)COOLANT_FLOOD_POLARITY);
}
coolant.mist.state = COOLANT_OFF;
coolant.flood.state = COOLANT_OFF;
}
void coolant_reset() {
coolant_init();
coolant_control_immediate(COOLANT_OFF, COOLANT_BOTH);
}
/****************************************************************************************
* coolant_control_immediate() - execute coolant control immediately
* coolant_control_sync() - queue a coolant control to the planner buffer
* _exec_coolant_control() - actually execute the coolant command
*/
stat_t coolant_control_immediate(coControl control, coSelect select) {
float value[] = {(float)control};
bool flags[] = {(select & COOLANT_MIST), (select & COOLANT_FLOOD)};
_exec_coolant_control(value, flags);
return (STAT_OK);
}
stat_t coolant_control_sync(coControl control, coSelect select) {
// Skip the PAUSE operation if pause_enable is not enabled (pause-on-hold)
// The pause setting in mist coolant is treated as the master even though it's replicated in flood
if ((control == COOLANT_PAUSE) && (!coolant.mist.pause_enable)) {
return (STAT_OK);
}
// queue the coolant control
float value[] = {(float)control};
bool flags[] = {(select & COOLANT_MIST), (select & COOLANT_FLOOD)};
mp_queue_command(_exec_coolant_control, value, flags);
return (STAT_OK);
}
static void _exec_coolant_helper(coCoolantChannel_t &co, coControl control) {
bool action = true;
int8_t enable_bit = 0;
switch (control) {
case COOLANT_OFF: {
co.state = COOLANT_OFF;
break;
}
case COOLANT_ON: {
co.state = COOLANT_ON;
enable_bit = 1;
break;
}
case COOLANT_PAUSE: {
if (co.state == COOLANT_ON) {
co.state = COOLANT_PAUSE;
} else {
action = false;
}
break;
}
case COOLANT_RESUME: {
if (co.state == COOLANT_PAUSE) {
co.state = COOLANT_ON;
enable_bit = 1;
} else {
action = false;
}
break;
}
}
if (action) {
if (co.output != nullptr) {
co.output->setValue(enable_bit);
}
}
}
static void _exec_coolant_control(float *value, bool *flag) {
coControl control = (coControl)value[0];
if (control > COOLANT_ACTION_MAX) {
return;
}
if (flag[0]) { // Mist, M7
_exec_coolant_helper(coolant.mist, control);
}
if (flag[1]) { // Flood, M8
_exec_coolant_helper(coolant.flood, control);
}
}
/****************************************************************************************
* coolant_ready() - is the coolant able to start - currently, the answer is yes
*/
bool coolant_ready() { return true; }
/****************************************************************************************
**** Coolant Settings ******************************************************************
****************************************************************************************/
stat_t co_get_com(nvObj_t *nv) { return (get_integer(nv, coolant.mist.state)); }
stat_t co_set_com(nvObj_t *nv) { return (coolant_control_immediate((coControl)nv->value_int, COOLANT_MIST)); }
stat_t co_get_cof(nvObj_t *nv) { return (get_integer(nv, coolant.flood.state)); }
stat_t co_set_cof(nvObj_t *nv) { return (coolant_control_immediate((coControl)nv->value_int, COOLANT_FLOOD)); }
stat_t co_get_coph(nvObj_t *nv) { return (get_integer(nv, coolant.mist.pause_enable)); }
stat_t co_set_coph(nvObj_t *nv) {
ritorno(set_integer(nv, (uint8_t &)coolant.mist.pause_enable, 0, 1));
return (set_integer(nv, (uint8_t &)coolant.flood.pause_enable, 0, 1));
}
stat_t co_get_comp(nvObj_t *nv) { return (get_integer(nv, coolant.mist.polarity)); }
stat_t co_set_comp(nvObj_t *nv) {
stat_t ret = set_integer(nv, (uint8_t &)coolant.mist.polarity, 0, 1);
if (ret == STAT_OK && coolant.mist.output) {
coolant.mist.output->setPolarity((ioPolarity)coolant.mist.polarity);
}
return ret;
}
stat_t co_get_cofp(nvObj_t *nv) { return (get_integer(nv, coolant.flood.polarity)); }
stat_t co_set_cofp(nvObj_t *nv) {
stat_t ret = set_integer(nv, (uint8_t &)coolant.flood.polarity, 0, 1);
if (ret == STAT_OK && coolant.flood.output) {
coolant.flood.output->setPolarity((ioPolarity)coolant.flood.polarity);
}
return ret;
}
/****************************************************************************************
* TEXT MODE SUPPORT
* Functions to print variables from the cfgArray table*****
***********************************************************************************/
#ifdef __TEXT_MODE
const char fmt_coph[] = "[coph] coolant pause on hold%7d [0=no,1=pause_on_hold]\n";
const char fmt_comp[] = "[comp] coolant mist polarity%7d [0=low is ON,1=high is ON]\n";
const char fmt_cofp[] = "[cofp] coolant flood polarity%6d [0=low is ON,1=high is ON]\n";
const char fmt_com[] = "[com] coolant mist%16d [0=OFF,1=ON]\n";
const char fmt_cof[] = "[cof] coolant flood%15d [0=OFF,1=ON]\n";
void co_print_coph(nvObj_t *nv) { text_print(nv, fmt_coph); } // TYPE_INT
void co_print_comp(nvObj_t *nv) { text_print(nv, fmt_comp); } // TYPE_INT
void co_print_cofp(nvObj_t *nv) { text_print(nv, fmt_cofp); } // TYPE_INT
void co_print_com(nvObj_t *nv) { text_print(nv, fmt_com); } // TYPE_INT
void co_print_cof(nvObj_t *nv) { text_print(nv, fmt_cof); } // TYPE_INT
#endif // __TEXT_MODE