
Engine Variable Advance Timing successfull code implementation
Diff: main.cpp
- Revision:
- 2:9ff654aaf923
- Parent:
- 1:e86fb31f7e95
- Child:
- 3:c89c3fa9395f
--- a/main.cpp Wed Dec 02 12:34:53 2020 +0000 +++ b/main.cpp Thu Jan 14 17:41:11 2021 +0000 @@ -1,10 +1,13 @@ +/* Konstantinos Fane + * kf289@kent.ac.uk + * ID: 20901830 + */ /* timing.cpp api verion 1.01 */ /* wajw 21/03/18 */ //V 1.02 mod to just do period measurement edh 24/03/18 /* Prog to measure period of rotation of flywheel */ - /* Software uses Pin 9 as an interrupt to determine rotation period */ /* A ticker is used to give a count of elapsed time */ @@ -15,47 +18,54 @@ #include "C12832_lcd.h" #define counts_per_min 600000 // .1ms clock = 600k pulses minute -//advance degree and RPM data arrays -static int map[] = {5,5,6,10,15,15,21,25,27,34,38}; // advance angles in degrees BTDC -static int rpm_range[] = {500,600,700,800,900,1000,1100,1200,1300,1400,1500}; //rpm range for which the advance angle should change +// Advance degree and RPM data arrays: -//Global Variables: +// advance angles in degrees BTDC +static int map[] = {5,5,6,10,15,15,21,25,27,34,38}; +// rpm range for which the advance angle should change +static int rpm_range[] = {500,600,700,800,900,1000,1100,1200,1300,1400,1500}; -volatile int period = 0, count1 = 0; //this was changed to float to display the period more precisely +// Global Variables: +volatile int period = 0, count1 = 0; volatile int pflag = 0, rpm = 0; -int lag; -int spark_advance; //to be used for the advance degree input +// Student added global variables: + +int lag; // to be used to calculate the ignition lag +int spark_advance; // to be used for the advance degree input C12832_LCD lcd; // Function Prototypes: -void Tick1_isr (void); -void TDC_isr (void); -void Spark_on (void); // set the output Spark (p10) high -void Spark_off (void); // set it low -void Ignition(int); +void Tick1_isr (void); // ISR for Ticker interrupt +void TDC_isr (void); // ISR for rising edge of TDC interrupt +void Spark_on (void); // set the output Spark (p10) high +void Spark_off (void); // set it low +void Ignition(int); -//custom function prototypes +// Custom function prototypes: + void advance_calculator(int); // Hardware Definitions: -Ticker Tick; //Tick produces a periodic interrupt -DigitalOut Spark(p10); //Spark Output to Flywheel Board -InterruptIn tdc(p9); //Pulse from TDC Sensor on Flywheel Board +Ticker Tick; // Tick produces a periodic interrupt +DigitalOut Spark(p10); // Spark Output to Flywheel Board +InterruptIn tdc(p9); // Pulse from TDC Sensor on Flywheel Board -Timeout StartSpark; //one-off interrupt to set spark o/p high -Timeout EndSpark; // and this one sets spark low +Timeout StartSpark; // one-off interrupt to set spark o/p high +Timeout EndSpark; // and this one sets spark low -//Function Declarations: +// Function Declarations: + +// triggers ignition spark based on the current advance angle provided void Ignition(int lag) { - StartSpark.attach_us(&Spark_on, lag) ; // set time of Spark - EndSpark.attach_us(&Spark_off, lag + 200); //and duration of spark at 200usec + StartSpark.attach_us(&Spark_on, lag) ; // set time of Spark + EndSpark.attach_us(&Spark_off, lag + 200); // set spark duration at 200usec } void Spark_on() @@ -68,28 +78,32 @@ Spark = 0; } - +/* Interrupt cause by Ticker + * Increments count every 100usec + */ void Tick1_isr() -//Interrupt cause by Ticker -//Increments count every 100usec { count1++; } +/* Interrupt caused by +ve edge on TDC + * Captured value = period + */ void TDC_isr() - //Interrupt caused by +ve edge on TDC - //Captured value = period { - period = count1; //Capture time since last TDC + period = count1; // Capture time since last TDC count1 = 0; - pflag = 1; // New value of period available + pflag = 1; // New value of period available } -//custom functions declaration +//Custom functions declaration: + +/* calculates the advance angle */ void advance_calculator(int rpm) { if(rpm < rpm_range[0]) { + // advance angle for any speeds lower than 500 RPM spark_advance = 360 - map[0]; } else if(rpm >= rpm_range[0] && rpm < rpm_range[1]) @@ -132,8 +146,9 @@ { spark_advance = 360 - map[9]; } - else //this computes the angle for any speeds higher than 1500RPM (still same angle as 1500RPM) + else { + // advance angle for speeds higher than 1500RPM(same angle as 1500RPM) spark_advance = 360 - map[10]; } } @@ -142,41 +157,58 @@ int main() { -//Produce regular clock tick for timing period: - Tick.attach_us(&Tick1_isr, 100); //initializes the ticker with period of 100usec - //and attaches it to Tick1 ISR + /* Produce regular clock tick for timing period: + * initializes the ticker with period of 100usec + * and attaches it to Tick1 ISR + */ + Tick.attach_us(&Tick1_isr, 100); - tdc.rise(&TDC_isr); // Generate Interrupt on each TDC pulse - - //ance_calculator(rpm); //called for the first time, since rpm is 0, 5 will be assigned to advance angle - - while(1) { + tdc.rise(&TDC_isr); // Generate Interrupt on each TDC pulse + + while(1) + { - - //spark_advance = 360 - 15; //360(TDC point) - advance angle = spark_advance + // spark advance calculation = TDC point(360) - desired advance angle + //spark_advance = 360 - 15; + + // function to calculate the spark advance for variating RPM speeds advance_calculator(rpm); - if (pflag == 1) // new timing data? - { - //lag = (period*100)*180/360; // Period is in units of 100usec - lag = (period*100)*spark_advance/360; // Period is in units of 100usec - Ignition(lag); + // checks if new timing data is available + if (pflag == 1) + { + //lag = (period*100)*180/360; // Period in units of 100usec + lag = (period*100)*spark_advance/360; // Period in units of 100usec + Ignition(lag); //triggers ignition spark for given advance angle - //your code here to calculate rpm - rpm = (1/(period*100e-6))*60; //1 period = 1*100us(period counter incrments every 100us, this conversion is needed to get the Hz frequency + /* RPM calculation command + * RPM = f(Hz) * 60 = (1/T() * 60 + * 1 period = 1*100us(period counter incrments every 100us, + * this conversion is needed to get the Hz frequency + */ + rpm = (1/(period*100e-6))*60; - lcd.cls(); //this was moved in the while to refresh the screen at every itteration + /* "lcd.cls();" was moved inside the while() + * to fix a bug in the display, by refreshing + * the screen at every itteration + */ + lcd.cls(); + + // divided by 10 to display captured period same as the osciloscope lcd.locate(0,0); - lcd.printf("Period(T): %4.2d ms", period/10); // display captured data in ms to match the osciloscope reading + lcd.printf("Period(T): %4.2d ms", period/10); + + // display the captured RPM lcd.locate(0,11); - lcd.printf("Speed(RPM): %4.2d ", rpm); // display captured data + lcd.printf("Speed(RPM): %4.2d ", rpm); - //this is used to print the advance angle for troubleshooting purposes + // display the advance angle for troubleshooting purposes + // 360 - spark advance = advance angle in BTDC (reverse operation) lcd.locate(0,22); - lcd.printf("Advance(deg): %4.2d ", 360-spark_advance); // display captured data + lcd.printf("Advance(deg): %4.2d ", 360-spark_advance); pflag = 0; //reset flag } - + } }