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

📄 stepper.c

📁 CNC.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
//*****************************************************************************
//
// stepper.c - Stepper motor control functions.
//
// Copyright (c) 2006-2007 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 220 of sw01246.
//
//*****************************************************************************

#include "hw_memmap.h"
#include "hw_pwm.h"
#include "hw_types.h"
#include "gpio.h"
#include "pwm.h"
#include "sysctl.h"
#include "main.h"
#include "stepper.h"
#include "virtualtimer.h"

//*****************************************************************************
//
//! \page main_stepper_intro Introduction
//!
//! The stepper motors used are Econostep 260 oz/in. NEMA 23 motors from
//! www.stepper3.com.  The relevant specsifications on the motors are
//! 2.8 A/phase, 1.13 Ohms/phase, and 3.6 mH/phase.  The equation V = I R give
//! a voltage of 3.2 V/phase in order to maintain the maximum rated winding
//! current.
//!
//! The drive of the motor is split into two portions; the "constant voltage"
//! region and the "constant current" region.  When a winding is first
//! energized (or the polarity is reversed), the winding is driven with the
//! supply voltage and the current in the winding builds (therefore, "constant
//! voltage").  This allows for a rapid build-up of current in the winding,
//! which is translated into torque.  Once the current in the winding reaches
//! the rated value, the winding is driven with a PWM signal alternating
//! between the supply voltage and fast decay mode, which maintains the current
//! in the winding (therefore, "constant current", though there is some current
//! ripple caused by the PWM nature of the signalling).
//!
//! By driving the motor at a higher voltage than its rated voltage, more
//! torque is generated and higher motor speeds are achieved.  But, great care
//! must be taken when adjusting the parameters of this simplistic, open-loop
//! drive scheme.  The drive of the motor is unaware of the actual behavior of
//! the motor; adjusting the parameters of the drive scheme can easily exceed
//! the ratings of the motor.  Excessive current in the motor windings cause a
//! dangerously large magnetic field (which can permanently demagnetize the
//! motor's rotor) and heat (which can melt the insulation in the motor's
//! windings, causing shorts).  Proceed only with the utmost caution.
//!
//! The code for driving the stepper motors is contained in
//! <tt>main_micro/stepper.c</tt>, with <tt>main_micro/stepper.h</tt>
//! containing the API definitions for use by the remainder of the application.
//
//*****************************************************************************

//*****************************************************************************
//
//! \defgroup main_stepper_api Definitions
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
//! The frequency, in Hertz, of the PWM signal used to drive the stepper motor
//! winding when in constant current mode.  This value should be above the
//! human audible region (~18 KHz) in order to reduce the noise produced by the
//! motor, but not excessively high to reduce switching losses.
//
//*****************************************************************************
#define STEPPER_PWM_FREQUENCY   20000       // 20 KHz

//*****************************************************************************
//
//! The voltage, in tenths of a volt, to apply to the stepper motor winding
//! when in constant current mode.  This should not exceed the rated voltage
//! (either explicit or implied via the rated current and resistance) of the
//! motor to avoid potentially causing permanent damage to the motor.
//
//*****************************************************************************
#define STEPPER_MAX_VOLTAGE     10          // 1.0 Volts

//*****************************************************************************
//
//! The voltage, in tenths of a volt, to apply to the stepper motor winding
//! when not turning the motor.  When connected to a geared load (such as the
//! lead screws on the CNC machine), no holding voltage is required since it
//! would take a huge amount of force to turn the motor through the gearing.
//
//*****************************************************************************
#define STEPPER_HOLD_VOLTAGE    0           // 0 Volts

//*****************************************************************************
//
//! The voltage, in tenths of a volt, of the power supply to the bridge.
//
//*****************************************************************************
#define STEPPER_SUPPLY_VOLTAGE  240         // 24 Volts

//*****************************************************************************
//
//! The amount of time, in microseconds, that the motor is driven in constant
//! voltage mode after taking a step.  After this delay, driving of the motor
//! changes to constant current mode.
//!
//! This value was measured empirically by observing the time required for the
//! winding current to reach the rated value.  Since dI/dt = V / L, changing
//! the power supply voltage will change the rate at which the winding current
//! reaches the rated value; great care must be taken to adjust this value if
//! the power supply voltage is changed to avoid driving the motor in a over-
//! current situation, which could cause permanent damage.
//
//*****************************************************************************
#define STEPPER_START_DELAY     800         // 800 us

//*****************************************************************************
//
//! The GPIO pin on port D to which the polarity signal for the first winding
//! of the X motor is connected.
//
//*****************************************************************************
#define STEPPER_X_PHA_POL       GPIO_PIN_2

