2017-01-17 22:37:55 -08:00
# ifndef IDLE_H
# define IDLE_H
2016-04-06 20:28:13 -07:00
# include "globals.h"
2021-11-17 16:30:29 -08:00
# include "table2d.h"
2019-01-18 02:15:27 -08:00
# include BOARD_H //Note that this is not a real file, it is defined in globals.h.
2016-04-06 20:28:13 -07:00
2017-03-13 23:25:59 -07:00
# define IAC_ALGORITHM_NONE 0
# define IAC_ALGORITHM_ONOFF 1
# define IAC_ALGORITHM_PWM_OL 2
# define IAC_ALGORITHM_PWM_CL 3
# define IAC_ALGORITHM_STEP_OL 4
# define IAC_ALGORITHM_STEP_CL 5
2020-12-01 13:37:58 -08:00
# define IAC_ALGORITHM_PWM_OLCL 6 //Openloop plus closedloop IAC control
2021-12-08 13:52:56 -08:00
# define IAC_ALGORITHM_STEP_OLCL 7 //Openloop plus closedloop IAC control
2017-03-13 23:25:59 -07:00
2022-01-04 04:23:15 -08:00
# define IDLE_PIN_LOW() *idle_pin_port &= ~(idle_pin_mask)
# define IDLE_PIN_HIGH() *idle_pin_port |= (idle_pin_mask)
2015-08-25 20:27:50 -07:00
# define STEPPER_FORWARD 0
# define STEPPER_BACKWARD 1
2015-08-20 06:21:27 -07:00
# define IDLE_TABLE_SIZE 10
2015-05-29 00:33:00 -07:00
2021-10-22 15:04:31 -07:00
enum StepperStatus { SOFF , STEPPING , COOLING } ; //The 2 statuses that a stepper can have. STEPPING means that a high pulse is currently being sent and will need to be turned off at some point.
2015-05-29 00:33:00 -07:00
struct StepperIdle
{
2017-03-20 04:29:42 -07:00
int curIdleStep ; //Tracks the current location of the stepper
int targetIdleStep ; //What the targetted step is
2017-02-28 19:42:24 -08:00
volatile StepperStatus stepperStatus ;
2015-05-29 00:33:00 -07:00
volatile unsigned long stepStartTime ; //The time the curren
2018-12-02 14:36:41 -08:00
byte lessAirDirection ;
byte moreAirDirection ;
2015-05-29 00:33:00 -07:00
} ;
2015-08-20 06:21:27 -07:00
struct table2D iacClosedLoopTable ;
struct table2D iacPWMTable ;
struct table2D iacStepTable ;
//Open loop tables specifically for cranking
struct table2D iacCrankStepsTable ;
struct table2D iacCrankDutyTable ;
2015-08-20 21:14:47 -07:00
struct StepperIdle idleStepper ;
2015-08-20 06:21:27 -07:00
bool idleOn ; //Simply tracks whether idle was on last time around
2017-01-12 03:33:26 -08:00
byte idleInitComplete = 99 ; //TRacks which idle method was initialised. 99 is a method that will never exist
2019-12-01 19:47:30 -08:00
unsigned int iacStepTime_uS ;
2019-12-14 14:57:22 -08:00
unsigned int iacCoolTime_uS ;
2016-12-11 04:23:54 -08:00
unsigned int completedHomeSteps ;
2015-05-29 00:33:00 -07:00
2019-01-18 02:15:27 -08:00
volatile PORT_TYPE * idle_pin_port ;
2019-03-03 14:15:57 -08:00
volatile PINMASK_TYPE idle_pin_mask ;
2019-01-18 02:15:27 -08:00
volatile PORT_TYPE * idle2_pin_port ;
2019-03-03 14:15:57 -08:00
volatile PINMASK_TYPE idle2_pin_mask ;
2020-11-15 13:49:22 -08:00
volatile PORT_TYPE * idleUpOutput_pin_port ;
volatile PINMASK_TYPE idleUpOutput_pin_mask ;
2019-01-18 02:15:27 -08:00
2015-09-25 17:12:59 -07:00
volatile bool idle_pwm_state ;
2021-12-08 13:52:56 -08:00
bool lastDFCOValue ;
2015-09-25 17:12:59 -07:00
unsigned int idle_pwm_max_count ; //Used for variable PWM frequency
2015-09-29 16:27:37 -07:00
volatile unsigned int idle_pwm_cur_value ;
2017-03-10 01:34:39 -08:00
long idle_pid_target_value ;
2020-12-01 13:37:58 -08:00
long FeedForwardTerm ;
2020-07-29 16:15:24 -07:00
unsigned long idle_pwm_target_value ;
2016-04-06 20:28:13 -07:00
long idle_cl_target_rpm ;
2017-03-13 23:25:59 -07:00
byte idleCounter ; //Used for tracking the number of calls to the idle control function
2022-04-04 14:27:54 -07:00
uint8_t idleTaper ;
2015-09-25 17:12:59 -07:00
2020-11-15 13:49:22 -08:00
byte idleUpOutputHIGH = HIGH ; // Used to invert the idle Up Output
byte idleUpOutputLOW = LOW ; // Used to invert the idle Up Output
2015-05-29 00:33:00 -07:00
void initialiseIdle ( ) ;
2021-04-21 17:58:50 -07:00
void initialiseIdleUpOutput ( ) ;
2021-04-20 23:33:19 -07:00
void disableIdle ( ) ;
void idleInterrupt ( ) ;
2017-01-17 22:37:55 -08:00
# endif