Skip to content

Commit

Permalink
Increased PWM freq to 40kHz and improved logging
Browse files Browse the repository at this point in the history
Increased PWM freq to 40kHz:
- set PWMFREQUENCY define.
- re-scale Vq, Vd with respect to the PWM resolution and fullscale.
- fixed PTPER computation.
- set P1SECMPbits instead of SEVTCMPbits.
Improved logging:
- added a counter incremented at every CAN periodic essage transmission.
- turn off PWM for specific `loggedVarSelector` values (set through SKp).
  • Loading branch information
nunoguedelha committed Aug 2, 2019
1 parent c34c9fb commit d934228
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
// I2T parameters for FILTER implementation
#define UDEF_I2T_LIMIT 90 // %

#define PWMFREQUENCY 20000
#define PWMFREQUENCY 40000

// Deadtime in seconds (range 1.6 us to 25 ns)
// DHES accept a greater zero cross distortion in order to keep lower temperature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/************ FIRMWARE AND CAN PROTOCOL VERSION DEFINITION *******************************************/
#define FW_VERSION_MAJOR 3
#define FW_VERSION_MINOR 2
#define FW_VERSION_BUILD 8
#define FW_VERSION_BUILD 6

#define CAN_PROTOCOL_VERSION_MAJOR 1
#define CAN_PROTOCOL_VERSION_MINOR 6
Expand Down
25 changes: 21 additions & 4 deletions emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/2FOC.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ _FICD(ICS_PGD3 & JTAGEN_OFF); // & COE_ON ); //BKBUG_OFF
//#define CALIBRATION

#define BOARD_CAN_ADDR_DEFAULT 0xE
#define VOLT_REF_SHIFT 5
#define VOLT_REF_SHIFT 5 // for a PWM resolution of 1000
#define PWM_50_DUTY_CYC (LOOPINTCY/2)

#define isDriveEnabled() bDriveEnabled

Expand Down Expand Up @@ -191,7 +192,7 @@ volatile int iQerror_old = 0;
volatile int iDerror_old = 0;
volatile char limit = 0;

static const int PWM_MAX = (8*LOOPINTCY)/20; // = 80%
static const int PWM_MAX = 8*PWM_50_DUTY_CYC/10; // = 80%

volatile int gMaxCurrent = 0;
volatile long sI2Tlimit = 0;
Expand Down Expand Up @@ -952,6 +953,11 @@ void __attribute__((__interrupt__, no_auto_psv)) _DMA0Interrupt(void)
FaultConditionsHandler();
}
}

// Re-scale Vq, Vd with respect to the PWM resolution and fullscale.
Vq = Vq/(1000/PWM_50_DUTY_CYC);
Vd = Vd/(1000/PWM_50_DUTY_CYC);

//
////////////////////////////////////////////////////////////////////////////

Expand All @@ -977,7 +983,18 @@ void __attribute__((__interrupt__, no_auto_psv)) _DMA0Interrupt(void)
ParkParm.qIbOffset = IKd;
ParkParm.qIcOffset = IKi;

pwmOut(Va+ParkParm.qIaOffset,Vb+ParkParm.qIbOffset,Vc+ParkParm.qIcOffset);
if (loggedVarSelector > 20)
{
// Pin override register
PWM1CON2bits.OSYNC = 1;
P1OVDCON = 0x0000;
}
else
{
P1OVDCON = 0xFFFF;
pwmOut(Va+ParkParm.qIaOffset,Vb+ParkParm.qIbOffset,Vc+ParkParm.qIcOffset);
}

//
////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -1030,7 +1047,7 @@ void DisableAuxServiceTimer()
void DriveInit()
// Perform drive SW/HW init
{
pwmInit(LOOPINTCY/2, DDEADTIME, (8*LOOPINTCY)/20 /*pwm max = 80%*/);
pwmInit(PWM_50_DUTY_CYC, DDEADTIME, PWM_MAX /*pwm max = 80%*/);

// setup and perform ADC offset calibration in MeasCurrParm.Offseta and Offsetb
ADCDoOffsetCalibration();
Expand Down
8 changes: 5 additions & 3 deletions emBODY/eBcode/arch-dspic/board/2foc/appl/2FOC-V3/src/PWM.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void pwmInit(short pwm_50_duty_cycle, short pwm_deadtime, short pwm_max)
// Note: The PWM period setting register is set to LoopInTcy/2 but since it counts up and
// then down => the interrupt flag is set to 1 at zero => actual
// interrupt period is LoopInTcy
PTPER = PWM_50_DUTY_CYCLE; // Setup PWM period to Loop Time defined in parms.h
PTPER = PWM_50_DUTY_CYCLE-1; // Setup PWM period to Loop Time defined in parms.h
PWMCON1 = 0x0077; // Enable PWM 1,2,3 pairs (L+H) for complementary mode
DTCON1 = (0x00 | (PWM_DEADTIME)); // Dead time. Prescaler 0
DTCON2 = 0;
Expand All @@ -45,8 +45,10 @@ void pwmInit(short pwm_50_duty_cycle, short pwm_deadtime, short pwm_max)
// SEVTCMP: Special Event Compare Count Register
// Phase of ADC capture set relative to PWM cycle: when arrive to PTPER offset and counting down
// to avoid ripple on the current measurement
SEVTCMP = 0;
SEVTCMPbits.SEVTDIR = 1;
P1SECMP = 0;
P1SECMPbits.SEVTDIR = 1; // when PWM time base is counting down
P1SECMPbits.SEVTCMP = 0;
PWM1CON2bits.SEVOPS = 0b0000; // Output Postscale value to 1:1
}

volatile unsigned char PWM_ovcurr_fault = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,30 @@ extern void CanIcubProtoTrasmitterSendPeriodicData(void)
{
static tCanData payload; // = {{0}};
unsigned long msgid;

static int counter = 0;
int loggedVar;

counter = (counter+1)%1000;
switch (loggedVarSelector)
{
case 1:
case 21:
loggedVar = (int)ParkParm.qIa;
break;
case 2:
case 22:
loggedVar = (int)ParkParm.qIb;
break;
case 3:
case 23:
loggedVar = (int)ParkParm.qIc;
break;
case 4:
case 24:
loggedVar = I2Tdata.IQMeasured;
break;
case 5:
case 25:
loggedVar = I2Tdata.IDMeasured;
break;
case 6:
Expand Down Expand Up @@ -117,10 +124,10 @@ extern void CanIcubProtoTrasmitterSendPeriodicData(void)
loggedVar = (int)ParkParm.qIcOffset;
break;
default:
loggedVar = (int)0x8000;
loggedVar = (int)counter;
}

payload.w[0] = loggedVar;
payload.w[0] = loggedVar*10;
payload.w[1] = motorElecPhase;
payload.w[2] = gQEPosition & 0xFFFF;
payload.w[3] = gQEPosition >> 16;
Expand Down

0 comments on commit d934228

Please sign in to comment.