From a602f20e018113075d04b45e59aad1ee8f56c02c Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Thu, 12 Oct 2023 22:15:24 +0100 Subject: [PATCH] add comments --- Lab_1/MotorEncoderAAR.ino | 6 ++++-- Lab_1/TestEncoderAAR.c | 9 +++++++-- Lab_1/TwoSensorsAAR.c | 34 ++++++++++++++++++++-------------- Lab_1/TwoSensorsAAR.ino | 32 +++++++++++++++++++------------- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/Lab_1/MotorEncoderAAR.ino b/Lab_1/MotorEncoderAAR.ino index b4c021a..9690ec8 100644 --- a/Lab_1/MotorEncoderAAR.ino +++ b/Lab_1/MotorEncoderAAR.ino @@ -286,6 +286,7 @@ enum states get_current_state() // also speed impact probably doesn't matter much since setup() runs once per boot void initialiseEncoderStateMachine() { + // gets current state (initial) state and sets it state = get_current_state(); } @@ -294,11 +295,12 @@ void updateEncoderStateMachine() { enum states new_state = get_current_state(); // get new state, don't update state var yet so states can be compared + // check new state is valid, coming from state1, increment error count if not + // adjust count as appropriate if new state is valid + // repeat for all other state cases switch (state) { case state1: - // check new state is valid, coming from state1, increment error count if not - // adjust count as appropriate if new state is valid switch (new_state) { case state1: break; case state2: count++; break; diff --git a/Lab_1/TestEncoderAAR.c b/Lab_1/TestEncoderAAR.c index 1c3fa59..db13cea 100644 --- a/Lab_1/TestEncoderAAR.c +++ b/Lab_1/TestEncoderAAR.c @@ -70,8 +70,12 @@ enum states get_current_state() } +// doesn't have to be a fn anymore, but will be to maintain readability, expandability, +// compatibility with setup() fn +// also speed impact probably doesn't matter much since setup() runs once per boot void initialiseEncoderStateMachine() { + // gets current state (initial) state and sets it state = get_current_state(); } @@ -80,11 +84,12 @@ void updateEncoderStateMachine() { enum states new_state = get_current_state(); // get new state, don't update state yet so states can be compared + // check new state is valid, coming from state1, increment error count if not + // adjust count as appropriate if new state is valid + // repeat for all other state cases switch (state) { case state1: - // check new state is valid, coming from state1, increment error count if not - // adjust count as appropriate if new state is valid switch (new_state) { case state1: break; case state2: count++; break; diff --git a/Lab_1/TwoSensorsAAR.c b/Lab_1/TwoSensorsAAR.c index af8b85a..6e9062f 100644 --- a/Lab_1/TwoSensorsAAR.c +++ b/Lab_1/TwoSensorsAAR.c @@ -9,8 +9,8 @@ float NISTmilliVoltsToDegCKtype(float tcEMFmV); // returns temp in degC assumin float adc_to_voltage(float v_ref, int n_adc); float kelvin_to_c(float k); -float resistance_to_temperature(float r); -float voltage_to_resistance(float v); +float resistance_to_thermistor_temperature(float r); +float voltage_to_thermistor_resistance(float v); float voltage_to_erc(float v); int main() @@ -23,12 +23,12 @@ int main() scanf("%d %d", &thermistor_val, &thermocouple_val); // Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v) - thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(v_ref, thermistor_val)))); + thermistor_temp = kelvin_to_c(resistance_to_thermistor_temperature(voltage_to_thermistor_resistance(adc_to_voltage(v_ref, thermistor_val)))); // Calculate thermocouple temperature in degrees C ( Part c, i - iv) e_rc = 1000*voltage_to_erc(adc_to_voltage(v_ref, thermocouple_val)); // convert to millivolts - e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); - thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); + e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); // eqn (6) lab prep sheet + thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); // eqn (7) lab prep sheet // Output results @@ -42,7 +42,7 @@ int main() /* Write a function here to convert ADC value to voltages. (Part a, equation 1) Call it from the main() function above */ float adc_to_voltage(float v_ref, int n_adc) { - return (float)n_adc*v_ref/1024.0; + return (float)n_adc*v_ref/1024.0; // eqn (1) lab prep sheet } @@ -54,23 +54,29 @@ float kelvin_to_c(float k) { } -float resistance_to_temperature(float r) { +// Convert Resistance (Ohms) to Temperature (Kelvin) (for thermistor) +float resistance_to_thermistor_temperature(float r) { // Define Thermistor constants - float t_0 = 298.15; - float r_0 = 10000; - float b = 3975; + float t_0 = 298.15; // Kelvin + float r_0 = 10000; // Ohms + float b = 3975; // Kelvin - return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); + return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); // eqn (3) lab prep sheet } -float voltage_to_resistance(float v) { - return 1000*((10.0*3.3/v)-10.0); +// Convert Voltage (Volts) to Resistance (Ohms) +float voltage_to_thermistor_resistance(float v) { + float pull_down_resistance = 10; // kOhms + float v_hi = 3.3; // Volts + + return 1000*((pull_down_resistance*v_hi/v)-10.0); // eqn (4) lab prep sheet } +// Convert Voltage to E_RC float voltage_to_erc(float v) { - return (v-0.35)/54.4; + return (v-0.35)/54.4; // eqn (5) lab prep sheet } diff --git a/Lab_1/TwoSensorsAAR.ino b/Lab_1/TwoSensorsAAR.ino index be17051..6b15bed 100644 --- a/Lab_1/TwoSensorsAAR.ino +++ b/Lab_1/TwoSensorsAAR.ino @@ -17,7 +17,7 @@ float adc_to_voltage(int n_adc); float kelvin_to_c(float k); float resistance_to_temperature(float r); -float voltage_to_resistance(float v); +float voltage_to_thermistor_resistance(float v); float voltage_to_erc(float v); void setup() @@ -34,12 +34,12 @@ void loop() thermistor_val = analogRead(ThermistorPin); thermocouple_val = analogRead(TCpin) // Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v) - thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(thermistor_val)))); + thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_thermistor_resistance(adc_to_voltage(thermistor_val)))); // Calculate thermocouple temperature in degrees C ( Part c, i - iv) e_rc = 1000*voltage_to_erc(adc_to_voltage(thermocouple_val)); // convert to millivolts - e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); - thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); + e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); // eqn (6) lab prep sheet + thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); // eqn (7) lab prep sheet /* Display results. Don't use printf or formatting etc., they don't work on the Arduino. Just use the serial print statements given here, inserting your own code as needed */ @@ -55,7 +55,7 @@ void loop() /* Write a function to convert ADC value to voltage: put it here and use it in your code above*/ float adc_to_voltage(int n_adc) { - return (float)n_adc*V_REF/1024.0; + return (float)n_adc*V_REF/1024.0; // eqn (1) lab prep sheet } @@ -66,23 +66,29 @@ float kelvin_to_c(float k) { } -float resistance_to_temperature(float r) { +// Convert Resistance (Ohms) to Temperature (Kelvin) (for thermistor) +float resistance_to_thermistor_temperature(float r) { // Define Thermistor constants - float t_0 = 298.15; - float r_0 = 10000; - float b = 3975; + float t_0 = 298.15; // Kelvin + float r_0 = 10000; // Ohms + float b = 3975; // Kelvin - return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); + return 1.0 / ( (1.0/t_0) + (1.0/b)*log(r/r_0)); // eqn (3) lab prep sheet } -float voltage_to_resistance(float v) { - return 1000*((10.0*3.3/v)-10.0); +// Convert Voltage (Volts) to Resistance (Ohms) +float voltage_to_thermistor_resistance(float v) { + float pull_down_resistance = 10; // kOhms + float v_hi = 3.3; // Volts + + return 1000*((pull_down_resistance*v_hi/v)-10.0); // eqn (4) lab prep sheet } +// Convert Voltage to E_RC float voltage_to_erc(float v) { - return (v-0.35)/54.4; + return (v-0.35)/54.4; // eqn (5) lab prep sheet }