Hortau / Mbed 2 deprecated Tensiometer_Simulator_waterbench

Dependencies:   mbed

Committer:
Blanglois
Date:
Thu Nov 01 17:43:10 2018 +0000
Revision:
3:29925a0f88da
Parent:
2:d0308b3aaf69
Child:
4:5f2b51fa096a
Beta version

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 3:29925a0f88da 13 int saveswitch;
Blanglois 3:29925a0f88da 14 int counter = 0;
Blanglois 3:29925a0f88da 15
Blanglois 3:29925a0f88da 16 int I2Cswitch = 1;
Blanglois 3:29925a0f88da 17
Blanglois 3:29925a0f88da 18 float risetime = 40;
Blanglois 3:29925a0f88da 19 float falltime = 20;
Blanglois 3:29925a0f88da 20 float plateautime = 10;
Blanglois 3:29925a0f88da 21
Blanglois 3:29925a0f88da 22 float steptime = 5;
Blanglois 3:29925a0f88da 23
Blanglois 3:29925a0f88da 24 float highvalue = 100;
Blanglois 3:29925a0f88da 25 float lowvalue = 20;
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 modeswitch = saveswitch;
Blanglois 3:29925a0f88da 191 }
Blanglois 3:29925a0f88da 192 else if (saveswitch == 1)
Blanglois 3:29925a0f88da 193 {
Blanglois 3:29925a0f88da 194 printf("Tension rise begins\n");
Blanglois 3:29925a0f88da 195 modeswitch = saveswitch;
Blanglois 3:29925a0f88da 196 }
Blanglois 3:29925a0f88da 197 counter = 0;
Blanglois 3:29925a0f88da 198 }
Blanglois 3:29925a0f88da 199 else
Blanglois 3:29925a0f88da 200 {
Blanglois 3:29925a0f88da 201 if(modeswitch == 1)
Blanglois 3:29925a0f88da 202 {
Blanglois 3:29925a0f88da 203 printf("High plateau begins\n");
Blanglois 3:29925a0f88da 204 stepvalue = steptime * (lowvalue - highvalue) / falltime;
Blanglois 3:29925a0f88da 205 saveswitch = 2;
Blanglois 3:29925a0f88da 206 modeswitch = 3;
Blanglois 3:29925a0f88da 207 }
Blanglois 3:29925a0f88da 208 else if (modeswitch == 2)
Blanglois 3:29925a0f88da 209 {
Blanglois 3:29925a0f88da 210 printf("Low plateau begins\n");
Blanglois 3:29925a0f88da 211 stepvalue = steptime * (highvalue - lowvalue) / risetime;
Blanglois 3:29925a0f88da 212 saveswitch = 1;
Blanglois 3:29925a0f88da 213 modeswitch = 3;
Blanglois 3:29925a0f88da 214 }
Blanglois 3:29925a0f88da 215 }
Blanglois 3:29925a0f88da 216 }
Blanglois 3:29925a0f88da 217
Blanglois 3:29925a0f88da 218 void commandselect()
Blanglois 3:29925a0f88da 219 {
Blanglois 3:29925a0f88da 220 if(PcUart.readable())
Blanglois 3:29925a0f88da 221 {
Blanglois 3:29925a0f88da 222 char command = PcUart.getc();
Blanglois 3:29925a0f88da 223 switch(command)
Blanglois 3:29925a0f88da 224 {
Blanglois 3:29925a0f88da 225 case 'w':
Blanglois 3:29925a0f88da 226 {
Blanglois 3:29925a0f88da 227 I2Cswitch = 0;
Blanglois 3:29925a0f88da 228 flow_ticker.detach();
Blanglois 3:29925a0f88da 229
Blanglois 3:29925a0f88da 230 printf("Setting parameters\n");
Blanglois 3:29925a0f88da 231
Blanglois 3:29925a0f88da 232 printf("Enter tension high value in kPa\n");
Blanglois 3:29925a0f88da 233 scanf("%s", buffer);
Blanglois 3:29925a0f88da 234 highvalue = atoi(buffer);
Blanglois 3:29925a0f88da 235
Blanglois 3:29925a0f88da 236 printf("Enter tension low value in kPa\n");
Blanglois 3:29925a0f88da 237 scanf("%s", buffer);
Blanglois 3:29925a0f88da 238 lowvalue = atoi(buffer);
Blanglois 3:29925a0f88da 239
Blanglois 3:29925a0f88da 240 printf("Enter tension rise time in seconds\n");
Blanglois 3:29925a0f88da 241 scanf("%s", buffer);
Blanglois 3:29925a0f88da 242 risetime = atoi(buffer);
Blanglois 3:29925a0f88da 243
Blanglois 3:29925a0f88da 244 printf("Enter tension fall time in seconds\n");
Blanglois 3:29925a0f88da 245 scanf("%s", buffer);
Blanglois 3:29925a0f88da 246 falltime = atoi(buffer);
Blanglois 3:29925a0f88da 247
Blanglois 3:29925a0f88da 248 printf("Enter plateau time in seconds\n");
Blanglois 3:29925a0f88da 249 scanf("%s", buffer);
Blanglois 3:29925a0f88da 250 plateautime = atoi(buffer);
Blanglois 3:29925a0f88da 251
Blanglois 3:29925a0f88da 252 printf("Enter step time in seconds\n");
Blanglois 3:29925a0f88da 253 scanf("%s", buffer);
Blanglois 3:29925a0f88da 254 steptime = atoi(buffer);
Blanglois 3:29925a0f88da 255
Blanglois 3:29925a0f88da 256 printf("Resetting cycle\n");
Blanglois 3:29925a0f88da 257 counter = 0;
Blanglois 3:29925a0f88da 258 tension = lowvalue;
Blanglois 3:29925a0f88da 259 stepvalue = steptime * (highvalue - lowvalue) / risetime;
Blanglois 3:29925a0f88da 260 modeswitch = 1;
Blanglois 3:29925a0f88da 261
Blanglois 3:29925a0f88da 262 flow_ticker.attach(&flow, steptime);
Blanglois 3:29925a0f88da 263 I2Cswitch = 1;
Blanglois 3:29925a0f88da 264 break;
Blanglois 3:29925a0f88da 265 }
Blanglois 3:29925a0f88da 266
Blanglois 3:29925a0f88da 267 case 'i':
Blanglois 3:29925a0f88da 268 {
Blanglois 3:29925a0f88da 269 printf("List of parameter values\n");
Blanglois 3:29925a0f88da 270 printf("High tension: %df kPa\n", (int)highvalue);
Blanglois 3:29925a0f88da 271 printf("Low tension: %d kPa\n", (int)lowvalue);
Blanglois 3:29925a0f88da 272 printf("Cycle rise time: %d seconds\n", (int)risetime);
Blanglois 3:29925a0f88da 273 printf("Cycle fall time: %d seconds\n", (int)falltime);
Blanglois 3:29925a0f88da 274 printf("Cycle plateau time: %d seconds\n", (int)plateautime);
Blanglois 3:29925a0f88da 275 printf("Step time: %d seconds\n", (int)steptime);
Blanglois 3:29925a0f88da 276 if(modeswitch == 1)
Blanglois 3:29925a0f88da 277 printf("Cycle currently in rising phase\n");
Blanglois 3:29925a0f88da 278 else if(modeswitch == 2)
Blanglois 3:29925a0f88da 279 printf("Cycle currently in falling phase\n");
Blanglois 3:29925a0f88da 280 else if(modeswitch == 3)
Blanglois 3:29925a0f88da 281 printf("Cycle currently in plateau phase\n");
Blanglois 3:29925a0f88da 282 break;
Blanglois 3:29925a0f88da 283 }
Blanglois 3:29925a0f88da 284 }
Blanglois 3:29925a0f88da 285 }
Blanglois 3:29925a0f88da 286 }
Blanglois 3:29925a0f88da 287
Blanglois 3:29925a0f88da 288 void flow()
Blanglois 3:29925a0f88da 289 {
Blanglois 3:29925a0f88da 290 if(modeswitch == 3)
Blanglois 3:29925a0f88da 291 {
Blanglois 3:29925a0f88da 292 counter += steptime;
Blanglois 3:29925a0f88da 293
Blanglois 3:29925a0f88da 294 if(counter >= plateautime)
Blanglois 3:29925a0f88da 295 cycle();
Blanglois 3:29925a0f88da 296 }
Blanglois 3:29925a0f88da 297 else
Blanglois 3:29925a0f88da 298 {
Blanglois 3:29925a0f88da 299 tension += stepvalue;
Blanglois 3:29925a0f88da 300 setTension(tension);
Blanglois 3:29925a0f88da 301
Blanglois 3:29925a0f88da 302 if(modeswitch == 1)
Blanglois 3:29925a0f88da 303 printf("Rising, tension = %f\n", tension);
Blanglois 3:29925a0f88da 304 if(modeswitch == 2)
Blanglois 3:29925a0f88da 305 printf("Falling, tension = %f\n", tension);
Blanglois 3:29925a0f88da 306
Blanglois 3:29925a0f88da 307 if(modeswitch == 1 && tension >= highvalue - 0.0001f)
Blanglois 3:29925a0f88da 308 {
Blanglois 3:29925a0f88da 309 tension = highvalue;
Blanglois 3:29925a0f88da 310 cycle();
Blanglois 3:29925a0f88da 311 }
Blanglois 3:29925a0f88da 312 else if(modeswitch == 2 && tension <= lowvalue + 0.0001f)
Blanglois 3:29925a0f88da 313 {
Blanglois 3:29925a0f88da 314 tension = lowvalue;
Blanglois 3:29925a0f88da 315 cycle();
Blanglois 3:29925a0f88da 316 }
Blanglois 3:29925a0f88da 317 }
Blanglois 3:29925a0f88da 318 }
Blanglois 3:29925a0f88da 319
Blanglois 3:29925a0f88da 320 int main()
Blanglois 3:29925a0f88da 321 {
Blanglois 3:29925a0f88da 322 printf("Initiating\n");
Blanglois 3:29925a0f88da 323 InitI2CSlaveComm();
Blanglois 3:29925a0f88da 324 flow_ticker.attach(&flow, steptime);
Blanglois 3:29925a0f88da 325 command_ticker.attach(&commandselect, 1);
Blanglois 3:29925a0f88da 326 while(1)
Blanglois 3:29925a0f88da 327 {
Blanglois 3:29925a0f88da 328 if(I2Cswitch == 1)
Blanglois 3:29925a0f88da 329 {
Blanglois 3:29925a0f88da 330 I2CSlaveProcess();
Blanglois 3:29925a0f88da 331 }
Blanglois 3:29925a0f88da 332 }
Blanglois 0:265fff2cfb0a 333 }