38#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)
257#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)
284#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)
291 Serial.begin(115200);
309 TIMSK4 |= (1 << TOIE4);
387 PLLFRQ = (0 << PINMUX) | (1 << PLLUSB) |
PLL_POSTSCALER_DIV_1_5 | (1 << PDIV3) | (0 << PDIV2) | (1 << PDIV1) | (0 << PDIV0);
390 PLLCSR = (1 << PINDIV) | (1 << PLLE);
393 while ((PLLCSR & (1 << PLOCK)) == 0)
409#if (EMULATE_HALL == TRUE)
417#if ((HALL_PULLUP_ENABLE == TRUE) && (EMULATE_HALL != TRUE))
458 TCCR1A = (1 << WGM11) | (0 << WGM10);
459 TCCR1B = (0 << WGM13) | (1 << WGM12);
460 TIMSK1 = (1 << TOIE1);
465#if (EMULATE_HALL == TRUE)
467 TCCR3A = (1 << WGM31) | (1 << WGM30);
468 TCCR3B = (1 << WGM33) | (1 << WGM32);
469 TIMSK3 = (1 << TOIE3);
473 OCR3AL = (uint8_t)(0xff &
TIM3_TOP);
479 TCCR4A = (0 << COM4A1) | (1 << COM4A0) | (0 << COM4B1) | (1 << COM4B0) | (1 << PWM4A) | (1 << PWM4B);
481 TCCR4C |= (0 << COM4D1) | (1 << COM4D0) | (1 << PWM4D);
482 TCCR4E = (1 << ENHC4);
504 EICRA = (0 << ISC21) | (1 << ISC20) | (0 << ISC01) | (1 << ISC00);
505 EIMSK = (1 << INT2) | (1 << INT0);
508 PCMSK0 = (1 << PCINT3) | (1 << PCINT2) | (1 << PCINT1);
511 PCICR = (1 << PCIE0);
538#if (WAIT_FOR_BOARD == TRUE)
545 ADCSRA = (1 << ADEN);
561 while (adc_reading > 0x3C0)
590 ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (1 << ADIF) | (1 << ADIE) |
ADC_PRESCALER;
603 ADCSRA |= (1 << ADSC);
606 while (ADCSRA & (1 << ADSC))
612 uint16_t value = (ADCL >> 6);
613 value |= 0x3ff & (ADCH << 2);
664#if (TURN_OFF_MODE == TURN_OFF_MODE_COAST)
720#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)
725 uint16_t outputValue;
729 if (outputValue > 255)
823 TCCR4A = (0 << COM4A1) | (1 << COM4A0) | (0 << COM4B1) | (1 << COM4B0) | (1 << PWM4A) | (1 << PWM4B);
824 TCCR4C |= (0 << COM4D1) | (1 << COM4D0) | (1 << PWM4D);
825 TCCR4D = (1 << WGM41) | (1 << WGM40);
850 while (!(TIFR4 & (1 << TOV4)))
867 const uint8_t *tableAddress;
877 tableAddress += (hall * 4);
880 TCCR4E &= ~0b00111111;
884 PORTB |= (uint8_t)pgm_read_byte_near(tableAddress++);
885 PORTC |= (uint8_t)pgm_read_byte_near(tableAddress++);
886 PORTD |= (uint8_t)pgm_read_byte_near(tableAddress++);
887 TCCR4E |= (uint8_t)pgm_read_byte_near(tableAddress);
1056 if ((hall == 0) || (hall == 0b111))
1075#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)
1084#if (TURN_OFF_MODE == TURN_OFF_MODE_RAMP)
1117 static uint8_t lastHall = 0xff;
1191 static uint8_t speedRegTicks = 0;
1201#if (EMULATE_HALL == TRUE)
1281 ibus |= (ADCH << 2);
1287#if (IBUS_FAULT_ENABLE == TRUE)
1289 static uint8_t currentErrorCount = 0;
1292 if (currentErrorCount < 3)
1294 currentErrorCount++;
1310#if (IBUS_FAULT_ENABLE == TRUE)
1311 currentErrorCount = 0;
1317#if (IBUS_FAULT_ENABLE == TRUE)
1318 currentErrorCount = 0;
1368 TIFR0 = (1 << TOV0);
Motor config header file.
void SweepLEDsBlocking(void)
Sweeps through all LEDs individually with a delay.
void faultSequentialStateMachine(volatile faultflags_t *faultFlags, volatile motorflags_t *motorFlags)
Sequential State Machine for Handling Fault Flags.
int16_t calculateEMA(uint16_t currentSample, uint16_t previousEMA, uint8_t alphaExponent)
Exponential Moving Average (EMA) calculation algorithm.
#define ADC_MUX_H_BITS
High ADC channel selection bit (MUX5) mask.
#define ADC_MUX_L_BITS
Lower ADC channel selection bits (MUX4:0) mask.
#define TIM4_TOP(tim4Freq)
#define DEAD_TIME_HALF(deadTime)
This value specifies half the dead time in number of clock cycles. Divide by frequency to get duratio...
#define DT_PRESCALER_DIV_PATTERN(dtPrescaler)
Deadtime generator pre-scaler selection bits based on pre-scaler value.
#define TIM3_TOP
Calculated top value for Timer 3.
#define TIM4_PRESCALER_DIV_PATTERN(tim4Prescaler)
Timer 4 clock select bits based on pre-scaler value.
#define PLL_POSTSCALER_DIV_1_5
PLL Post-scaler - division factor 1.5.
#define H2_PIN
Pin where H2 is connected.
#define WAVEFORM_BLOCK_COMMUTATION
Waveform constant for block commutation.
#define ADC_MUX_L_IBUS
Lower analog channel selection bits (MUX4:0) for motor current measurement.
#define DIRECTION_FORWARD
Forward direction flag value.
#define ADC_MUX_H_IPHASE_V
High analog channel selection bit (MUX5) for for motor current measurement.
#define ADC_MUX_H_VBUSVREF
High analog channel selection bit (MUX5) for for motor vbusVref measurement.
#define WAVEFORM_UNDEFINED
Waveform status flag used for coasting.
#define FAULT_PIN_2
Fault Pin 2.
#define ADC_MUX_L_IPHASE_W
Lower analog channel selection bits (MUX4:0) for motor current measurement.
#define DIRECTION_COMMAND_PIN
Pin where direction command input is located.
#define SPEED_INPUT_SOURCE_LOCAL
Speed input source - Local or speed input pin.
#define FAST_ACCESS(register_address)
Assign a specific memory address to a variable for fast access.
#define CHOOSE_DT_PRESCALER(deadTime)
Macro to choose Timer4 dead time pre-scaler based on the dead time.
#define ADC_MUX_L_VBUSVREF
Lower analog channel selection bits (MUX4:0) for motor vbusVref measurement.
#define SPEED_CONTROLLER_MAX_INPUT
Maximum Speed Reference Input.
#define DIRECTION_REVERSE
Reverse direction flag value.
struct motorflags motorflags_t
Collection of all motor control flags.
#define ENABLE_PIN
Enable input pin.
#define ADC_MUX_H_IPHASE_W
High analog channel selection bit (MUX5) for for motor current measurement.
#define PWM_PATTERN_PORTC
Bit pattern of PWM pins placed on PORTC (Phase B).
fault_flag_t
Enumeration of fault flags.
#define ADC_TRIGGER
ADC trigger used in this application.
#define HALL_PIN
PIN register for Hall sensor input.
struct motorconfigs motorconfigs_t
Collection of motor configurations.
#define ADC_MUX_L_IPHASE_V
Lower analog channel selection bits (MUX4:0) for motor current measurement.
#define IBUS_PIN
IBUS ADC input pin (used to check if board is attached).
#define ADC_MUX_H_IBUS
High analog channel selection bit (MUX5) for for motor current measurement.
#define TRUE
TRUE constant value, defined to be compatible with comparisons.
#define FALSE
FALSE constant value.
#define CHOOSE_TIM4_PRESCALER(tim4Freq)
Macro to choose Timer4 pre-scaler.
#define ADC_PRESCALER
ADC clock pre-scaler used in this application.
#define REMOTE_PIN
Remote input pin.
#define ADC_MUX_L_IPHASE_U
Lower analog channel selection bits (MUX4:0) for motor current measurement.
#define FORCE_INLINE
Macro for forcing inline expansion of functions.
#define PWM_PATTERN_PORTB
Bit pattern of PWM pins placed on PORTB (Phase A).
#define PWM_PATTERN_PORTD
Bit pattern of PWM pins placed on PORTD (Phase C).
#define FAULT_PIN_3
Fault Pin 3.
#define DIRECTION_UNKNOWN
Unknown direction flag value.
struct faultflags faultflags_t
Collection of all fault flags.
#define ADC_MUX_H_IPHASE_U
High analog channel selection bit (MUX5) for for motor current measurement.
#define H3_PIN
Pin where H3 is connected.
#define H1_PIN
Pin where H1 is connected.
#define ADC_REFERENCE_VOLTAGE
ADC voltage reference used in this application.
#define FAULT_PIN_1
Fault Pin 1.
@ FAULT_OVER_CURRENT
Has it tripped the over current limit?
@ FAULT_USER_FLAG1
Is user flag 1 set?
@ FAULT_NO_HALL_CONNECTIONS
Is there no hall connections?
@ FAULT_USER_FLAG2
Is user flag 2 set?
@ FAULT_RESERVED
Reserved flag, always false.
@ FAULT_USER_FLAG3
Is user flag 3 set?
@ FAULT_REVERSE_DIRECTION
Is motor spinning in an unexpected direction?
@ FAULT_MOTOR_STOPPED
Is motor stopped?
#define TIM1_CLOCK_DIV_64
Timer1 clock - i/o clk with division factor 64.
#define TIM1_CLOCK_DIV_8
Timer1 clock - i/o clk with division factor 8.
#define IBUS_WARNING_THRESHOLD
Hi-side Current (IBUS) Warning Threshold (Register Value)
#define PID_K_P
PID Controller Proportional Gain Constant (Only for Closed Loop)
#define F_MOSFET
Desired Switching Frequency for MOSFET Gate Signals.
#define IBUS_ERROR_THRESHOLD
Hi-side Current (IBUS) Error Threshold (Register Value)
#define SPEED_CONTROLLER_MAX_DELTA
Speed Controller Maximum Delta (Applicable for Open Loop Control)
#define PID_K_D
PID Controller Derivative Gain Constant (Only for Closed Loop)
#define SPEED_CONTROLLER_MAX_SPEED
Speed Controller Maximum Speed.
#define SPEED_CONTROLLER_TIME_BASE
Speed Controller Time Base.
#define COMMUTATION_TICKS_STOPPED
Commutation Stopped Limit.
#define PID_K_I
PID Controller Integral Gain Constant (Only for Closed Loop)
#define DEAD_TIME
Dead Time Specification.
static void BlockCommutate(const uint8_t direction, uint8_t hall)
Perform block commutation based on direction and hall sensor input.
static uint8_t GetHall(void)
Read the hall sensor inputs and decode the hall state.
static void PortsInit(void)
Initialize I/O port directions and pull-up resistors.
volatile uint16_t vbusVref
VBUS voltage measurement (Register Value)
static void TimersSetModeBlockCommutation(void)
Configures timers for block commutation.
static void TimersWaitForNextPWMCycle(void)
Wait for the start of the next PWM cycle.
static void ADCInit(void)
Initializes the ADC.
static void SetFaultFlag(fault_flag_t flag, uint8_t value)
Sets a fault flag value.
volatile motorflags_t motorFlags
Motor control flags placed in I/O space for fast access.
static void SpeedController(void)
Speed regulator loop.
static void DisablePWMOutputs(void)
Disable PWM output pins.
volatile uint8_t speedInput
The most recent "speed" input measurement.
static void FlagsInit(void)
Initializes motorFlags and faultFlags.
volatile uint16_t ibus
Hi-side Current (IBUS) measurement (Register Value).
volatile motorconfigs_t motorConfigs
Motor Configs.
static void PinChangeIntInit(void)
Initialize pin change interrupts.
void setup(void)
Main initialization function.
static void PLLInit(void)
Initialize PLL (Phase-Locked Loop)
static void ActualDirectionUpdate(uint8_t lastHall, const uint8_t newHall)
Update the global actual direction flag based on the two latest hall values.
static void ReverseRotationSignalUpdate(void)
Update the reverse rotation flag.
volatile uint16_t commutationTicks
The number of 'ticks' between two hall sensor changes (counter).
static void CommutationTicksUpdate(void)
Update the 'tick' counter and check for a stopped motor.
static void DesiredDirectionUpdate(void)
Updates global desired direction flag.
volatile int16_t iphaseW
In-line Phase W current current measurement (Register Value).
volatile uint8_t speedOutput
The most recent "speed" output from the speed controller.
volatile faultflags_t faultFlags
Fault flags placed in I/O space for fast access.
static void SetDuty(const uint16_t duty)
Set duty cycle for TIM4.
static void EnablePWMOutputs(void)
Enable PWM output pins.
volatile uint16_t lastCommutationTicks
The number of 'ticks' between two hall sensor changes (store).
void TimersInit(void)
Initializes and synchronizes Timers.
static void EnableUpdate(void)
Check whether the enable pin is set and update flags accordingly.
static uint16_t ADCSingleConversion(void)
Perform a single ADC conversion.
static void ClearPWMPorts(void)
Clear PWM output ports.
volatile int16_t iphaseU
In-line Phase U current current measurement (Register Value).
static void FatalError()
Handle a fatal error and enter a fault state.
static void ConfigsInit(void)
Initializes motorConfigs.
static void DisableMotor(void)
Disable motor.
volatile int16_t iphaseV
In-line Phase V current current measurement (Register Value).
static void RemoteUpdate(void)
Check whether the remote pin is set and update flags accordingly.
void loop()
Main Loop Function.
ISR(INT0_vect)
Enable interrupt service routine.
void PIDInit(int16_t p_factor, int16_t i_factor, int16_t d_factor, pidData_t *pid)
Initialisation of PID controller parameters.
uint16_t PIDController(int16_t setPoint, int16_t processValue, pidData_t *pid_st)
PID control algorithm.
void PIDResetIntegrator(pidData_t *pid_st)
Resets the integrator in the PID regulator.
PID controller header file.
struct pidData pidData_t
PID Status.
void ScpiInput(Stream &interface)
Processes incoming data from a serial interface for SCPI commands.
void ScpiInit(void)
Initializes the SCPI command parser and registers all supported commands.
SCPI implementation header file.
const uint8_t expectedHallSequenceReverse[7]
Table of Expected Hall Sensor Values in Reverse Direction.
const uint8_t expectedHallSequenceForward[7]
Table of Expected Hall Sensor Values in Forward Direction.
const uint8_t blockCommutationTableReverse[32]
Block Commutation Port Direction Masks for Reverse Driving.
const uint8_t blockCommutationTableForward[32]
Block Commutation Port Direction Masks for Forward Driving.