Li Weiyi
/
BalanceCar
虽然移植完毕,但是不work。需要细调……
Embed:
(wiki syntax)
Show/hide line numbers
Microduino_Stepper_PWM.cpp
00001 // Microduino_Stepper.cpp 00002 // 00003 // Copyright (C) 2009-2013 Shenyang 00004 // $Id: Microduino_Stepper.cpp,v 1.00 2016/04/07 $ 00005 00006 #include "Microduino_Stepper_PWM.h" 00007 extern Timer g_Timer; 00008 extern Serial mpc; 00009 DigitalOut gSteperE(PIN_EN); 00010 Ticker g_Ticker; 00011 static Stepper_t steppers[MAX_STEPPERS] = { 00012 {false,NULL},{false,NULL},{false,NULL},{false,NULL} 00013 }; 00014 00015 uint8_t StepperCount = 0; 00016 00017 // Some debugging assistance 00018 00019 void stepperAllEnable() 00020 { 00021 //digitalWrite(PIN_EN, LOW); 00022 gSteperE = 0; 00023 } 00024 00025 void stepperAllDisable() 00026 { 00027 //digitalWrite(PIN_EN, HIGH); 00028 gSteperE = 1; 00029 } 00030 00031 #if 0 00032 static inline void handle_interrupts() 00033 { 00034 for(uint8_t channel=0; channel < MAX_STEPPERS; channel++) { 00035 if(steppers[channel].isActive) 00036 steppers[channel].stepper->computeStep(); 00037 } 00038 } 00039 #endif 00040 00041 #if 0 00042 static void initISR() 00043 { 00044 #if defined(_useTimer1) 00045 cli(); 00046 TCCR1A = 0; 00047 TCCR1B = _BV(WGM12)|_BV(CS11); 00048 OCR1A = TIMER_COMP; 00049 TCNT1 = 0; 00050 TIMSK1 = _BV(OCIE1A); 00051 sei(); 00052 #endif 00053 pinMode(PIN_EN, OUTPUT); 00054 stepperAllEnable(); 00055 } 00056 00057 #if defined(_useTimer1) 00058 ISR(TIMER1_COMPA_vect) 00059 { 00060 handle_interrupts(); 00061 } 00062 #endif 00063 #endif 00064 00065 static void timerHandle() 00066 { 00067 for (uint8_t channel=0; channel < MAX_STEPPERS; channel++) { 00068 if(steppers[channel].isActive) 00069 steppers[channel].stepper->computeStep(); 00070 } 00071 } 00072 00073 static bool isTimerActive() 00074 { 00075 for(uint8_t channel=0; channel <MAX_STEPPERS ; channel++) { 00076 if(steppers[channel].isActive == true) 00077 return true; 00078 } 00079 return false; 00080 } 00081 /****************** end of static functions ******************************/ 00082 00083 Stepper::Stepper(PinName dirPin, PinName stepPin) 00084 { 00085 if(StepperCount < MAX_STEPPERS) { 00086 this->stepperIndex = StepperCount++; 00087 } else { 00088 this->stepperIndex = INVALID_STEPPER; 00089 } 00090 gpio_init_out(&dirOUT, dirPin); 00091 00092 period = PERIOD_MIN; 00093 pwmout_init(&pwmStep, stepPin); 00094 pwmout_period_us(&pwmStep, period); 00095 pwmout_pulsewidth_us(&pwmStep, 2); 00096 speed = 0; 00097 00098 steppers[this->stepperIndex].isActive = false; 00099 } 00100 00101 uint8_t Stepper::begin() 00102 { 00103 if (this->stepperIndex < MAX_STEPPERS) { 00104 setMaxAccel(DEFAULT_ACCEL); 00105 if (isTimerActive() == false) { 00106 //initISR(); 00107 //pinMode(PIN_EN, OUTPUT); 00108 //g_Ticker.attach_us(&timerHandle, 40); 00109 stepperAllEnable(); 00110 //mpc.printf("isTimerActive==false\r\n"); 00111 } 00112 steppers[this->stepperIndex].isActive = true; 00113 steppers[this->stepperIndex].stepper = this; 00114 //pwmout_pulsewidth_us(&pwmStep, 1); // Stepper will work. 00115 } 00116 return this->stepperIndex; 00117 } 00118 00119 extern long map(long x, long in_min, long in_max, long out_min, long out_max); 00120 #define constrain(x,a,b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) 00121 void Stepper::setSpeed(int16_t _speed) 00122 { 00123 //mpc.printf("Enter setSpeed(%d)\r\n", _speed); 00124 speed += constrain((_speed-speed), -(int16_t)maxAccel, (int16_t)maxAccel); 00125 if (speed == 0) { 00126 //pwmout_pulsewidth_us(&pwmStep, 0); 00127 stepperAllDisable(); 00128 return; 00129 } else if (speed > 0) { 00130 gpio_write(&dirOUT, 0); 00131 period = map(speed, 1, 1200, PERIOD_MAX, PERIOD_MIN); 00132 //pwmout_period_us(&pwmStep, period); 00133 } 00134 else { 00135 gpio_write(&dirOUT, 1); 00136 period = map(speed, -1200, -1, PERIOD_MAX, PERIOD_MIN); 00137 pwmout_period_us(&pwmStep, period); 00138 } 00139 //stepperAllEnable(); 00140 //pwmout_period_us(&pwmStep, period); 00141 //mpc.printf("speed : %d, period : %d\r\n", speed, period); 00142 } 00143 00144 void Stepper::setMaxAccel(uint16_t _accel) 00145 { 00146 maxAccel = _accel; 00147 } 00148 00149 int16_t Stepper::getSpeed() 00150 { 00151 return speed; 00152 } 00153 00154 uint16_t Stepper::getMaxAccel() 00155 { 00156 return maxAccel; 00157 } 00158 00159 #if 0 00160 void Stepper::computeStep() 00161 { 00162 counter++; 00163 if(counter > period) { 00164 counter = 0; 00165 if(period > 0) { 00166 //PIN_SET(stepPin); 00167 gpio_write(&stepOUT, 1); 00168 //delayMicroseconds(1); 00169 wait_us(1); 00170 //PIN_CLR(stepPin); 00171 gpio_write(&stepOUT, 0); 00172 } 00173 } 00174 } 00175 #endif
Generated on Wed Jul 13 2022 03:34:51 by 1.7.2