diff --git a/PowerConverter.X/CmdInterpreter.c b/PowerConverter.X/CmdInterpreter.c index e680f1a..1e51f9f 100644 --- a/PowerConverter.X/CmdInterpreter.c +++ b/PowerConverter.X/CmdInterpreter.c @@ -14,3 +14,3 @@ void SetScaleFactor(unsigned char *RcvdBytes); //CmdID = 21 void SetRetractFactor(unsigned char *RcvdBytes); //CmdID = 22 void SetTargetVoltage(unsigned char *RcvdBytes); //CmdID = 23 @@ -18,8 +18,8 @@ void SetMaxBattChargeCurrent(unsigned char *RcvdBytes); //CmdID = 24 void SetBattSwitch(unsigned char *RcvdBytes); //CmdID = 25 void SetGainScheduleGain(unsigned char *RcvdBytes); //CmdID = 27 void SetRPM_StdDevTarget(unsigned char *RcvdBytes); //CmdID = 28 void SetMaxBattDrawCurrent(unsigned char *RcvdBytes); //CmdID = 29 void SetWindCurrent(unsigned char *RcvdBytes); //CmdID = 31 void SetBiasWindCurrent(unsigned char *RcvdBytes); //CmdID = 34 void SetCANPacketRate(unsigned char *RcvdBytes); //CmdID = 35 void Reset(unsigned char *RcvdBytes); //CmdID = 255 @@ -30 +30 @@ void Reset(unsigned char *RcvdBytes); //CmdID = 255 ReceivedCmdParams CmdParams[NUMBER_OF_CMDS_TO_INTERPRET] = { @@ -45,4 +45,3 @@ ReceivedCmdParams CmdParams[NUMBER_OF_CMDS_TO_INTERPRET] ={ /*SetScaleFactor: CmdID = 21 * Input: takes single unsigned byte as argument from CANbus. * Scaling: CRSF_PERCENTAGES cnts per unit factor. @@ -52,2 +51 @@ ReceivedCmdParams CmdParams[NUMBER_OF_CMDS_TO_INTERPRET] ={ void SetScaleFactor(unsigned char *RcvdBytes) { @@ -58,3 +56,2 @@ void SetScaleFactor(unsigned char *RcvdBytes) Set_CRSALValue(local_setpoint, &scale_factor); //Sets if within limits, otherwise does not change scale_factor. } @@ -62,3 +59,3 @@ void SetScaleFactor(unsigned char *RcvdBytes) /*SetRetractFactor: CmdID = 22 * Input: takes single unsigned byte as argument from CANbus. * Scaling: CRSF_PERCENTAGES cnts per unit factor. @@ -68,2 +65 @@ void SetScaleFactor(unsigned char *RcvdBytes) void SetRetractFactor(unsigned char *RcvdBytes) { @@ -74 +70 @@ void SetRetractFactor(unsigned char *RcvdBytes) Set_CRSALValue(local_setpoint, &retract_factor); //Sets if within limits, otherwise does not change retract_factor. @@ -78,4 +74,3 @@ void SetRetractFactor(unsigned char *RcvdBytes) /*SetTargetVoltage: CmdID = 23 * Input: takes unsigned int from CANbus (two bytes). * Scaling: CRSF_VOLTAGE cnts per Volt. @@ -85,2 +80 @@ void SetRetractFactor(unsigned char *RcvdBytes) void SetTargetVoltage(unsigned char *RcvdBytes) { @@ -92,2 +85,0 @@ void SetTargetVoltage(unsigned char *RcvdBytes) [- -] [- Set_CRSALValue(local_setpoint,&TargetVoltage); //Sets if within limits, otherwise does not change TargetVoltage.-] @@ -95 +87 @@ void SetTargetVoltage(unsigned char *RcvdBytes) [-}-]{+Set_CRSALValue(local_setpoint, &TargetVoltage); //Sets if within limits, otherwise does not change TargetVoltage.+} @@ -96,0 +89 @@ void SetTargetVoltage(unsigned char *RcvdBytes) {+}+} @@ -98,3 +91,3 @@ void SetTargetVoltage(unsigned char *RcvdBytes) /*SetMaxBattChargeCurrent: CmdID = 24 * Input: takes unsigned int from CANbus (two bytes). * Scaling: CRSF_CURRENT cnts per Amp. @@ -104,2 +97 @@ void SetTargetVoltage(unsigned char *RcvdBytes) void SetMaxBattChargeCurrent(unsigned char *RcvdBytes) { @@ -111 +103 @@ void SetMaxBattChargeCurrent(unsigned char *RcvdBytes) Set_CRSALValue(local_setpoint, &MaxBattChargeCurrent); //Sets if within limits, otherwise does not change MaxBattChargeCurrent. @@ -115,4 +107,3 @@ void SetMaxBattChargeCurrent(unsigned char *RcvdBytes) /*SetMaxBattDrawCurrent: CmdID = 29 * Input: takes unsigned int from CANbus (two bytes). * Scaling: CRSF_CURRENT cnts per Amp. @@ -122,2 +113 @@ void SetMaxBattChargeCurrent(unsigned char *RcvdBytes) void SetMaxBattDrawCurrent(unsigned char *RcvdBytes) { @@ -129 +119 @@ void SetMaxBattDrawCurrent(unsigned char *RcvdBytes) Set_CRSALValue(local_setpoint, &MaxBattDrawCurrent); //Sets if within limits, otherwise does not change MaxBattDrawCurrent. @@ -132,4 +122,3 @@ void SetMaxBattDrawCurrent(unsigned char *RcvdBytes) /*SetBattSwitchMode: * Input: takes single from CANbus (two bytes). * Scaling: None @@ -145,3 +134,2 @@ void SetBattSwitch(unsigned char *RcvdBytes) //25 /*SetGainScheduleGain: CmdID = 27 * Input: takes unsigned int from CANbus (two bytes). @@ -153,2 +141 @@ void SetBattSwitch(unsigned char *RcvdBytes) //25 void SetGainScheduleGain(unsigned char *RcvdBytes) { @@ -159,10 +146 @@ void SetGainScheduleGain(unsigned char *RcvdBytes) local_setpoint = (signed int) local_uint * GAIN_SCHEDULE_INTEGRATION_TIME / 60;[-if(Set_CRSALValue(local_setpoint,&ScaleIntegrationGain) == 0) //Sets if within limits, otherwise does not change ScaleIntegrationGain.-] [- {-] [- if (local_setpoint == 0)-] [- StatusBitFields.GainScheduleMode = 0;-] [- else-] [- StatusBitFields.GainScheduleMode = 1;-] [- }-] [-}-] @@ -169,0 +148,8 @@ void SetGainScheduleGain(unsigned char *RcvdBytes) {+if (Set_CRSALValue(local_setpoint, &ScaleIntegrationGain) == 0) //Sets if within limits, otherwise does not change ScaleIntegrationGain.+} {+ {+} {+ if (local_setpoint == 0)+} {+ StatusBitFields.GainScheduleMode = 0;+} {+ else+} {+ StatusBitFields.GainScheduleMode = 1;+} {+ }+} {+}+} @@ -172,2 +158,2 @@ void SetGainScheduleGain(unsigned char *RcvdBytes) * Input: takes unsigned int from CANbus (two bytes). * Scaling: CRSF_ROM cnts per RPM. @@ -177,2 +163 @@ void SetGainScheduleGain(unsigned char *RcvdBytes) void SetRPM_StdDevTarget(unsigned char *RcvdBytes) { @@ -184 +169 @@ void SetRPM_StdDevTarget(unsigned char *RcvdBytes) Set_CRSALValue(local_setpoint, &GainScheduleRPMStdDevTarget); @@ -187 +171,0 @@ void SetRPM_StdDevTarget(unsigned char *RcvdBytes) @@ -189,2 +173,2 @@ void SetRPM_StdDevTarget(unsigned char *RcvdBytes) * Input: takes signed int from CANbus (two bytes). * Scaling: CRSF_CURRENT cnts per Amp. @@ -194,2 +178 @@ void SetRPM_StdDevTarget(unsigned char *RcvdBytes) void SetWindCurrent(unsigned char *RcvdBytes) { @@ -205 +188 @@ void SetWindCurrent(unsigned char *RcvdBytes) if (Set_CRSALValue(local_setpoint, &UserCommandedCurrent) == 0) //Sets if within limits and returns 0, otherwise does not change UserCommandedCurrent. @@ -208,2 +191,2 @@ void SetWindCurrent(unsigned char *RcvdBytes) StatusBitFields.Mode = TORQUE_MODE; } @@ -213 +195,0 @@ void SetWindCurrent(unsigned char *RcvdBytes) @@ -215,2 +197,2 @@ void SetWindCurrent(unsigned char *RcvdBytes) * Input: takes signed int from CANbus (two bytes). * Scaling: CRSF_CURRENT cnts per Amp. @@ -220,2 +202 @@ void SetWindCurrent(unsigned char *RcvdBytes) void SetBiasWindCurrent(unsigned char *RcvdBytes) { @@ -227,6 +207,0 @@ void SetBiasWindCurrent(unsigned char *RcvdBytes) [- -] [- if(Set_CRSALValue(local_setpoint,&BiasCurrent) == 0) //Sets if within limits and returns 0, otherwise does not change BiasCurrent.-] [- {-] [- BiasCmdTimeout_ctr = BiasCmdTimeout; //Reset Command Timeout Counter-] [- } -] [-}-] @@ -233,0 +209,5 @@ void SetBiasWindCurrent(unsigned char *RcvdBytes) {+if (Set_CRSALValue(local_setpoint, &BiasCurrent) == 0) //Sets if within limits and returns 0, otherwise does not change BiasCurrent.+} {+ {+} {+ BiasCmdTimeout_ctr = BiasCmdTimeout; //Reset Command Timeout Counter+} {+ }+} {+}+} @@ -236,5 +216,2 @@ void SetBiasWindCurrent(unsigned char *RcvdBytes) * Input: takes single unsigned byte from CANbus. * Scaling: 1 Count per Hz.[-* Upper Limit (Engineering Units): 40 Hz-] [- * Lower Limit (Engineering Units): 10 Hz-] [- * Notes: Only 10 or 40 allowed.-] @@ -242,2 +219 @@ void SetBiasWindCurrent(unsigned char *RcvdBytes) void SetCANPacketRate(unsigned char *RcvdBytes) { @@ -247,5 +223,5 @@ void SetCANPacketRate(unsigned char *RcvdBytes) if (Set_CRSALValue(local_setpoint, &CAN_packet_rate) == 0) //Sets if within limits and returns 0, otherwise does not change BiasCurrent. { PR1 = SetCANTimer(CAN_packet_rate.Value); [-}-]{+if (TMR1 > PR1) //Need to reset timer to smaller value do it doesn't wait to roll over before interrupting+} {+ TMR1 = 0;+} @@ -253 +229 @@ void SetCANPacketRate(unsigned char *RcvdBytes) } @@ -254,0 +231 @@ void SetCANPacketRate(unsigned char *RcvdBytes) {+}+} diff --git a/PowerConverter.X/GainScheduling.c b/PowerConverter.X/GainScheduling.c index 5694209..689f16d 100644 --- a/PowerConverter.X/GainScheduling.c +++ b/PowerConverter.X/GainScheduling.c @@ -25 +25 @@ unsigned int AdjustScaleFactor(float N) @@ -46 +46 @@ unsigned int AdjustScaleFactor(float N) {+//+}iTemp1 = ScaleIntegrationGain.Value; diff --git a/PowerConverter.X/ToDo.txt b/PowerConverter.X/ToDo.txt index 0d5ab9a..9a13ab3 100644 --- a/PowerConverter.X/ToDo.txt +++ b/PowerConverter.X/ToDo.txt @@ -1,0 +2,2 @@ ToDo: {+- Determine why Timer 3 Interrupt priority affects voltage control...+} {+- Calibrate pressure and torque sensors.+} diff --git a/PowerConverter.X/UARTRxInterrupt.c b/PowerConverter.X/UARTRxInterrupt.c index 2204a3e..11d2fca 100644 --- a/PowerConverter.X/UARTRxInterrupt.c +++ b/PowerConverter.X/UARTRxInterrupt.c @@ -17,0 +18 @@ void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void) {+int tmpTargetVoltage;+} @@ -127,0 +129 @@ void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void) @@ -129 +130,0 @@ void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void) [- int tmpTargetVoltage;-] diff --git a/PowerConverter.X/WindingCurrentControl.c b/PowerConverter.X/WindingCurrentControl.c index 38fe80f..6c91a10 100644 --- a/PowerConverter.X/WindingCurrentControl.c +++ b/PowerConverter.X/WindingCurrentControl.c @@ -36,0 +37 @@ void WindingCurrentControl(void) { {+//AUXIO_PIN4 = 0;+} @@ -45,23 +45,0 @@ void WindingCurrentControl(void) { [-//#define VARIABLESWITCHFREQUENCY //Change Switching Frequency Here if desired: Still need adjust Integral gains where affected by PWM rate. -] [-#ifdef VARIABLESWITCHFREQUENCY -] [-#define fMax 20000 -] [-#define NMax 6000 //RPMs at which FMax is defined, code maintains the Transistions per Elec Cycle to other speeds within the limits of fMax and fMin -] [-#define fMin 20000 //Setting this to fMax effectively disables feature. -] [-#define Fcy 40000000L -] [- -] [- int fPWM; -] [- int TargetPeriod; -] [- fPWM = _Q15abs((int) (((long) RPMs) * fMax / (2 * NMax))); //Times 10 for testing at low speed -] [- //iTemp1 = fPWM; -] [- if (fPWM > fMax) -] [- fPWM = fMax; -] [- if (fPWM < fMin) -] [- fPWM = fMin; -] [- TargetPeriod = (int) ((((long) Fcy) / 2) / fPWM); -] [- if (P1TPER < TargetPeriod) //Step P1TPER one count at a time. -] [- P1TPER++; -] [- if (P1TPER > TargetPeriod) -] [- P1TPER--; -] [-#endif -] [- -] [- -] @@ -308,0 +287,7 @@ void WindingCurrentControl(void) { {+AUXIO_PIN2 = 1; +} {+AUXIO_PIN4 = 1; +} {+ //Restart ADC Sampling now that current control is done, gives a bit more time for it to finish compared to starting on the PWM interrupt. +} {+ TMR3 = 0; +} {+ //DMA0CONbits.CHEN = 1; +} {+ T3CON = 0x8000; //Start timer 3, an ADC sample and convert will then happen every ~4.04uSec until four are completed and DMA occurs.+} @@ -310 +294,0 @@ void WindingCurrentControl(void) { [- AUXIO_PIN4 = 0; -] diff --git a/PowerConverter.X/config.h b/PowerConverter.X/config.h index 7d52058..1e8ae76 100644 --- a/PowerConverter.X/config.h +++ b/PowerConverter.X/config.h @@ -53 +53 @@ #define DEFAULT_CAN_PACKET_RATE [-2-]{+1+}0 diff --git a/PowerConverter.X/init.c b/PowerConverter.X/init.c index dbcb9e7..4bc3b7b 100644 --- a/PowerConverter.X/init.c +++ b/PowerConverter.X/init.c @@ -532 +532 @@ DMA0STB = __builtin_dmaoffset(BufferB); _DMA0IP = [-5-]{+6+}; @@ -604 +604 @@ void InitADC_WithIntrpt() {+_AD1IP = 7;+} @@ -606 +606 @@ void InitADC_WithIntrpt() _AD1IE = [-0-]{+1+}; //Do not Enable ADC Interrupt diff --git a/PowerConverter.X/isr.c b/PowerConverter.X/isr.c index aa3c1eb..abc3738 100644 --- a/PowerConverter.X/isr.c +++ b/PowerConverter.X/isr.c @@ -50,0 +51,4 @@ void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) {+AUXIO_PIN4 = 0; +} {+ iTemp2 = TMR3;+} {+ //+} @@ -56 +60 @@ void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) @@ -127 +131,2 @@ void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) {+//AUXIO_PIN4 = 0; +} {+ //DMA0CONbits.CHEN = 0;+} @@ -142,2 +147,5 @@ void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void) {+//if(AUXIO_PIN4 == 0)+} {+ // AUXIO_PIN4 = 1;+} {+ //else +} {+ // AUXIO_PIN4 = 0;+} {+ //+}AD1CON1bits.DONE = 0; //clear flag @@ -157 +164,0 @@ void __attribute__((interrupt, no_auto_psv)) _T3Interrupt(void) [- // AUXIO_PIN4 = 1;-] @@ -159 +166,3 @@ void __attribute__((interrupt, no_auto_psv)) _T3Interrupt(void) {+AUXIO_PIN2 = 0; +} {+ iTemp1 = TMR3;+} T3CON = 0x0000; //Period match has occurred once for each analog input, so stop timer. PWM interrupt will restart. {+Stop here instead of DMA because it's faster, the DMA takes some time so there's some risk an extra conversion will occur.]+} @@ -172,0 +182 @@ void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) //PR1 is set to {+// AUXIO_PIN2 = 1;+} @@ -174 +183,0 @@ void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) //PR1 is set to [- AUXIO_PIN2 = 1;-] @@ -233,2 +242,2 @@ void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) //PR1 is set to {+//+} AUXIO_PIN2 = 0; @@ -286,0 +296 @@ void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void) { @@ -319,3 +329,4 @@ void __attribute__((interrupt, no_auto_psv)) _PWMInterrupt(void) {+//+}AUXIO_PIN4 = 1; {+// AUXIO_+}P{+IN2 = 1;+} {+ TM+}R3 =[-16-] 0; //Reset[-coun-] t{+im+}er before {+s+}t[-u-]{+a+}r[-n-]{+t+}ing[-on Timer-]. {+//+} T3CON = 0x8000; //Start timer 3, an ADC sample and convert will then happen every ~4.04uSec until four are completed and DMA occurs. @@ -325,0 +337 @@ void __attribute__((interrupt, no_auto_psv)) _PWMInterrupt(void) {+//There is only barely enough time to do this before the SPI bus reads complete which launches the current winding control, alternative is to start this at the end of winding control so everything is all set.+} @@ -327,2 +339 @@ void __attribute__((interrupt, no_auto_psv)) _PWMInterrupt(void) //Start SPI Bus process to measure phase currents, SPIinterrupt will occur when done.{+AUXIO_PIN2+} @@ -486 +496,0 @@ void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt(void) { [- // LOAD_SWITCH = 1;-] @@ -526 +535,0 @@ void __attribute__((interrupt, no_auto_psv)) _T5Interrupt(void) [- AUXIO_PIN3 = 1;-] @@ -637 +645,0 @@ void __attribute__((interrupt, no_auto_psv)) _T5Interrupt(void) [- AUXIO_PIN3 = 0;-] diff --git a/PowerConverter.X/main.c b/PowerConverter.X/main.c index 9d01a9e..b64da15 100644 --- a/PowerConverter.X/main.c +++ b/PowerConverter.X/main.c @@ -92 +91,0 @@ int main(void) { [- // _T1IP = ;-] @@ -106,2 +105,2 @@ int main(void) { PR3 = [-16-]{+24+}0; //Rolls over about every 4.039uSec and initiates ADC sampling. This timing needs to be compared to ADC sample and hold times... _T3IP = [-5-]{+6+}; {+//This needs to be a higher priority than the DMA that transfers this data, otherwise it's a race and this timer can keep running and result in more sampling while the data is being read...+} @@ -209 +208 @@ int main(void) { sprintf(s, "%.3f %s %7.1f RPM %5.2f A %5.2f A %5.2f A %5.2f A %5.2f A %5.2f A %5.2f A %5.2f A %6.1f V %[-6-]{+8+}.[-1-]{+3+}f V %6.1f A %6.1f V %6.1f V %5d %5d %5d %5d %5d %5d %6.3f %8.5f %6.3f %x-%d %d %d - %d %d - %d %d %d %d %d {+%x+}\r\n", @@ -233 +232,2 @@ int main(void) { halls, TorqueCmdTimeout_ctr, BiasCmdTimeout_ctr, P1DC4{+,+} {+ T3CON+}