📄 main.c
字号:
//*****************************************************************************
//
// main.c - Brushless DC motor drive main application.
//
// Copyright (c) 2007-2008 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. You may not combine
// this software with "viral" open-source software in order to form a larger
// program. 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 716 of the BLDC motor application.
//
//*****************************************************************************
#include "../../DriverLib/hw_ints.h"
#include "../../DriverLib/hw_memmap.h"
#include "../../DriverLib/hw_types.h"
#include "../../DriverLib/hw_sysctl.h"
#include "../../DriverLib/hw_nvic.h"
#include "../../DriverLib/src/gpio.h"
#include "../../DriverLib/src/interrupt.h"
#include "../../DriverLib/src/sysctl.h"
#include "../../DriverLib/src/watchdog.h"
#include "adc_ctrl.h"
#include "brake.h"
#include "commands.h"
#include "faults.h"
#include "flash_pb.h"
#include "main.h"
#include "pwm_ctrl.h"
#include "speed_sense.h"
#include "hall_ctrl.h"
#include "ui.h"
#include "ui_ethernet.h"
#include "pins.h"
#include "adc_ctrl.h"
#include "sinemod.h"
#include "trapmod.h"
//*****************************************************************************
//
//! \page main_intro Introduction
//!
//! This is the main Brushless DC motor application code. It contains a state
//! machine that controls the operation of the drive, an interrupt handler for
//! the waveform update software interrupt, an interrupt handler for the
//! millisecond speed update software interrupt, and the main application
//! startup code.
//!
//! The waveform update interrupt handler is responsible for computing new
//! values for the waveforms being driven to the inverter bridge. Based on the
//! update rate, it will advance the drive angle and recompute new waveforms.
//! The new waveform values are passed to the PWM module to be supplied to
//! the PWM hardware at the correct time.
//!
//! The millisecond speed update interrupt handler is responsible for
//! handling the dynamic brake, computing the new drive speed, and checking
//! for fault conditions. If the drive is just starting, this is where the
//! precharging of the high-side gate drivers is handled. If the drive has
//! just stopped, this is where the DC injection braking is handled. Dynamic
//! braking is handled by simply calling the update function for the dynamic
//! braking module.
//!
//! When running, a variety of things are done to adjust the drive speed.
//! First, the acceleration or deceleration rate is applied as appropriate to
//! move the drive speed towards the target speed. Also, the amplitude of
//! the PWM outputs is adjusted by a PI controller, moving the rotor speed to
//! the desired speed. In the case of deceleration, the deceleration rate
//! may be reduced based on the DC bus voltage. The result of this speed
//! adjustment is a new step angle, which is subsequently used by the waveform
//! update interrupt handler to generate the output waveforms.
//!
//! The over-temperature, DC bus under-voltage, DC bus over-voltage, motor
//! under-current, and motor over-current faults are all checked for by
//! examining the readings from the ADC. Fault conditions are handled by
//! turning off the drive output and indicating the appropriate fault, which
//! must be cleared before the drive will run again.
//!
//! The state machine that controls the operation of the drive is woven
//! throughout the millisecond speed update interrupt handler and the
//! routines that start, stop, and adjust the parameters of the motor drive.
//! Together, it ensures that the motor drive responds to commands and
//! parameter changes in a logical and predictable manner.
//!
//! The application startup code performs high-level initialization of the
//! microcontroller (such as enabling peripherals) and calls the initialization
//! routines for the various support modules. Since all the work within the
//! motor drive occurs with interrupt handlers, its final task is to go into
//! an infinite loop that puts the processor into sleep mode. This serves two
//! purposes; it allows the processor to wait until there is work to be done
//! (for example, an interrupt) before it executes any further code, and it
//! allows the processor usage meter to gather the data it needs to determine
//! processor usage.
//!
//! The main application code is contained in <tt>main.c</tt>, with
//! <tt>main.h</tt> containing the definitions for the defines, variables, and
//! functions exported to the remainder of the application.
//
//*****************************************************************************
//*****************************************************************************
//
//! \defgroup main_api Definitions
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is in the forward
//! direction.
//
//*****************************************************************************
#define STATE_FLAG_FORWARD 0x01
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is in the backward
//! direction.
//
//*****************************************************************************
#define STATE_FLAG_BACKWARD 0x00
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is running.
//
//*****************************************************************************
#define STATE_FLAG_RUN 0x02
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is stopping.
//
//*****************************************************************************
#define STATE_FLAG_STOPPING 0x04
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is reversing direction.
//
//*****************************************************************************
#define STATE_FLAG_REV 0x08
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is precharging the
//! bootstrap capacitors on the high side gate drivers.
//
//*****************************************************************************
#define STATE_FLAG_PRECHARGE 0x10
//*****************************************************************************
//
//! A state flag that indicates that the motor drive is in the startup
//! condition, getting the motor spinning for sensorless opertation.
//
//*****************************************************************************
#define STATE_FLAG_STARTUP 0x20
//*****************************************************************************
//
//! The motor drive is stopped. A run request will cause a transition to the
//! #STATE_PRECHARGE or #STATE_BACK_PRECHARGE states, depending upon the
//! direction flag.
//
//*****************************************************************************
#define STATE_STOPPED 0x00
//*****************************************************************************
//
//! The motor drive is precharging the bootstrap capacitors on the high side
//! gate drivers. Once the capacitors are charged, the state machine will
//! automatically transition to #STATE_RUN.
//
//*****************************************************************************
#define STATE_PRECHARGE (STATE_FLAG_PRECHARGE | STATE_FLAG_FORWARD)
//*****************************************************************************
//
//! The motor drive is starting. The motor will be spun in the forward
//! direction until a minimum speed is reached. At that point, the motor
//! state will be transitioned to #STATE_RUN.
//
//*****************************************************************************
#define STATE_STARTUP (STATE_FLAG_STARTUP | STATE_FLAG_FORWARD)
//*****************************************************************************
//
//! The motor drive is running, either at the target speed or slewing to
//! the target speed.
//
//*****************************************************************************
#define STATE_RUN (STATE_FLAG_RUN | STATE_FLAG_FORWARD)
//*****************************************************************************
//
//! The motor drive is decelerating down to a stop, at which point the state
//! machine will automatically transition to #STATE_BACK_RUN. This results in
//! a direction change of the motor drive.
//
//*****************************************************************************
#define STATE_REV (STATE_FLAG_RUN | STATE_FLAG_REV | \
STATE_FLAG_FORWARD)
//*****************************************************************************
//
//! The motor drive is decelerating down to a stop, at which point the state
//! machine will automatically transition to #STATE_STOPPED. This results in
//! the motor drive being stopped.
//
//*****************************************************************************
#define STATE_STOPPING (STATE_FLAG_RUN | STATE_FLAG_STOPPING | \
STATE_FLAG_FORWARD)
//*****************************************************************************
//
//! The motor drive is precharging the bootstrap capacitors on the high side
//! gate drivers while running in the backward direction. Once the capacitors
//! are charged, the state machine will automatically transition to
//! #STATE_BACK_RUN.
//
//*****************************************************************************
#define STATE_BACK_PRECHARGE (STATE_FLAG_PRECHARGE | STATE_FLAG_BACKWARD)
//*****************************************************************************
//
//! The motor drive is starting. The motor will be spun in the backward
//! direction until a minimum speed is reached. At that point, the motor
//! state will be transitioned to #STATE_BACK_RUN.
//
//*****************************************************************************
#define STATE_BACK_STARTUP (STATE_FLAG_STARTUP | STATE_FLAG_BACKWARD)
//*****************************************************************************
//
//! The motor drive is running in the backward direction, either at the target
//! speed or slewing to the target speed.
//
//*****************************************************************************
#define STATE_BACK_RUN (STATE_FLAG_RUN | STATE_FLAG_BACKWARD)
//*****************************************************************************
//
//! The motor drive is decelerating down to a stop while running in the
//! backward direction, at which point the state machine will automatically
//! transition to #STATE_RUN. This results in a direction change of the motor
//! drive.
//
//*****************************************************************************
#define STATE_BACK_REV (STATE_FLAG_RUN | STATE_FLAG_REV | \
STATE_FLAG_BACKWARD)
//*****************************************************************************
//
//! The motor drive is decelerating down to a stop while running in the
//! backward direction, at which point the state machine will automatically
//! transition to #STATE_STOPPED. This results in the motor drive being
//! stopped.
//
//*****************************************************************************
#define STATE_BACK_STOPPING (STATE_FLAG_RUN | STATE_FLAG_STOPPING | \
STATE_FLAG_BACKWARD)
//*****************************************************************************
//
//! The latched fault status flags for the motor drive, enumerated by
//! #FAULT_EMERGENCY_STOP, #FAULT_VBUS_LOW, #FAULT_VBUS_HIGH,
//! #FAULT_CURRENT_LOW, #FAULT_CURRENT_HIGH, and #FAULT_TEMPERATURE_HIGH.
//
//*****************************************************************************
unsigned long g_ulFaultFlags;
//*****************************************************************************
//
//! The current operation state of the motor drive.
//
//*****************************************************************************
unsigned char g_ucMotorStatus = MOTOR_STATUS_STOP;
//*****************************************************************************
//
//! The current motor drive speed in RPM, expressed as a 18.14 fixed-point
//! value.
//
//*****************************************************************************
static unsigned long g_ulSpeed = 0;
//*****************************************************************************
//
//! The whole part of the current motor drive speed. This is used in
//! conjunction with #g_ulSpeedFract to compute the value for
//! #g_ulSpeed.
//
//*****************************************************************************
static unsigned long g_ulSpeedWhole = 0;
//*****************************************************************************
//
//! The fractional part of the current motor drive speed. This value is
//! expressed as the numerator of a fraction whose denominator is the PWM
//! frequency. This is used in conjunction with #g_ulSpeedWhole to compute
//! the value for #g_ulSpeed.
//
//*****************************************************************************
static unsigned long g_ulSpeedFract = 0;
//*****************************************************************************
//
//! The current duty cycle for the motor drive, expressed as a 16.16
//! fixed-point value in the range from 0.0 to 1.0.
//
//*****************************************************************************
static unsigned long g_ulDutyCycle = 0;
//*****************************************************************************
//
//! The current angle of the motor drive output, expressed as a 0.32
//! fixed-point value that is the percentage of the way around a circle.
//
//*****************************************************************************
unsigned long g_ulAngle = 0;
//*****************************************************************************
//
//! The amount by which the motor drive angle is updated for a single PWM
//! period, expressed as a 0.32 fixed-point value. For example, if the motor
//! drive is being updated every fifth PWM period, this value should be
//! multiplied by five to determine the amount to adjust the angle.
//
//*****************************************************************************
static unsigned long g_ulAngleDelta = 0;
//*****************************************************************************
//
//! A count of the number of milliseconds to remain in a particular state.
//
//*****************************************************************************
static unsigned long g_ulStateCount = 0;
//*****************************************************************************
//
//! A count of the number of milliseconds to remain in a particular startup
//! Hall state.
//
//*****************************************************************************
static unsigned long g_ulStartupHallStateCount = 0;
//*****************************************************************************
//
//! A count of the number of milliseconds to remain in a particular startup
//! Hall state.
//
//*****************************************************************************
static unsigned long g_ulStartupHallStateCountReload = 0;
//*****************************************************************************
//
//! The current Hall state for Startup Mode.
//
//*****************************************************************************
static unsigned long g_ulStartupHallValue;
//*****************************************************************************
//
//! The current rate of acceleration. This will start as the parameter value,
//! but may be reduced in order to manage increases in the motor current.
//
//*****************************************************************************
static unsigned long g_ulAccelRate;
//*****************************************************************************
//
//! The current rate of deceleration. This will start as the parameter value,
//! but may be reduced in order to manage increases in the DC bus voltage.
//
//*****************************************************************************
static unsigned long g_ulDecelRate;
//*****************************************************************************
//
//! The accumulator for the integral term of the PI controller for the motor
//! drive duty cycle.
//
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -