asdf

Dependencies:   stepper mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stepper_motor.cpp Source File

Stepper_motor.cpp

00001 #include "Stepper_motor.h"
00002 
00003 Stepper_motor::Stepper_motor(PinName _en,PinName _stepPin,PinName _dirPin,PinName Inter,int _ratio,int _micorstep,int _dir,float _rec):Stepper(_en,_stepPin,_dirPin),InterruptIn(Inter)
00004 {
00005     ratio = _ratio;
00006     microstep = _micorstep;
00007     dir = _dir;
00008     Ori_rec = _rec;
00009     pos_dir = 1;
00010     this->enable();
00011 }
00012 
00013 void Stepper_motor::Config(float rec_rate,float rec)
00014 {
00015     if(rec < 0 && rec != -1)
00016     {
00017         rec_rate = 0 - rec_rate;
00018         rec = 0 - rec;
00019     }
00020     long long int frequency = rec_rate * ratio * microstep / Ori_rec;
00021     if(frequency < 0)
00022     {
00023         dir = 1 - pos_dir;
00024         frequency = 0 - frequency;
00025     }
00026     else if(frequency == 0)
00027     {
00028         this->disable();
00029         return;
00030     }
00031     else if(frequency > 0)
00032     {
00033         dir = pos_dir;
00034     }
00035     long long int remain = rec * ratio * microstep / Ori_rec;
00036     this->enable();
00037     this->step(dir,frequency,remain);
00038     
00039 }
00040 
00041 int Stepper_motor::getDir()
00042 {
00043     return this->dir;
00044 }
00045 void Stepper_motor::reConfig(float _rec)
00046 {   
00047     dir = 1 - dir;
00048     long long int frequency = 5 * ratio * microstep / Ori_rec;
00049     long long int remain = _rec * ratio * microstep / Ori_rec;
00050     this->enable();
00051     this->step(dir,frequency,remain);
00052 }
00053 void Stepper_motor::set()
00054 {
00055     this -> Config(0,0);
00056     wait(0.01);
00057     this -> enable();
00058     this -> Config(-5,7);
00059 }
00060 void Stepper_motor::Init()
00061 {
00062     this->Config(10,-1);
00063     this->fall(this,&Stepper_motor::set); 
00064 }