📄 main.c
字号:
}
//
// Otherwise, the target speed is less than the current speed.
//
else
{
//
// Compute the new maximum deceleration rate, based on the current bus
// voltage.
//
if(g_ulBusVoltage > (g_sParameters.ulDecelV + 63))
{
ulNewValue = g_sParameters.usDecel * 1024;
}
else
{
ulNewValue = (g_sParameters.usDecel * 1024 *
(64 - g_ulBusVoltage + g_sParameters.ulDecelV));
}
//
// See if the deceleration rate is greater than the requested
// deceleration rate (i.e. the deceleration rate has been changed).
//
if(g_ulDecelRate > (g_sParameters.usDecel << 16))
{
//
// Reduce the deceleration rate to the requested rate.
//
g_ulDecelRate = g_sParameters.usDecel << 16;
}
//
// Then, see if the bus voltage exceeds the voltage at which the
// deceleration rate should be reduced, and the newly computed
// deceleration rate is less than the current rate.
//
else if((g_ulBusVoltage > g_sParameters.ulDecelV) &&
(ulNewValue < g_ulDecelRate))
{
//
// Set the deceleration rate to the newly computed deceleration
// rate.
//
g_ulDecelRate = ulNewValue;
}
//
// Otherwise, see if the deceleration rate is less than the requested
// deceleration rate.
//
else if(g_ulDecelRate < (g_sParameters.usDecel << 16))
{
//
// Increase the deceleration rate by 15 RPM, slowly
// returning it to the desired rate.
//
g_ulDecelRate += (15 << 16);
}
//
// Decrease the speed fraction by the decleration rate.
//
g_ulSpeedFract -= (g_ulDecelRate >> 16);
//
// Loop while the fraction is less than zero.
//
while(g_ulSpeedFract >= 1000)
{
//
// Decrement the speed whole part.
//
g_ulSpeedWhole--;
//
// Increment the sped fraction by one.
//
g_ulSpeedFract += 1000;
}
//
// Convert the speed fraction and whole part into a 16.16 motor
// drive speed.
//
g_ulSpeed = ((g_ulSpeedWhole << 14) +
((g_ulSpeedFract << 14) / 1000));
//
// See if the target speed has been reached (for non-zero target
// speeds).
//
if((ulTarget != 0) && (g_ulSpeed < ulTarget))
{
//
// Set the motor drive speed to the target speed.
//
g_ulSpeed = ulTarget;
//
// Compute the speed fraction and whole part from the drive
// speed.
//
g_ulSpeedWhole = g_ulSpeed >> 14;
g_ulSpeedFract = (((g_ulSpeed & 0x3FFF) * 1000) >> 14);
//
// Set the motor status to running.
//
g_ucMotorStatus = MOTOR_STATUS_RUN;
}
//
// See if the speed has reached zero.
//
else if((g_ulSpeed > 0xff000000) || (g_ulSpeed == 0))
{
//
// Set the motor drive speed to zero.
//
g_ulSpeed = 0;
//
// The speed fraction and whole part are zero as well.
//
g_ulSpeedWhole = 0;
g_ulSpeedFract = 0;
//
// See if the motor drive is stopping.
//
if(g_ulState & STATE_FLAG_STOPPING)
{
//
// Turn off the PWM outputs.
//
PWMOutputOff();
//
// Indicate that the motor drive is no longer running by
// changing the blink rate on the run LED.
//
UIRunLEDBlink(200, 25);
//
// Advance the state machine to the stopped state.
//
g_ulState = STATE_STOPPED;
//
// Set the motor status to stopped.
//
g_ucMotorStatus = MOTOR_STATUS_STOP;
}
//
// Otherwise, the motor drive is not stopping.
//
else
{
//
// Set the motor drive to the correct run state based on the
// present direction (i.e. reverse direction).
//
if(HWREGBITH(&(g_sParameters.usFlags), FLAG_SENSOR_BIT) ==
FLAG_SENSOR_PRESENT)
{
if(g_ulState == STATE_REV)
{
g_ulState = STATE_BACK_RUN;
}
else
{
g_ulState = STATE_RUN;
}
}
//
// If sensorless operation, run the same basic code as would
// be run if starting up from a stopped state ... i.e. run
// the "precharge" state code without actually changing the
// PWM drive to the precharge state.
//
else
{
//
// Force the previous "Hall" sensor value to non-valid
// Hall state to to trigger a Hall edge.
//
g_ulHallPrevious = 8;
//
// Reset the Stall Detection Count.
//
g_ulStallDetectCount = 0;
//
// Get the number of milliseconds to remain in the
// "precharge" state.
//
g_ulStateCount = g_sParameters.ucPrechargeTime;
//
// Set the appropriate "precharge" state.
//
if(g_ulState == STATE_REV)
{
g_ulState = STATE_BACK_RUN;
}
else
{
g_ulState = STATE_RUN;
}
}
}
}
else
{
//
// Set the motor status to decelerating.
//
g_ucMotorStatus = MOTOR_STATUS_DECEL;
}
}
}
//*****************************************************************************
//
//! Handles the millisecond speed update software interrupt.
//!
//! This function is called as a result of the speed update software
//! interrupt being asserted. This interrupted is asserted every millisecond
//! by the PWM interrupt handler.
//!
//! The speed of the motor drive will be updated, along with handling state
//! changes of the drive (such as initiating braking when the motor drive has
//! come to a stop).
//!
//! \note Since this interrupt is software triggered, there is no interrupt
//! source to clear in this handler.
//!
//! \return None.
//
//*****************************************************************************
void
MainMillisecondTick(void)
{
unsigned long ulTarget;
//
// Update the state of the dynamic brake.
//
BrakeTick();
//
// Check for fault conditions.
//
MainCheckFaults();
//
// In case watchdog interrupted during the millisecond tick, check for
// fault condition and revert to a stopped state.
//
if((g_ulFaultFlags & FAULT_EMERGENCY_STOP) == FAULT_EMERGENCY_STOP)
{
MainEmergencyStop();
return;
}
//
// If the motor drive is currently stopped then there is nothing to be
// done.
//
if(g_ulState == STATE_STOPPED)
{
g_ulMeasuredSpeed = 0;
return;
}
//
// Set the measured speed based on the encoder/sensor settings.
//
if(HWREGBITH(&(g_sParameters.usFlags), FLAG_ENCODER_BIT) ==
FLAG_ENCODER_PRESENT)
{
g_ulMeasuredSpeed = g_ulRotorSpeed;
}
else if(HWREGBITH(&(g_sParameters.usFlags), FLAG_SENSOR_BIT) ==
FLAG_SENSOR_PRESENT)
{
if(HWREGBITH(&(g_sParameters.usFlags), FLAG_SENSOR_TYPE_BIT) ==
FLAG_SENSOR_TYPE_GPIO)
{
g_ulMeasuredSpeed = g_ulHallRotorSpeed;
}
else
{
g_ulMeasuredSpeed = g_ulLinearRotorSpeed;
}
}
else
{
g_ulMeasuredSpeed = g_ulBEMFRotorSpeed;
}
//
// See if the motor drive is in precharge mode.
//
if(g_ulState & STATE_FLAG_PRECHARGE)
{
//
// Handle precharge mode.
//
MainPrechargeHandler();
//
// There is nothing further to be done for this state.
//
return;
}
//
// See if the motor drive is in startup mode.
//
if(g_ulState & STATE_FLAG_STARTUP)
{
//
// Handle precharge mode.
//
MainStartupHandler();
//
// There is nothing further to be done for this state.
//
return;
}
//
// See if the motor drive is in run mode.
//
if(g_ulState & STATE_FLAG_RUN)
{
//
// Determine the target speed. First, see if the motor drive is
// stopping or reversing direction.
//
if((g_ulState & STATE_FLAG_STOPPING) || (g_ulState & STATE_FLAG_REV))
{
//
// When stopping or reversing direction, the target speed is
// zero.
//
g_ulSineTarget = ulTarget = 0;
}
//
// Otherwise, use the target speed specified from the UI.
//
else
{
//
// The target speed is the user supplied value,
// converted to 18.14 fixed-point format.
//
g_ulSineTarget = ulTarget = (g_ulTargetSpeed << 14);
}
//
// Handle the update to the motor drive speed based on the target
// speed.
//
MainSpeedHandler(ulTarget);
//
// Compute the angle delta based on the new motor drive speed and the
// number of poles in the motor.
//
g_ulAngleDelta = (((g_ulSpeed / 60) << 9) / g_ulPWMFrequency) << 9;
g_ulAngleDelta *= (g_sParameters.ucNumPoles + 1);
//
// Update the target Amplitude/Duty Cycle for the motor drive.
//
if((g_sParameters.sTargetCurrent == 0) ||
((g_sParameters.sTargetCurrent != 0) &&
(g_sMotorCurrent <= g_sParameters.sTargetCurrent)))
{
g_ulDutyCycle = MainSpeedController();
}
}
}
//*****************************************************************************
//
//! Starts the motor drive.
//!
//! This function starts the mot
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -