Romain Ame
/
Stressed
coucou
Fork of Robot2016_2-0 by
StepperMotor/Stepper.cpp@71:5590dbe8393a, 2016-05-05 (annotated)
- Committer:
- Jagang
- Date:
- Thu May 05 03:47:05 2016 +0000
- Revision:
- 71:5590dbe8393a
- Parent:
- 58:02dc8328975a
Uodate Controlleur, ajout des mouvement d et c, ajout des AX12
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jagang | 58:02dc8328975a | 1 | #include "defines.h" |
Jagang | 58:02dc8328975a | 2 | #include "Stepper.h" |
Jagang | 58:02dc8328975a | 3 | #include "mbed.h" |
Jagang | 58:02dc8328975a | 4 | |
Jagang | 58:02dc8328975a | 5 | Stepper::Stepper(PinName _en, PinName _stepPin, PinName _dir, PinName _minEndStop, float step_per_mm):en(_en), |
Jagang | 58:02dc8328975a | 6 | stepPin(_stepPin), |
Jagang | 58:02dc8328975a | 7 | direction(_dir), |
Jagang | 58:02dc8328975a | 8 | minEndStop(_minEndStop) |
Jagang | 58:02dc8328975a | 9 | { |
Jagang | 58:02dc8328975a | 10 | Step_Per_MM = step_per_mm; |
Jagang | 58:02dc8328975a | 11 | } |
Jagang | 58:02dc8328975a | 12 | |
Jagang | 71:5590dbe8393a | 13 | bool Stepper::step(int _number, int _dir, float _speed, bool _async) |
Jagang | 58:02dc8328975a | 14 | { |
Jagang | 71:5590dbe8393a | 15 | if(done()) |
Jagang | 71:5590dbe8393a | 16 | { |
Jagang | 71:5590dbe8393a | 17 | dir = _dir; |
Jagang | 71:5590dbe8393a | 18 | number = _number; |
Jagang | 71:5590dbe8393a | 19 | speed = _speed; |
Jagang | 71:5590dbe8393a | 20 | async = _async; |
Jagang | 71:5590dbe8393a | 21 | |
Jagang | 71:5590dbe8393a | 22 | if (dir == HAUT) |
Jagang | 71:5590dbe8393a | 23 | direction = 0; |
Jagang | 71:5590dbe8393a | 24 | else if (dir == BAS) |
Jagang | 71:5590dbe8393a | 25 | direction = 1; |
Jagang | 71:5590dbe8393a | 26 | |
Jagang | 71:5590dbe8393a | 27 | ticker.attach(this,&Stepper::tick,_speed); |
Jagang | 71:5590dbe8393a | 28 | if(!_async) |
Jagang | 71:5590dbe8393a | 29 | while(!done()) |
Jagang | 71:5590dbe8393a | 30 | wait(speed); |
Jagang | 71:5590dbe8393a | 31 | |
Jagang | 71:5590dbe8393a | 32 | return true; |
Jagang | 58:02dc8328975a | 33 | } |
Jagang | 58:02dc8328975a | 34 | |
Jagang | 71:5590dbe8393a | 35 | return false; |
Jagang | 71:5590dbe8393a | 36 | |
Jagang | 71:5590dbe8393a | 37 | |
Jagang | 71:5590dbe8393a | 38 | |
Jagang | 71:5590dbe8393a | 39 | /* Step... |
Jagang | 58:02dc8328975a | 40 | for(int i=0; i<number && !(minEndStop.read() == 0 && dir == BAS); i++) |
Jagang | 58:02dc8328975a | 41 | { |
Jagang | 58:02dc8328975a | 42 | stepPin = 1; |
Jagang | 58:02dc8328975a | 43 | wait_us(5); |
Jagang | 58:02dc8328975a | 44 | stepPin = 0; |
Jagang | 58:02dc8328975a | 45 | wait_us(5); |
Jagang | 58:02dc8328975a | 46 | wait(speed); |
Jagang | 71:5590dbe8393a | 47 | }*/ |
Jagang | 71:5590dbe8393a | 48 | } |
Jagang | 71:5590dbe8393a | 49 | |
Jagang | 71:5590dbe8393a | 50 | bool Stepper::mm(int _number, int _dir, bool _async) |
Jagang | 71:5590dbe8393a | 51 | { |
Jagang | 71:5590dbe8393a | 52 | return step(_number*Step_Per_MM, _dir, DELAY-0.001, _async); |
Jagang | 71:5590dbe8393a | 53 | } |
Jagang | 71:5590dbe8393a | 54 | |
Jagang | 71:5590dbe8393a | 55 | bool Stepper::mm(float _distance, bool _async) |
Jagang | 71:5590dbe8393a | 56 | { |
Jagang | 71:5590dbe8393a | 57 | return step(abs(_distance)*Step_Per_MM, (_distance>0?HAUT:BAS), DELAY-0.001, _async); |
Jagang | 71:5590dbe8393a | 58 | } |
Jagang | 71:5590dbe8393a | 59 | |
Jagang | 71:5590dbe8393a | 60 | bool Stepper::done() |
Jagang | 71:5590dbe8393a | 61 | { |
Jagang | 71:5590dbe8393a | 62 | return number == 0; |
Jagang | 71:5590dbe8393a | 63 | } |
Jagang | 71:5590dbe8393a | 64 | |
Jagang | 71:5590dbe8393a | 65 | void Stepper::tick() |
Jagang | 71:5590dbe8393a | 66 | { |
Jagang | 71:5590dbe8393a | 67 | stepPin = 1; |
Jagang | 71:5590dbe8393a | 68 | wait_us(5); |
Jagang | 71:5590dbe8393a | 69 | stepPin = 0; |
Jagang | 71:5590dbe8393a | 70 | |
Jagang | 71:5590dbe8393a | 71 | number--; |
Jagang | 71:5590dbe8393a | 72 | |
Jagang | 71:5590dbe8393a | 73 | if(number <= 0 || (minEndStop.read() == 0 && dir == BAS)) |
Jagang | 71:5590dbe8393a | 74 | { |
Jagang | 71:5590dbe8393a | 75 | ticker.detach(); |
Jagang | 71:5590dbe8393a | 76 | number = 0; |
Jagang | 58:02dc8328975a | 77 | } |
Jagang | 58:02dc8328975a | 78 | } |
Jagang | 58:02dc8328975a | 79 | |
Jagang | 58:02dc8328975a | 80 | |
Jagang | 58:02dc8328975a | 81 | void Stepper::enable() |
Jagang | 58:02dc8328975a | 82 | { |
Jagang | 58:02dc8328975a | 83 | en = 0; |
Jagang | 58:02dc8328975a | 84 | } |
Jagang | 58:02dc8328975a | 85 | |
Jagang | 58:02dc8328975a | 86 | void Stepper::disable() |
Jagang | 58:02dc8328975a | 87 | { |
Jagang | 58:02dc8328975a | 88 | en = 1; |
Jagang | 58:02dc8328975a | 89 | } |