⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bldc.c

📁 luminary芯片直流无刷电机控制程序
💻 C
📖 第 1 页 / 共 3 页
字号:
//*****************************************************************************//// bldc.c - Motor control application for a brushless DC motor.//// Copyright (c) 2005,2006 Luminary Micro, Inc.  All rights reserved.//// Software License Agreement//// Luminary Micro, Inc. (LMI) is supplying this software for use solely and// exclusively on LMI's microcontroller products.//// The software is owned by LMI and/or its suppliers, and is protected under// applicable copyright laws.  All rights are reserved.  Any use in violation// of the foregoing restrictions may subject the user to criminal sanctions// under applicable laws, as well as to civil liability for the breach of the// terms and conditions of this license.//// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.//// This is part of revision 196 of an01238.////*****************************************************************************#include <string.h>#include "../../hw_ints.h"#include "../../hw_memmap.h"#include "../../hw_pwm.h"#include "../../hw_types.h"#include "../../src/debug.h"#include "../../src/gpio.h"#include "../../src/interrupt.h"#include "../../src/pwm.h"#include "../../src/qei.h"#include "../../src/sysctl.h"#include "../../src/timer.h"#include "pid.h"#include "ui.h"#include "bldc.h"//*****************************************************************************//// This application is designed to drive a Pittman N2341S001 brushless DC// motor.  The commutation sequence and PID controller tuning may need to be// modified when using a different motor; the PID controller can be tuned for// different response behavior of the Pittman motor.//// The rotational speed of the motor is measured with either the quadrature// encoder module (if present in hardware) or a software interface to the// optical encoder.  When requested, the measured speed is used to perform// closed loop speed control of the motor.//// A simple user interface is provided on UART0 operating at 115,200, 8-N-1.// The status is provided in the following format:////     Cr s:1000 a:0996//     ab   cccc   dddd//// Where 'a' is "C" for closed loop operation and "O" for open loop operation,// 'b' is 'r' for running and 's' for stopped, 'cccc' is the target motor// speed, and 'dddd' is the measured motor speed.//// The following commands are available:////     r   Start/stop the motor.//     l   Toggle between open and closed loop operation.//     p   Set a new proportional gain factor for the motor speed PID//         controller.//     i   Set a new integral gain factor for the motor speed PID controller.//     d   Set a new differential gain factor for the motor speed PID//         controller.//     z   Display the current gain factors for the motor speed PID controller.//     D   Toggle display of each motor speed sample (every 1/100th of a//         second).//// See the accompanying application note for full details of these commands.////*****************************************************************************//*****************************************************************************//// One of the following must be defined to choose the system clock rate://      SYSTEM_CLOCK_6MHZ, SYSTEM_CLOCK_20MHZ, SYSTEM_CLOCK_25MHZ,//      SYSTEM_CLOCK_50MHZ////*****************************************************************************//#define SYSTEM_CLOCK_6MHZ#define SYSTEM_CLOCK_20MHZ//#define SYSTEM_CLOCK_25MHZ//#define SYSTEM_CLOCK_50MHZ//*****************************************************************************//// Clock and PWM dividers used depend on which system clock rate is chosen.////*****************************************************************************#if defined(SYSTEM_CLOCK_6MHZ)#define SYSDIV SYSCTL_SYSDIV_1#define PWMDIV SYSCTL_PWMDIV_1#define CLKUSE SYSCTL_USE_OSC#elif defined(SYSTEM_CLOCK_20MHZ)#define SYSDIV SYSCTL_SYSDIV_10#define PWMDIV SYSCTL_PWMDIV_2#define CLKUSE SYSCTL_USE_PLL#elif defined(SYSTEM_CLOCK_25MHZ)#define SYSDIV SYSCTL_SYSDIV_8#define PWMDIV SYSCTL_PWMDIV_2#define CLKUSE SYSCTL_USE_PLL#elif defined(SYSTEM_CLOCK_50MHZ)#define SYSDIV SYSCTL_SYSDIV_4#define PWMDIV SYSCTL_PWMDIV_2#define CLKUSE SYSCTL_USE_PLL#else#error "System clock speed is not defined!"#endif//*****************************************************************************//// These define the pair of PWM signals that correspond to each phase of the// motor.////*****************************************************************************#define PHASE_A                 (PWM_OUT_0_BIT | PWM_OUT_1_BIT)#define PHASE_B                 (PWM_OUT_2_BIT | PWM_OUT_3_BIT)#define PHASE_C                 (PWM_OUT_4_BIT | PWM_OUT_5_BIT)//*****************************************************************************//// These define the PWM signals that correspond to the high and low side FETs// for each phase of the motor.////*****************************************************************************#define PHASE_A_PLUS            PWM_OUT_0_BIT#define PHASE_A_MINUS           PWM_OUT_1_BIT#define PHASE_B_PLUS            PWM_OUT_2_BIT#define PHASE_B_MINUS           PWM_OUT_3_BIT#define PHASE_C_PLUS            PWM_OUT_4_BIT#define PHASE_C_MINUS           PWM_OUT_5_BIT//*****************************************************************************//// These define the GPIO pins that are used for the Hall effect sensors.////*****************************************************************************#define HALL_PORT               GPIO_PORTB_BASE#define HALL_INT                INT_GPIOB#define HALL_A                  GPIO_PIN_6#define HALL_B                  GPIO_PIN_5#define HALL_C                  GPIO_PIN_4#define HALL_SHIFT              4//*****************************************************************************//// The minimum allowed motor speed.  This is the slowest speed at which the// motor will be allowed to be run by this application.////*****************************************************************************#define MIN_APP_MOTOR_SPEED     200//*****************************************************************************//// The maximum allowed motor speed.  This is the fastest speed at which the// motor will be allowed to be run by this application.////*****************************************************************************#define MAX_APP_MOTOR_SPEED     7000//*****************************************************************************//// The number of lines in the quadrature encoder on the motor.////*****************************************************************************#define ENCODER_LINES           1000//*****************************************************************************////! Mapping from Hall effect sensor state to PWM drive state.//!//! This array maps the state of the Hall effect sensors on the motor to the//! set of PWM signals that should be driving at that time.  This mapping//! corresponds to the following table, taken from the data sheet for the//! motor://!//! \verbatim//!     Hall State    Winding State//!        100           B+, A-//!        110           C+, A-//!        010           C+, B-//!        011           A+, B-//!        001           A+, C-//!        101           B+, C-//! \endverbatim////*****************************************************************************static const unsigned long g_pulHallToPhase[] ={    0,    PHASE_A_PLUS | PHASE_C_MINUS,    PHASE_C_PLUS | PHASE_B_MINUS,    PHASE_A_PLUS | PHASE_B_MINUS,    PHASE_B_PLUS | PHASE_A_MINUS,    PHASE_B_PLUS | PHASE_C_MINUS,    PHASE_C_PLUS | PHASE_A_MINUS};//*****************************************************************************////! Determines if the motor is running.//!//! This variable contains the value of a state machine that handles//! transitioning the motor from a running to a non-running state.  This can//! have any of the following values://!//! \verbatim//!     MOTOR_STOPPED       The motor is stopped.//!     MOTOR_RUNNING       The motor is running.//!     MOTOR_STOPPING      The motor is stopping, and the high side switches//!                         have been turned off//!     MOTOR_STOP_DELAY    The motor is stopping, the high side switches have//!                         been turned off, and a delay time has expired (to//!                         prevent shoot-through on the inverter bridge).//! \endverbatim////*****************************************************************************#define MOTOR_STOPPED           0#define MOTOR_RUNNING           1#define MOTOR_STOPPING          2#define MOTOR_STOP_DELAY        3static int g_iRunning = 0;//*****************************************************************************////! Determines if the motor is rotating in the forward or reverse direction.//!//! This flag determines if the motor is rotating in the forward or reverse//! direction.  This determines the direction of current flow through the motor//! windings for each Hall effect state; reversing the current flow reverses//! the direction of rotation.////*****************************************************************************static tBoolean g_bReverse = false;//*****************************************************************************////! Determines if the motor is run open or closed loop.//!//! This flag determines if the motor is run open loop (i.e. with no feedback)//! or closed loop (with feedback from the quadrature encoder being used to//! track the motor speed to the target speed).////*****************************************************************************static tBoolean g_bOpenLoop = true;//*****************************************************************************////! A flag to indicate debug mode.//!//! This flag determines if the closed loop debug mode is enabled.  If it is,//! each speed measurement is printed out so that the performance of the PID//! gain factors can be evaluated.////*****************************************************************************static volatile tBoolean g_bDebug = false;//*****************************************************************************////! A flag to indicate the QEI type.//!//! This flag determines if software QEI is enabled.  If it is, the number of//! edges on a single encoder channel is counted via GPIO interrupts.////*****************************************************************************static tBoolean g_bSoftwareQEI = false;//*****************************************************************************////! The target speed.//!//! This variable contains the desired speed of the motor.  It is updated when//! the user requests a new speed for the motor.////*****************************************************************************static unsigned long g_ulTarget = 0;//*****************************************************************************////! The current speed.//!//! This variable contains the measured speed of the motor.  It is updated//! every 100th of a second.////*****************************************************************************static volatile unsigned long g_ulSpeed = 0;//*****************************************************************************////! The number of speed measurements.//!//! This variables contains the number of times the motor speed has been//! measured.  It is effectively a 100Hz counter.////*****************************************************************************static volatile unsigned long g_ulSpeedCount = 0;//*****************************************************************************////! The count of edges from the optical encoder.//!//! This variable contains the count of edges seen from the optical encoder//! during the current time period.  The count is incremented for each edge//! interrupt and copied/reset every 1/100th of a second; the number of edges//! per 1/100th of a second is used to compute the speed of the motor in RPM.////*****************************************************************************static unsigned long g_ulVelCount = 0;//*****************************************************************************////! The base PWM duty cycle.//!//! This variable contains the base PWM duty cycle.  This is the duty cycle//! that is used if the motor is being operated open-loop; this is the base//! duty cycle to which the error delta is added when operating closed-loop.////*****************************************************************************static unsigned long g_ulBaseDutyCycle = 0;//*****************************************************************************////! The period of the PWM generators.//!//! Calculated based on the system clock and the desired PWM frequency////*****************************************************************************static unsigned long g_ulPwmPeriod = 0;//*****************************************************************************////! PID algorithm state.//!//! This structure contains the state of the PID algorithm that is processing//! the speed feedback loop.////*****************************************************************************static tPIDState g_sPID;//*****************************************************************************////! Proportional gain factor.//!//! This variable contains the proportional gain factor for the PID algorithm.////*****************************************************************************static long g_lPGain = 25;//*****************************************************************************////! Integral gain factor.//!//! This variable contains the integral gain factor for the PID algorithm.////*****************************************************************************static long g_lIGain = 100;//*****************************************************************************////! Derivitive gain factor.//!//! This variable contains the derivitive gain factor for the PID algorithm.////*****************************************************************************static long g_lDGain = 25;//*****************************************************************************////! Linear speed mapping table.//!//! This table maps the desired motor speed to the PWM duty cycle required to//! achieve this speed.  These duty cycles assume no load on the motor and//! nominal operating conditions; this will result in the motor going//! approximately the desired speed.  The duty cycle is represented as a value//! ranging from 0 (0%) to 10000 (100%).////*****************************************************************************typedef struct{    unsigned long ulSpeed;    unsigned long ulDutyCycle;} tSpeedMap;static const tSpeedMap g_psSpeedMap[] ={    {  200, 2587 },    {  500, 3067 },    { 1000, 3733 },    { 1500, 4293 },    { 2000, 4773 },    { 2500, 5413 },    { 3000, 5820 },    { 3500, 6440 },    { 4000, 6940 },    { 4500, 7427 },    { 5000, 8057 },    { 5500, 8560 },    { 6000, 8933 },    { 6500, 9307 },    { 7000, 9627 },};#define SPEEDMAP_ENTRIES ((sizeof(g_psSpeedMap) / sizeof(tSpeedMap)) - 1)//*****************************************************************************////! Storage for the main user interface display.//!//! This contains the contents of the main display on the user interface;//! updates to this array are reflected to the user interface.////*****************************************************************************static char g_pcDisplay[20];//*****************************************************************************////! The user interface commands.//!//! This contains the list of commands supported by this application.////*****************************************************************************static void DebugHandler(unsigned long ulValue);static void RunHandler(unsigned long ulValue);static void SpeedHandler(unsigned long ulValue);static void LoopHandler(unsigned long ulValue);static void PGainHandler(unsigned long ulValue);static void IGainHandler(unsigned long ulValue);static void DGainHandler(unsigned long ulValue);static void StatusHandler(unsigned long ulValue);static const tUICommandList g_psCommands[] ={    { 'D', 0, 0, DebugHandler },    { 'r', 0, 0, RunHandler },    { 's', 1, "Speed: ", SpeedHandler },    { 'l', 0, 0, LoopHandler },    { 'p', 1, "PGain: ", PGainHandler },    { 'i', 1, "IGain: ", IGainHandler },    { 'd', 1, "DGain: ", DGainHandler },    { 'z', 0, 0, StatusHandler }};//*****************************************************************************////! The user interface description.//!//! This contains a description of the user interface.////*****************************************************************************static const tUIDescription g_sUI ={    g_pcDisplay, 8, g_psCommands};//*****************************************************************************//// The error routine that is called if the driver library encounters an error.////*****************************************************************************#ifdef DEBUGvoid__error__(char *pcFilename, unsigned long ulLine){}#endif//*****************************************************************************////! Interrupt handler for the Hall effect GPIO signals.//!//! Handles the interrupt generated by the GPIO block when one of the Hall//! effect sensors changes state.  The set of PWM signals to be driven is//! changed based on the new state of the Hall effect sensors, performing//! commutation of the motor.//!//! \return None.////*****************************************************************************voidGPIOIntHandler(void){    unsigned long ulHall, ulPWM;    //    // Clear the GPIO pin interrupts.    //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -