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