Code from part C of the servo lab
Dependencies: Motor Servo mbed
main.cpp@0:9e14f994576d, 2015-02-27 (annotated)
- Committer:
- probst
- Date:
- Fri Feb 27 04:21:15 2015 +0000
- Revision:
- 0:9e14f994576d
Part D servo lab;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
probst | 0:9e14f994576d | 1 | // define necessary includes |
probst | 0:9e14f994576d | 2 | #include "mbed.h" |
probst | 0:9e14f994576d | 3 | #include "Motor.h" |
probst | 0:9e14f994576d | 4 | #include "Servo.h" |
probst | 0:9e14f994576d | 5 | #define PULSE .0009 |
probst | 0:9e14f994576d | 6 | #define MAX 90 |
probst | 0:9e14f994576d | 7 | |
probst | 0:9e14f994576d | 8 | //define the two servos |
probst | 0:9e14f994576d | 9 | Servo servoOne(p21); |
probst | 0:9e14f994576d | 10 | Servo servoTwo(p22); |
probst | 0:9e14f994576d | 11 | |
probst | 0:9e14f994576d | 12 | //define the motor |
probst | 0:9e14f994576d | 13 | Motor m(p23,p24,p25); |
probst | 0:9e14f994576d | 14 | Serial pc(USBTX, USBRX); |
probst | 0:9e14f994576d | 15 | |
probst | 0:9e14f994576d | 16 | |
probst | 0:9e14f994576d | 17 | int main() |
probst | 0:9e14f994576d | 18 | { |
probst | 0:9e14f994576d | 19 | //calibrate the two servos |
probst | 0:9e14f994576d | 20 | servoOne.calibrate(PULSE,MAX); |
probst | 0:9e14f994576d | 21 | servoTwo.calibrate(PULSE,MAX); |
probst | 0:9e14f994576d | 22 | |
probst | 0:9e14f994576d | 23 | //declare the initial motor speed |
probst | 0:9e14f994576d | 24 | double motorSpeed = .1; |
probst | 0:9e14f994576d | 25 | |
probst | 0:9e14f994576d | 26 | //set the tow servoes to their initial positions |
probst | 0:9e14f994576d | 27 | float position = 0; |
probst | 0:9e14f994576d | 28 | float positionTwo = 1; |
probst | 0:9e14f994576d | 29 | |
probst | 0:9e14f994576d | 30 | while(1) |
probst | 0:9e14f994576d | 31 | { |
probst | 0:9e14f994576d | 32 | //declare the initial positions |
probst | 0:9e14f994576d | 33 | servoOne = position; //0 |
probst | 0:9e14f994576d | 34 | servoTwo = positionTwo; //1 |
probst | 0:9e14f994576d | 35 | |
probst | 0:9e14f994576d | 36 | //spin the servos in opposite directions forward |
probst | 0:9e14f994576d | 37 | while(position < 1 && positionTwo > 0) |
probst | 0:9e14f994576d | 38 | { |
probst | 0:9e14f994576d | 39 | if(motorSpeed != 1){motorSpeed = motorSpeed + .1;}; |
probst | 0:9e14f994576d | 40 | m.speed(motorSpeed); |
probst | 0:9e14f994576d | 41 | position = position + .0001; |
probst | 0:9e14f994576d | 42 | positionTwo = positionTwo - .0001; |
probst | 0:9e14f994576d | 43 | servoOne = position; |
probst | 0:9e14f994576d | 44 | servoTwo = positionTwo; |
probst | 0:9e14f994576d | 45 | |
probst | 0:9e14f994576d | 46 | |
probst | 0:9e14f994576d | 47 | wait(.00025); |
probst | 0:9e14f994576d | 48 | |
probst | 0:9e14f994576d | 49 | } |
probst | 0:9e14f994576d | 50 | |
probst | 0:9e14f994576d | 51 | //spin the servos in opposite positions backwards |
probst | 0:9e14f994576d | 52 | while(position > 0 && positionTwo < 1) |
probst | 0:9e14f994576d | 53 | { |
probst | 0:9e14f994576d | 54 | m.speed(-motorSpeed); |
probst | 0:9e14f994576d | 55 | position = position - .0001; |
probst | 0:9e14f994576d | 56 | positionTwo = positionTwo + .0001; |
probst | 0:9e14f994576d | 57 | servoOne = position; |
probst | 0:9e14f994576d | 58 | servoTwo = positionTwo; |
probst | 0:9e14f994576d | 59 | wait(.00025); |
probst | 0:9e14f994576d | 60 | |
probst | 0:9e14f994576d | 61 | } |
probst | 0:9e14f994576d | 62 | |
probst | 0:9e14f994576d | 63 | |
probst | 0:9e14f994576d | 64 | |
probst | 0:9e14f994576d | 65 | } |
probst | 0:9e14f994576d | 66 | |
probst | 0:9e14f994576d | 67 | } |