?? bldc.c
字號(hào):
{ // // Is debug mode currently enabled or disabled? // if(g_bDebug) { // // Disable debug mode. // g_bDebug = false; // // Restore the user interface display. // UIDisplay("\n"); } else { // // Enable debug mode. // g_bDebug = true; // // Start on a fresh line for the user interface. // UIDisplay("\r\n"); }}//*****************************************************************************////! Run command callback function.//!//! \param ulValue is ignored.//!//! This function is called when the run command is entered by the user. It//! toggles the running state of the motor.//!//! \return None.////*****************************************************************************static voidRunHandler(unsigned long ulValue){ // // Is the motor running? // if(g_iRunning == MOTOR_RUNNING) { // // Have the UI indicate that the motor is stopped. // g_pcDisplay[2] = 's'; // // Begin stopping the motor. This prevents the QEI interrupt from // changing the state of the PWM outputs. // g_iRunning = MOTOR_STOPPING; // // Disable the PWM outputs. // PWMOutputState(PWM_BASE, PHASE_A | PHASE_B | PHASE_C, false); } else { // // Have the UI indicate that the motor is running. // g_pcDisplay[2] = 'r'; // // Start the motor running. // g_bReverse = g_bReverse ? false : true; g_iRunning = MOTOR_RUNNING; // // Turn off the inversion of all the switches. // PWMOutputInvert(PWM_BASE, PHASE_A | PHASE_B | PHASE_C, false); // // Fake an interrupt from the Hall effect sensors to get the correct // PWM outputs enabled. // GPIOIntHandler(); }}//*****************************************************************************////! Speed command callback function.//!//! \param ulValue is the new target speed.//!//! This function is called when the speed command is entered by the user. It//! causes the supplied speed to become the target speed for the motor.//!//! \return None.////*****************************************************************************static voidSpeedHandler(unsigned long ulValue){ unsigned long ulIdx; // // Enforce the upper and lower bound on the motor speed. // if(ulValue > MAX_APP_MOTOR_SPEED) { ulValue = MAX_APP_MOTOR_SPEED; } if(ulValue < MIN_APP_MOTOR_SPEED) { ulValue = MIN_APP_MOTOR_SPEED; } // // Set the target speed in the display. // g_pcDisplay[6] = '0' + ((ulValue / 1000) % 10); g_pcDisplay[7] = '0' + ((ulValue / 100) % 10); g_pcDisplay[8] = '0' + ((ulValue / 10) % 10); g_pcDisplay[9] = '0' + (ulValue % 10); // // Find range entry in speed mapping table. // for(ulIdx = 0; ulIdx < SPEEDMAP_ENTRIES; ulIdx++) { if(ulValue < g_psSpeedMap[ulIdx + 1].ulSpeed) { break; } } // // Perform a linear interpolation between the two selected entries of the // speed map. // g_ulBaseDutyCycle = (((g_psSpeedMap[ulIdx].ulDutyCycle + (((ulValue - g_psSpeedMap[ulIdx].ulSpeed) * (g_psSpeedMap[ulIdx + 1].ulDutyCycle - g_psSpeedMap[ulIdx].ulDutyCycle)) / (g_psSpeedMap[ulIdx + 1].ulSpeed - g_psSpeedMap[ulIdx].ulSpeed))) * g_ulPwmPeriod) / 10000); // // Save the new target speed. // g_ulTarget = ulValue; // // See if open loop operation is enabled. // if(g_bOpenLoop) { // // Set the PWM duty cycle based on the requested target speed. // SetPWMDutyCycle(g_ulBaseDutyCycle); }}//*****************************************************************************////! Loop command callback function.//!//! \param ulValue is ignored.//!//! This function is called when the loop command is entered by the user. It//! causes the control of the motor to toggle between open-loop and closed-loop//! operation.//!//! \return None.////*****************************************************************************static voidLoopHandler(unsigned long ulValue){ // // Is the motor currently being driven open- or closed-loop? // if(g_bOpenLoop) { // // Set the UI indicator for closed-loop operation. // g_pcDisplay[1] = 'C'; // // Initialize the PID controller. // PIDInitialize(&g_sPID, 100000, -100000, g_lPGain, g_lIGain, g_lDGain); // // Indicate that the motor is now being driven in a closed-loop // fashion. Nothing further is done at this point; at the next QEI // interrupt the PID controller will be run and start adjusting the // duty cycle as it sees fit. // g_bOpenLoop = false; } else { // // Indicate that the motor is now being driven in an open-loop fashion. // g_bOpenLoop = true; // // Set the UI indicator for open-loop operation. // g_pcDisplay[1] = 'O'; // // Set the PWM duty cycle based to the base duty cycle. // SetPWMDutyCycle(g_ulBaseDutyCycle); }}//*****************************************************************************////! Proportional gain callback function.//!//! \param ulValue is the new proportional gain factor.//!//! This function is called when the proportional feedback command is entered//! by the user. It causes the supplied gain factor to become the proportional//! gain for the PID algorithm.//!//! \return None.////*****************************************************************************static voidPGainHandler(unsigned long ulValue){ // // Save the new proportional gain factor. // g_lPGain = (long)ulValue; // // Adjust the settings of the PID algorithm. // PIDSetGains(&g_sPID, g_lPGain, g_lIGain, g_lDGain);}//*****************************************************************************////! Integral gain callback function.//!//! \param ulValue is the new integral gain factor.//!//! This function is called when the integral feedback command is entered by//! the user. It causes the supplied gain factor to become the integral gain//! for the PID algorithm.//!//! \return None.////*****************************************************************************static voidIGainHandler(unsigned long ulValue){ // // Save the new integral gain factor. // g_lIGain = (long)ulValue; // // Adjust the settings of the PID algorithm. // PIDSetGains(&g_sPID, g_lPGain, g_lIGain, g_lDGain);}//*****************************************************************************////! Derivitive gain callback function.//!//! \param ulValue is the new derivitive gain factor.//!//! This function is called when the derivitive feedback command is entered by//! the user. It causes the supplied gain factor to become the derivitive gain//! for the PID algorithm.//!//! \return None.////*****************************************************************************static voidDGainHandler(unsigned long ulValue){ // // Save the new derivitive gain factor. // g_lDGain = (long)ulValue; // // Adjust the settings of the PID algorithm. // PIDSetGains(&g_sPID, g_lPGain, g_lIGain, g_lDGain);}//*****************************************************************************////! Displays the current PID gain factors.//!//! \param ulValue is ignored.//!//! This function is called when the status command is entered by the user. It//! displays the current PID gain factors.//!//! \return None.////*****************************************************************************static voidStatusHandler(unsigned long ulValue){ char pcBuffer[24]; // // Set the basic format of the string to be displayed. // strcpy(pcBuffer, "\r\nP=000 I=000 D=000\r\n"); // // Put the proportional gain factor into the string. // pcBuffer[4] = '0' + ((g_lPGain / 100) % 10); pcBuffer[5] = '0' + ((g_lPGain / 10) % 10); pcBuffer[6] = '0' + (g_lPGain % 10); // // Put the integral gain factor into the string. // pcBuffer[10] = '0' + ((g_lIGain / 100) % 10); pcBuffer[11] = '0' + ((g_lIGain / 10) % 10); pcBuffer[12] = '0' + (g_lIGain % 10); // // Put the derivitive gain factor into the string. // pcBuffer[16] = '0' + ((g_lDGain / 100) % 10); pcBuffer[17] = '0' + ((g_lDGain / 10) % 10); pcBuffer[18] = '0' + (g_lDGain % 10); // // Display the current gain factors for the user. // UIDisplay(pcBuffer);}//*****************************************************************************////! Hard fault handler.//!//! This is the handler for hard faults, if they occur. Immediate action must//! be taken to shut off the PWM outputs to prevent any potential damage to the//! motor.//!//! \return None.////*****************************************************************************voidHardFault(void){ // // Disable all the PWM outputs. // PWMOutputState(PWM_BASE, PHASE_A | PHASE_B | PHASE_C, false); // // Indicate that a hard fault has occurred. // UIDisplay("\r\nHard Fault!"); // // Loop forever. // while(1) { }}//*****************************************************************************////! Program entry point.//!//! This is the entry point for the brushless DC motor control application. It//! is responsible for getting the system setup and operational. This is//! called at the base activation level, so any exception will preempt this//! function.//!//! \return Never returns.////*****************************************************************************intmain(void){ // // Setup the device clocking. // SysCtlClockSet(SYSDIV | CLKUSE | SYSCTL_XTAL_6MHZ | SYSCTL_OSC_MAIN); // // Compute the PWM period based on the clock. // g_ulPwmPeriod = SysCtlClockGet() / 16000;#if (PWMDIV == SYSCTL_PWMDIV_2) g_ulPwmPeriod /= 2;#endif // // Enable the GPIO peripherals. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Set the static portion of the user interface display. // strcpy(g_pcDisplay, "\rOs s:0000 a:0000"); // // Enable processor interrupts. // IntMasterEnable(); // // Initialize the user interface, GPIO, PWM, and QEI drivers. // UIInitialize(&g_sUI); SetupGPIO(); SetupPWM(); SetupQEI(); // // Set the priority of the UART interrupt. This needs to be lower than // the hall effect sensors and the QEI interrupt since it is the least // critical operation in the system. // IntPrioritySet(INT_UART0, 0x80); // // The default motor speed is 1000rpm. // SpeedHandler(1000); // // Loop forever. // while(1) { // // Update the user interface every 32nd time through the loop. // if((g_ulSpeedCount & 63) == 0) { // // Set the current speed in the display. // g_pcDisplay[13] = '0' + ((g_ulSpeed / 1000) % 10); g_pcDisplay[14] = '0' + ((g_ulSpeed / 100) % 10); g_pcDisplay[15] = '0' + ((g_ulSpeed / 10) % 10); g_pcDisplay[16] = '0' + (g_ulSpeed % 10); // // Update the user interface if not in debug mode. // if(!g_bDebug) { UIUpdate(); } } // // Go to sleep. // // This makes it impossible to break into the processor via the // debugger so it is commented out for now. // //SysCtlSleep(); }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -