ddd

Dependencies:   a4998ss mbed

Fork of whouse by Keegan Hu

Committer:
glintligo
Date:
Fri May 11 10:25:12 2018 +0000
Revision:
2:ecf401a37aa3
Parent:
1:6bf8f2422228
Child:
3:09f6061e6e5d
test1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bilybill 0:2e00b81c9137 1 #include "Stepper_motor.h"
glintligo 2:ecf401a37aa3 2 #include "mbed.h"
glintligo 2:ecf401a37aa3 3 extern Serial Info;
glintligo 1:6bf8f2422228 4 /**
glintligo 1:6bf8f2422228 5 * [Stepper_motor::Stepper_motor 构造函数 初始化一个步进电机对象]
glintligo 1:6bf8f2422228 6 * @param _en [en引脚]
glintligo 1:6bf8f2422228 7 * @param _stepPin [step引脚]
glintligo 1:6bf8f2422228 8 * @param _dirPin [dir引脚]
glintligo 1:6bf8f2422228 9 * @param Inter [行程开关引脚]
glintligo 1:6bf8f2422228 10 * @param _ratio [减速比]
glintligo 1:6bf8f2422228 11 * @param _micorstep [细分数]
glintligo 1:6bf8f2422228 12 * @param _dir [初始方向]
glintligo 2:ecf401a37aa3 13 * @param _rec [步距角]
glintligo 1:6bf8f2422228 14 */
Bilybill 0:2e00b81c9137 15 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 16 {
Bilybill 0:2e00b81c9137 17 ratio = _ratio;
Bilybill 0:2e00b81c9137 18 microstep = _micorstep;
Bilybill 0:2e00b81c9137 19 dir = _dir;
glintligo 2:ecf401a37aa3 20 Ori_rec = _rec;
glintligo 2:ecf401a37aa3 21 pos_dir = _dir;
Bilybill 0:2e00b81c9137 22 this->enable();
Bilybill 0:2e00b81c9137 23 }
glintligo 1:6bf8f2422228 24 /**
glintligo 1:6bf8f2422228 25 * [Stepper_motor::Config 步进电机角度速度控制]
glintligo 1:6bf8f2422228 26 * @param rec_rate [转速 单位deg/s]
glintligo 1:6bf8f2422228 27 * @param rec [转动的角度 单位 °]
glintligo 1:6bf8f2422228 28 * 若转动的角度为-1 则会一直转,此时rec_rate需大于0
glintligo 1:6bf8f2422228 29 * 除此之外,若转动角度或转速小于0,则电机反转
glintligo 1:6bf8f2422228 30 * 如在电机尚未停止前再次调用该函数,则会立刻开始新的电机运动
glintligo 1:6bf8f2422228 31 */
Bilybill 0:2e00b81c9137 32 void Stepper_motor::Config(float rec_rate,float rec)
Bilybill 0:2e00b81c9137 33 {
Bilybill 0:2e00b81c9137 34 if(rec < 0 && rec != -1)
Bilybill 0:2e00b81c9137 35 {
Bilybill 0:2e00b81c9137 36 rec_rate = 0 - rec_rate;
Bilybill 0:2e00b81c9137 37 rec = 0 - rec;
Bilybill 0:2e00b81c9137 38 }
Bilybill 0:2e00b81c9137 39 long long int frequency = rec_rate * ratio * microstep / Ori_rec;
Bilybill 0:2e00b81c9137 40 if(frequency < 0)
Bilybill 0:2e00b81c9137 41 {
Bilybill 0:2e00b81c9137 42 dir = 1 - pos_dir;
Bilybill 0:2e00b81c9137 43 frequency = 0 - frequency;
Bilybill 0:2e00b81c9137 44 }
Bilybill 0:2e00b81c9137 45 else if(frequency == 0)
Bilybill 0:2e00b81c9137 46 {
Bilybill 0:2e00b81c9137 47 this->disable();
Bilybill 0:2e00b81c9137 48 return;
Bilybill 0:2e00b81c9137 49 }
Bilybill 0:2e00b81c9137 50 else if(frequency > 0)
Bilybill 0:2e00b81c9137 51 {
Bilybill 0:2e00b81c9137 52 dir = pos_dir;
Bilybill 0:2e00b81c9137 53 }
Bilybill 0:2e00b81c9137 54 long long int remain = rec * ratio * microstep / Ori_rec;
glintligo 2:ecf401a37aa3 55 Info.printf("remain:%d frequency:%d\n",remain, frequency);
Bilybill 0:2e00b81c9137 56 this->enable();
Bilybill 0:2e00b81c9137 57 this->step(dir,frequency,remain);
Bilybill 0:2e00b81c9137 58
Bilybill 0:2e00b81c9137 59 }
glintligo 1:6bf8f2422228 60 /**
glintligo 1:6bf8f2422228 61 * [Stepper_motor::getDir 获取电机的转动方向dir]
glintligo 1:6bf8f2422228 62 * @return [dir]
glintligo 1:6bf8f2422228 63 */
Bilybill 0:2e00b81c9137 64 int Stepper_motor::getDir()
Bilybill 0:2e00b81c9137 65 {
Bilybill 0:2e00b81c9137 66 return this->dir;
Bilybill 0:2e00b81c9137 67 }
glintligo 1:6bf8f2422228 68
glintligo 1:6bf8f2422228 69 /**
glintligo 1:6bf8f2422228 70 * [Stepper_motor::reConfig 步进电机反转]
glintligo 1:6bf8f2422228 71 * @param _rec [反转的角度]
glintligo 1:6bf8f2422228 72 * 速度为5°/s
glintligo 1:6bf8f2422228 73 */
Bilybill 0:2e00b81c9137 74 void Stepper_motor::reConfig(float _rec)
Bilybill 0:2e00b81c9137 75 {
Bilybill 0:2e00b81c9137 76 dir = 1 - dir;
Bilybill 0:2e00b81c9137 77 long long int frequency = 5 * ratio * microstep / Ori_rec;
Bilybill 0:2e00b81c9137 78 long long int remain = _rec * ratio * microstep / Ori_rec;
Bilybill 0:2e00b81c9137 79 this->enable();
Bilybill 0:2e00b81c9137 80 this->step(dir,frequency,remain);
Bilybill 0:2e00b81c9137 81 }
glintligo 1:6bf8f2422228 82
glintligo 1:6bf8f2422228 83 /**
glintligo 1:6bf8f2422228 84 * [Stepper_motor::set 行程开关按下后的运动函数]
glintligo 1:6bf8f2422228 85 */
Bilybill 0:2e00b81c9137 86 void Stepper_motor::set()
Bilybill 0:2e00b81c9137 87 {
glintligo 1:6bf8f2422228 88 this -> Config(0,0);//停止运动
Bilybill 0:2e00b81c9137 89 wait(0.01);
Bilybill 0:2e00b81c9137 90 this -> enable();
Bilybill 0:2e00b81c9137 91 this -> Config(-5,7);
Bilybill 0:2e00b81c9137 92 }
glintligo 1:6bf8f2422228 93
glintligo 1:6bf8f2422228 94 /** [Stepper_motor::Init 步进电机初始化] */
Bilybill 0:2e00b81c9137 95 void Stepper_motor::Init()
Bilybill 0:2e00b81c9137 96 {
Bilybill 0:2e00b81c9137 97 this->Config(10,-1);
Bilybill 0:2e00b81c9137 98 this->fall(this,&Stepper_motor::set);
Bilybill 0:2e00b81c9137 99 }