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 Debounced PID
main.cpp
00001 #include "mbed.h" 00002 #include "DebouncedIn.h" 00003 00004 LocalFileSystem local("local"); 00005 00006 DigitalOut SysLiveLED(LED1); //LED blinking to tell operator that system is live 00007 DigitalOut TestLED1(LED2); //TestLED 00008 DigitalOut TestLED2(LED3); //TestLED 00009 DigitalOut UpRelay(p21); //Relay controlling valve to move actuator towards gear up position !!TBC for PWM out!! 00010 DigitalOut DownRelay(p22); //Relay controllinig calve to move actuator towards gear down position !!TBC for PWM out!! 00011 DebouncedIn ChangeDown(p16); //Debounced class creating digital input to invoke a gear change down 00012 DebouncedIn ChangeUp(p15); //Debounced class creating digital input to invoke a gear change up 00013 AnalogIn Position(p20); //Analogue input to read current position of actuator from POT 00014 CAN CAN1(p9, p10); //create CAN inteface under the name CAN1 00015 //PID Actuator(0.4312, 0.1, 0.0, 0.01); //(Kc, Ti, Td, Interval) PID class for control of actuator 00016 00017 00018 Ticker SysLive; //create Ticker SysLive to blink SysLiveLED 00019 00020 00021 static char ID4_data[8] = {0}; //ID4_data is identifier 0x2003 data containing - Gear, Advance (x10), Injection (x10), Fuel Con L/100km (x10) 00022 static char ID3_data[8] = {0}; //ID3_data is identifier 0x2002 data containing - Fuel Kpa, Oil temp C, Volts x 10, Fuel Con. L/Hr (x10) 00023 static char ID2_data[8] = {0}; //ID2_data is identifier 0x2001 data containing - MAP Kpa, Lamda (x100), KPH x 10, Oil P Kpa 00024 static char ID1_data[8] = {0}; //ID2_data is identifier 0x2001 data containing - RPM, TPS %, Water Temp C , Air Temp C 00025 //static int LookUp[2,100] {0}; //2D look up table of two columns (2 PWM ouputs) and 100??? rows (0-100 percent) to change PID value into desired PWM output 00026 char CurrentGear = 0; 00027 int i = 0; 00028 00029 00030 void SysLive_ISR(void); 00031 void ReadGear(void); 00032 void ChangeGearUp(void); 00033 void ChangeGearDown(void); 00034 void ChangeGearNeutral(void); 00035 00036 int main() 00037 { 00038 SysLive.attach(SysLive_ISR, 0.1); //create Ticker 00039 CAN1.frequency(1000000); 00040 00041 DownRelay = 1; 00042 UpRelay = 1; 00043 00044 static CANMessage ID4 = CANMessage(0x2003, ID4_data, 8, CANData, CANExtended); 00045 static CANMessage ID3 = CANMessage(0x2002, ID3_data, 8, CANData, CANExtended); 00046 static CANMessage ID2 = CANMessage(0x2001, ID2_data, 8, CANData, CANExtended); 00047 static CANMessage ID1 = CANMessage(0x2000, ID1_data, 8, CANData, CANExtended); 00048 00049 while(1) 00050 { 00051 if(ChangeUp.falling()) 00052 { 00053 TestLED1 = 1; 00054 wait(0.1); 00055 TestLED1 = 0; 00056 switch (CurrentGear) 00057 { 00058 case 0: 00059 case 1: 00060 case 2: 00061 case 3: 00062 case 4: 00063 case 5: 00064 ChangeGearUp(); 00065 break; 00066 case 6: 00067 CurrentGear--; 00068 break; 00069 }//end up change switch 00070 CurrentGear = CurrentGear++; 00071 }// end up change if 00072 00073 if(ChangeDown.falling()) 00074 { 00075 TestLED2 = 1; 00076 wait(0.1); 00077 TestLED2 = 0; 00078 switch (CurrentGear) 00079 { 00080 case 6: 00081 case 5: 00082 case 4: 00083 case 3: 00084 case 2: 00085 ChangeGearDown(); 00086 break; 00087 case 1: 00088 ChangeGearNeutral(); 00089 break; 00090 case 0: 00091 CurrentGear++; 00092 break; 00093 }//end down change switch 00094 CurrentGear = CurrentGear--; 00095 }// end down change if 00096 00097 }//end while loop 00098 }//end program 00099 00100 /* 00101 00102 */ 00103 void SysLive_ISR() 00104 { 00105 SysLiveLED = !SysLiveLED; 00106 } 00107 00108 void ChangeGearUp(void) 00109 { 00110 /* 00111 Testing boring stuff 00112 UpRelay = 0; 00113 wait(0.2); 00114 UpRelay = 1; 00115 DownRelay = 0; 00116 wait(0.2); 00117 DownRelay = 1; 00118 */ 00119 //Actuator.setSetPoint(GearUpEndPoint) 00120 //while((Position != GearUpEndPoint)) 00121 //{ 00122 //update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); 00123 //Actuator.compute(); 00124 //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table 00125 //} 00126 //Actuator.setSetPoint(GearCentral) 00127 //while((Position != GearCentral)) 00128 //{ 00129 ////update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); 00130 //Actuator.compute(); 00131 //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table 00132 //} 00133 } 00134 00135 void ChangeGearDown(void) 00136 { 00137 /* 00138 DownRelay = 0; 00139 wait(0.2); 00140 DownRelay = 1; 00141 UpRelay = 0; 00142 wait(0.2); 00143 UpRelay = 1; 00144 */ 00145 //Actuator.setSetPoint(GearDownEndPoint) 00146 //while((Position != GearDownEndPoint)) 00147 //{ 00148 //update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); 00149 //Actuator.compute(); 00150 //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table 00151 //} 00152 //Actuator.setSetPoint(GearCentral) 00153 //while((Position != GearCentral)) 00154 //{ 00155 ////update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); 00156 //Actuator.compute(); 00157 //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table 00158 //} 00159 } 00160 00161 void ChangeGearNeutral(void) 00162 { 00163 while (Position!=0.744) 00164 { 00165 UpRelay = 0; 00166 wait_ms(1); 00167 UpRelay = 1; 00168 wait_ms(9); 00169 } 00170 } 00171 00172 00173 void ReadGear() 00174 { 00175 00176 } 00177 00178 00179 00180 //M40 south, roundabout A34 oxford, Whitchurch, past Station Road go down Dancers Lane, left through overton, oakley come to oakley hall, on right pack lane after big white house, crossroads straight across,
Generated on Wed Jul 20 2022 15:25:44 by
1.7.2