Slow version

Dependencies:   mbed

Fork of SunflowerMach1 by Milan Draganic

Committer:
cvitas
Date:
Fri Nov 08 22:33:31 2013 +0000
Revision:
1:2e7d4aa6e79e
Parent:
0:7447b8021b33
Version 1a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mdraganic 0:7447b8021b33 1 #include "Motor.h"
mdraganic 0:7447b8021b33 2
cvitas 1:2e7d4aa6e79e 3 Motor::Motor(PinName positivePin, PinName negativePin, PinName enablePin): positiveOut(positivePin), negativeOut(negativePin), enableOut(enablePin) {
cvitas 1:2e7d4aa6e79e 4 enableOut.period(0.010); // set PWM period to 10 ms
mdraganic 0:7447b8021b33 5 }
mdraganic 0:7447b8021b33 6
mdraganic 0:7447b8021b33 7 void Motor::movePositive() {
mdraganic 0:7447b8021b33 8
mdraganic 0:7447b8021b33 9 direction = 1;
mdraganic 0:7447b8021b33 10 move();
mdraganic 0:7447b8021b33 11 }
mdraganic 0:7447b8021b33 12
cvitas 1:2e7d4aa6e79e 13 void Motor::moveNegativeSlow() {
cvitas 1:2e7d4aa6e79e 14
cvitas 1:2e7d4aa6e79e 15 direction = -1;
cvitas 1:2e7d4aa6e79e 16 moveslow();
cvitas 1:2e7d4aa6e79e 17 }
cvitas 1:2e7d4aa6e79e 18 void Motor::movePositiveSlow() {
cvitas 1:2e7d4aa6e79e 19
cvitas 1:2e7d4aa6e79e 20 direction = 1;
cvitas 1:2e7d4aa6e79e 21 moveslow();
cvitas 1:2e7d4aa6e79e 22 }
cvitas 1:2e7d4aa6e79e 23
mdraganic 0:7447b8021b33 24 void Motor::moveNegative() {
mdraganic 0:7447b8021b33 25
mdraganic 0:7447b8021b33 26 direction = -1;
mdraganic 0:7447b8021b33 27 move();
mdraganic 0:7447b8021b33 28 }
mdraganic 0:7447b8021b33 29 void Motor::move() {
mdraganic 0:7447b8021b33 30
mdraganic 0:7447b8021b33 31 positiveOut = 0;
mdraganic 0:7447b8021b33 32 negativeOut = 0;
mdraganic 0:7447b8021b33 33
mdraganic 0:7447b8021b33 34 switch(direction) {
mdraganic 0:7447b8021b33 35 case 0:
mdraganic 0:7447b8021b33 36 return;
mdraganic 0:7447b8021b33 37 case 1:
mdraganic 0:7447b8021b33 38 positiveOut = 1;
mdraganic 0:7447b8021b33 39 break;
mdraganic 0:7447b8021b33 40 case -1:
mdraganic 0:7447b8021b33 41 negativeOut = 1;
mdraganic 0:7447b8021b33 42 break;
mdraganic 0:7447b8021b33 43 }
mdraganic 0:7447b8021b33 44
mdraganic 0:7447b8021b33 45 wait_ms(motorDriveTime);
mdraganic 0:7447b8021b33 46 }
cvitas 1:2e7d4aa6e79e 47 void Motor::moveslow() {
mdraganic 0:7447b8021b33 48
cvitas 1:2e7d4aa6e79e 49 positiveOut = 0;
cvitas 1:2e7d4aa6e79e 50 negativeOut = 0;
cvitas 1:2e7d4aa6e79e 51
cvitas 1:2e7d4aa6e79e 52 switch(direction) {
cvitas 1:2e7d4aa6e79e 53 case 0:
cvitas 1:2e7d4aa6e79e 54 return;
cvitas 1:2e7d4aa6e79e 55 case 1:
cvitas 1:2e7d4aa6e79e 56 positiveOut = 1;
cvitas 1:2e7d4aa6e79e 57 break;
cvitas 1:2e7d4aa6e79e 58 case -1:
cvitas 1:2e7d4aa6e79e 59 negativeOut = 1;
cvitas 1:2e7d4aa6e79e 60 break;
cvitas 1:2e7d4aa6e79e 61 }
cvitas 1:2e7d4aa6e79e 62 for (int i=0; i<1; i=i+0.1) { // slow running start 0.5 s
cvitas 1:2e7d4aa6e79e 63 enableOut= i;
cvitas 1:2e7d4aa6e79e 64 wait(0.05);
cvitas 1:2e7d4aa6e79e 65 }
cvitas 1:2e7d4aa6e79e 66 wait_ms(motorDriveTime);
cvitas 1:2e7d4aa6e79e 67 }
cvitas 1:2e7d4aa6e79e 68 void Motor::initpwm() {
cvitas 1:2e7d4aa6e79e 69
cvitas 1:2e7d4aa6e79e 70 enableOut.period(0.010); // set PWM period to 10 ms
cvitas 1:2e7d4aa6e79e 71 enableOut = 0.5; // set initial duty cycle to 50%
cvitas 1:2e7d4aa6e79e 72 }
mdraganic 0:7447b8021b33 73 void Motor::stop() {
mdraganic 0:7447b8021b33 74
mdraganic 0:7447b8021b33 75 positiveOut = 0;
mdraganic 0:7447b8021b33 76 negativeOut = 0;
mdraganic 0:7447b8021b33 77 direction = 0;
mdraganic 0:7447b8021b33 78 }
cvitas 1:2e7d4aa6e79e 79 void Motor::stopslow() {
cvitas 1:2e7d4aa6e79e 80
cvitas 1:2e7d4aa6e79e 81 for (int i=1; i>0; i=i-0.1) {
cvitas 1:2e7d4aa6e79e 82 enableOut= i;
cvitas 1:2e7d4aa6e79e 83 wait(0.05);
cvitas 1:2e7d4aa6e79e 84 }
cvitas 1:2e7d4aa6e79e 85 positiveOut = 0;
cvitas 1:2e7d4aa6e79e 86 negativeOut = 0;
cvitas 1:2e7d4aa6e79e 87 direction = 0;
cvitas 1:2e7d4aa6e79e 88 }