Shawn Hobbs / Meshed-Motor-Monitoring
Committer:
shawnhobbs
Date:
Tue Apr 30 19:35:08 2019 +0000
Revision:
0:7f0d0acde96b
Current revision of the program.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shawnhobbs 0:7f0d0acde96b 1 #include "mbed.h"
shawnhobbs 0:7f0d0acde96b 2 #include "monitor.h"
shawnhobbs 0:7f0d0acde96b 3
shawnhobbs 0:7f0d0acde96b 4 Serial pc(USBTX, USBRX);
shawnhobbs 0:7f0d0acde96b 5 Serial coordinator(p9, p10);
shawnhobbs 0:7f0d0acde96b 6
shawnhobbs 0:7f0d0acde96b 7 xbee nodes[DEVS];
shawnhobbs 0:7f0d0acde96b 8 //globals
shawnhobbs 0:7f0d0acde96b 9 float data[DEVS][4][SAMPLES]; //array to hold all of the data for averaging
shawnhobbs 0:7f0d0acde96b 10 float avgs[DEVS][4]; //array to hold averages
shawnhobbs 0:7f0d0acde96b 11 uint16_t source; //source id of the node sending data
shawnhobbs 0:7f0d0acde96b 12 //most and least significant bytes of data
shawnhobbs 0:7f0d0acde96b 13 uint8_t MSB;
shawnhobbs 0:7f0d0acde96b 14 uint8_t LSB;
shawnhobbs 0:7f0d0acde96b 15 int num_samples = 0;
shawnhobbs 0:7f0d0acde96b 16 //functions
shawnhobbs 0:7f0d0acde96b 17 void check_nodes();
shawnhobbs 0:7f0d0acde96b 18 void average();
shawnhobbs 0:7f0d0acde96b 19 uint8_t error_check();
shawnhobbs 0:7f0d0acde96b 20
shawnhobbs 0:7f0d0acde96b 21 int main()
shawnhobbs 0:7f0d0acde96b 22 {
shawnhobbs 0:7f0d0acde96b 23 //loop instantiates and ids all of the nodes in the network
shawnhobbs 0:7f0d0acde96b 24 for(int i=0; i<DEVS; i++)
shawnhobbs 0:7f0d0acde96b 25 nodes[i].id = i+1;
shawnhobbs 0:7f0d0acde96b 26
shawnhobbs 0:7f0d0acde96b 27 //loop continually checks for data from the nodes
shawnhobbs 0:7f0d0acde96b 28 while(1) {
shawnhobbs 0:7f0d0acde96b 29 check_nodes();
shawnhobbs 0:7f0d0acde96b 30 if(num_samples==SAMPLES){
shawnhobbs 0:7f0d0acde96b 31 average();
shawnhobbs 0:7f0d0acde96b 32 if(error_check()){
shawnhobbs 0:7f0d0acde96b 33 //do something
shawnhobbs 0:7f0d0acde96b 34 }
shawnhobbs 0:7f0d0acde96b 35 num_samples = 0;
shawnhobbs 0:7f0d0acde96b 36 //wait(1);
shawnhobbs 0:7f0d0acde96b 37 }
shawnhobbs 0:7f0d0acde96b 38 num_samples++;
shawnhobbs 0:7f0d0acde96b 39 }
shawnhobbs 0:7f0d0acde96b 40 }
shawnhobbs 0:7f0d0acde96b 41
shawnhobbs 0:7f0d0acde96b 42 //retrieves a data packet and parses it, then calls the functions to calculate
shawnhobbs 0:7f0d0acde96b 43 //the data
shawnhobbs 0:7f0d0acde96b 44 void check_nodes(){
shawnhobbs 0:7f0d0acde96b 45 //check for the begining of the packet
shawnhobbs 0:7f0d0acde96b 46 if(coordinator.readable() && coordinator.getc()==0x7E){
shawnhobbs 0:7f0d0acde96b 47 //discard unecessary bytes from the packet
shawnhobbs 0:7f0d0acde96b 48 coordinator.getc(); //payload length is 2 bytes
shawnhobbs 0:7f0d0acde96b 49 coordinator.getc(); //this is always a constant so it is uneccesary
shawnhobbs 0:7f0d0acde96b 50 coordinator.getc(); //packet type (packets are always data type)
shawnhobbs 0:7f0d0acde96b 51 coordinator.getc();
shawnhobbs 0:7f0d0acde96b 52 //find the source node (-1 to adjust for indexing as coordinator
shawnhobbs 0:7f0d0acde96b 53 //is always node 0
shawnhobbs 0:7f0d0acde96b 54
shawnhobbs 0:7f0d0acde96b 55 source = coordinator.getc() - 1;
shawnhobbs 0:7f0d0acde96b 56 //pc.printf("%d\r\n", source);
shawnhobbs 0:7f0d0acde96b 57 //discard more unecesary bytes
shawnhobbs 0:7f0d0acde96b 58 for(int i=0; i<5; i++)
shawnhobbs 0:7f0d0acde96b 59 coordinator.getc();
shawnhobbs 0:7f0d0acde96b 60
shawnhobbs 0:7f0d0acde96b 61 //temperature
shawnhobbs 0:7f0d0acde96b 62 MSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 63 LSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 64 nodes[source].temp = get_temp(MSB, LSB);
shawnhobbs 0:7f0d0acde96b 65 data[source][0][num_samples] = nodes[source].temp;
shawnhobbs 0:7f0d0acde96b 66
shawnhobbs 0:7f0d0acde96b 67 //voltage
shawnhobbs 0:7f0d0acde96b 68 MSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 69 LSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 70 nodes[source].volt = get_volt(MSB, LSB);
shawnhobbs 0:7f0d0acde96b 71 data[source][1][num_samples] = nodes[source].volt;
shawnhobbs 0:7f0d0acde96b 72
shawnhobbs 0:7f0d0acde96b 73 //current
shawnhobbs 0:7f0d0acde96b 74 MSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 75 LSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 76 nodes[source].amps = get_amps(MSB, LSB);
shawnhobbs 0:7f0d0acde96b 77 data[source][2][num_samples] = nodes[source].amps;
shawnhobbs 0:7f0d0acde96b 78
shawnhobbs 0:7f0d0acde96b 79 //vibrations
shawnhobbs 0:7f0d0acde96b 80 MSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 81 LSB = coordinator.getc();
shawnhobbs 0:7f0d0acde96b 82 nodes[source].vibs = get_vibs(MSB, LSB);
shawnhobbs 0:7f0d0acde96b 83 data[source][3][num_samples] = nodes[source].vibs;
shawnhobbs 0:7f0d0acde96b 84
shawnhobbs 0:7f0d0acde96b 85 if(DEBUG){
shawnhobbs 0:7f0d0acde96b 86 //print statements
shawnhobbs 0:7f0d0acde96b 87 pc.printf("TEMP %d: %0.1fC ", nodes[source].id, nodes[source].temp);
shawnhobbs 0:7f0d0acde96b 88 pc.printf("VOLT %d: %0.1fV ", nodes[source].id, nodes[source].volt);
shawnhobbs 0:7f0d0acde96b 89 pc.printf("AMPS %d: %0.1fmA ", nodes[source].id, nodes[source].amps);
shawnhobbs 0:7f0d0acde96b 90 pc.printf("VIBS %d: %0.1fB\r\n", nodes[source].id, nodes[source].vibs);
shawnhobbs 0:7f0d0acde96b 91 }
shawnhobbs 0:7f0d0acde96b 92
shawnhobbs 0:7f0d0acde96b 93 }
shawnhobbs 0:7f0d0acde96b 94 }
shawnhobbs 0:7f0d0acde96b 95
shawnhobbs 0:7f0d0acde96b 96 //calculates the averages of the data then sends them to the gui
shawnhobbs 0:7f0d0acde96b 97 void average(){
shawnhobbs 0:7f0d0acde96b 98 for(int i=0; i<2; i++){
shawnhobbs 0:7f0d0acde96b 99 float sum = 0;
shawnhobbs 0:7f0d0acde96b 100 for(int j=0; j<SAMPLES; j++){
shawnhobbs 0:7f0d0acde96b 101 sum = sum + data[source][i][j];
shawnhobbs 0:7f0d0acde96b 102 }
shawnhobbs 0:7f0d0acde96b 103 avgs[source][i] = sum;
shawnhobbs 0:7f0d0acde96b 104 }
shawnhobbs 0:7f0d0acde96b 105 }
shawnhobbs 0:7f0d0acde96b 106
shawnhobbs 0:7f0d0acde96b 107 uint8_t error_check(){
shawnhobbs 0:7f0d0acde96b 108 return 0;
shawnhobbs 0:7f0d0acde96b 109 }