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