Richard Hoekstra
/
TLS2-Sensorcontroller
De sensorcontroller van het TLS2 project.
main.cpp@3:8e50fc7b82b7, 2016-11-16 (annotated)
- Committer:
- RichardHoekstra
- Date:
- Wed Nov 16 18:44:24 2016 +0000
- Revision:
- 3:8e50fc7b82b7
- Parent:
- 2:4c5952cf26d0
- Child:
- 4:512492f73e90
Added I2C support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RichardHoekstra | 0:eea3cc9d2701 | 1 | #include "mbed.h" |
RichardHoekstra | 0:eea3cc9d2701 | 2 | |
RichardHoekstra | 3:8e50fc7b82b7 | 3 | //I2C settings |
RichardHoekstra | 3:8e50fc7b82b7 | 4 | #define SDA D10 |
RichardHoekstra | 3:8e50fc7b82b7 | 5 | #define SCL D11 |
RichardHoekstra | 3:8e50fc7b82b7 | 6 | #define sensor_addr 0x93 |
RichardHoekstra | 3:8e50fc7b82b7 | 7 | #define I2C_BUFFER_SIZE 10 |
RichardHoekstra | 3:8e50fc7b82b7 | 8 | I2CSlave slave(SDA,SCL); |
RichardHoekstra | 0:eea3cc9d2701 | 9 | |
RichardHoekstra | 0:eea3cc9d2701 | 10 | float sensorVal[3] = {0}; |
RichardHoekstra | 0:eea3cc9d2701 | 11 | //Index 0: druksensor waarde |
RichardHoekstra | 0:eea3cc9d2701 | 12 | //Index 1: flowsensor waarde |
RichardHoekstra | 0:eea3cc9d2701 | 13 | //Index 2: temperatuursenor waarde |
RichardHoekstra | 0:eea3cc9d2701 | 14 | #define druksensor 0 |
RichardHoekstra | 0:eea3cc9d2701 | 15 | #define flowsensor 1 |
RichardHoekstra | 1:c9fae063e6f3 | 16 | #define tempsensor 2 |
RichardHoekstra | 1:c9fae063e6f3 | 17 | |
RichardHoekstra | 1:c9fae063e6f3 | 18 | bool pressure_is_main = true; //Determine the most important sensor as in, on which value is the motor controller regulating |
RichardHoekstra | 1:c9fae063e6f3 | 19 | bool smoothing = true; //Determine to activate the moving average |
RichardHoekstra | 1:c9fae063e6f3 | 20 | |
RichardHoekstra | 1:c9fae063e6f3 | 21 | float calc_moving_average(float val, int samples = 10){ |
RichardHoekstra | 2:4c5952cf26d0 | 22 | static float sample_arr[100] = {0}; //[0] is the newest |
RichardHoekstra | 1:c9fae063e6f3 | 23 | float moving_average = 0; |
RichardHoekstra | 2:4c5952cf26d0 | 24 | if(samples > 0 && samples < 100){ //Sanity check |
RichardHoekstra | 2:4c5952cf26d0 | 25 | //Put the new val into the sample_arr and push out the oldest one |
RichardHoekstra | 2:4c5952cf26d0 | 26 | for(int i=samples-1; i>0; i--){ |
RichardHoekstra | 2:4c5952cf26d0 | 27 | //[9]<-[8]<-[7] |
RichardHoekstra | 2:4c5952cf26d0 | 28 | sample_arr[i] = sample_arr[i-1]; |
RichardHoekstra | 2:4c5952cf26d0 | 29 | } |
RichardHoekstra | 2:4c5952cf26d0 | 30 | sample_arr[0] = val; |
RichardHoekstra | 2:4c5952cf26d0 | 31 | //Calculate the moving average |
RichardHoekstra | 2:4c5952cf26d0 | 32 | for(int i=0; i<samples; i++){ |
RichardHoekstra | 2:4c5952cf26d0 | 33 | moving_average += sample_arr[i]; |
RichardHoekstra | 2:4c5952cf26d0 | 34 | } |
RichardHoekstra | 2:4c5952cf26d0 | 35 | return moving_average/(float)samples; |
RichardHoekstra | 2:4c5952cf26d0 | 36 | } else { |
RichardHoekstra | 2:4c5952cf26d0 | 37 | return 3.1415926; //Improv error code |
RichardHoekstra | 1:c9fae063e6f3 | 38 | } |
RichardHoekstra | 1:c9fae063e6f3 | 39 | } |
RichardHoekstra | 3:8e50fc7b82b7 | 40 | //Split an integer into two char |
RichardHoekstra | 3:8e50fc7b82b7 | 41 | void int_to_2_char(char* arr, int val, int first_element = 0){ |
RichardHoekstra | 3:8e50fc7b82b7 | 42 | arr[first_element] = val>>8; |
RichardHoekstra | 3:8e50fc7b82b7 | 43 | arr[first_element+1] = val&255; |
RichardHoekstra | 3:8e50fc7b82b7 | 44 | } |
RichardHoekstra | 3:8e50fc7b82b7 | 45 | //Join two char to make an integer. |
RichardHoekstra | 3:8e50fc7b82b7 | 46 | int char2_to_int(char* arr, int first_element = 0){ |
RichardHoekstra | 3:8e50fc7b82b7 | 47 | return ((arr[first_element]<<8)+arr[first_element+1]); |
RichardHoekstra | 3:8e50fc7b82b7 | 48 | } |
RichardHoekstra | 0:eea3cc9d2701 | 49 | int main() { |
RichardHoekstra | 0:eea3cc9d2701 | 50 | //Pins |
RichardHoekstra | 0:eea3cc9d2701 | 51 | AnalogIn drukSensor(A0); |
RichardHoekstra | 0:eea3cc9d2701 | 52 | AnalogIn flowSensor(A1); |
RichardHoekstra | 0:eea3cc9d2701 | 53 | AnalogIn tempSensor(A2); |
RichardHoekstra | 0:eea3cc9d2701 | 54 | |
RichardHoekstra | 0:eea3cc9d2701 | 55 | //Sample time |
RichardHoekstra | 0:eea3cc9d2701 | 56 | int tick_ms_druksensor = 10, //100 Hz |
RichardHoekstra | 0:eea3cc9d2701 | 57 | tick_ms_flowsensor = 10, //100 Hz |
RichardHoekstra | 0:eea3cc9d2701 | 58 | tick_ms_tempsensor = 1000; //1 Hz |
RichardHoekstra | 0:eea3cc9d2701 | 59 | |
RichardHoekstra | 0:eea3cc9d2701 | 60 | //mbed ondersteund onneindig veel timers |
RichardHoekstra | 0:eea3cc9d2701 | 61 | Timer t_druk, |
RichardHoekstra | 0:eea3cc9d2701 | 62 | t_flow, |
RichardHoekstra | 0:eea3cc9d2701 | 63 | t_temp; |
RichardHoekstra | 0:eea3cc9d2701 | 64 | |
RichardHoekstra | 0:eea3cc9d2701 | 65 | t_druk.start(); |
RichardHoekstra | 0:eea3cc9d2701 | 66 | t_flow.start(); |
RichardHoekstra | 0:eea3cc9d2701 | 67 | t_temp.start(); |
RichardHoekstra | 3:8e50fc7b82b7 | 68 | int respondSensor = 0; |
RichardHoekstra | 3:8e50fc7b82b7 | 69 | char buffer[I2C_BUFFER_SIZE] = {0}; //Create the buffer for I2C |
RichardHoekstra | 3:8e50fc7b82b7 | 70 | bool buffer_changed = false; |
RichardHoekstra | 3:8e50fc7b82b7 | 71 | char data[2]; |
RichardHoekstra | 3:8e50fc7b82b7 | 72 | slave.address(sensor_addr); |
RichardHoekstra | 0:eea3cc9d2701 | 73 | while(1) { |
RichardHoekstra | 3:8e50fc7b82b7 | 74 | //I2C time |
RichardHoekstra | 3:8e50fc7b82b7 | 75 | int i = slave.receive(); |
RichardHoekstra | 3:8e50fc7b82b7 | 76 | switch (i) { |
RichardHoekstra | 3:8e50fc7b82b7 | 77 | case I2CSlave::ReadAddressed: |
RichardHoekstra | 3:8e50fc7b82b7 | 78 | //Received a request to be read |
RichardHoekstra | 3:8e50fc7b82b7 | 79 | //Irrelevant for now |
RichardHoekstra | 3:8e50fc7b82b7 | 80 | switch(respondSensor){ |
RichardHoekstra | 3:8e50fc7b82b7 | 81 | case 1: //Druksensor |
RichardHoekstra | 3:8e50fc7b82b7 | 82 | int_to_2_char(data,((int)(sensorVal[0]*65535))); |
RichardHoekstra | 3:8e50fc7b82b7 | 83 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 84 | case 2: //Flowsensor |
RichardHoekstra | 3:8e50fc7b82b7 | 85 | int_to_2_char(data,((int)(sensorVal[1]*65535))); |
RichardHoekstra | 3:8e50fc7b82b7 | 86 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 87 | case 3://Temperatuursensor |
RichardHoekstra | 3:8e50fc7b82b7 | 88 | int_to_2_char(data,((int)(sensorVal[2]*65535))); |
RichardHoekstra | 3:8e50fc7b82b7 | 89 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 90 | } |
RichardHoekstra | 3:8e50fc7b82b7 | 91 | slave.write(data,2); |
RichardHoekstra | 3:8e50fc7b82b7 | 92 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 93 | case I2CSlave::WriteGeneral: |
RichardHoekstra | 3:8e50fc7b82b7 | 94 | //Received a request to be written to |
RichardHoekstra | 3:8e50fc7b82b7 | 95 | slave.read(buffer,I2C_BUFFER_SIZE); |
RichardHoekstra | 3:8e50fc7b82b7 | 96 | buffer_changed = true; |
RichardHoekstra | 3:8e50fc7b82b7 | 97 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 98 | case I2CSlave::WriteAddressed: |
RichardHoekstra | 3:8e50fc7b82b7 | 99 | //Received a request to be written to a specific location |
RichardHoekstra | 3:8e50fc7b82b7 | 100 | slave.read(buffer,I2C_BUFFER_SIZE); |
RichardHoekstra | 3:8e50fc7b82b7 | 101 | buffer_changed = true; |
RichardHoekstra | 3:8e50fc7b82b7 | 102 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 103 | } |
RichardHoekstra | 3:8e50fc7b82b7 | 104 | if(buffer_changed == true){ |
RichardHoekstra | 3:8e50fc7b82b7 | 105 | for(int i=0;i<I2C_BUFFER_SIZE;i++){ |
RichardHoekstra | 3:8e50fc7b82b7 | 106 | buffer[i] = 0; |
RichardHoekstra | 3:8e50fc7b82b7 | 107 | } |
RichardHoekstra | 3:8e50fc7b82b7 | 108 | int command = buffer[0]; |
RichardHoekstra | 3:8e50fc7b82b7 | 109 | switch(command){ |
RichardHoekstra | 3:8e50fc7b82b7 | 110 | case 0: |
RichardHoekstra | 3:8e50fc7b82b7 | 111 | //Command is recognized |
RichardHoekstra | 3:8e50fc7b82b7 | 112 | //Write what the command is going to do |
RichardHoekstra | 3:8e50fc7b82b7 | 113 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 114 | case 1: |
RichardHoekstra | 3:8e50fc7b82b7 | 115 | //Command is recognized |
RichardHoekstra | 3:8e50fc7b82b7 | 116 | //Write what the command is going to do |
RichardHoekstra | 3:8e50fc7b82b7 | 117 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 118 | default: |
RichardHoekstra | 3:8e50fc7b82b7 | 119 | //Command is not recognied |
RichardHoekstra | 3:8e50fc7b82b7 | 120 | break; |
RichardHoekstra | 3:8e50fc7b82b7 | 121 | } |
RichardHoekstra | 3:8e50fc7b82b7 | 122 | } |
RichardHoekstra | 0:eea3cc9d2701 | 123 | if(t_druk.read_ms() >= tick_ms_druksensor){ |
RichardHoekstra | 1:c9fae063e6f3 | 124 | //Lees de druksensor uit |
RichardHoekstra | 1:c9fae063e6f3 | 125 | if(pressure_is_main == true && smoothing == true){ |
RichardHoekstra | 1:c9fae063e6f3 | 126 | calc_moving_average(drukSensor.read()); |
RichardHoekstra | 1:c9fae063e6f3 | 127 | } else { |
RichardHoekstra | 1:c9fae063e6f3 | 128 | sensorVal[druksensor] = drukSensor.read(); |
RichardHoekstra | 1:c9fae063e6f3 | 129 | } |
RichardHoekstra | 0:eea3cc9d2701 | 130 | t_druk.reset(); |
RichardHoekstra | 0:eea3cc9d2701 | 131 | } |
RichardHoekstra | 0:eea3cc9d2701 | 132 | if(t_flow.read_ms() >= tick_ms_flowsensor){ |
RichardHoekstra | 0:eea3cc9d2701 | 133 | //Lees de flowsensor uit |
RichardHoekstra | 1:c9fae063e6f3 | 134 | if(pressure_is_main == false && smoothing == true){ |
RichardHoekstra | 1:c9fae063e6f3 | 135 | calc_moving_average(flowSensor.read()); |
RichardHoekstra | 1:c9fae063e6f3 | 136 | } else { |
RichardHoekstra | 1:c9fae063e6f3 | 137 | sensorVal[flowsensor] = flowSensor.read(); |
RichardHoekstra | 1:c9fae063e6f3 | 138 | } |
RichardHoekstra | 0:eea3cc9d2701 | 139 | t_flow.reset(); |
RichardHoekstra | 0:eea3cc9d2701 | 140 | } |
RichardHoekstra | 0:eea3cc9d2701 | 141 | if(t_temp.read_ms() >= tick_ms_tempsensor){ |
RichardHoekstra | 0:eea3cc9d2701 | 142 | //Lees de temperatuursensor uit |
RichardHoekstra | 0:eea3cc9d2701 | 143 | sensorVal[tempsensor] = tempSensor.read(); |
RichardHoekstra | 0:eea3cc9d2701 | 144 | t_temp.reset(); |
RichardHoekstra | 0:eea3cc9d2701 | 145 | } |
RichardHoekstra | 0:eea3cc9d2701 | 146 | } |
RichardHoekstra | 0:eea3cc9d2701 | 147 | } |