📄 ui.c
字号:
//*****************************************************************************
//
// ui.c - User interface module.
//
// 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_nvic.h"
#include "../../DriverLib/hw_types.h"
#include "../../DriverLib/hw_watchdog.h"
#include "../../DriverLib/src/gpio.h"
#include "../../DriverLib/src/interrupt.h"
#include "../../DriverLib/src/sysctl.h"
#include "../../DriverLib/src/systick.h"
#include "../../DriverLib/src/watchdog.h"
#include "../../DriverLib/src/timer.h"
#include "adc_ctrl.h"
#include "commands.h"
#include "cpu_usage.h"
#include "faults.h"
#include "flash_pb.h"
#include "main.h"
#include "pins.h"
#include "pwm_ctrl.h"
#include "speed_sense.h"
#include "hall_ctrl.h"
#include "ui.h"
#include "ui_onboard.h"
#include "ui_serial.h"
#include "ui_ethernet.h"
#include "ui_can.h"
//*****************************************************************************
//
//! \page ui_intro Introduction
//!
//! There are two user interfaces for the the Brushless DC motor application.
//! One uses an push button for basic control of
//! the motor and two LEDs for basic status feedback, and the other uses the
//! Ethernet port to provide complete control of all aspects of the motor drive
//! as well as monitoring of real-time performance data.
//!
//! The on-board user interface consists of a push button and
//! two LEDs. The push button cycles between run
//! forward, stop, run backward, stop.
//!
//! The ``Run'' LED flashes the entire time the application is running. The
//! LED is off most of the time if the motor drive is stopped and on most of
//! the time if it is running. The ``Fault'' LED is normally off but flashes
//! at a fast rate when a fault occurs.
//!
//! A periodic interrupt is used to poll the state of the push button and
//! perform debouncing.
//!
//! The Ethernet user interface is entirely handled by the Ethernet user
//! interface
//! module. The only thing provided here is the list of parameters and
//! real-time data items, plus a set of helper functions that are required in
//! order to properly set the values of some of the parameters.
//!
//! This user interface (and the accompanying Ethernet and on-board user
//! interface modules) is more complicated and consumes more program space than
//! would typically exist in a real motor drive application. The added
//! complexity allows a great deal of flexibility to configure and evaluate the
//! motor drive, its capabilities, and adjust it for the target motor.
//!
//! The code for the user interface is contained in <tt>ui.c</tt>, with
//! <tt>ui.h</tt> containing the definitions for the structures, defines,
//! variables, and functions exported to the remainder of the application.
//
//*****************************************************************************
//*****************************************************************************
//
//! \defgroup ui_api Definitions
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! The rate at which the user interface interrupt occurs.
//
//*****************************************************************************
#define UI_INT_RATE 200
#define UI_TICK_MS (1000/UI_INT_RATE)
#define UI_TICK_US (1000000/UI_INT_RATE)
#define UI_TICK_NS (1000000000/UI_INT_RATE)
//*****************************************************************************
//
//! The rate at which the timer interrupt occurs.
//
//*****************************************************************************
#define TIMER1A_INT_RATE 100
#define TIMER1A_TICK_MS (1000/TIMER1A_INT_RATE)
#define TIMER1A_TICK_US (1000000/TIMER1A_INT_RATE)
#define TIMER1A_TICK_NS (1000000000/TIMER1A_INT_RATE)
//*****************************************************************************
//
// Forward declarations for functions declared within this source file for use
// in the parameter and real-time data structures.
//
//*****************************************************************************
static void UIConnectionTimeout(void);
static void UIEncoderPresent(void);
static void UISensorPresent(void);
static void UISensorType(void);
static void UISensorPolarity(void);
static void UIModulationType(void);
static void UIDirectionSet(void);
static void UIPWMFrequencySet(void);
static void UIUpdateRate(void);
static void UIFAdjI(void);
static void UIDynamicBrake(void);
void UIButtonPress(void);
static void UIButtonHold(void);
static void UIDecayMode(void);
//*****************************************************************************
//
//! Debug Information.
//
//*****************************************************************************
unsigned long g_ulDebugInfo = 0;
//*****************************************************************************
//
//! The blink rate of the two LEDs on the board; this is the number of user
//! interface interrupts for an entire blink cycle. The run LED is the first
//! entry of the array and the fault LED is the second entry of the array.
//
//*****************************************************************************
static unsigned short g_pusBlinkRate[2] =
{
0, 0
};
//*****************************************************************************
//
//! The blink period of the two LEDs on the board; this is the number of user
//! interface interrupts for which the LED will be turned on. The run LED is
//! the first entry of the array and the fault LED is the second entry of the
//! array.
//
//*****************************************************************************
static unsigned short g_pusBlinkPeriod[2];
//*****************************************************************************
//
//! The count of count of user interface interrupts that have occurred. This
//! is used to determine when to toggle the LEDs that are blinking.
//
//*****************************************************************************
static unsigned long g_ulBlinkCount = 0;
//*****************************************************************************
//
//! This array contains the base address of the GPIO blocks for the two LEDs
//! on the board.
//
//*****************************************************************************
static const unsigned long g_pulLEDBase[2] =
{
PIN_LEDRUN_PORT,
PIN_LEDFAULT_PORT
};
//*****************************************************************************
//
//! This array contains the pin numbers of the two LEDs on the board.
//
//*****************************************************************************
static const unsigned char g_pucLEDPin[2] =
{
PIN_LEDRUN_PIN,
PIN_LEDFAULT_PIN
};
//*****************************************************************************
//
//! The specification of the encoder presence on the motor. This variable is
//! used by the serial interface as a staging area before the value gets
//! placed into the flags in the parameter block by UIEncoderPresent().
//
//*****************************************************************************
static unsigned char g_ucEncoder = 0;
//*****************************************************************************
//
//! The specification of the sensor presence on the motor. This variable is
//! used by the serial interface as a staging area before the value gets
//! placed into the flags in the parameter block by UISensorPresent().
//
//*****************************************************************************
static unsigned char g_ucSensor = 0;
//*****************************************************************************
//
//! The specification of the type of sensor presence on the motor. This
//! variable is used by the serial interface as a staging area before the
//! value gets placed into the flags in the parameter block by
//! UISensorType().
//
//*****************************************************************************
static unsigned char g_ucSensorType = 0;
//*****************************************************************************
//
//! The specification of the polarity of sensor on the motor. This
//! variable is used by the serial interface as a staging area before the
//! value gets placed into the flags in the parameter block by
//! UISensorPolarity().
//
//*****************************************************************************
static unsigned char g_ucSensorPolarity = 0;
//*****************************************************************************
//
//! The specification of the modulation waveform type for the motor drive.
//! This variable is used by the serial interface as a staging area before the
//! value gets placed into the flags in the parameter block by
//! UIModulationType().
//
//*****************************************************************************
static unsigned char g_ucModulation = 0;
//*****************************************************************************
//
//! The specification of the motor drive direction. This variable is used by
//! the serial interface as a staging area before the value gets placed into
//! the flags in the parameter block by UIDirectionSet().
//
//*****************************************************************************
static unsigned char g_ucDirection = 0;
//*****************************************************************************
//
//! The specification of the PWM frequency for the motor drive. This variable
//! is used by the serial interface as a staging area before the value gets
//! placed into the flags in the parameter block by UIPWMFrequencySet().
//
//*****************************************************************************
static unsigned char g_ucFrequency = 0;
//*****************************************************************************
//
//! The specification of the update rate for the motor drive. This variable is
//! used by the serial interface as a staging area before the value gets
//! updated in a synchronous manner by UIUpdateRate().
//
//*****************************************************************************
static unsigned char g_ucUpdateRate = 0;
//*****************************************************************************
//
//! The I coefficient of the frequency PI controller. This variable is used by
//! the serial interface as a staging area before the value gets placed into
//! the parameter block by UIFAdjI().
//
//*****************************************************************************
static long g_lFAdjI = 0;
//*****************************************************************************
//
//! A boolean that is true when the on-board user interface should be active
//! and false when it should not be.
//
//*****************************************************************************
static unsigned long g_ulUIUseOnboard = 1;
//*****************************************************************************
//
//! A boolean that is true when dynamic braking should be utilized. This
//! variable is used by the serial interface as a staging area before the value
//! gets placed into the flags in the parameter block by UIDynamicBrake().
//
//*****************************************************************************
static unsigned char g_ucDynamicBrake = 0;
//*****************************************************************************
//
//! The processor usage for the most recent measurement period. This is a
//! value between 0 and 100, inclusive.
//
//*****************************************************************************
unsigned char g_ucCPUUsage = 0;
//*****************************************************************************
//
//! A boolean that is true when slow decay mode should be utilized. This
//! variable is used by the serial interface as a staging area before the value
//! gets placed into the flags in the parameter block by UIDecayMode().
//
//*****************************************************************************
static unsigned char g_ucDecayMode = 1;
//*****************************************************************************
//
//! A 32-bit unsigned value that represents the value of various GPIO signals
//! on the board. Bit 0 corresponds to CFG0; Bit 1 corresponds to CFG1; Bit
//! 2 correpsonds to CFG2; Bit 8 corresponds to the Encoder A input; Bit 8
//! corresponds to the Encode B input; Bit 10 corresponds to the Encoder
//! Index input.
//
//*****************************************************************************
unsigned long g_ulGPIOData = 0;
//*****************************************************************************
//
//! This structure instance contains the configuration values for the
//! Brushless DC motor drive.
//
//*****************************************************************************
tDriveParameters g_sParameters =
{
//
// The sequence number (ucSequenceNum); this value is not important for
// the copy in SRAM.
//
0,
//
// The CRC (ucCRC); this value is not important for the copy in SRAM.
//
0,
//
// The parameter block version number (ucVersion).
//
2,
//
// The minimum pulse width (ucMinPulseWidth).
//
10,
//
// The PWM dead time (ucDeadTime).
//
25,
//
// The PWM update rate (ucUpdateRate).
//
0,
//
// The number of poles (ucNumPoles).
//
0,
//
// Padding (1 Byte)
//
{0},
//
// The acceleration rate (usAccel).
//
1000,
//
// The deceleration rate (usDecel).
//
1000,
//
// The minimum motor drive current (sMinCurrent).
//
0,
//
// The maximum motor drive current (sMaxCurrent).
//
10000,
//
// The precharge time (ucPrechargeTime).
//
2,
//
// The maximum ambient microcontroller temperature (ucMaxTemperature).
//
85,
//
// The flags (usFlags).
//
(FLAG_PWM_FREQUENCY_20K |
(FLAG_DECAY_SLOW << FLAG_DECAY_BIT) |
(FLAG_DRIVE_TRAPEZOID << FLAG_DRIVE_BIT) |
(FLAG_DIR_FORWARD << FLAG_DIR_BIT) |
(FLAG_BRAKE_ON << FLAG_BRAKE_BIT) |
(FLAG_DC_BRAKE_ON << FLAG_DC_BRAKE_BIT) |
(FLAG_SENSOR_PRESENT << FLAG_SENSOR_BIT) |
(FLAG_SENSOR_POLARITY_HIGH << FLAG_SENSOR_POLARITY_BIT)),
//
// The number of encoder lines (usNumEncoderLines).
//
1000,
//
// Padding (2 Bytes)
//
{0, 0},
//
// The minimum motor speed (ulMinSpeed).
//
200,
//
// The maximum motor speed (ulMaxSpeed).
//
12000,
//
// The minimum DC bus voltage (ulMinVBus).
//
10000,
//
// The maximum DC bus voltage (ulMaxVBus).
//
40000,
//
// The brake engage voltage (ulBrakeOnV).
//
38000,
//
// The brake disengage voltage (ulBrakeOffV).
//
37000,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -