Hortau / Mbed 2 deprecated Tensiometer_Simulator_waterbench

Dependencies:   mbed

Committer:
Blanglois
Date:
Thu Nov 01 19:33:01 2018 +0000
Revision:
4:5f2b51fa096a
Parent:
3:29925a0f88da
Child:
5:1eb90dace1c7
Changed counter variable from int to float; Changed default parameters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Blanglois 0:265fff2cfb0a 1 #include "mbed.h"
Blanglois 0:265fff2cfb0a 2 #include "I2CSlaveComm.h"
Blanglois 0:265fff2cfb0a 3
eboily1 2:d0308b3aaf69 4 I2CSlaveCustom slave(D4, D7);
eboily1 2:d0308b3aaf69 5 I2CSlaveCustom slave2(D14, D15); //use another I2C to emulate the adc
Blanglois 0:265fff2cfb0a 6
Blanglois 3:29925a0f88da 7 Ticker command_ticker;
Blanglois 3:29925a0f88da 8 Ticker flow_ticker;
Blanglois 3:29925a0f88da 9
Blanglois 3:29925a0f88da 10 Serial PcUart(USBTX, USBRX);
Blanglois 3:29925a0f88da 11
Blanglois 3:29925a0f88da 12 int modeswitch = 1;
Blanglois 4:5f2b51fa096a 13 int saveswitch = 0;
Blanglois 4:5f2b51fa096a 14 float counter = 0;
Blanglois 3:29925a0f88da 15
Blanglois 3:29925a0f88da 16 int I2Cswitch = 1;
Blanglois 3:29925a0f88da 17
Blanglois 4:5f2b51fa096a 18 float risetime = 60;
Blanglois 4:5f2b51fa096a 19 float falltime = 10;
Blanglois 4:5f2b51fa096a 20 float plateautime = 5;
Blanglois 4:5f2b51fa096a 21 float steptime = 0.2;
Blanglois 3:29925a0f88da 22
Blanglois 4:5f2b51fa096a 23 float highvalue = 6;
Blanglois 4:5f2b51fa096a 24 float lowvalue = -1;
Blanglois 3:29925a0f88da 25
Blanglois 3:29925a0f88da 26 float stepvalue = steptime * (highvalue - lowvalue) / risetime;
Blanglois 3:29925a0f88da 27
Blanglois 3:29925a0f88da 28 float tension = lowvalue;
Blanglois 3:29925a0f88da 29
Blanglois 0:265fff2cfb0a 30 unsigned char PointOnAddress = 0;
eboily1 2:d0308b3aaf69 31
Blanglois 3:29925a0f88da 32 char buffer[64];
Blanglois 3:29925a0f88da 33
eboily1 2:d0308b3aaf69 34 unsigned char ADCValue[2];
Blanglois 0:265fff2cfb0a 35 #pragma pack(push,1)
Blanglois 0:265fff2cfb0a 36 struct SmartSensorStruct {
Blanglois 0:265fff2cfb0a 37 char crc; ///< Checksum CRC8
Blanglois 0:265fff2cfb0a 38 char serial[11]; ///< No Série du capteur. Doit demeurer à l'offset 1 dans la structure
Blanglois 0:265fff2cfb0a 39 char gain; ///< Gain à appliquer
Blanglois 0:265fff2cfb0a 40 char sampling_rate; ///< Vitesse de sampling
Blanglois 0:265fff2cfb0a 41 char model; ///< Model de capteur
Blanglois 0:265fff2cfb0a 42 short c1; ///< Consigne c1 de calcul
Blanglois 0:265fff2cfb0a 43 short c2; ///< Consigne c2 de calcul
Blanglois 0:265fff2cfb0a 44 short c3; ///< Consigne c3 de calcul
Blanglois 0:265fff2cfb0a 45 char depth; ///< Profondeur du capteur en mètres ou en pieds
Blanglois 0:265fff2cfb0a 46 short c4; ///< Consigne c4 de calcul
Blanglois 0:265fff2cfb0a 47 unsigned long code;///< Code de détection du type de smartSensor (Salinité ou Tension)
Blanglois 0:265fff2cfb0a 48 }SmartSensorStruct_packed;
Blanglois 0:265fff2cfb0a 49 #pragma pack(pop)
Blanglois 0:265fff2cfb0a 50
Blanglois 0:265fff2cfb0a 51 struct SmartSensorStruct stSensor;
Blanglois 0:265fff2cfb0a 52
Blanglois 0:265fff2cfb0a 53 char RAMBuffer[256]; //simulate EEPROM
Blanglois 0:265fff2cfb0a 54
Blanglois 0:265fff2cfb0a 55 void DoCRC8(char* a_crc8, char b)
Blanglois 0:265fff2cfb0a 56 {
Blanglois 0:265fff2cfb0a 57 char i, j;
Blanglois 0:265fff2cfb0a 58
Blanglois 0:265fff2cfb0a 59 for (i = 0; i < 8; b >>= 1, i++) {
Blanglois 0:265fff2cfb0a 60
Blanglois 0:265fff2cfb0a 61 j = (b ^ (*a_crc8)) & 1;
Blanglois 0:265fff2cfb0a 62 (*a_crc8) >>= 1;
Blanglois 0:265fff2cfb0a 63
Blanglois 0:265fff2cfb0a 64 if (j) (*a_crc8) ^= 0x8C;
Blanglois 0:265fff2cfb0a 65 }
Blanglois 0:265fff2cfb0a 66 }
Blanglois 0:265fff2cfb0a 67
eboily1 2:d0308b3aaf69 68 void setTension(double value)
Blanglois 0:265fff2cfb0a 69 {
Blanglois 3:29925a0f88da 70 int tensionset = (int)(value*100);
eboily1 2:d0308b3aaf69 71
Blanglois 3:29925a0f88da 72 int adc = ((1000 * (tensionset - stSensor.c3)) / stSensor.c2) - stSensor.c1;
eboily1 2:d0308b3aaf69 73 adc = adc / 4; //into low read of the ST
eboily1 2:d0308b3aaf69 74 ADCValue[0] = (adc >> 8)&0xFF;
eboily1 2:d0308b3aaf69 75 ADCValue[1] = (adc)&0xFF;
Blanglois 0:265fff2cfb0a 76 }
Blanglois 0:265fff2cfb0a 77
Blanglois 0:265fff2cfb0a 78 char ComputeCRC8(char *buff, char len, char start_data)
Blanglois 0:265fff2cfb0a 79 {
Blanglois 0:265fff2cfb0a 80 char crc8 = 0;
Blanglois 0:265fff2cfb0a 81 DoCRC8(&crc8, start_data);
Blanglois 0:265fff2cfb0a 82 while (len--) DoCRC8(&crc8, *buff++);
Blanglois 0:265fff2cfb0a 83
Blanglois 0:265fff2cfb0a 84 return crc8;
Blanglois 0:265fff2cfb0a 85 }
Blanglois 0:265fff2cfb0a 86
Blanglois 0:265fff2cfb0a 87
Blanglois 0:265fff2cfb0a 88 void SaveRamBuffer(char address, char* value, unsigned char length)
Blanglois 0:265fff2cfb0a 89 {
Blanglois 0:265fff2cfb0a 90
Blanglois 0:265fff2cfb0a 91 unsigned char i;
Blanglois 0:265fff2cfb0a 92 for(i = 0; i < length; i++)
Blanglois 0:265fff2cfb0a 93 RAMBuffer[address + i] = value[i];
Blanglois 0:265fff2cfb0a 94 }
Blanglois 0:265fff2cfb0a 95
Blanglois 0:265fff2cfb0a 96 void SaveData(char add, long value)
Blanglois 0:265fff2cfb0a 97 {
Blanglois 0:265fff2cfb0a 98 SaveRamBuffer(add, (char*)&value, 4);
Blanglois 0:265fff2cfb0a 99 }
Blanglois 0:265fff2cfb0a 100
Blanglois 0:265fff2cfb0a 101 void I2C_1Process()
Blanglois 0:265fff2cfb0a 102 {
Blanglois 0:265fff2cfb0a 103 char buf[MAX_WRITE_SIZE + 1];
Blanglois 0:265fff2cfb0a 104 int nbRx = 0;
Blanglois 0:265fff2cfb0a 105
Blanglois 0:265fff2cfb0a 106 for(int i = 0; i < MAX_WRITE_SIZE; i++) buf[i] = 0; // Clear buffer
Blanglois 0:265fff2cfb0a 107
Blanglois 0:265fff2cfb0a 108 int rx = slave.receive();
Blanglois 0:265fff2cfb0a 109
Blanglois 0:265fff2cfb0a 110 switch (rx)
Blanglois 0:265fff2cfb0a 111 {
Blanglois 0:265fff2cfb0a 112 case I2CSlave::ReadAddressed:
eboily1 1:85afd4bd4651 113 slave.write(&RAMBuffer[PointOnAddress], 0xFF - PointOnAddress);
Blanglois 0:265fff2cfb0a 114 break;
Blanglois 0:265fff2cfb0a 115 /*case I2CSlave::WriteGeneral:
Blanglois 0:265fff2cfb0a 116
Blanglois 0:265fff2cfb0a 117
Blanglois 0:265fff2cfb0a 118 break;*/
Blanglois 0:265fff2cfb0a 119 case I2CSlave::WriteAddressed:
eboily1 1:85afd4bd4651 120 int ret = slave.read(buf, 1);
Blanglois 0:265fff2cfb0a 121 PointOnAddress = buf[0];
Blanglois 0:265fff2cfb0a 122 nbRx = slave.getCount();
eboily1 1:85afd4bd4651 123 if (nbRx > 0) //to simulate write on EEPROM need to test
eboily1 1:85afd4bd4651 124 {
eboily1 1:85afd4bd4651 125 ret = slave.read(buf, nbRx);
eboily1 1:85afd4bd4651 126 SaveRamBuffer(PointOnAddress, buf, nbRx);
eboily1 1:85afd4bd4651 127 }
Blanglois 0:265fff2cfb0a 128 break;
Blanglois 0:265fff2cfb0a 129 }
Blanglois 0:265fff2cfb0a 130 }
Blanglois 0:265fff2cfb0a 131
Blanglois 0:265fff2cfb0a 132 void I2C_2Process()
Blanglois 0:265fff2cfb0a 133 {
eboily1 1:85afd4bd4651 134 char buf[MAX_WRITE_SIZE + 1];
Blanglois 0:265fff2cfb0a 135 int rx = slave2.receive();
eboily1 1:85afd4bd4651 136 int nbRx = 0;
Blanglois 0:265fff2cfb0a 137 switch (rx)
Blanglois 0:265fff2cfb0a 138 {
Blanglois 0:265fff2cfb0a 139 case I2CSlave::ReadAddressed:
eboily1 2:d0308b3aaf69 140 slave2.write((char*)&ADCValue, 2);
Blanglois 0:265fff2cfb0a 141 break;
Blanglois 0:265fff2cfb0a 142 /*case I2CSlave::WriteGeneral:
Blanglois 0:265fff2cfb0a 143
Blanglois 0:265fff2cfb0a 144
Blanglois 0:265fff2cfb0a 145 break;*/
eboily1 1:85afd4bd4651 146 case I2CSlave::WriteAddressed:
eboily1 1:85afd4bd4651 147 //to empty read buffer we do nothing with the data
eboily1 1:85afd4bd4651 148 int ret = slave2.read(buf, 1);
eboily1 2:d0308b3aaf69 149 nbRx = slave2.getCount();
eboily1 1:85afd4bd4651 150 if (nbRx > 0) //to simulate write on EEPROM need to test
eboily1 1:85afd4bd4651 151 {
eboily1 1:85afd4bd4651 152 ret = slave2.read(buf, nbRx);
eboily1 1:85afd4bd4651 153 }
eboily1 1:85afd4bd4651 154 break;
Blanglois 0:265fff2cfb0a 155 }
Blanglois 0:265fff2cfb0a 156 }
Blanglois 0:265fff2cfb0a 157
Blanglois 0:265fff2cfb0a 158 void I2CSlaveProcess()
Blanglois 0:265fff2cfb0a 159 {
Blanglois 0:265fff2cfb0a 160 I2C_1Process();
Blanglois 0:265fff2cfb0a 161 I2C_2Process();
Blanglois 0:265fff2cfb0a 162 }
Blanglois 0:265fff2cfb0a 163
Blanglois 0:265fff2cfb0a 164 void InitI2CSlaveComm()
Blanglois 0:265fff2cfb0a 165 {
Blanglois 0:265fff2cfb0a 166 slave.address(0xA0);
Blanglois 0:265fff2cfb0a 167 sprintf(stSensor.serial, "2059123456");
Blanglois 0:265fff2cfb0a 168 stSensor.gain = 0x01;
Blanglois 0:265fff2cfb0a 169 stSensor.sampling_rate = 0x03;
Blanglois 0:265fff2cfb0a 170 stSensor.c1 = -37;
Blanglois 0:265fff2cfb0a 171 stSensor.c2 = 634;
Blanglois 0:265fff2cfb0a 172 stSensor.c3 = -7;
Blanglois 0:265fff2cfb0a 173 stSensor.c4 = -1;
Blanglois 0:265fff2cfb0a 174 stSensor.depth = 0x00;
Blanglois 0:265fff2cfb0a 175 stSensor.model = 2; //Nombre de données à transmettre vers le ST
Blanglois 0:265fff2cfb0a 176 stSensor.code = 0xFFFF; //Type of sensor
Blanglois 0:265fff2cfb0a 177 stSensor.crc = ComputeCRC8(((char *)&stSensor)+1, sizeof(SmartSensorStruct_packed) - 7, 0);
Blanglois 0:265fff2cfb0a 178 SaveRamBuffer(0, (char*)&stSensor, sizeof(SmartSensorStruct_packed));
Blanglois 0:265fff2cfb0a 179
Blanglois 0:265fff2cfb0a 180 slave2.address(0x90);
Blanglois 3:29925a0f88da 181 }
Blanglois 3:29925a0f88da 182
Blanglois 3:29925a0f88da 183 void cycle()
Blanglois 3:29925a0f88da 184 {
Blanglois 3:29925a0f88da 185 if(modeswitch == 3 )
Blanglois 3:29925a0f88da 186 {
Blanglois 3:29925a0f88da 187 if(saveswitch == 2)
Blanglois 3:29925a0f88da 188 {
Blanglois 3:29925a0f88da 189 printf("Tension fall begins\n");
Blanglois 3:29925a0f88da 190 }
Blanglois 3:29925a0f88da 191 else if (saveswitch == 1)
Blanglois 3:29925a0f88da 192 {
Blanglois 3:29925a0f88da 193 printf("Tension rise begins\n");
Blanglois 3:29925a0f88da 194 }
Blanglois 4:5f2b51fa096a 195
Blanglois 3:29925a0f88da 196 counter = 0;
Blanglois 4:5f2b51fa096a 197 modeswitch = saveswitch;
Blanglois 4:5f2b51fa096a 198
Blanglois 3:29925a0f88da 199 }
Blanglois 3:29925a0f88da 200 else
Blanglois 3:29925a0f88da 201 {
Blanglois 3:29925a0f88da 202 if(modeswitch == 1)
Blanglois 3:29925a0f88da 203 {
Blanglois 3:29925a0f88da 204 printf("High plateau begins\n");
Blanglois 3:29925a0f88da 205 stepvalue = steptime * (lowvalue - highvalue) / falltime;
Blanglois 3:29925a0f88da 206 saveswitch = 2;
Blanglois 3:29925a0f88da 207 modeswitch = 3;
Blanglois 3:29925a0f88da 208 }
Blanglois 3:29925a0f88da 209 else if (modeswitch == 2)
Blanglois 3:29925a0f88da 210 {
Blanglois 3:29925a0f88da 211 printf("Low plateau begins\n");
Blanglois 3:29925a0f88da 212 stepvalue = steptime * (highvalue - lowvalue) / risetime;
Blanglois 3:29925a0f88da 213 saveswitch = 1;
Blanglois 3:29925a0f88da 214 modeswitch = 3;
Blanglois 3:29925a0f88da 215 }
Blanglois 3:29925a0f88da 216 }
Blanglois 3:29925a0f88da 217 }
Blanglois 3:29925a0f88da 218
Blanglois 3:29925a0f88da 219 void commandselect()
Blanglois 3:29925a0f88da 220 {
Blanglois 3:29925a0f88da 221 if(PcUart.readable())
Blanglois 3:29925a0f88da 222 {
Blanglois 3:29925a0f88da 223 char command = PcUart.getc();
Blanglois 3:29925a0f88da 224 switch(command)
Blanglois 3:29925a0f88da 225 {
Blanglois 3:29925a0f88da 226 case 'w':
Blanglois 3:29925a0f88da 227 {
Blanglois 3:29925a0f88da 228 I2Cswitch = 0;
Blanglois 3:29925a0f88da 229 flow_ticker.detach();
Blanglois 3:29925a0f88da 230
Blanglois 3:29925a0f88da 231 printf("Setting parameters\n");
Blanglois 3:29925a0f88da 232
Blanglois 3:29925a0f88da 233 printf("Enter tension high value in kPa\n");
Blanglois 3:29925a0f88da 234 scanf("%s", buffer);
Blanglois 3:29925a0f88da 235 highvalue = atoi(buffer);
Blanglois 3:29925a0f88da 236
Blanglois 3:29925a0f88da 237 printf("Enter tension low value in kPa\n");
Blanglois 3:29925a0f88da 238 scanf("%s", buffer);
Blanglois 3:29925a0f88da 239 lowvalue = atoi(buffer);
Blanglois 3:29925a0f88da 240
Blanglois 3:29925a0f88da 241 printf("Enter tension rise time in seconds\n");
Blanglois 3:29925a0f88da 242 scanf("%s", buffer);
Blanglois 3:29925a0f88da 243 risetime = atoi(buffer);
Blanglois 3:29925a0f88da 244
Blanglois 3:29925a0f88da 245 printf("Enter tension fall time in seconds\n");
Blanglois 3:29925a0f88da 246 scanf("%s", buffer);
Blanglois 3:29925a0f88da 247 falltime = atoi(buffer);
Blanglois 3:29925a0f88da 248
Blanglois 3:29925a0f88da 249 printf("Enter plateau time in seconds\n");
Blanglois 3:29925a0f88da 250 scanf("%s", buffer);
Blanglois 3:29925a0f88da 251 plateautime = atoi(buffer);
Blanglois 3:29925a0f88da 252
Blanglois 3:29925a0f88da 253 printf("Enter step time in seconds\n");
Blanglois 3:29925a0f88da 254 scanf("%s", buffer);
Blanglois 3:29925a0f88da 255 steptime = atoi(buffer);
Blanglois 3:29925a0f88da 256
Blanglois 3:29925a0f88da 257 printf("Resetting cycle\n");
Blanglois 3:29925a0f88da 258 counter = 0;
Blanglois 3:29925a0f88da 259 tension = lowvalue;
Blanglois 3:29925a0f88da 260 stepvalue = steptime * (highvalue - lowvalue) / risetime;
Blanglois 3:29925a0f88da 261 modeswitch = 1;
Blanglois 3:29925a0f88da 262
Blanglois 3:29925a0f88da 263 flow_ticker.attach(&flow, steptime);
Blanglois 3:29925a0f88da 264 I2Cswitch = 1;
Blanglois 3:29925a0f88da 265 break;
Blanglois 3:29925a0f88da 266 }
Blanglois 3:29925a0f88da 267
Blanglois 3:29925a0f88da 268 case 'i':
Blanglois 3:29925a0f88da 269 {
Blanglois 3:29925a0f88da 270 printf("List of parameter values\n");
Blanglois 3:29925a0f88da 271 printf("High tension: %df kPa\n", (int)highvalue);
Blanglois 3:29925a0f88da 272 printf("Low tension: %d kPa\n", (int)lowvalue);
Blanglois 3:29925a0f88da 273 printf("Cycle rise time: %d seconds\n", (int)risetime);
Blanglois 3:29925a0f88da 274 printf("Cycle fall time: %d seconds\n", (int)falltime);
Blanglois 3:29925a0f88da 275 printf("Cycle plateau time: %d seconds\n", (int)plateautime);
Blanglois 3:29925a0f88da 276 printf("Step time: %d seconds\n", (int)steptime);
Blanglois 3:29925a0f88da 277 if(modeswitch == 1)
Blanglois 3:29925a0f88da 278 printf("Cycle currently in rising phase\n");
Blanglois 3:29925a0f88da 279 else if(modeswitch == 2)
Blanglois 3:29925a0f88da 280 printf("Cycle currently in falling phase\n");
Blanglois 3:29925a0f88da 281 else if(modeswitch == 3)
Blanglois 3:29925a0f88da 282 printf("Cycle currently in plateau phase\n");
Blanglois 3:29925a0f88da 283 break;
Blanglois 3:29925a0f88da 284 }
Blanglois 3:29925a0f88da 285 }
Blanglois 3:29925a0f88da 286 }
Blanglois 3:29925a0f88da 287 }
Blanglois 3:29925a0f88da 288
Blanglois 3:29925a0f88da 289 void flow()
Blanglois 3:29925a0f88da 290 {
Blanglois 3:29925a0f88da 291 if(modeswitch == 3)
Blanglois 3:29925a0f88da 292 {
Blanglois 3:29925a0f88da 293 counter += steptime;
Blanglois 3:29925a0f88da 294
Blanglois 3:29925a0f88da 295 if(counter >= plateautime)
Blanglois 3:29925a0f88da 296 cycle();
Blanglois 3:29925a0f88da 297 }
Blanglois 3:29925a0f88da 298 else
Blanglois 3:29925a0f88da 299 {
Blanglois 3:29925a0f88da 300 tension += stepvalue;
Blanglois 3:29925a0f88da 301 setTension(tension);
Blanglois 3:29925a0f88da 302
Blanglois 3:29925a0f88da 303 if(modeswitch == 1)
Blanglois 3:29925a0f88da 304 printf("Rising, tension = %f\n", tension);
Blanglois 3:29925a0f88da 305 if(modeswitch == 2)
Blanglois 3:29925a0f88da 306 printf("Falling, tension = %f\n", tension);
Blanglois 3:29925a0f88da 307
Blanglois 4:5f2b51fa096a 308 if(modeswitch == 1 && tension >= highvalue - 0.001f)
Blanglois 3:29925a0f88da 309 {
Blanglois 3:29925a0f88da 310 tension = highvalue;
Blanglois 3:29925a0f88da 311 cycle();
Blanglois 3:29925a0f88da 312 }
Blanglois 4:5f2b51fa096a 313 else if(modeswitch == 2 && tension <= lowvalue + 0.001f)
Blanglois 3:29925a0f88da 314 {
Blanglois 3:29925a0f88da 315 tension = lowvalue;
Blanglois 3:29925a0f88da 316 cycle();
Blanglois 3:29925a0f88da 317 }
Blanglois 3:29925a0f88da 318 }
Blanglois 3:29925a0f88da 319 }
Blanglois 3:29925a0f88da 320
Blanglois 3:29925a0f88da 321 int main()
Blanglois 3:29925a0f88da 322 {
Blanglois 3:29925a0f88da 323 printf("Initiating\n");
Blanglois 3:29925a0f88da 324 InitI2CSlaveComm();
Blanglois 3:29925a0f88da 325 flow_ticker.attach(&flow, steptime);
Blanglois 3:29925a0f88da 326 command_ticker.attach(&commandselect, 1);
Blanglois 3:29925a0f88da 327 while(1)
Blanglois 3:29925a0f88da 328 {
Blanglois 3:29925a0f88da 329 if(I2Cswitch == 1)
Blanglois 3:29925a0f88da 330 {
Blanglois 3:29925a0f88da 331 I2CSlaveProcess();
Blanglois 3:29925a0f88da 332 }
Blanglois 3:29925a0f88da 333 }
Blanglois 0:265fff2cfb0a 334 }