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
Diff: I2CSlaveComm.cpp
- Revision:
- 3:29925a0f88da
- Parent:
- 2:d0308b3aaf69
- Child:
- 4:5f2b51fa096a
--- a/I2CSlaveComm.cpp Wed Oct 31 16:08:34 2018 +0000 +++ b/I2CSlaveComm.cpp Thu Nov 01 17:43:10 2018 +0000 @@ -4,8 +4,33 @@ I2CSlaveCustom slave(D4, D7); I2CSlaveCustom slave2(D14, D15); //use another I2C to emulate the adc +Ticker command_ticker; +Ticker flow_ticker; + +Serial PcUart(USBTX, USBRX); + +int modeswitch = 1; +int saveswitch; +int counter = 0; + +int I2Cswitch = 1; + +float risetime = 40; +float falltime = 20; +float plateautime = 10; + +float steptime = 5; + +float highvalue = 100; +float lowvalue = 20; +float stepvalue = steptime * (highvalue - lowvalue) / risetime; + +float tension = lowvalue; + unsigned char PointOnAddress = 0; +char buffer[64]; + unsigned char ADCValue[2]; #pragma pack(push,1) struct SmartSensorStruct { @@ -42,9 +67,9 @@ void setTension(double value) { - int tension = (int)(value*100); + int tensionset = (int)(value*100); - int adc = ((1000 * (tension - stSensor.c3)) / stSensor.c2) - stSensor.c1; + int adc = ((1000 * (tensionset - stSensor.c3)) / stSensor.c2) - stSensor.c1; adc = adc / 4; //into low read of the ST ADCValue[0] = (adc >> 8)&0xFF; ADCValue[1] = (adc)&0xFF; @@ -153,4 +178,156 @@ SaveRamBuffer(0, (char*)&stSensor, sizeof(SmartSensorStruct_packed)); slave2.address(0x90); +} + +void cycle() +{ + if(modeswitch == 3 ) + { + if(saveswitch == 2) + { + printf("Tension fall begins\n"); + modeswitch = saveswitch; + } + else if (saveswitch == 1) + { + printf("Tension rise begins\n"); + modeswitch = saveswitch; + } + counter = 0; + } + else + { + if(modeswitch == 1) + { + printf("High plateau begins\n"); + stepvalue = steptime * (lowvalue - highvalue) / falltime; + saveswitch = 2; + modeswitch = 3; + } + else if (modeswitch == 2) + { + printf("Low plateau begins\n"); + stepvalue = steptime * (highvalue - lowvalue) / risetime; + saveswitch = 1; + modeswitch = 3; + } + } +} + +void commandselect() +{ + if(PcUart.readable()) + { + char command = PcUart.getc(); + switch(command) + { + case 'w': + { + I2Cswitch = 0; + flow_ticker.detach(); + + printf("Setting parameters\n"); + + printf("Enter tension high value in kPa\n"); + scanf("%s", buffer); + highvalue = atoi(buffer); + + printf("Enter tension low value in kPa\n"); + scanf("%s", buffer); + lowvalue = atoi(buffer); + + printf("Enter tension rise time in seconds\n"); + scanf("%s", buffer); + risetime = atoi(buffer); + + printf("Enter tension fall time in seconds\n"); + scanf("%s", buffer); + falltime = atoi(buffer); + + printf("Enter plateau time in seconds\n"); + scanf("%s", buffer); + plateautime = atoi(buffer); + + printf("Enter step time in seconds\n"); + scanf("%s", buffer); + steptime = atoi(buffer); + + printf("Resetting cycle\n"); + counter = 0; + tension = lowvalue; + stepvalue = steptime * (highvalue - lowvalue) / risetime; + modeswitch = 1; + + flow_ticker.attach(&flow, steptime); + I2Cswitch = 1; + break; + } + + case 'i': + { + printf("List of parameter values\n"); + printf("High tension: %df kPa\n", (int)highvalue); + printf("Low tension: %d kPa\n", (int)lowvalue); + printf("Cycle rise time: %d seconds\n", (int)risetime); + printf("Cycle fall time: %d seconds\n", (int)falltime); + printf("Cycle plateau time: %d seconds\n", (int)plateautime); + printf("Step time: %d seconds\n", (int)steptime); + if(modeswitch == 1) + printf("Cycle currently in rising phase\n"); + else if(modeswitch == 2) + printf("Cycle currently in falling phase\n"); + else if(modeswitch == 3) + printf("Cycle currently in plateau phase\n"); + break; + } + } + } +} + +void flow() +{ + if(modeswitch == 3) + { + counter += steptime; + + if(counter >= plateautime) + cycle(); + } + else + { + tension += stepvalue; + setTension(tension); + + if(modeswitch == 1) + printf("Rising, tension = %f\n", tension); + if(modeswitch == 2) + printf("Falling, tension = %f\n", tension); + + if(modeswitch == 1 && tension >= highvalue - 0.0001f) + { + tension = highvalue; + cycle(); + } + else if(modeswitch == 2 && tension <= lowvalue + 0.0001f) + { + tension = lowvalue; + cycle(); + } + } +} + +int main() +{ + printf("Initiating\n"); + InitI2CSlaveComm(); + flow_ticker.attach(&flow, steptime); + command_ticker.attach(&commandselect, 1); + while(1) + { + if(I2Cswitch == 1) + { + I2CSlaveProcess(); + } + } } \ No newline at end of file