This file contains all defines, typedefs and prototypes related to the motor control.
Definition in file PMSM.h.
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Data Structures | |
| struct | PMSMflags |
| Collection of all motor control flags. More... | |
Defines | |
| #define | ADC_CHANNEL_CURRENT 4 |
| The ADC channel where the motor current shunt resistor is connected. | |
| #define | ADC_CHANNEL_SPEED_REF 3 |
| The ADC channel where the analog speed reference is connected. | |
| #define | ADC_PRESCALER ADC_PRESCALER_8 |
| ADC clock prescaler used in this application. | |
| #define | ADC_PRESCALER_64 ((1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0)) |
| ADC clock prescaler 64 setting. | |
| #define | ADC_PRESCALER_8 ((0 << ADPS2) | (1 << ADPS1) | (0 << ADPS0)) |
| ADC clock prescaler 8 setting. | |
| #define | ADC_REFERENCE_VOLTAGE ADC_REFERENCE_VOLTAGE_INTERNAL |
| #define | ADC_REFERENCE_VOLTAGE_AREF ((0 << REFS1) | (0 << REFS0)) |
| ADC AREF voltage reference channel value. | |
| #define | ADC_REFERENCE_VOLTAGE_INTERNAL ((1 << REFS1) | (1 << REFS0)) |
| ADC internal voltage reference channel value. | |
| #define | ADC_REFERENCE_VOLTAGE_VCC ((0 << REFS1) | (1 << REFS0)) |
| ADC VCC voltage reference channel value. | |
| #define | ADMUX_CURRENT (ADC_REFERENCE_VOLTAGE | (1 << ADLAR) | (ADC_CHANNEL_CURRENT << MUX0)) |
| ADMUX settings for motor current measurement. | |
| #define | ADMUX_SPEED_REF (ADC_REFERENCE_VOLTAGE | (1 << ADLAR) | (ADC_CHANNEL_SPEED_REF << MUX0)) |
| ADMUX settings for analog speed reference measurement. | |
| #define | BLOCK_COMMUTATION_DUTY_MULTIPLIER 3 |
| The number to multiply speed input with to produce duty cycle compare value (0-255). | |
| #define | COMMUTATION_TICKS_STOPPED 6000 |
| #define | DEAD_TIME_HALF 4 |
| #define | DIRECTION_COMMAND_PIN PD2 |
| #define | DIRECTION_FORWARD 0 |
| Forward direction flag value. | |
| #define | DIRECTION_REVERSE 1 |
| Reverse direction flag value. | |
| #define | DIRECTION_UNKNOWN 3 |
| Unknown direction flag value. | |
| #define | EMERGENCY_SHUTDOWN_PIN PB5 |
| #define | FALSE 0 |
| FALSE constant value. | |
| #define | H1_PIN PC0 |
| Pin where H1 is connected. | |
| #define | H2_PIN PC1 |
| Pin where H2 is connected. | |
| #define | H3_PIN PC2 |
| Pin where H3 is connected. | |
| #define | HALL_PIN PINC |
| PIN register for Hall sensor input. | |
| #define | HALL_PULLUP_ENABLE FALSE |
| #define | PID_K_D 10 |
| #define | PID_K_I 5 |
| #define | PID_K_P 50 |
| #define | PWM_PATTERN_PORTB ((1 << PB1) | (1 << PB2) | (1 << PB3)) |
| Bit pattern of PWM pins placed on PORTB. | |
| #define | PWM_PATTERN_PORTD ((1 << PD3) | (1 << PD5) | (1 << PD6)) |
| Bit pattern of PWM pins placed on PORTD. | |
| #define | REV_ROTATION_PIN PD4 |
| #define | REVERSE_ROTATION_SIGNAL_ENABLE TRUE |
| #define | SINE_TABLE_LENGTH 192U |
| The number of elements in the sine modulation table per phase. | |
| #define | SPEED_CONTROL_CLOSED_LOOP 1 |
| Speed control selection for closed loop control. | |
| #define | SPEED_CONTROL_METHOD SPEED_CONTROL_OPEN_LOOP |
| #define | SPEED_CONTROL_OPEN_LOOP 0 |
| Speed control selection for open loop control. | |
| #define | SPEED_CONTROLLER_MAX_INCREMENT 550 |
| #define | SPEED_CONTROLLER_MAX_INPUT 256 |
| #define | SPEED_CONTROLLER_TIME_BASE 150 |
| #define | SYNCHRONIZATION_COUNT 2 |
| #define | TABLE_ELEMENTS_PER_COMMUTATION_SECTOR (SINE_TABLE_LENGTH / 6) |
| The number of elements in the sine modulation table for each phase per commutation sector. | |
| #define | TACHO_OUTPUT_ENABLED TRUE |
| #define | TACHO_OUTPUT_PIN PD7 |
| #define | TRUE (!FALSE) |
| TRUE constant value, defined to be compatible with comparisons. | |
| #define | TURN_MODE TURN_MODE_BRAKE |
| #define | TURN_MODE_BRAKE 1 |
| TURN_MODE value for braking (drivers connected to ground). | |
| #define | TURN_MODE_COAST 0 |
| TURN_MODE value for coasting (disabled drivers). | |
| #define | WAVEFORM_BLOCK_COMMUTATION 0 |
| Waveform constant for block commutation. Used as status flag. | |
| #define | WAVEFORM_BRAKING 2 |
| Waveform status flag for braking. | |
| #define | WAVEFORM_SINUSOIDAL 1 |
| Waveform status flag for sinusoidal driving. | |
| #define | WAVEFORM_UNDEFINED 3 |
| Waveform status flag used in transitions between different types of driving. | |
Typedefs | |
| typedef PMSMflags | PMSMflags_t |
| Collection of all motor control flags. | |
Functions | |
| static void | ActualDirectionUpdate (uint8_t lastHall, const uint8_t newHall) |
| __interrupt void | ADCCompleteISR (void) |
| AD conversion complete interrupt service routine. | |
| static void | ADCInit (void) |
| static void | AdjustSineTableIndex (const uint16_t increment) |
| static void | BlockCommutate (const uint8_t direction, uint8_t hall) |
| static void | BlockCommutationSetDuty (const uint8_t duty) |
| static uint8_t | CalculateActualDirection (const uint8_t lastHall, const uint8_t newHall) |
| static void | CheckEmergencyShutdown (void) |
| static void | CommutationTicksUpdate (void) |
| static void | DesiredDirectionUpdate (void) |
| __interrupt void | DirectionInputChangeISR (void) |
| Direction input change interrupt service routine. | |
| static void | DisablePWMOutputs (void) |
| __interrupt void | EmergencyInterruptISR (void) |
| Emergency shut-off interrupt service routine. | |
| static void | EnablePWMOutputs (void) |
| static uint8_t | GetActualDirection (void) |
| static uint8_t | GetDesiredDirection (void) |
| static uint8_t | GetHall (void) |
| __interrupt void | HallChangeISR (void) |
| Hall sensor change interrupt service routine. | |
| static void | InsertDeadband (const uint8_t compareValue, uint8_t *compareHighPtr, uint8_t *compareLowPtr) |
| static uint8_t | IsMotorSynchronized (void) |
| static void | MotorSynchronizedUpdate (void) |
| static void | PinChangeIntInit (void) |
| static void | PortsInit (void) |
| static void | ReverseRotationSignalUpdate (void) |
| static void | SetAdvanceCommutation (const uint8_t leadAngle) |
| static uint16_t | SineTableIncrementCalculate (const uint16_t ticks) |
| static void | SpeedController (void) |
| static void | TachoOutputUpdate (const uint8_t hall) |
| __interrupt void | Timer1CaptureISR (void) |
| Timer1 Capture Evente interrupt service routine. | |
| __interrupt void | Timer2OverflowISR (void) |
| static void | TimersInit (void) |
| static void | TimersSetModeBlockCommutation (void) |
| static void | TimersSetModeBrake (void) |
| static void | TimersSetModeSinusoidal (void) |
| static void | TimersWaitForNextPWMCycle (void) |
| __interrupt void | WatchdogISR (void) |
|
|
The ADC channel where the motor current shunt resistor is connected.
|
|
|
The ADC channel where the analog speed reference is connected.
|
|
|
ADC clock prescaler used in this application.
Definition at line 101 of file PMSM.h. Referenced by ADCInit(). |
|
|
ADC clock prescaler 64 setting.
|
|
|
ADC clock prescaler 8 setting.
|
|
|
ADC voltage reference used in this application.
|
|
|
ADC AREF voltage reference channel value.
|
|
|
ADC internal voltage reference channel value.
|
|
|
ADC VCC voltage reference channel value.
|
|
|
ADMUX settings for motor current measurement.
Definition at line 122 of file PMSM.h. Referenced by ADCCompleteISR(). |
|
|
ADMUX settings for analog speed reference measurement.
Definition at line 119 of file PMSM.h. Referenced by ADCCompleteISR(), and ADCInit(). |
|
|
The number to multiply speed input with to produce duty cycle compare value (0-255).
Definition at line 192 of file PMSM.h. Referenced by Timer1CaptureISR(). |
|
|
The number of commutation 'ticks' that must pass without any hall changes before the motor is considered to be stopped.
Definition at line 165 of file PMSM.h. Referenced by CommutationTicksUpdate(). |
|
|
This value specifies half the dead time in number of clock cycles. Divide by frequency to get duration.
Definition at line 42 of file PMSM.h. Referenced by InsertDeadband(). |
|
|
Pin where direction command input is located. Do not change to pin on a different PORT! Definition at line 128 of file PMSM.h. Referenced by DesiredDirectionUpdate(), and PortsInit(). |
|
|
Forward direction flag value.
Definition at line 61 of file PMSM.h. Referenced by ActualDirectionUpdate(), BlockCommutate(), DesiredDirectionUpdate(), HallChangeISR(), and Timer1CaptureISR(). |
|
|
Reverse direction flag value.
Definition at line 64 of file PMSM.h. Referenced by ActualDirectionUpdate(), and DesiredDirectionUpdate(). |
|
|
Unknown direction flag value.
Definition at line 67 of file PMSM.h. Referenced by ActualDirectionUpdate(), and main(). |
|
|
Emergency shutdown input pin Do not change to pin on a different PORT! Definition at line 146 of file PMSM.h. Referenced by CheckEmergencyShutdown(). |
|
|
FALSE constant value.
|
|
|
Pin where H1 is connected.
Definition at line 80 of file PMSM.h. Referenced by GetHall(), and PortsInit(). |
|
|
Pin where H2 is connected.
Definition at line 83 of file PMSM.h. Referenced by GetHall(), and PortsInit(). |
|
|
Pin where H3 is connected.
Definition at line 86 of file PMSM.h. Referenced by GetHall(), and PortsInit(). |
|
|
PIN register for Hall sensor input.
|
|
|
Uncomment this to disable internal pullup resistors on hall sensor inputs.
|
|
|
PID controller derivative gain constant.
Definition at line 231 of file PMSM.h. Referenced by main(). |
|
|
PID controller integral gain constant.
Definition at line 226 of file PMSM.h. Referenced by main(). |
|
|
PID controller proportional gain constant.
Definition at line 221 of file PMSM.h. Referenced by main(). |
|
|
Bit pattern of PWM pins placed on PORTB.
Definition at line 55 of file PMSM.h. Referenced by DisablePWMOutputs(), and EnablePWMOutputs(). |
|
|
Bit pattern of PWM pins placed on PORTD.
Definition at line 58 of file PMSM.h. Referenced by DisablePWMOutputs(), and EnablePWMOutputs(). |
|
|
Pin where reverse rotation signal output is located. Do not change to pin on a different PORT! Definition at line 134 of file PMSM.h. Referenced by PortsInit(), and ReverseRotationSignalUpdate(). |
|
|
Enables/disables the reverse rotation signal output. TRUE = enabled, FALSE = disabled.
|
|
|
The number of elements in the sine modulation table per phase.
Definition at line 45 of file PMSM.h. Referenced by AdjustSineTableIndex(), and SineTableIncrementCalculate(). |
|
|
Speed control selection for closed loop control.
|
|
|
Type of speed control. select either SPEED_CONTROL_OPEN_LOOP or SPEED_CONTROL_CLOSED_LOOP.
|
|
|
Speed control selection for open loop control.
|
|
|
The maximum increment (maximum speed) to use as setpoint when the maximum speed reference value is input.
Definition at line 238 of file PMSM.h. Referenced by SpeedController(). |
|
|
Max speed reference input. (Rounded up to closest power of 2 in this case, which is recommended to speed up division.
Definition at line 245 of file PMSM.h. Referenced by SpeedController(). |
|
|
The number of ticks between each iteration of the speed loop.
Definition at line 216 of file PMSM.h. Referenced by Timer1CaptureISR(). |
|
|
This constant specifies the number of subsequent detections of correct direction of rotation needed before the firmware is considered synchronized with the motor. (SYNCHRONIZATION_COUNT + 1) hall sensor changes in the same direction are needed. Definition at line 199 of file PMSM.h. Referenced by MotorSynchronizedUpdate(). |
|
|
The number of elements in the sine modulation table for each phase per commutation sector.
Definition at line 52 of file PMSM.h. Referenced by HallChangeISR(). |
|
|
Enables/disables the tacho signal output. TRUE = enabled, FALSE = disabled.
|
|
|
Pin where tacho signal output is located. Do not change to pin on a different PORT! Definition at line 140 of file PMSM.h. Referenced by PortsInit(), and TachoOutputUpdate(). |
|
|
TRUE constant value, defined to be compatible with comparisons.
|
|
|
Turn mode. Set to either TURN_MODE_COAST or TURN_MODE_BRAKE.
|
|
|
TURN_MODE value for braking (drivers connected to ground).
|
|
|
TURN_MODE value for coasting (disabled drivers).
|
|
|
Waveform constant for block commutation. Used as status flag.
Definition at line 149 of file PMSM.h. Referenced by CommutationTicksUpdate(), HallChangeISR(), Timer1CaptureISR(), and TimersSetModeBlockCommutation(). |
|
|
Waveform status flag for braking.
Definition at line 155 of file PMSM.h. Referenced by TimersSetModeBrake(). |
|
|
Waveform status flag for sinusoidal driving.
Definition at line 152 of file PMSM.h. Referenced by HallChangeISR(), Timer1CaptureISR(), and TimersSetModeSinusoidal(). |
|
|
Waveform status flag used in transitions between different types of driving.
Definition at line 158 of file PMSM.h. Referenced by DirectionInputChangeISR(), and main(). |
|
|
Collection of all motor control flags. This struct contains all motor control flags used in this implementation. |
|
||||||||||||
|
|
|
|
AD conversion complete interrupt service routine. This interrupt service routine is automatically executed every time an AD conversion is finished and the converted result is available in the ADC data register. The switch/case construct makes sure the converted value is stored in the variable corresponding to the selected channel and changes the channel for the next ADC measurement. More ADC measurements can be added to the cycle by extending the switch/ case construct. Only the 8 most significant bits of the ADC result are used. Definition at line 1122 of file main.c. References ADMUX_CURRENT, ADMUX_SPEED_REF, current, and speedInput. 01123 { 01124 switch (ADMUX) 01125 { 01126 case ADMUX_SPEED_REF: 01127 speedInput = ADCH; 01128 ADMUX = ADMUX_CURRENT; 01129 break; 01130 case ADMUX_CURRENT: 01131 current = ADCH; 01132 ADMUX = ADMUX_SPEED_REF; 01133 break; 01134 default: 01135 //This is probably an error and should be handled. 01136 break; 01137 } 01138 01139 //Clear Timer/counter0 overflow flag. 01140 TIFR0 = (1 << TOV0); 01141 }
|
|
|
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
Direction input change interrupt service routine. This ISR is called every time the direction input pin changes state. The desired direction flag is updated accordingly. The motor control then goes into a state where it needs a stopped motor or a synchronization before any driving of the motor is performed. Definition at line 999 of file main.c. References DesiredDirectionUpdate(), DisablePWMOutputs(), FALSE, TimersSetModeBrake(), and WAVEFORM_UNDEFINED. 01000 { 01001 //Update desired direction flag. 01002 DesiredDirectionUpdate(); 01003 01004 #if (TURN_MODE == TURN_MODE_COAST) 01005 //Disable driver signals to let motor coast. The motor will automatically 01006 //start once it is synchronized or stopped. 01007 DisablePWMOutputs(); 01008 fastFlags.motorSynchronized = FALSE; 01009 fastFlags.motorStopped = FALSE; 01010 fastFlags.driveWaveform = WAVEFORM_UNDEFINED; 01011 #endif 01012 01013 #if (TURN_MODE == TURN_MODE_BRAKE) 01014 //Set motor in brake mode. The motor will automatically start once it is 01015 //synchronized or stopped. 01016 fastFlags.motorSynchronized = FALSE; 01017 fastFlags.motorStopped = FALSE; 01018 TimersSetModeBrake(); // Automatically sets driveWaveform. 01019 #endif 01020 }
Here is the call graph for this function: ![]() |
|
|
|
|
|
Emergency shut-off interrupt service routine. This ISR is triggered if the emergency shutdown input changes state.
Definition at line 901 of file main.c. References DisablePWMOutputs(). 00902 { 00903 DisablePWMOutputs(); 00904 for (;;) 00905 { 00906 00907 } 00908 }
Here is the call graph for this function: ![]() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hall sensor change interrupt service routine. This interrupt service routine is called every time any of the hall sensors change. The sine table index is updated to reflect the position indicated by the hall sensors. A new increment is calculated, based on the time since last hall sensor change. The Tacho output signal is updated. The actual direction is updated. The reverse rotation output signal is updated. The motor stopped flag is set to false, since the motor is obviously not stopped when there is a hall change. Definition at line 930 of file main.c. References ActualDirectionUpdate(), advanceCommutationSteps, BlockCommutate(), commutationTicks, CSOffsetsForward, CSOffsetsReverse, DIRECTION_FORWARD, FALSE, GetDesiredDirection(), GetHall(), IsMotorSynchronized(), MotorSynchronizedUpdate(), ReverseRotationSignalUpdate(), sineTableIncrement, SineTableIncrementCalculate(), sineTableIndex, sineTableNextSectorStart, TABLE_ELEMENTS_PER_COMMUTATION_SECTOR, TachoOutputUpdate(), TimersSetModeSinusoidal(), WAVEFORM_BLOCK_COMMUTATION, and WAVEFORM_SINUSOIDAL. 00931 { 00932 static uint8_t lastHall = 0xff; 00933 uint8_t hall; 00934 00935 hall = GetHall(); 00936 00937 MotorSynchronizedUpdate(); 00938 uint8_t synch = IsMotorSynchronized(); 00939 if ((fastFlags.driveWaveform != WAVEFORM_SINUSOIDAL) && (synch)) 00940 { 00941 TimersSetModeSinusoidal(); 00942 } 00943 00944 //If sinusoidal driving is used, synchronize sine wave generation to the 00945 //current hall sensor value. Advance commutation is also 00946 //added in the process. 00947 if (fastFlags.driveWaveform == WAVEFORM_SINUSOIDAL) 00948 { 00949 uint16_t tempIndex; 00950 if (GetDesiredDirection() == DIRECTION_FORWARD) 00951 { 00952 tempIndex = (CSOffsetsForward[hall] + advanceCommutationSteps) << 8; 00953 } 00954 else 00955 { 00956 tempIndex = (CSOffsetsReverse[hall] + advanceCommutationSteps) << 8; 00957 } 00958 sineTableIndex = tempIndex; 00959 00960 //Adjust next sector start index. It might be set to a value larger than 00961 //SINE_TABLE_LENGTH at this point. This is adjusted in AdjustSineTableIndex 00962 //and should not be done here, as it will cause problems when advance 00963 //commutation is used. 00964 sineTableNextSectorStart = (tempIndex >> 8) + TABLE_ELEMENTS_PER_COMMUTATION_SECTOR; 00965 } 00966 00967 //If block commutation is used. Commutate according to hall signal. 00968 else if (fastFlags.driveWaveform == WAVEFORM_BLOCK_COMMUTATION) 00969 { 00970 BlockCommutate(GetDesiredDirection(), hall); 00971 } 00972 00973 //Update internal and external signals that depend on hall sensor value. 00974 TachoOutputUpdate(hall); 00975 ActualDirectionUpdate(lastHall, hall); 00976 ReverseRotationSignalUpdate(); 00977 00978 lastHall = hall; 00979 00980 //Calculate new increment for sine wave generation and reset commutation 00981 //timer. 00982 sineTableIncrement = SineTableIncrementCalculate(commutationTicks); 00983 commutationTicks = 0; 00984 00985 //Since the hall sensors are changing, the motor can not be stopped. 00986 fastFlags.motorStopped = FALSE; 00987 }
Here is the call graph for this function: ![]() |
|
||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Timer1 Capture Evente interrupt service routine. This interrupt service routine is run everytime the up-down counting timer0 reaches TOP (0xff). New sinusoidal output values are calculated and the timers are updated to reflect the new values. Definition at line 1030 of file main.c. References AdjustSineTableIndex(), amplitude, BLOCK_COMMUTATION_DUTY_MULTIPLIER, BlockCommutationSetDuty(), CommutationTicksUpdate(), DIRECTION_FORWARD, GetDesiredDirection(), InsertDeadband(), sineTable, sineTableIncrement, sineTableIndex, SPEED_CONTROLLER_TIME_BASE, SpeedControllerRun, TRUE, WAVEFORM_BLOCK_COMMUTATION, and WAVEFORM_SINUSOIDAL. 01031 { 01032 if (fastFlags.driveWaveform == WAVEFORM_SINUSOIDAL) 01033 { 01034 uint8_t tempU, tempV, tempW; 01035 { 01036 uint8_t const __flash * sineTablePtr = sineTable; 01037 01038 AdjustSineTableIndex(sineTableIncrement); 01039 01040 //Add sine table offset to pointer. Must be multiplied by 3, since one 01041 //value for each phase is stored in the table. 01042 sineTablePtr += (sineTableIndex >> 8) * 3; 01043 01044 tempU = *sineTablePtr++; 01045 if (GetDesiredDirection() == DIRECTION_FORWARD) 01046 { 01047 tempV = *sineTablePtr++; 01048 tempW = *sineTablePtr; 01049 } 01050 else 01051 { 01052 tempW = *sineTablePtr++; 01053 tempV = *sineTablePtr; 01054 } 01055 } 01056 01057 //Scale sine modulation values to the current amplitude. 01058 tempU = ((uint16_t)(amplitude * tempU) >> 8); 01059 tempV = ((uint16_t)(amplitude * tempV) >> 8); 01060 tempW = ((uint16_t)(amplitude * tempW) >> 8); 01061 01062 { 01063 uint8_t compareHigh, compareLow; 01064 01065 01066 InsertDeadband(tempU, &compareHigh, &compareLow); 01067 OCR0A = compareHigh; 01068 OCR0B = compareLow; 01069 01070 InsertDeadband(tempV, &compareHigh, &compareLow); 01071 OCR1AL = compareHigh; 01072 OCR1BL = compareLow; 01073 01074 InsertDeadband(tempW, &compareHigh, &compareLow); 01075 OCR2A = compareHigh; 01076 OCR2B = compareLow; 01077 } 01078 } 01079 else if (fastFlags.driveWaveform == WAVEFORM_BLOCK_COMMUTATION) 01080 { 01081 uint16_t blockCommutationDuty = amplitude * BLOCK_COMMUTATION_DUTY_MULTIPLIER; 01082 01083 if (blockCommutationDuty > 255) 01084 { 01085 blockCommutationDuty = 255; 01086 } 01087 01088 BlockCommutationSetDuty((uint8_t)blockCommutationDuty); 01089 } 01090 01091 CommutationTicksUpdate(); 01092 01093 { 01094 //Run the speed regulation loop with constant intervals. 01095 static uint8_t speedRegTicks = 0; 01096 speedRegTicks++; 01097 if (speedRegTicks >= SPEED_CONTROLLER_TIME_BASE) 01098 { 01099 SpeedControllerRun = TRUE; 01100 speedRegTicks -= SPEED_CONTROLLER_TIME_BASE; 01101 } 01102 } 01103 }
Here is the call graph for this function: ![]() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.4.4