Init
Dependents: JetflyerMotorController
Motor.cpp@1:39b173360768, 2017-07-13 (annotated)
- Committer:
- skrickl
- Date:
- Thu Jul 13 13:42:32 2017 +0000
- Revision:
- 1:39b173360768
- Parent:
- 0:5eb2bad9ea40
bla
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
skrickl | 1:39b173360768 | 1 | #include "Motor.h" |
skrickl | 1:39b173360768 | 2 | |
skrickl | 1:39b173360768 | 3 | |
skrickl | 1:39b173360768 | 4 | Motor::Motor() : forward(pin_forward),backward(pin_backward),throttle(pin_pwm_motor) |
skrickl | 1:39b173360768 | 5 | { |
skrickl | 1:39b173360768 | 6 | |
skrickl | 1:39b173360768 | 7 | direction_old = 1; |
skrickl | 1:39b173360768 | 8 | } |
skrickl | 1:39b173360768 | 9 | |
skrickl | 1:39b173360768 | 10 | Motor::~Motor() |
skrickl | 1:39b173360768 | 11 | { |
skrickl | 1:39b173360768 | 12 | |
skrickl | 1:39b173360768 | 13 | } |
skrickl | 1:39b173360768 | 14 | |
skrickl | 1:39b173360768 | 15 | //speed in percent |
skrickl | 1:39b173360768 | 16 | int Motor::drive(int speed, int direction) |
skrickl | 1:39b173360768 | 17 | { |
skrickl | 1:39b173360768 | 18 | // if direction change is detected both pins are set to 0 |
skrickl | 1:39b173360768 | 19 | if(direction_old != direction) |
skrickl | 1:39b173360768 | 20 | { |
skrickl | 1:39b173360768 | 21 | backward =0; |
skrickl | 1:39b173360768 | 22 | forward = 0; |
skrickl | 1:39b173360768 | 23 | } |
skrickl | 1:39b173360768 | 24 | else |
skrickl | 1:39b173360768 | 25 | { |
skrickl | 1:39b173360768 | 26 | |
skrickl | 1:39b173360768 | 27 | if(direction == 1) |
skrickl | 1:39b173360768 | 28 | { |
skrickl | 1:39b173360768 | 29 | backward =0; |
skrickl | 1:39b173360768 | 30 | forward =1; |
skrickl | 1:39b173360768 | 31 | } |
skrickl | 1:39b173360768 | 32 | if(direction == -1) |
skrickl | 1:39b173360768 | 33 | { |
skrickl | 1:39b173360768 | 34 | backward =1; |
skrickl | 1:39b173360768 | 35 | forward =0; |
skrickl | 1:39b173360768 | 36 | |
skrickl | 1:39b173360768 | 37 | } |
skrickl | 1:39b173360768 | 38 | |
skrickl | 1:39b173360768 | 39 | } |
skrickl | 1:39b173360768 | 40 | |
skrickl | 1:39b173360768 | 41 | throttle.write(((float)speed)/100.0f); |
skrickl | 1:39b173360768 | 42 | direction_old = direction; |
skrickl | 1:39b173360768 | 43 | return 0; |
skrickl | 1:39b173360768 | 44 | } |