Frank Hsu
/
MY_DC_FUCKING_DATA
why
Fork of MY_DC_FUCKING_DATA by
main.cpp@0:449c53fccaef, 2018-04-02 (annotated)
- Committer:
- enormousjj
- Date:
- Mon Apr 02 16:54:32 2018 +0000
- Revision:
- 0:449c53fccaef
- Child:
- 1:303fa4b0fa67
??,???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
enormousjj | 0:449c53fccaef | 1 | #include "mbed.h" |
enormousjj | 0:449c53fccaef | 2 | |
enormousjj | 0:449c53fccaef | 3 | ////////////////////////////////// |
enormousjj | 0:449c53fccaef | 4 | InterruptIn HallA_1(A1); |
enormousjj | 0:449c53fccaef | 5 | InterruptIn HallB_1(A2); |
enormousjj | 0:449c53fccaef | 6 | // Motor2 sensor |
enormousjj | 0:449c53fccaef | 7 | InterruptIn HallA_2(D13); |
enormousjj | 0:449c53fccaef | 8 | InterruptIn HallB_2(D12); |
enormousjj | 0:449c53fccaef | 9 | |
enormousjj | 0:449c53fccaef | 10 | /////////////////////////////////////// |
enormousjj | 0:449c53fccaef | 11 | DigitalOut led_D3(A5); |
enormousjj | 0:449c53fccaef | 12 | DigitalOut led_D4(A4); |
enormousjj | 0:449c53fccaef | 13 | /////////////////////////////////////// |
enormousjj | 0:449c53fccaef | 14 | enum State {CW, CCW}state; |
enormousjj | 0:449c53fccaef | 15 | int speed_count = 0; |
enormousjj | 0:449c53fccaef | 16 | void Speed_count(); |
enormousjj | 0:449c53fccaef | 17 | float DetectRPM_Motor_1(); |
enormousjj | 0:449c53fccaef | 18 | ///////////////////////////////// |
enormousjj | 0:449c53fccaef | 19 | Timer RPM_timer; |
enormousjj | 0:449c53fccaef | 20 | Ticker timer; |
enormousjj | 0:449c53fccaef | 21 | |
enormousjj | 0:449c53fccaef | 22 | int hallA_1 = 0; |
enormousjj | 0:449c53fccaef | 23 | int hallA_1_old = 0; |
enormousjj | 0:449c53fccaef | 24 | int hallB_1 = 0; |
enormousjj | 0:449c53fccaef | 25 | int hallB_1_old =0; |
enormousjj | 0:449c53fccaef | 26 | int hallA_2 = 0; |
enormousjj | 0:449c53fccaef | 27 | int hallA_2_old = 0; |
enormousjj | 0:449c53fccaef | 28 | int hallB_2 = 0; |
enormousjj | 0:449c53fccaef | 29 | int hallB_2_old = 0; |
enormousjj | 0:449c53fccaef | 30 | |
enormousjj | 0:449c53fccaef | 31 | //////// |
enormousjj | 0:449c53fccaef | 32 | float DetectRPM_Motor_1(); |
enormousjj | 0:449c53fccaef | 33 | void Speed_count(); |
enormousjj | 0:449c53fccaef | 34 | void HallSensor(); |
enormousjj | 0:449c53fccaef | 35 | ///////////////////////////////// |
enormousjj | 0:449c53fccaef | 36 | float DetectRPM_Motor_1() |
enormousjj | 0:449c53fccaef | 37 | { |
enormousjj | 0:449c53fccaef | 38 | float time_difference = 0; |
enormousjj | 0:449c53fccaef | 39 | RPM_timer.start(); |
enormousjj | 0:449c53fccaef | 40 | |
enormousjj | 0:449c53fccaef | 41 | while(1) |
enormousjj | 0:449c53fccaef | 42 | { |
enormousjj | 0:449c53fccaef | 43 | HallA_1.rise(&Speed_count); |
enormousjj | 0:449c53fccaef | 44 | HallA_1.fall(&Speed_count); |
enormousjj | 0:449c53fccaef | 45 | HallB_1.rise(&Speed_count); |
enormousjj | 0:449c53fccaef | 46 | HallB_1.fall(&Speed_count); |
enormousjj | 0:449c53fccaef | 47 | if(speed_count == 12) |
enormousjj | 0:449c53fccaef | 48 | { |
enormousjj | 0:449c53fccaef | 49 | speed_count = 0; |
enormousjj | 0:449c53fccaef | 50 | RPM_timer.stop(); |
enormousjj | 0:449c53fccaef | 51 | break; |
enormousjj | 0:449c53fccaef | 52 | } |
enormousjj | 0:449c53fccaef | 53 | } |
enormousjj | 0:449c53fccaef | 54 | |
enormousjj | 0:449c53fccaef | 55 | time_difference = RPM_timer.read()*1000; |
enormousjj | 0:449c53fccaef | 56 | time_difference = 60/ time_difference; |
enormousjj | 0:449c53fccaef | 57 | return time_difference; |
enormousjj | 0:449c53fccaef | 58 | |
enormousjj | 0:449c53fccaef | 59 | } |
enormousjj | 0:449c53fccaef | 60 | |
enormousjj | 0:449c53fccaef | 61 | void Speed_count() |
enormousjj | 0:449c53fccaef | 62 | { |
enormousjj | 0:449c53fccaef | 63 | speed_count++; |
enormousjj | 0:449c53fccaef | 64 | |
enormousjj | 0:449c53fccaef | 65 | } |
enormousjj | 0:449c53fccaef | 66 | void HallSensor() |
enormousjj | 0:449c53fccaef | 67 | { |
enormousjj | 0:449c53fccaef | 68 | hallA_1 = HallA_1.read(); |
enormousjj | 0:449c53fccaef | 69 | hallB_1 = HallB_1.read(); |
enormousjj | 0:449c53fccaef | 70 | //State state; |
enormousjj | 0:449c53fccaef | 71 | |
enormousjj | 0:449c53fccaef | 72 | if(hallB_1-hallB_1_old==1 && hallA_1==0) |
enormousjj | 0:449c53fccaef | 73 | { |
enormousjj | 0:449c53fccaef | 74 | state = CW; |
enormousjj | 0:449c53fccaef | 75 | led_D4=1; |
enormousjj | 0:449c53fccaef | 76 | led_D3=0; |
enormousjj | 0:449c53fccaef | 77 | } |
enormousjj | 0:449c53fccaef | 78 | else if(hallB_1-hallB_1_old==-1 && hallA_1 == 1) |
enormousjj | 0:449c53fccaef | 79 | { |
enormousjj | 0:449c53fccaef | 80 | state = CW; |
enormousjj | 0:449c53fccaef | 81 | led_D4=1; |
enormousjj | 0:449c53fccaef | 82 | led_D3=0; |
enormousjj | 0:449c53fccaef | 83 | } |
enormousjj | 0:449c53fccaef | 84 | |
enormousjj | 0:449c53fccaef | 85 | else if(hallB_1-hallB_1_old==1 && hallA_1==1) |
enormousjj | 0:449c53fccaef | 86 | { |
enormousjj | 0:449c53fccaef | 87 | state =CCW; |
enormousjj | 0:449c53fccaef | 88 | led_D3=1; |
enormousjj | 0:449c53fccaef | 89 | led_D4=0; |
enormousjj | 0:449c53fccaef | 90 | |
enormousjj | 0:449c53fccaef | 91 | } |
enormousjj | 0:449c53fccaef | 92 | else if(hallB_1-hallB_1_old==-1 && hallA_1==0) |
enormousjj | 0:449c53fccaef | 93 | { |
enormousjj | 0:449c53fccaef | 94 | state = CCW; |
enormousjj | 0:449c53fccaef | 95 | led_D3=1; |
enormousjj | 0:449c53fccaef | 96 | led_D4=0; |
enormousjj | 0:449c53fccaef | 97 | } |
enormousjj | 0:449c53fccaef | 98 | |
enormousjj | 0:449c53fccaef | 99 | |
enormousjj | 0:449c53fccaef | 100 | |
enormousjj | 0:449c53fccaef | 101 | } |
enormousjj | 0:449c53fccaef | 102 | |
enormousjj | 0:449c53fccaef | 103 | |
enormousjj | 0:449c53fccaef | 104 | int main() |
enormousjj | 0:449c53fccaef | 105 | { |
enormousjj | 0:449c53fccaef | 106 | |
enormousjj | 0:449c53fccaef | 107 | return 0; |
enormousjj | 0:449c53fccaef | 108 | |
enormousjj | 0:449c53fccaef | 109 | } |
enormousjj | 0:449c53fccaef | 110 | |
enormousjj | 0:449c53fccaef | 111 | |
enormousjj | 0:449c53fccaef | 112 | void init_timer() |
enormousjj | 0:449c53fccaef | 113 | { |
enormousjj | 0:449c53fccaef | 114 | timer.attach_us(&timer_interrupt, 10000);//10ms interrupt period (100 Hz) |
enormousjj | 0:449c53fccaef | 115 | } |