test

Dependencies:   mbed mbed-rtos

Committer:
injaeyoon
Date:
Sat Sep 19 10:44:35 2020 +0000
Revision:
0:87fed0658150
thread

Who changed what in which revision?

UserRevisionLine numberNew contents of line
injaeyoon 0:87fed0658150 1 ///////////설정값//////////////////////
injaeyoon 0:87fed0658150 2 #define Pulley_radius 7 //풀리의 반지름
injaeyoon 0:87fed0658150 3 #define MOTOR_STEP_ANGLE 1.8 //스텝각
injaeyoon 0:87fed0658150 4 #define MICRO_STEP_DIV 2 // 마이크로스텝 분할수
injaeyoon 0:87fed0658150 5 #define STEP_CYCLE 1.25//스텝주기, 단위 : ms (스텝모터의 속도 설정)
injaeyoon 0:87fed0658150 6 ///////////설정값//////////////////////
injaeyoon 0:87fed0658150 7
injaeyoon 0:87fed0658150 8 #define DESIRED_STEPS_PER_SEC (1000/STEP_CYCLE) //1000ms / 20ms(1step주기) ,50, 초당 스텝수
injaeyoon 0:87fed0658150 9 #define MOTOR_STEPS_PER_REV 360/MOTOR_STEP_ANGLE // 모터 1회전 스텝수
injaeyoon 0:87fed0658150 10 #define MICRO_STEP_PER_REV (MOTOR_STEPS_PER_REV*MICRO_STEP_DIV)//1회전당 총 마이크로 스텝수
injaeyoon 0:87fed0658150 11 #define DESIRED_MICRO_STEPS_PER_SEC (MICRO_STEP_DIV*DESIRED_STEPS_PER_SEC) // 1600 , 초당 마이크로 스텝수
injaeyoon 0:87fed0658150 12 #define SAMPLE_TIME (1000000L/DESIRED_MICRO_STEPS_PER_SEC) //625 , 샘플타임
injaeyoon 0:87fed0658150 13
injaeyoon 0:87fed0658150 14 class DRV8825
injaeyoon 0:87fed0658150 15 {
injaeyoon 0:87fed0658150 16
injaeyoon 0:87fed0658150 17 BusOut _bus;
injaeyoon 0:87fed0658150 18 Timer tmr;
injaeyoon 0:87fed0658150 19
injaeyoon 0:87fed0658150 20 private:
injaeyoon 0:87fed0658150 21 void step();
injaeyoon 0:87fed0658150 22 int Calculation(double distance);
injaeyoon 0:87fed0658150 23
injaeyoon 0:87fed0658150 24 int32_t _steps;
injaeyoon 0:87fed0658150 25 enum {CW=0,CCW=1} _dir;
injaeyoon 0:87fed0658150 26 int InputStep;
injaeyoon 0:87fed0658150 27 bool start;
injaeyoon 0:87fed0658150 28 double radian, degree;
injaeyoon 0:87fed0658150 29 int micro_step;
injaeyoon 0:87fed0658150 30
injaeyoon 0:87fed0658150 31
injaeyoon 0:87fed0658150 32 public:
injaeyoon 0:87fed0658150 33 DRV8825(PinName pin1, PinName pin2);
injaeyoon 0:87fed0658150 34 void Move(double distance);
injaeyoon 0:87fed0658150 35 };
injaeyoon 0:87fed0658150 36
injaeyoon 0:87fed0658150 37 ////////////////////////////////////Private////////////////////////////////////
injaeyoon 0:87fed0658150 38 void DRV8825::step()
injaeyoon 0:87fed0658150 39 {
injaeyoon 0:87fed0658150 40 _bus= _dir ? 0b11:0b01; //_dir이 1면 11반환 , _dir이 거짓이면 01반환
injaeyoon 0:87fed0658150 41 wait_us(1); //minimum pulse width
injaeyoon 0:87fed0658150 42 _bus= _dir ? 0b10:0b00;
injaeyoon 0:87fed0658150 43 _steps+= _dir? -1: 1; // updating current steps
injaeyoon 0:87fed0658150 44 }
injaeyoon 0:87fed0658150 45
injaeyoon 0:87fed0658150 46 int DRV8825::Calculation(double distance)
injaeyoon 0:87fed0658150 47 {
injaeyoon 0:87fed0658150 48 radian=0, degree = 0;
injaeyoon 0:87fed0658150 49 radian = distance / Pulley_radius; //theta값을 구한다
injaeyoon 0:87fed0658150 50 //pc.printf("radian= %f\n", radian);
injaeyoon 0:87fed0658150 51 degree = radian * (180/ 3.141592); //회전할 각도를 구한다
injaeyoon 0:87fed0658150 52 //pc.printf("degree= %f\n", degree);
injaeyoon 0:87fed0658150 53 micro_step = int(degree / (MOTOR_STEP_ANGLE / MICRO_STEP_DIV)); //돌아가야할 마이크로스텝수를 구한다.
injaeyoon 0:87fed0658150 54
injaeyoon 0:87fed0658150 55 return micro_step;
injaeyoon 0:87fed0658150 56 }
injaeyoon 0:87fed0658150 57
injaeyoon 0:87fed0658150 58
injaeyoon 0:87fed0658150 59 ////////////////////////////////////Public////////////////////////////////////
injaeyoon 0:87fed0658150 60 DRV8825::DRV8825(PinName pin1, PinName pin2): _bus(pin1,pin2)
injaeyoon 0:87fed0658150 61 {
injaeyoon 0:87fed0658150 62 _dir =CW;
injaeyoon 0:87fed0658150 63 _steps=0;
injaeyoon 0:87fed0658150 64 }
injaeyoon 0:87fed0658150 65
injaeyoon 0:87fed0658150 66
injaeyoon 0:87fed0658150 67 void DRV8825::Move(double distance)
injaeyoon 0:87fed0658150 68 {
injaeyoon 0:87fed0658150 69
injaeyoon 0:87fed0658150 70 InputStep = Calculation(distance);
injaeyoon 0:87fed0658150 71 start = true; //회전 시작 flag
injaeyoon 0:87fed0658150 72
injaeyoon 0:87fed0658150 73 if(InputStep>0) _dir = CW;
injaeyoon 0:87fed0658150 74 else if(InputStep<0) _dir = CCW;
injaeyoon 0:87fed0658150 75 tmr.start();
injaeyoon 0:87fed0658150 76 while(start) {
injaeyoon 0:87fed0658150 77 if(tmr.read_us()>SAMPLE_TIME) {
injaeyoon 0:87fed0658150 78 tmr.reset();
injaeyoon 0:87fed0658150 79 step(); //1step 이동
injaeyoon 0:87fed0658150 80
injaeyoon 0:87fed0658150 81
injaeyoon 0:87fed0658150 82 if(_steps*_steps >= InputStep*InputStep) { //특정 스텝수만큼 이동하면 stop
injaeyoon 0:87fed0658150 83 start = false;
injaeyoon 0:87fed0658150 84 _steps = 0;
injaeyoon 0:87fed0658150 85 }
injaeyoon 0:87fed0658150 86 }
injaeyoon 0:87fed0658150 87 }
injaeyoon 0:87fed0658150 88 };