Program that combines a linefollower program with visible ligt communication.
Dependencies: m3pi_custom mbed
manchester.cpp@0:1f5782fc5ca3, 2018-05-09 (annotated)
- Committer:
- bertgereels
- Date:
- Wed May 09 15:35:31 2018 +0000
- Revision:
- 0:1f5782fc5ca3
- Child:
- 1:243ec35fafcd
this is a test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bertgereels | 0:1f5782fc5ca3 | 1 | #include "mbed.h" |
bertgereels | 0:1f5782fc5ca3 | 2 | #include "manchester.h" |
bertgereels | 0:1f5782fc5ca3 | 3 | |
bertgereels | 0:1f5782fc5ca3 | 4 | DigitalIn signal(p16); |
bertgereels | 0:1f5782fc5ca3 | 5 | Serial pc(USBTX, USBRX); |
bertgereels | 0:1f5782fc5ca3 | 6 | Timer timer; |
bertgereels | 0:1f5782fc5ca3 | 7 | int datal[] = {1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
bertgereels | 0:1f5782fc5ca3 | 8 | int datas[] = {1,1,1,1,0,0,0,0,0,0}; |
bertgereels | 0:1f5782fc5ca3 | 9 | |
bertgereels | 0:1f5782fc5ca3 | 10 | manchester::manchester() |
bertgereels | 0:1f5782fc5ca3 | 11 | { |
bertgereels | 0:1f5782fc5ca3 | 12 | |
bertgereels | 0:1f5782fc5ca3 | 13 | } |
bertgereels | 0:1f5782fc5ca3 | 14 | |
bertgereels | 0:1f5782fc5ca3 | 15 | int manchester::decode() |
bertgereels | 0:1f5782fc5ca3 | 16 | { |
bertgereels | 0:1f5782fc5ca3 | 17 | |
bertgereels | 0:1f5782fc5ca3 | 18 | int previous,current,begin,end=0; |
bertgereels | 0:1f5782fc5ca3 | 19 | int time=0; |
bertgereels | 0:1f5782fc5ca3 | 20 | int i = 0; |
bertgereels | 0:1f5782fc5ca3 | 21 | timer.start(); |
bertgereels | 0:1f5782fc5ca3 | 22 | while(i< DELAY) { |
bertgereels | 0:1f5782fc5ca3 | 23 | previous = current; |
bertgereels | 0:1f5782fc5ca3 | 24 | current = signal.read(); |
bertgereels | 0:1f5782fc5ca3 | 25 | if(current != previous) { |
bertgereels | 0:1f5782fc5ca3 | 26 | i=0; |
bertgereels | 0:1f5782fc5ca3 | 27 | } |
bertgereels | 0:1f5782fc5ca3 | 28 | i++; |
bertgereels | 0:1f5782fc5ca3 | 29 | wait_us(100); |
bertgereels | 0:1f5782fc5ca3 | 30 | } |
bertgereels | 0:1f5782fc5ca3 | 31 | i = 0; |
bertgereels | 0:1f5782fc5ca3 | 32 | while(i<4) { |
bertgereels | 0:1f5782fc5ca3 | 33 | end = begin; |
bertgereels | 0:1f5782fc5ca3 | 34 | previous = current; |
bertgereels | 0:1f5782fc5ca3 | 35 | current = signal.read(); |
bertgereels | 0:1f5782fc5ca3 | 36 | if(current<previous) { |
bertgereels | 0:1f5782fc5ca3 | 37 | begin = timer.read_us(); |
bertgereels | 0:1f5782fc5ca3 | 38 | if(end != 0) { |
bertgereels | 0:1f5782fc5ca3 | 39 | time +=(begin - end); |
bertgereels | 0:1f5782fc5ca3 | 40 | } |
bertgereels | 0:1f5782fc5ca3 | 41 | i++; |
bertgereels | 0:1f5782fc5ca3 | 42 | } |
bertgereels | 0:1f5782fc5ca3 | 43 | |
bertgereels | 0:1f5782fc5ca3 | 44 | |
bertgereels | 0:1f5782fc5ca3 | 45 | } |
bertgereels | 0:1f5782fc5ca3 | 46 | float atime=((float)time/3000000); |
bertgereels | 0:1f5782fc5ca3 | 47 | i=4; |
bertgereels | 0:1f5782fc5ca3 | 48 | wait(atime/2); |
bertgereels | 0:1f5782fc5ca3 | 49 | |
bertgereels | 0:1f5782fc5ca3 | 50 | while(i<10) { |
bertgereels | 0:1f5782fc5ca3 | 51 | int x = signal.read(); |
bertgereels | 0:1f5782fc5ca3 | 52 | wait(atime/2); |
bertgereels | 0:1f5782fc5ca3 | 53 | int y = signal.read(); |
bertgereels | 0:1f5782fc5ca3 | 54 | wait(atime/2); |
bertgereels | 0:1f5782fc5ca3 | 55 | |
bertgereels | 0:1f5782fc5ca3 | 56 | if(x<y) { |
bertgereels | 0:1f5782fc5ca3 | 57 | datas[i]=0; |
bertgereels | 0:1f5782fc5ca3 | 58 | } else if(x>y) { |
bertgereels | 0:1f5782fc5ca3 | 59 | datas[i]=1; |
bertgereels | 0:1f5782fc5ca3 | 60 | } |
bertgereels | 0:1f5782fc5ca3 | 61 | i++; |
bertgereels | 0:1f5782fc5ca3 | 62 | } |
bertgereels | 0:1f5782fc5ca3 | 63 | |
bertgereels | 0:1f5782fc5ca3 | 64 | wait(1); |
bertgereels | 0:1f5782fc5ca3 | 65 | printf("\n\raverage time is = %f s\n\r",atime); |
bertgereels | 0:1f5782fc5ca3 | 66 | printf("with frequency %f\n\r",1/atime); |
bertgereels | 0:1f5782fc5ca3 | 67 | printf("data is:"); |
bertgereels | 0:1f5782fc5ca3 | 68 | int direction = getDirection(datas); |
bertgereels | 0:1f5782fc5ca3 | 69 | for(int j = 0; j<10; j++) { |
bertgereels | 0:1f5782fc5ca3 | 70 | printf("%d",datas[j]); |
bertgereels | 0:1f5782fc5ca3 | 71 | } |
bertgereels | 0:1f5782fc5ca3 | 72 | printf("with direction:%d",direction); |
bertgereels | 0:1f5782fc5ca3 | 73 | return direction; |
bertgereels | 0:1f5782fc5ca3 | 74 | } |
bertgereels | 0:1f5782fc5ca3 | 75 | |
bertgereels | 0:1f5782fc5ca3 | 76 | int manchester::getDirection(int* data) |
bertgereels | 0:1f5782fc5ca3 | 77 | { |
bertgereels | 0:1f5782fc5ca3 | 78 | int direction; |
bertgereels | 0:1f5782fc5ca3 | 79 | if(data[4]==0) { |
bertgereels | 0:1f5782fc5ca3 | 80 | if(data[5]==0) { |
bertgereels | 0:1f5782fc5ca3 | 81 | direction = 0; |
bertgereels | 0:1f5782fc5ca3 | 82 | } else { |
bertgereels | 0:1f5782fc5ca3 | 83 | direction = 1; |
bertgereels | 0:1f5782fc5ca3 | 84 | } |
bertgereels | 0:1f5782fc5ca3 | 85 | } else { |
bertgereels | 0:1f5782fc5ca3 | 86 | if(data[5]==0) { |
bertgereels | 0:1f5782fc5ca3 | 87 | direction = 2; |
bertgereels | 0:1f5782fc5ca3 | 88 | } else { |
bertgereels | 0:1f5782fc5ca3 | 89 | direction = 3; |
bertgereels | 0:1f5782fc5ca3 | 90 | } |
bertgereels | 0:1f5782fc5ca3 | 91 | } |
bertgereels | 0:1f5782fc5ca3 | 92 | return direction; |
bertgereels | 0:1f5782fc5ca3 | 93 | } |
bertgereels | 0:1f5782fc5ca3 | 94 |