Elevator
/
Elevator
The first code
main.cpp@5:47d6ae63a1a3, 2015-02-14 (annotated)
- Committer:
- m172886
- Date:
- Sat Feb 14 02:30:41 2015 +0000
- Revision:
- 5:47d6ae63a1a3
- Parent:
- 4:059832a0c7b9
- Child:
- 6:922f762e7f92
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
170198 | 0:2f38be5ec9dc | 1 | #include "mbed.h" |
170198 | 1:a46a378d5561 | 2 | #include "time.h" |
m172886 | 3:7b0316e7cabc | 3 | #include "Motor.h" |
170198 | 0:2f38be5ec9dc | 4 | |
m172886 | 5:47d6ae63a1a3 | 5 | AnalogIn ain(p19); |
170198 | 1:a46a378d5561 | 6 | Serial pc(USBTX, USBRX); |
170198 | 1:a46a378d5561 | 7 | Timer t; |
m172886 | 3:7b0316e7cabc | 8 | Motor m(p25,p27,p28); |
m172886 | 5:47d6ae63a1a3 | 9 | |
m172886 | 3:7b0316e7cabc | 10 | float height; |
170198 | 2:559b5507c580 | 11 | |
m172886 | 3:7b0316e7cabc | 12 | float Lower_distance = 7; // lower floor in inches. |
m172886 | 3:7b0316e7cabc | 13 | |
m172886 | 5:47d6ae63a1a3 | 14 | float Upper_distance = 22; // upper floor in inches |
m172886 | 3:7b0316e7cabc | 15 | |
m172886 | 5:47d6ae63a1a3 | 16 | float Desired_distance = Upper_distance; // start low |
m172886 | 5:47d6ae63a1a3 | 17 | float err; |
m172886 | 5:47d6ae63a1a3 | 18 | |
m172886 | 5:47d6ae63a1a3 | 19 | int time_count = 0; |
m172886 | 5:47d6ae63a1a3 | 20 | int main() |
m172886 | 5:47d6ae63a1a3 | 21 | |
170198 | 1:a46a378d5561 | 22 | { |
170198 | 1:a46a378d5561 | 23 | pc.baud(9600); |
170198 | 1:a46a378d5561 | 24 | pc.format(7,SerialBase::None,1); |
m172886 | 5:47d6ae63a1a3 | 25 | t.reset(); //reset timer to zero |
m172886 | 5:47d6ae63a1a3 | 26 | t.start(); //start timer data timestamp |
m172886 | 5:47d6ae63a1a3 | 27 | |
m172886 | 5:47d6ae63a1a3 | 28 | while(1) { |
m172886 | 5:47d6ae63a1a3 | 29 | while(time_count<300) { |
m172886 | 5:47d6ae63a1a3 | 30 | |
m172886 | 5:47d6ae63a1a3 | 31 | height = 193.125176919012*(pow(ain.read(),3))-353.927724612106*(pow(ain.read(),2))+218.749286479109*(ain.read())-23.2532105680997; |
m172886 | 5:47d6ae63a1a3 | 32 | |
m172886 | 5:47d6ae63a1a3 | 33 | |
m172886 | 5:47d6ae63a1a3 | 34 | |
m172886 | 5:47d6ae63a1a3 | 35 | pc.printf("%f,",ain.read()); //this prints the voltage |
m172886 | 5:47d6ae63a1a3 | 36 | pc.printf("%f,",height); |
m172886 | 5:47d6ae63a1a3 | 37 | pc.printf("%f",t.read()); |
m172886 | 5:47d6ae63a1a3 | 38 | pc.printf("\n\r"); |
m172886 | 5:47d6ae63a1a3 | 39 | |
m172886 | 5:47d6ae63a1a3 | 40 | if(height < Desired_distance) { |
m172886 | 5:47d6ae63a1a3 | 41 | |
m172886 | 5:47d6ae63a1a3 | 42 | m.speed(0.7); |
m172886 | 5:47d6ae63a1a3 | 43 | } else if(height > Desired_distance) { |
m172886 | 5:47d6ae63a1a3 | 44 | |
m172886 | 5:47d6ae63a1a3 | 45 | m.speed(-0.7); |
m172886 | 5:47d6ae63a1a3 | 46 | } else if(height==Desired_distance) { |
m172886 | 5:47d6ae63a1a3 | 47 | |
m172886 | 5:47d6ae63a1a3 | 48 | m.speed(0.0); |
m172886 | 5:47d6ae63a1a3 | 49 | } |
m172886 | 5:47d6ae63a1a3 | 50 | |
m172886 | 5:47d6ae63a1a3 | 51 | time_count = time_count+1; |
m172886 | 4:059832a0c7b9 | 52 | } |
m172886 | 5:47d6ae63a1a3 | 53 | if (height == Desired_distance) |
m172886 | 4:059832a0c7b9 | 54 | { |
m172886 | 5:47d6ae63a1a3 | 55 | Desired_distance = Lower_distance; |
m172886 | 5:47d6ae63a1a3 | 56 | if(height < Desired_distance) { |
m172886 | 5:47d6ae63a1a3 | 57 | |
m172886 | 5:47d6ae63a1a3 | 58 | m.speed(0.7); |
m172886 | 5:47d6ae63a1a3 | 59 | } else if(height > Desired_distance) { |
m172886 | 5:47d6ae63a1a3 | 60 | |
m172886 | 5:47d6ae63a1a3 | 61 | m.speed(-0.7); |
m172886 | 5:47d6ae63a1a3 | 62 | } else if(height==Desired_distance) { |
m172886 | 5:47d6ae63a1a3 | 63 | |
m172886 | 5:47d6ae63a1a3 | 64 | m.speed(0.0); |
m172886 | 4:059832a0c7b9 | 65 | } |
m172886 | 5:47d6ae63a1a3 | 66 | } |
170198 | 1:a46a378d5561 | 67 | } |
m172886 | 5:47d6ae63a1a3 | 68 | } |