Sunday 12 February 2017

microcontroller - Help understanding the code



I am new to C programming. I need your help in understanding the some particular lines in a code segment for sensored control of a brushless dc motor. It was written for a 16-bit MCU (dsPIC33FJ32MC710). This particular section is from the interrupt service routine, and contains two ISRs. Can you please explain to me in simple words, the lines marked by //???? What is being done there, and why? Any other comments are also welcome.


int DesiredSpeed;
int ActualSpeed;
int SpeedError;
long SpeedIntegral = 0, SpeedIntegral_n_1 = 0, SpeedProportional = 0;
long DutyCycle = 0;
unsigned int Kps = 20000; // Proportional gain
unsigned int Kis = 2000; // Integral gain

void __attribute__((interrupt, no_auto_psv)) _T1Interrupt (void)

{
#ifdef CLOSEDLOOP
ActualSpeed = SPEEDMULT/timer3avg;
SpeedError = DesiredSpeed - ActualSpeed;
SpeedProportional = (int)(((long)Kps*(long)SpeedError) >> 15); // ???
SpeedIntegral = SpeedIntegral_n_1 + (int)(((long)Kis*(long)SpeedError) >> 15); //???
SpeedIntegral_n_1 = SpeedIntegral;
DutyCycle = SpeedIntegral + SpeedProportional;
PDC1 = (int)(((long)(PTPER*2)*(long)DutyCycle) >> 15); // ??? PWM duty cycle
1PDC2 = PDC1;

PDC3 = PDC1;
#endif // in closed loop algorithm

IFS0bits.T1IF = 0;
}

void __attribute__((interrupt, no_auto_psv)) _IC1Interrupt (void)
{
int Hall_Index;


IFS0bits.IC1IF = 0; // Clear interrupt flag
HallValue = (unsigned int)((PORTB >> 1) & 0x0007); // Read halls

if (Flags.Direction)
{
OVDCON = StateTableFwd[HallValue];
Hall_Index = HALL_INDEX_F;
}
else
{

OVDCON = StateTableRev[HallValue];
Hall_Index = HALL_INDEX_R;
}

// The code below is uses TMR3 to calculate the speed of the rotor
if (HallValue == Hall_Index) // has the same position been sensed?
if (polecount++ == POLEPAIRS) //has one mech rev elasped? // ???
{ // yes then read timer 3
timer3value = TMR3;
TMR3 = 0;

timer3avg = ((timer3avg + timer3value) >> 1); // ???
polecount = 1;
}
}


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...