part1 in progress

Dependencies:   Motordriver Servo mbed

Committer:
yifeng021
Date:
Tue Mar 24 02:38:53 2015 +0000
Revision:
0:306c3cd7fc05
part1 in progress

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yifeng021 0:306c3cd7fc05 1 // test code, this demonstrates working motor drivers.
yifeng021 0:306c3cd7fc05 2 // full reverse to full stop, dynamicaly brake and switch off.
yifeng021 0:306c3cd7fc05 3 #include "mbed.h"
yifeng021 0:306c3cd7fc05 4 #include "motordriver.h"
yifeng021 0:306c3cd7fc05 5 #include "Servo.h"
yifeng021 0:306c3cd7fc05 6 DigitalOut myled(LED1);
yifeng021 0:306c3cd7fc05 7 //PwmOut servo1(p23);
yifeng021 0:306c3cd7fc05 8 Motor A(p22, p11, p12, 1); // pwm, fwd, rev, can brake
yifeng021 0:306c3cd7fc05 9 //Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can brake
yifeng021 0:306c3cd7fc05 10 Servo s1(p23);
yifeng021 0:306c3cd7fc05 11
yifeng021 0:306c3cd7fc05 12 int main() {
yifeng021 0:306c3cd7fc05 13 //PWMout1.period(1.0/50.0);
yifeng021 0:306c3cd7fc05 14 int i = 0;
yifeng021 0:306c3cd7fc05 15 bool up = true;
yifeng021 0:306c3cd7fc05 16 s1.calibrate(0.02, 180); //myServo.calibrate(x,y)
yifeng021 0:306c3cd7fc05 17 //x sets servo rotate speed
yifeng021 0:306c3cd7fc05 18 //y sets full range of rotation
yifeng021 0:306c3cd7fc05 19 //in reality, even though we set range to 180deg, the actual range
yifeng021 0:306c3cd7fc05 20 //the servo reaches is slightly less than 180deg
yifeng021 0:306c3cd7fc05 21 while(1) {
yifeng021 0:306c3cd7fc05 22 s1 = i/100.0;
yifeng021 0:306c3cd7fc05 23 wait(0.01);
yifeng021 0:306c3cd7fc05 24 //Sensor moves from one extreme position to the other
yifeng021 0:306c3cd7fc05 25 if (up) {
yifeng021 0:306c3cd7fc05 26 i++;
yifeng021 0:306c3cd7fc05 27 if (i == 100) {up = false;}
yifeng021 0:306c3cd7fc05 28 }
yifeng021 0:306c3cd7fc05 29 //Sensor moves from one extreme position to the other
yifeng021 0:306c3cd7fc05 30 else {
yifeng021 0:306c3cd7fc05 31 i--;
yifeng021 0:306c3cd7fc05 32 if (i == 0) {up = true;}
yifeng021 0:306c3cd7fc05 33 }
yifeng021 0:306c3cd7fc05 34 A.speed(-0.8);
yifeng021 0:306c3cd7fc05 35 wait(0.01);
yifeng021 0:306c3cd7fc05 36 }
yifeng021 0:306c3cd7fc05 37
yifeng021 0:306c3cd7fc05 38 /*
yifeng021 0:306c3cd7fc05 39 while (1) {
yifeng021 0:306c3cd7fc05 40
yifeng021 0:306c3cd7fc05 41 //for (float s= -1.0; s < 0.0 ; s += 0.01) {
yifeng021 0:306c3cd7fc05 42 //A.speed(s);
yifeng021 0:306c3cd7fc05 43 //B.speed(s);
yifeng021 0:306c3cd7fc05 44 //wait(0.02);
yifeng021 0:306c3cd7fc05 45 //}
yifeng021 0:306c3cd7fc05 46
yifeng021 0:306c3cd7fc05 47 while (1) {
yifeng021 0:306c3cd7fc05 48 A.speed(-0.8);
yifeng021 0:306c3cd7fc05 49 wait(0.01);
yifeng021 0:306c3cd7fc05 50 }
yifeng021 0:306c3cd7fc05 51 A.stop(0.5);
yifeng021 0:306c3cd7fc05 52 wait(1);
yifeng021 0:306c3cd7fc05 53 A.coast();
yifeng021 0:306c3cd7fc05 54 }
yifeng021 0:306c3cd7fc05 55 */
yifeng021 0:306c3cd7fc05 56 }