虽然移植完毕,但是不work。需要细调……

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Microduino_Stepper.cpp Source File

Microduino_Stepper.cpp

00001 // Microduino_Stepper.cpp
00002 //
00003 // Copyright (C) 2009-2013 Shenyang
00004 // $Id: Microduino_Stepper.cpp,v 1.00 2016/04/07 $
00005 #if 0
00006 #include "Microduino_Stepper.h"
00007 extern Timer g_Timer;
00008 DigitalOut gSteperE(PIN_EN);
00009 Ticker g_Ticker;
00010 static Stepper_t steppers[MAX_STEPPERS] = {
00011     {false,NULL},{false,NULL},{false,NULL},{false,NULL}
00012 };
00013 
00014 uint8_t StepperCount = 0;
00015 
00016 // Some debugging assistance
00017 
00018 void stepperAllEnable()
00019 {
00020     //digitalWrite(PIN_EN, LOW);
00021     gSteperE = 0;
00022 }
00023 
00024 void stepperAllDisable()
00025 {
00026     //digitalWrite(PIN_EN, HIGH);
00027     gSteperE = 1;
00028 }
00029 
00030 #if 0
00031 static inline void handle_interrupts()
00032 {
00033     for(uint8_t channel=0; channel < MAX_STEPPERS; channel++) {
00034         if(steppers[channel].isActive)
00035             steppers[channel].stepper->computeStep();
00036     }
00037 }
00038 #endif
00039 
00040 #if 0
00041 static void initISR()
00042 {
00043 #if defined(_useTimer1)
00044     cli();
00045     TCCR1A = 0;
00046     TCCR1B = _BV(WGM12)|_BV(CS11);
00047     OCR1A = TIMER_COMP;
00048     TCNT1 = 0;
00049     TIMSK1 = _BV(OCIE1A);
00050     sei();
00051 #endif
00052     pinMode(PIN_EN, OUTPUT);
00053     stepperAllEnable();
00054 }
00055 
00056 #if defined(_useTimer1)
00057 ISR(TIMER1_COMPA_vect)
00058 {
00059     handle_interrupts();
00060 }
00061 #endif
00062 #endif
00063 
00064 static void timerHandle()
00065 {
00066     for (uint8_t channel=0; channel < MAX_STEPPERS; channel++) {
00067         if(steppers[channel].isActive)
00068             steppers[channel].stepper->computeStep();
00069     }
00070 }
00071 
00072 static bool isTimerActive()
00073 {
00074     for(uint8_t channel=0; channel <MAX_STEPPERS ; channel++) {
00075         if(steppers[channel].isActive == true)
00076             return  true;
00077     }
00078     return false;
00079 }
00080 /****************** end of static functions ******************************/
00081 
00082 Stepper::Stepper(uint8_t _dirPin, uint8_t _stepPin)
00083 {
00084     if(StepperCount < MAX_STEPPERS) {
00085         this->stepperIndex = StepperCount++;
00086     } else {
00087         this->stepperIndex = INVALID_STEPPER;
00088     }
00089     dirPin = _dirPin;
00090     stepPin = _stepPin;
00091     speed = 0;
00092     period = 0;
00093     counter = 0;
00094     steppers[this->stepperIndex].isActive = false;
00095 }
00096 
00097 uint8_t Stepper::begin()
00098 {
00099     if (this->stepperIndex < MAX_STEPPERS) {
00100 #if 0
00101         pinMode(dirPin, OUTPUT);
00102         pinMode(stepPin, OUTPUT);
00103 #else
00104         gpio_init_out(&dirOUT, (PinName)dirPin);
00105         gpio_init_out(&stepOUT, (PinName)stepPin);
00106 #endif
00107         setMaxAccel(DEFAULT_ACCEL);
00108         if (isTimerActive() == false) {
00109             //initISR();
00110             //pinMode(PIN_EN, OUTPUT);
00111             g_Ticker.attach_us(&timerHandle, 25);
00112             stepperAllEnable();
00113         }
00114         steppers[this->stepperIndex].isActive = true;
00115         steppers[this->stepperIndex].stepper = this;
00116     }
00117     return this->stepperIndex;
00118 }
00119 #define constrain(x,a,b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
00120 void Stepper::setSpeed(int16_t _speed)
00121 {
00122     speed += constrain((_speed-speed), -(int16_t)maxAccel, (int16_t)maxAccel);
00123     if (speed == 0) {
00124         period = 0;
00125     } else {
00126         period = MAX_SPEED_S / abs(speed);
00127     }
00128     //(speed>0) ? PIN_CLR(dirPin) : PIN_SET(dirPin);
00129     if (speed > 0) {
00130         gpio_write(&dirOUT, 0);
00131     } else {
00132         gpio_write(&dirOUT, 1);
00133     }
00134 }
00135 
00136 void Stepper::setMaxAccel(uint16_t _accel)
00137 {
00138     maxAccel = _accel;
00139 }
00140 
00141 int16_t Stepper::getSpeed()
00142 {
00143     return  speed;
00144 }
00145 
00146 uint16_t Stepper::getMaxAccel()
00147 {
00148     return  maxAccel;
00149 }
00150 
00151 void Stepper::computeStep()
00152 {
00153     counter++;
00154     if(counter > period) {
00155         counter = 0;
00156         if(period > 0) {
00157             //PIN_SET(stepPin);
00158             gpio_write(&stepOUT, 1);
00159             //delayMicroseconds(1);
00160             wait_us(1);
00161             //PIN_CLR(stepPin);
00162             gpio_write(&stepOUT, 0);
00163         }
00164     }
00165 }
00166 #endif