Created
February 7, 2011 14:53
-
-
Save fffergal/814473 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void pwmFd(float on, long duration) { | |
pwmAbstract(0, 0b00000011, on, duration); | |
} | |
void pwmBk(float on, long duration) { | |
pwmAbstract(1, 0b00000011, on, duration); | |
} | |
void pwmAbstract(int directionNumber, int motors, float on, long duration) { | |
long onTime = (long) (on * 100.0); | |
long offTime = 100L - onTime; | |
long totalTime = 0L; | |
if (directionNumber) directionNumber = 255; | |
while (totalTime < duration) { | |
long timeLeft = duration - totalTime; | |
long thisOnTime; | |
long thisOffTime; | |
if (timeLeft < onTime) thisOnTime = timeLeft; | |
else thisOnTime = onTime; | |
engageMotors(motors, directionNumber); | |
msleep(thisOnTime); | |
totalTime += thisOnTime; | |
if (thisOnTime < onTime) break; | |
timeLeft = duration - totalTime; | |
if (timeLeft < offTime) thisOffTime = timeLeft; | |
else thisOffTime = offTime; | |
disengageMotors(motors); | |
msleep(thisOffTime); | |
totalTime += thisOffTime; | |
} | |
} | |
void engageMotors(int motors, int directions) { | |
int i; | |
for (i = 0; i < 8; i++) { | |
if (motors & (1 << i)) { | |
if (directions & (1 << i)) { | |
bk(i); | |
} | |
else { | |
fd(i); | |
} | |
} | |
} | |
} | |
void disengageMotors(int motors) { | |
int i; | |
for (i = 0; i < 8; i++) { | |
if (motors & (1 << i)) { | |
off(i); | |
} | |
} | |
} | |
void main() { | |
pwmFd(0.3, 1000L); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment