Program that combines a linefollower program with visible ligt communication.

Dependencies:   m3pi_custom mbed

Committer:
bertgereels
Date:
Wed May 16 19:12:20 2018 +0000
Revision:
1:243ec35fafcd
Parent:
0:1f5782fc5ca3
Part two of the 'projectlab' course.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bertgereels 0:1f5782fc5ca3 1 #include "mbed.h"
bertgereels 0:1f5782fc5ca3 2 #include "manchester.h"
bertgereels 0:1f5782fc5ca3 3
bertgereels 1:243ec35fafcd 4 DigitalIn signal(p7);
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 1:243ec35fafcd 10 Manchester::Manchester()
bertgereels 0:1f5782fc5ca3 11 {
bertgereels 0:1f5782fc5ca3 12 }
bertgereels 0:1f5782fc5ca3 13
bertgereels 1:243ec35fafcd 14 int Manchester::decode()
bertgereels 0:1f5782fc5ca3 15 {
bertgereels 0:1f5782fc5ca3 16
bertgereels 0:1f5782fc5ca3 17 int previous,current,begin,end=0;
bertgereels 1:243ec35fafcd 18 int time = 0;
bertgereels 0:1f5782fc5ca3 19 int i = 0;
bertgereels 0:1f5782fc5ca3 20 timer.start();
bertgereels 0:1f5782fc5ca3 21 while(i< DELAY) {
bertgereels 0:1f5782fc5ca3 22 previous = current;
bertgereels 0:1f5782fc5ca3 23 current = signal.read();
bertgereels 0:1f5782fc5ca3 24 if(current != previous) {
bertgereels 0:1f5782fc5ca3 25 i=0;
bertgereels 0:1f5782fc5ca3 26 }
bertgereels 0:1f5782fc5ca3 27 i++;
bertgereels 0:1f5782fc5ca3 28 wait_us(100);
bertgereels 0:1f5782fc5ca3 29 }
bertgereels 0:1f5782fc5ca3 30 i = 0;
bertgereels 0:1f5782fc5ca3 31 while(i<4) {
bertgereels 0:1f5782fc5ca3 32 end = begin;
bertgereels 0:1f5782fc5ca3 33 previous = current;
bertgereels 0:1f5782fc5ca3 34 current = signal.read();
bertgereels 0:1f5782fc5ca3 35 if(current<previous) {
bertgereels 0:1f5782fc5ca3 36 begin = timer.read_us();
bertgereels 0:1f5782fc5ca3 37 if(end != 0) {
bertgereels 0:1f5782fc5ca3 38 time +=(begin - end);
bertgereels 0:1f5782fc5ca3 39 }
bertgereels 0:1f5782fc5ca3 40 i++;
bertgereels 0:1f5782fc5ca3 41 }
bertgereels 0:1f5782fc5ca3 42
bertgereels 0:1f5782fc5ca3 43
bertgereels 0:1f5782fc5ca3 44 }
bertgereels 0:1f5782fc5ca3 45 float atime=((float)time/3000000);
bertgereels 0:1f5782fc5ca3 46 i=4;
bertgereels 0:1f5782fc5ca3 47 wait(atime/2);
bertgereels 0:1f5782fc5ca3 48
bertgereels 0:1f5782fc5ca3 49 while(i<10) {
bertgereels 0:1f5782fc5ca3 50 int x = signal.read();
bertgereels 0:1f5782fc5ca3 51 wait(atime/2);
bertgereels 0:1f5782fc5ca3 52 int y = signal.read();
bertgereels 0:1f5782fc5ca3 53 wait(atime/2);
bertgereels 0:1f5782fc5ca3 54
bertgereels 0:1f5782fc5ca3 55 if(x<y) {
bertgereels 0:1f5782fc5ca3 56 datas[i]=0;
bertgereels 0:1f5782fc5ca3 57 } else if(x>y) {
bertgereels 0:1f5782fc5ca3 58 datas[i]=1;
bertgereels 0:1f5782fc5ca3 59 }
bertgereels 0:1f5782fc5ca3 60 i++;
bertgereels 0:1f5782fc5ca3 61 }
bertgereels 0:1f5782fc5ca3 62
bertgereels 0:1f5782fc5ca3 63 wait(1);
bertgereels 0:1f5782fc5ca3 64 printf("\n\raverage time is = %f s\n\r",atime);
bertgereels 0:1f5782fc5ca3 65 printf("with frequency %f\n\r",1/atime);
bertgereels 0:1f5782fc5ca3 66 printf("data is:");
bertgereels 0:1f5782fc5ca3 67 int direction = getDirection(datas);
bertgereels 0:1f5782fc5ca3 68 for(int j = 0; j<10; j++) {
bertgereels 0:1f5782fc5ca3 69 printf("%d",datas[j]);
bertgereels 0:1f5782fc5ca3 70 }
bertgereels 0:1f5782fc5ca3 71 printf("with direction:%d",direction);
bertgereels 0:1f5782fc5ca3 72 return direction;
bertgereels 0:1f5782fc5ca3 73 }
bertgereels 0:1f5782fc5ca3 74
bertgereels 1:243ec35fafcd 75 int Manchester::getDirection(int* data)
bertgereels 0:1f5782fc5ca3 76 {
bertgereels 0:1f5782fc5ca3 77 int direction;
bertgereels 0:1f5782fc5ca3 78 if(data[4]==0) {
bertgereels 0:1f5782fc5ca3 79 if(data[5]==0) {
bertgereels 0:1f5782fc5ca3 80 direction = 0;
bertgereels 0:1f5782fc5ca3 81 } else {
bertgereels 0:1f5782fc5ca3 82 direction = 1;
bertgereels 0:1f5782fc5ca3 83 }
bertgereels 1:243ec35fafcd 84 }else {
bertgereels 0:1f5782fc5ca3 85 if(data[5]==0) {
bertgereels 0:1f5782fc5ca3 86 direction = 2;
bertgereels 0:1f5782fc5ca3 87 } else {
bertgereels 0:1f5782fc5ca3 88 direction = 3;
bertgereels 0:1f5782fc5ca3 89 }
bertgereels 0:1f5782fc5ca3 90 }
bertgereels 0:1f5782fc5ca3 91 return direction;
bertgereels 0:1f5782fc5ca3 92 }
bertgereels 1:243ec35fafcd 93
bertgereels 0:1f5782fc5ca3 94