asdf
Dependencies: Motor Servo mbed
main.cpp@0:5c66eca35e35, 2016-04-18 (annotated)
- Committer:
- TheDoctor822
- Date:
- Mon Apr 18 01:39:24 2016 +0000
- Revision:
- 0:5c66eca35e35
Lab 7 part C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TheDoctor822 | 0:5c66eca35e35 | 1 | /************************************************************************************ |
TheDoctor822 | 0:5c66eca35e35 | 2 | Lab 07 - Problem 3 |
TheDoctor822 | 0:5c66eca35e35 | 3 | This program moves two servos back and forth in opposite directions, completing one |
TheDoctor822 | 0:5c66eca35e35 | 4 | full cycle every 10 seconds. At the same time, the program spins the DC |
TheDoctor822 | 0:5c66eca35e35 | 5 | motor forward while the servos are moving their first 180 degrees, then backward |
TheDoctor822 | 0:5c66eca35e35 | 6 | while the servos are moving back to their original position. |
TheDoctor822 | 0:5c66eca35e35 | 7 | MIDN 3/C Drew Moore |
TheDoctor822 | 0:5c66eca35e35 | 8 | 24 FEB 2016 |
TheDoctor822 | 0:5c66eca35e35 | 9 | ***********************************************************************************/ |
TheDoctor822 | 0:5c66eca35e35 | 10 | |
TheDoctor822 | 0:5c66eca35e35 | 11 | // Standard Includes |
TheDoctor822 | 0:5c66eca35e35 | 12 | #include "mbed.h" |
TheDoctor822 | 0:5c66eca35e35 | 13 | #include "Motor.h" |
TheDoctor822 | 0:5c66eca35e35 | 14 | #include "Servo.h" |
TheDoctor822 | 0:5c66eca35e35 | 15 | |
TheDoctor822 | 0:5c66eca35e35 | 16 | Serial pc(USBTX, USBRX); // tx, rx |
TheDoctor822 | 0:5c66eca35e35 | 17 | |
TheDoctor822 | 0:5c66eca35e35 | 18 | // Declare objects referencing pins in the application board |
TheDoctor822 | 0:5c66eca35e35 | 19 | Motor motor(p26, p30, p29); |
TheDoctor822 | 0:5c66eca35e35 | 20 | Servo myServo1(p21); |
TheDoctor822 | 0:5c66eca35e35 | 21 | Servo myServo2(p22); |
TheDoctor822 | 0:5c66eca35e35 | 22 | |
TheDoctor822 | 0:5c66eca35e35 | 23 | |
TheDoctor822 | 0:5c66eca35e35 | 24 | |
TheDoctor822 | 0:5c66eca35e35 | 25 | main(){ |
TheDoctor822 | 0:5c66eca35e35 | 26 | |
TheDoctor822 | 0:5c66eca35e35 | 27 | motor.speed(0.0); // Initialize motor speed to zero |
TheDoctor822 | 0:5c66eca35e35 | 28 | myServo1.calibrate(.0009, 90); // Calibrate Servo 1 |
TheDoctor822 | 0:5c66eca35e35 | 29 | myServo2.calibrate(.0009, 90); // Calibrate Servo 2 |
TheDoctor822 | 0:5c66eca35e35 | 30 | |
TheDoctor822 | 0:5c66eca35e35 | 31 | |
TheDoctor822 | 0:5c66eca35e35 | 32 | float i; // Declare a counter variable |
TheDoctor822 | 0:5c66eca35e35 | 33 | float speed = 0.2; // Beginning speed of motor |
TheDoctor822 | 0:5c66eca35e35 | 34 | |
TheDoctor822 | 0:5c66eca35e35 | 35 | while(1){ // Provide an infinite loop |
TheDoctor822 | 0:5c66eca35e35 | 36 | |
TheDoctor822 | 0:5c66eca35e35 | 37 | i = 0; |
TheDoctor822 | 0:5c66eca35e35 | 38 | |
TheDoctor822 | 0:5c66eca35e35 | 39 | myServo1 = 0.0; |
TheDoctor822 | 0:5c66eca35e35 | 40 | myServo2 = 1.0; |
TheDoctor822 | 0:5c66eca35e35 | 41 | |
TheDoctor822 | 0:5c66eca35e35 | 42 | while (i < 1.0){ // Loop to increment the servos |
TheDoctor822 | 0:5c66eca35e35 | 43 | // to their first 180 degrees |
TheDoctor822 | 0:5c66eca35e35 | 44 | motor.speed(speed); // Move motor forward |
TheDoctor822 | 0:5c66eca35e35 | 45 | myServo1 = i; |
TheDoctor822 | 0:5c66eca35e35 | 46 | myServo2 = 1-i; // Make the position of Servo2 |
TheDoctor822 | 0:5c66eca35e35 | 47 | // the opposite of Servo 1 to |
TheDoctor822 | 0:5c66eca35e35 | 48 | // get a windshield wiper motion |
TheDoctor822 | 0:5c66eca35e35 | 49 | i += .125; |
TheDoctor822 | 0:5c66eca35e35 | 50 | wait(.625); |
TheDoctor822 | 0:5c66eca35e35 | 51 | |
TheDoctor822 | 0:5c66eca35e35 | 52 | } |
TheDoctor822 | 0:5c66eca35e35 | 53 | |
TheDoctor822 | 0:5c66eca35e35 | 54 | i = 0; |
TheDoctor822 | 0:5c66eca35e35 | 55 | myServo1 = 1.0; |
TheDoctor822 | 0:5c66eca35e35 | 56 | myServo2 = 0.0; |
TheDoctor822 | 0:5c66eca35e35 | 57 | // Move the motor in the opposite direction |
TheDoctor822 | 0:5c66eca35e35 | 58 | if ( speed <= 1.0 ) { |
TheDoctor822 | 0:5c66eca35e35 | 59 | motor.speed(-speed); |
TheDoctor822 | 0:5c66eca35e35 | 60 | } |
TheDoctor822 | 0:5c66eca35e35 | 61 | |
TheDoctor822 | 0:5c66eca35e35 | 62 | while (i < 1.0){ // Similar to first while loop, |
TheDoctor822 | 0:5c66eca35e35 | 63 | // but moving the servos back |
TheDoctor822 | 0:5c66eca35e35 | 64 | myServo1 = 1-i; |
TheDoctor822 | 0:5c66eca35e35 | 65 | myServo2 = i; |
TheDoctor822 | 0:5c66eca35e35 | 66 | i += .125; |
TheDoctor822 | 0:5c66eca35e35 | 67 | wait(.625); |
TheDoctor822 | 0:5c66eca35e35 | 68 | } |
TheDoctor822 | 0:5c66eca35e35 | 69 | |
TheDoctor822 | 0:5c66eca35e35 | 70 | if( speed < 1.0) { // Increment speed |
TheDoctor822 | 0:5c66eca35e35 | 71 | speed += .1; |
TheDoctor822 | 0:5c66eca35e35 | 72 | } |
TheDoctor822 | 0:5c66eca35e35 | 73 | } |
TheDoctor822 | 0:5c66eca35e35 | 74 | } |