Time is good

Dependencies:   RoboClaw mbed

Fork of Robot2016_2-0 by ARES

Committer:
IceTeam
Date:
Fri May 06 15:25:21 2016 +0000
Revision:
92:f09f55aa992b
Child:
93:c0b040954eac
Rajout de ControlleurPince & StepperMotor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IceTeam 92:f09f55aa992b 1 #include "defines.h"
IceTeam 92:f09f55aa992b 2 #include "Stepper.h"
IceTeam 92:f09f55aa992b 3 #include "mbed.h"
IceTeam 92:f09f55aa992b 4
IceTeam 92:f09f55aa992b 5 Stepper::Stepper(PinName _en, PinName _stepPin, PinName _dir, PinName _minEndStop, float step_per_mm):en(_en),
IceTeam 92:f09f55aa992b 6 stepPin(_stepPin),
IceTeam 92:f09f55aa992b 7 direction(_dir),
IceTeam 92:f09f55aa992b 8 minEndStop(_minEndStop)
IceTeam 92:f09f55aa992b 9 {
IceTeam 92:f09f55aa992b 10 Step_Per_MM = step_per_mm;
IceTeam 92:f09f55aa992b 11 }
IceTeam 92:f09f55aa992b 12
IceTeam 92:f09f55aa992b 13 bool Stepper::step(int _number, int _dir, float _speed, bool _async)
IceTeam 92:f09f55aa992b 14 {
IceTeam 92:f09f55aa992b 15 if(done())
IceTeam 92:f09f55aa992b 16 {
IceTeam 92:f09f55aa992b 17 dir = _dir;
IceTeam 92:f09f55aa992b 18 number = _number;
IceTeam 92:f09f55aa992b 19 speed = _speed;
IceTeam 92:f09f55aa992b 20 async = _async;
IceTeam 92:f09f55aa992b 21
IceTeam 92:f09f55aa992b 22 if (dir == HAUT)
IceTeam 92:f09f55aa992b 23 direction = 0;
IceTeam 92:f09f55aa992b 24 else if (dir == BAS)
IceTeam 92:f09f55aa992b 25 direction = 1;
IceTeam 92:f09f55aa992b 26
IceTeam 92:f09f55aa992b 27 ticker.attach(this,&Stepper::tick,_speed);
IceTeam 92:f09f55aa992b 28 if(!_async)
IceTeam 92:f09f55aa992b 29 while(!done())
IceTeam 92:f09f55aa992b 30 wait(speed);
IceTeam 92:f09f55aa992b 31
IceTeam 92:f09f55aa992b 32 return true;
IceTeam 92:f09f55aa992b 33 }
IceTeam 92:f09f55aa992b 34
IceTeam 92:f09f55aa992b 35 return false;
IceTeam 92:f09f55aa992b 36
IceTeam 92:f09f55aa992b 37
IceTeam 92:f09f55aa992b 38
IceTeam 92:f09f55aa992b 39 /* Step...
IceTeam 92:f09f55aa992b 40 for(int i=0; i<number && !(minEndStop.read() == 0 && dir == BAS); i++)
IceTeam 92:f09f55aa992b 41 {
IceTeam 92:f09f55aa992b 42 stepPin = 1;
IceTeam 92:f09f55aa992b 43 wait_us(5);
IceTeam 92:f09f55aa992b 44 stepPin = 0;
IceTeam 92:f09f55aa992b 45 wait_us(5);
IceTeam 92:f09f55aa992b 46 wait(speed);
IceTeam 92:f09f55aa992b 47 }*/
IceTeam 92:f09f55aa992b 48 }
IceTeam 92:f09f55aa992b 49
IceTeam 92:f09f55aa992b 50 bool Stepper::mm(int _number, int _dir, bool _async)
IceTeam 92:f09f55aa992b 51 {
IceTeam 92:f09f55aa992b 52 return step(_number*Step_Per_MM, _dir, DELAY-0.001, _async);
IceTeam 92:f09f55aa992b 53 }
IceTeam 92:f09f55aa992b 54
IceTeam 92:f09f55aa992b 55 bool Stepper::mm(float _distance, bool _async)
IceTeam 92:f09f55aa992b 56 {
IceTeam 92:f09f55aa992b 57 return step(abs(_distance)*Step_Per_MM, (_distance>0?HAUT:BAS), DELAY-0.001, _async);
IceTeam 92:f09f55aa992b 58 }
IceTeam 92:f09f55aa992b 59
IceTeam 92:f09f55aa992b 60 bool Stepper::done()
IceTeam 92:f09f55aa992b 61 {
IceTeam 92:f09f55aa992b 62 return number == 0;
IceTeam 92:f09f55aa992b 63 }
IceTeam 92:f09f55aa992b 64
IceTeam 92:f09f55aa992b 65 void Stepper::tick()
IceTeam 92:f09f55aa992b 66 {
IceTeam 92:f09f55aa992b 67 stepPin = 1;
IceTeam 92:f09f55aa992b 68 wait_us(5);
IceTeam 92:f09f55aa992b 69 stepPin = 0;
IceTeam 92:f09f55aa992b 70
IceTeam 92:f09f55aa992b 71 number--;
IceTeam 92:f09f55aa992b 72
IceTeam 92:f09f55aa992b 73 if(number <= 0 || (minEndStop.read() == 0 && dir == BAS))
IceTeam 92:f09f55aa992b 74 {
IceTeam 92:f09f55aa992b 75 ticker.detach();
IceTeam 92:f09f55aa992b 76 number = 0;
IceTeam 92:f09f55aa992b 77 }
IceTeam 92:f09f55aa992b 78 }
IceTeam 92:f09f55aa992b 79
IceTeam 92:f09f55aa992b 80
IceTeam 92:f09f55aa992b 81 void Stepper::enable()
IceTeam 92:f09f55aa992b 82 {
IceTeam 92:f09f55aa992b 83 en = 0;
IceTeam 92:f09f55aa992b 84 }
IceTeam 92:f09f55aa992b 85
IceTeam 92:f09f55aa992b 86 void Stepper::disable()
IceTeam 92:f09f55aa992b 87 {
IceTeam 92:f09f55aa992b 88 en = 1;
IceTeam 92:f09f55aa992b 89 }