Keegan Hu
/
Nucleo_pwmgg
dfd
Fork of whouse by
Stepper_motor.cpp@0:2e00b81c9137, 2018-05-11 (annotated)
- Committer:
- Bilybill
- Date:
- Fri May 11 06:06:10 2018 +0000
- Revision:
- 0:2e00b81c9137
- Child:
- 1:6bf8f2422228
afd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bilybill | 0:2e00b81c9137 | 1 | #include "Stepper_motor.h" |
Bilybill | 0:2e00b81c9137 | 2 | |
Bilybill | 0:2e00b81c9137 | 3 | 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) |
Bilybill | 0:2e00b81c9137 | 4 | { |
Bilybill | 0:2e00b81c9137 | 5 | ratio = _ratio; |
Bilybill | 0:2e00b81c9137 | 6 | microstep = _micorstep; |
Bilybill | 0:2e00b81c9137 | 7 | dir = _dir; |
Bilybill | 0:2e00b81c9137 | 8 | Ori_rec = _rec; |
Bilybill | 0:2e00b81c9137 | 9 | pos_dir = 1; |
Bilybill | 0:2e00b81c9137 | 10 | this->enable(); |
Bilybill | 0:2e00b81c9137 | 11 | } |
Bilybill | 0:2e00b81c9137 | 12 | |
Bilybill | 0:2e00b81c9137 | 13 | void Stepper_motor::Config(float rec_rate,float rec) |
Bilybill | 0:2e00b81c9137 | 14 | { |
Bilybill | 0:2e00b81c9137 | 15 | if(rec < 0 && rec != -1) |
Bilybill | 0:2e00b81c9137 | 16 | { |
Bilybill | 0:2e00b81c9137 | 17 | rec_rate = 0 - rec_rate; |
Bilybill | 0:2e00b81c9137 | 18 | rec = 0 - rec; |
Bilybill | 0:2e00b81c9137 | 19 | } |
Bilybill | 0:2e00b81c9137 | 20 | long long int frequency = rec_rate * ratio * microstep / Ori_rec; |
Bilybill | 0:2e00b81c9137 | 21 | if(frequency < 0) |
Bilybill | 0:2e00b81c9137 | 22 | { |
Bilybill | 0:2e00b81c9137 | 23 | dir = 1 - pos_dir; |
Bilybill | 0:2e00b81c9137 | 24 | frequency = 0 - frequency; |
Bilybill | 0:2e00b81c9137 | 25 | } |
Bilybill | 0:2e00b81c9137 | 26 | else if(frequency == 0) |
Bilybill | 0:2e00b81c9137 | 27 | { |
Bilybill | 0:2e00b81c9137 | 28 | this->disable(); |
Bilybill | 0:2e00b81c9137 | 29 | return; |
Bilybill | 0:2e00b81c9137 | 30 | } |
Bilybill | 0:2e00b81c9137 | 31 | else if(frequency > 0) |
Bilybill | 0:2e00b81c9137 | 32 | { |
Bilybill | 0:2e00b81c9137 | 33 | dir = pos_dir; |
Bilybill | 0:2e00b81c9137 | 34 | } |
Bilybill | 0:2e00b81c9137 | 35 | long long int remain = rec * ratio * microstep / Ori_rec; |
Bilybill | 0:2e00b81c9137 | 36 | this->enable(); |
Bilybill | 0:2e00b81c9137 | 37 | this->step(dir,frequency,remain); |
Bilybill | 0:2e00b81c9137 | 38 | |
Bilybill | 0:2e00b81c9137 | 39 | } |
Bilybill | 0:2e00b81c9137 | 40 | |
Bilybill | 0:2e00b81c9137 | 41 | int Stepper_motor::getDir() |
Bilybill | 0:2e00b81c9137 | 42 | { |
Bilybill | 0:2e00b81c9137 | 43 | return this->dir; |
Bilybill | 0:2e00b81c9137 | 44 | } |
Bilybill | 0:2e00b81c9137 | 45 | void Stepper_motor::reConfig(float _rec) |
Bilybill | 0:2e00b81c9137 | 46 | { |
Bilybill | 0:2e00b81c9137 | 47 | dir = 1 - dir; |
Bilybill | 0:2e00b81c9137 | 48 | long long int frequency = 5 * ratio * microstep / Ori_rec; |
Bilybill | 0:2e00b81c9137 | 49 | long long int remain = _rec * ratio * microstep / Ori_rec; |
Bilybill | 0:2e00b81c9137 | 50 | this->enable(); |
Bilybill | 0:2e00b81c9137 | 51 | this->step(dir,frequency,remain); |
Bilybill | 0:2e00b81c9137 | 52 | } |
Bilybill | 0:2e00b81c9137 | 53 | void Stepper_motor::set() |
Bilybill | 0:2e00b81c9137 | 54 | { |
Bilybill | 0:2e00b81c9137 | 55 | this -> Config(0,0); |
Bilybill | 0:2e00b81c9137 | 56 | wait(0.01); |
Bilybill | 0:2e00b81c9137 | 57 | this -> enable(); |
Bilybill | 0:2e00b81c9137 | 58 | this -> Config(-5,7); |
Bilybill | 0:2e00b81c9137 | 59 | } |
Bilybill | 0:2e00b81c9137 | 60 | void Stepper_motor::Init() |
Bilybill | 0:2e00b81c9137 | 61 | { |
Bilybill | 0:2e00b81c9137 | 62 | this->Config(10,-1); |
Bilybill | 0:2e00b81c9137 | 63 | this->fall(this,&Stepper_motor::set); |
Bilybill | 0:2e00b81c9137 | 64 | } |