//*****************************************************************************
//
//! The GPIO pin on port D to which the polarity signal for the second winding
//! of the X motor is connected.
//
//*****************************************************************************
#define STEPPER_X_PHB_POL       GPIO_PIN_3

//*****************************************************************************
//
//! The GPIO pins on port D to which the polarity signals for the X motor are
//! connected.
//
//*****************************************************************************
#define STEPPER_X_POL           (STEPPER_X_PHA_POL | STEPPER_X_PHB_POL)

//*****************************************************************************
//
//! The GPIO pin on port D to which the polarity signal for the first winding
//! of the Y motor is connected.
//
//*****************************************************************************
#define STEPPER_Y_PHA_POL       GPIO_PIN_4

//*****************************************************************************
//
//! The GPIO pin on port D to which the polarity signal for the second winding
//! of the Y motor is connected.
//
//*****************************************************************************
#define STEPPER_Y_PHB_POL       GPIO_PIN_5

//*****************************************************************************
//
//! The GPIO pins on port D to which the polarity signals for the Y motor are
//! connected.
//
//*****************************************************************************
#define STEPPER_Y_POL           (STEPPER_Y_PHA_POL | STEPPER_Y_PHB_POL)

//*****************************************************************************
//
//! The GPIO pin on port D to which the polarity signal for the first winding
//! of the Z motor is connected.
//
//*****************************************************************************
#define STEPPER_Z_PHA_POL       GPIO_PIN_6

//*****************************************************************************
//
//! The GPIO pin on port D to which the polarity signal for the second winding
//! of the Z motor is connected.
//
//*****************************************************************************
#define STEPPER_Z_PHB_POL       GPIO_PIN_7

//*****************************************************************************
//
//! The GPIO pins on port D to which the polarity signals for the Z motor are
//! connected.
//
//*****************************************************************************
#define STEPPER_Z_POL           (STEPPER_Z_PHA_POL | STEPPER_Z_PHB_POL)

//*****************************************************************************
//
//! The number of PWM clocks in a complete PWM cycle.  This is used to
//! determine the count (i.e. duty cycle) required to achieve a given voltage
//! at the motor.
//
//*****************************************************************************
static unsigned long g_ulPWMClock;

//*****************************************************************************
//
//! This structure contains the details of a single stepper motor.
//
//*****************************************************************************
typedef struct
{
    //
    //! The step number currently being driven to the motor.
    //
    unsigned long ulStep;

    //
    //! The offset of the PWM generator used to drive the motor.
    //
    unsigned char ucPWM;

    //
    //! The GPIO pins used to drive the polarity signals to the two phases of
    //! the motor.
    //
    unsigned char ucPhases;

    //
    //! The GPIO pin used to drive the polarity signal to phase A of the motor.
    //
    unsigned char ucPHA;

    //
    //! The GPIO pin used to drive the polarity signal to phase B of the motor.
    //
    unsigned char ucPHB;

    //
    //! The virtual timer structure used for the delay between a step that
    //! affects the polarity on phase A and the changing of phase A from
    //! constant voltage mode to constant current mode.
    //
    tVirtualTimer sTimerA;

    //
    //! The virtual timer structure used for the delay between a step that
    //! affects the polarity on phase B and the changing of phase B from
    //! constant voltage mode to constant current mode.
    //
    tVirtualTimer sTimerB;
}
tStepperState;

//*****************************************************************************
//
// Forward declarations for the functions that take each motor's winding from
// constant voltage mode to constant current mode.
//
//*****************************************************************************
static void StepperXACurrent(void);
static void StepperXBCurrent(void);
static void StepperYACurrent(void);
static void StepperYBCurrent(void);
static void StepperZACurrent(void);
static void StepperZBCurrent(void);

//*****************************************************************************
//
//! The stepper state structure for the X axis stepper.
//
//*****************************************************************************
static tStepperState g_sXStepper =
{
    0,
    PWM_GEN_0_OFFSET,
    STEPPER_X_POL,
    STEPPER_X_PHA_POL,
    STEPPER_X_PHB_POL,
    { StepperXACurrent, 0 },
    { StepperXBCurrent, 0 }
};

//*****************************************************************************
//
//! The stepper state structure for the Y axis stepper.
//
//*****************************************************************************
static tStepperState g_sYStepper =
{
    0,
    PWM_GEN_1_OFFSET,
    STEPPER_Y_POL,
    STEPPER_Y_PHA_POL,
    STEPPER_Y_PHB_POL,
    { StepperYACurrent, 0 },
    { StepperYBCurrent, 0 }
};

//*****************************************************************************

⌨️ 快捷键说明

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