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@0:158dae8711ed, 2010-11-28 (annotated)
- Committer:
- WarwickRacing
- Date:
- Sun Nov 28 14:28:36 2010 +0000
- Revision:
- 0:158dae8711ed
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| WarwickRacing | 0:158dae8711ed | 1 | #include "mbed.h" |
| WarwickRacing | 0:158dae8711ed | 2 | #include "DebouncedIn.h" |
| WarwickRacing | 0:158dae8711ed | 3 | |
| WarwickRacing | 0:158dae8711ed | 4 | LocalFileSystem local("local"); |
| WarwickRacing | 0:158dae8711ed | 5 | |
| WarwickRacing | 0:158dae8711ed | 6 | DigitalOut SysLiveLED(LED1); //LED blinking to tell operator that system is live |
| WarwickRacing | 0:158dae8711ed | 7 | DigitalOut TestLED1(LED2); //TestLED |
| WarwickRacing | 0:158dae8711ed | 8 | DigitalOut TestLED2(LED3); //TestLED |
| WarwickRacing | 0:158dae8711ed | 9 | DigitalOut UpRelay(p21); //Relay controlling valve to move actuator towards gear up position !!TBC for PWM out!! |
| WarwickRacing | 0:158dae8711ed | 10 | DigitalOut DownRelay(p22); //Relay controllinig calve to move actuator towards gear down position !!TBC for PWM out!! |
| WarwickRacing | 0:158dae8711ed | 11 | DebouncedIn ChangeDown(p16); //Debounced class creating digital input to invoke a gear change down |
| WarwickRacing | 0:158dae8711ed | 12 | DebouncedIn ChangeUp(p15); //Debounced class creating digital input to invoke a gear change up |
| WarwickRacing | 0:158dae8711ed | 13 | AnalogIn Position(p20); //Analogue input to read current position of actuator from POT |
| WarwickRacing | 0:158dae8711ed | 14 | CAN CAN1(p9, p10); //create CAN inteface under the name CAN1 |
| WarwickRacing | 0:158dae8711ed | 15 | //PID Actuator(0.4312, 0.1, 0.0, 0.01); //(Kc, Ti, Td, Interval) PID class for control of actuator |
| WarwickRacing | 0:158dae8711ed | 16 | |
| WarwickRacing | 0:158dae8711ed | 17 | |
| WarwickRacing | 0:158dae8711ed | 18 | Ticker SysLive; //create Ticker SysLive to blink SysLiveLED |
| WarwickRacing | 0:158dae8711ed | 19 | |
| WarwickRacing | 0:158dae8711ed | 20 | |
| WarwickRacing | 0:158dae8711ed | 21 | static char ID4_data[8] = {0}; //ID4_data is identifier 0x2003 data containing - Gear, Advance (x10), Injection (x10), Fuel Con L/100km (x10) |
| WarwickRacing | 0:158dae8711ed | 22 | 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) |
| WarwickRacing | 0:158dae8711ed | 23 | static char ID2_data[8] = {0}; //ID2_data is identifier 0x2001 data containing - MAP Kpa, Lamda (x100), KPH x 10, Oil P Kpa |
| WarwickRacing | 0:158dae8711ed | 24 | static char ID1_data[8] = {0}; //ID2_data is identifier 0x2001 data containing - RPM, TPS %, Water Temp C , Air Temp C |
| WarwickRacing | 0:158dae8711ed | 25 | //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 |
| WarwickRacing | 0:158dae8711ed | 26 | char CurrentGear = 0; |
| WarwickRacing | 0:158dae8711ed | 27 | int i = 0; |
| WarwickRacing | 0:158dae8711ed | 28 | |
| WarwickRacing | 0:158dae8711ed | 29 | |
| WarwickRacing | 0:158dae8711ed | 30 | void SysLive_ISR(void); |
| WarwickRacing | 0:158dae8711ed | 31 | void ReadGear(void); |
| WarwickRacing | 0:158dae8711ed | 32 | void ChangeGearUp(void); |
| WarwickRacing | 0:158dae8711ed | 33 | void ChangeGearDown(void); |
| WarwickRacing | 0:158dae8711ed | 34 | void ChangeGearNeutral(void); |
| WarwickRacing | 0:158dae8711ed | 35 | |
| WarwickRacing | 0:158dae8711ed | 36 | int main() |
| WarwickRacing | 0:158dae8711ed | 37 | { |
| WarwickRacing | 0:158dae8711ed | 38 | SysLive.attach(SysLive_ISR, 0.1); //create Ticker |
| WarwickRacing | 0:158dae8711ed | 39 | CAN1.frequency(1000000); |
| WarwickRacing | 0:158dae8711ed | 40 | |
| WarwickRacing | 0:158dae8711ed | 41 | DownRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 42 | UpRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 43 | |
| WarwickRacing | 0:158dae8711ed | 44 | static CANMessage ID4 = CANMessage(0x2003, ID4_data, 8, CANData, CANExtended); |
| WarwickRacing | 0:158dae8711ed | 45 | static CANMessage ID3 = CANMessage(0x2002, ID3_data, 8, CANData, CANExtended); |
| WarwickRacing | 0:158dae8711ed | 46 | static CANMessage ID2 = CANMessage(0x2001, ID2_data, 8, CANData, CANExtended); |
| WarwickRacing | 0:158dae8711ed | 47 | static CANMessage ID1 = CANMessage(0x2000, ID1_data, 8, CANData, CANExtended); |
| WarwickRacing | 0:158dae8711ed | 48 | |
| WarwickRacing | 0:158dae8711ed | 49 | while(1) |
| WarwickRacing | 0:158dae8711ed | 50 | { |
| WarwickRacing | 0:158dae8711ed | 51 | if(ChangeUp.falling()) |
| WarwickRacing | 0:158dae8711ed | 52 | { |
| WarwickRacing | 0:158dae8711ed | 53 | TestLED1 = 1; |
| WarwickRacing | 0:158dae8711ed | 54 | wait(0.1); |
| WarwickRacing | 0:158dae8711ed | 55 | TestLED1 = 0; |
| WarwickRacing | 0:158dae8711ed | 56 | switch (CurrentGear) |
| WarwickRacing | 0:158dae8711ed | 57 | { |
| WarwickRacing | 0:158dae8711ed | 58 | case 0: |
| WarwickRacing | 0:158dae8711ed | 59 | case 1: |
| WarwickRacing | 0:158dae8711ed | 60 | case 2: |
| WarwickRacing | 0:158dae8711ed | 61 | case 3: |
| WarwickRacing | 0:158dae8711ed | 62 | case 4: |
| WarwickRacing | 0:158dae8711ed | 63 | case 5: |
| WarwickRacing | 0:158dae8711ed | 64 | ChangeGearUp(); |
| WarwickRacing | 0:158dae8711ed | 65 | break; |
| WarwickRacing | 0:158dae8711ed | 66 | case 6: |
| WarwickRacing | 0:158dae8711ed | 67 | CurrentGear--; |
| WarwickRacing | 0:158dae8711ed | 68 | break; |
| WarwickRacing | 0:158dae8711ed | 69 | }//end up change switch |
| WarwickRacing | 0:158dae8711ed | 70 | CurrentGear = CurrentGear++; |
| WarwickRacing | 0:158dae8711ed | 71 | }// end up change if |
| WarwickRacing | 0:158dae8711ed | 72 | |
| WarwickRacing | 0:158dae8711ed | 73 | if(ChangeDown.falling()) |
| WarwickRacing | 0:158dae8711ed | 74 | { |
| WarwickRacing | 0:158dae8711ed | 75 | TestLED2 = 1; |
| WarwickRacing | 0:158dae8711ed | 76 | wait(0.1); |
| WarwickRacing | 0:158dae8711ed | 77 | TestLED2 = 0; |
| WarwickRacing | 0:158dae8711ed | 78 | switch (CurrentGear) |
| WarwickRacing | 0:158dae8711ed | 79 | { |
| WarwickRacing | 0:158dae8711ed | 80 | case 6: |
| WarwickRacing | 0:158dae8711ed | 81 | case 5: |
| WarwickRacing | 0:158dae8711ed | 82 | case 4: |
| WarwickRacing | 0:158dae8711ed | 83 | case 3: |
| WarwickRacing | 0:158dae8711ed | 84 | case 2: |
| WarwickRacing | 0:158dae8711ed | 85 | ChangeGearDown(); |
| WarwickRacing | 0:158dae8711ed | 86 | break; |
| WarwickRacing | 0:158dae8711ed | 87 | case 1: |
| WarwickRacing | 0:158dae8711ed | 88 | ChangeGearNeutral(); |
| WarwickRacing | 0:158dae8711ed | 89 | break; |
| WarwickRacing | 0:158dae8711ed | 90 | case 0: |
| WarwickRacing | 0:158dae8711ed | 91 | CurrentGear++; |
| WarwickRacing | 0:158dae8711ed | 92 | break; |
| WarwickRacing | 0:158dae8711ed | 93 | }//end down change switch |
| WarwickRacing | 0:158dae8711ed | 94 | CurrentGear = CurrentGear--; |
| WarwickRacing | 0:158dae8711ed | 95 | }// end down change if |
| WarwickRacing | 0:158dae8711ed | 96 | |
| WarwickRacing | 0:158dae8711ed | 97 | }//end while loop |
| WarwickRacing | 0:158dae8711ed | 98 | }//end program |
| WarwickRacing | 0:158dae8711ed | 99 | |
| WarwickRacing | 0:158dae8711ed | 100 | /* |
| WarwickRacing | 0:158dae8711ed | 101 | |
| WarwickRacing | 0:158dae8711ed | 102 | */ |
| WarwickRacing | 0:158dae8711ed | 103 | void SysLive_ISR() |
| WarwickRacing | 0:158dae8711ed | 104 | { |
| WarwickRacing | 0:158dae8711ed | 105 | SysLiveLED = !SysLiveLED; |
| WarwickRacing | 0:158dae8711ed | 106 | } |
| WarwickRacing | 0:158dae8711ed | 107 | |
| WarwickRacing | 0:158dae8711ed | 108 | void ChangeGearUp(void) |
| WarwickRacing | 0:158dae8711ed | 109 | { |
| WarwickRacing | 0:158dae8711ed | 110 | /* |
| WarwickRacing | 0:158dae8711ed | 111 | Testing boring stuff |
| WarwickRacing | 0:158dae8711ed | 112 | UpRelay = 0; |
| WarwickRacing | 0:158dae8711ed | 113 | wait(0.2); |
| WarwickRacing | 0:158dae8711ed | 114 | UpRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 115 | DownRelay = 0; |
| WarwickRacing | 0:158dae8711ed | 116 | wait(0.2); |
| WarwickRacing | 0:158dae8711ed | 117 | DownRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 118 | */ |
| WarwickRacing | 0:158dae8711ed | 119 | //Actuator.setSetPoint(GearUpEndPoint) |
| WarwickRacing | 0:158dae8711ed | 120 | //while((Position != GearUpEndPoint)) |
| WarwickRacing | 0:158dae8711ed | 121 | //{ |
| WarwickRacing | 0:158dae8711ed | 122 | //update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); |
| WarwickRacing | 0:158dae8711ed | 123 | //Actuator.compute(); |
| WarwickRacing | 0:158dae8711ed | 124 | //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table |
| WarwickRacing | 0:158dae8711ed | 125 | //} |
| WarwickRacing | 0:158dae8711ed | 126 | //Actuator.setSetPoint(GearCentral) |
| WarwickRacing | 0:158dae8711ed | 127 | //while((Position != GearCentral)) |
| WarwickRacing | 0:158dae8711ed | 128 | //{ |
| WarwickRacing | 0:158dae8711ed | 129 | ////update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); |
| WarwickRacing | 0:158dae8711ed | 130 | //Actuator.compute(); |
| WarwickRacing | 0:158dae8711ed | 131 | //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table |
| WarwickRacing | 0:158dae8711ed | 132 | //} |
| WarwickRacing | 0:158dae8711ed | 133 | } |
| WarwickRacing | 0:158dae8711ed | 134 | |
| WarwickRacing | 0:158dae8711ed | 135 | void ChangeGearDown(void) |
| WarwickRacing | 0:158dae8711ed | 136 | { |
| WarwickRacing | 0:158dae8711ed | 137 | /* |
| WarwickRacing | 0:158dae8711ed | 138 | DownRelay = 0; |
| WarwickRacing | 0:158dae8711ed | 139 | wait(0.2); |
| WarwickRacing | 0:158dae8711ed | 140 | DownRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 141 | UpRelay = 0; |
| WarwickRacing | 0:158dae8711ed | 142 | wait(0.2); |
| WarwickRacing | 0:158dae8711ed | 143 | UpRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 144 | */ |
| WarwickRacing | 0:158dae8711ed | 145 | //Actuator.setSetPoint(GearDownEndPoint) |
| WarwickRacing | 0:158dae8711ed | 146 | //while((Position != GearDownEndPoint)) |
| WarwickRacing | 0:158dae8711ed | 147 | //{ |
| WarwickRacing | 0:158dae8711ed | 148 | //update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); |
| WarwickRacing | 0:158dae8711ed | 149 | //Actuator.compute(); |
| WarwickRacing | 0:158dae8711ed | 150 | //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table |
| WarwickRacing | 0:158dae8711ed | 151 | //} |
| WarwickRacing | 0:158dae8711ed | 152 | //Actuator.setSetPoint(GearCentral) |
| WarwickRacing | 0:158dae8711ed | 153 | //while((Position != GearCentral)) |
| WarwickRacing | 0:158dae8711ed | 154 | //{ |
| WarwickRacing | 0:158dae8711ed | 155 | ////update current process value i.e. call the Actuator.setProcessValue(fabs(Position)); |
| WarwickRacing | 0:158dae8711ed | 156 | //Actuator.compute(); |
| WarwickRacing | 0:158dae8711ed | 157 | //Call the PWM output to reflect the new desired PID value by look up this PID value (%) against the lookup table |
| WarwickRacing | 0:158dae8711ed | 158 | //} |
| WarwickRacing | 0:158dae8711ed | 159 | } |
| WarwickRacing | 0:158dae8711ed | 160 | |
| WarwickRacing | 0:158dae8711ed | 161 | void ChangeGearNeutral(void) |
| WarwickRacing | 0:158dae8711ed | 162 | { |
| WarwickRacing | 0:158dae8711ed | 163 | while (Position!=0.744) |
| WarwickRacing | 0:158dae8711ed | 164 | { |
| WarwickRacing | 0:158dae8711ed | 165 | UpRelay = 0; |
| WarwickRacing | 0:158dae8711ed | 166 | wait_ms(1); |
| WarwickRacing | 0:158dae8711ed | 167 | UpRelay = 1; |
| WarwickRacing | 0:158dae8711ed | 168 | wait_ms(9); |
| WarwickRacing | 0:158dae8711ed | 169 | } |
| WarwickRacing | 0:158dae8711ed | 170 | } |
| WarwickRacing | 0:158dae8711ed | 171 | |
| WarwickRacing | 0:158dae8711ed | 172 | |
| WarwickRacing | 0:158dae8711ed | 173 | void ReadGear() |
| WarwickRacing | 0:158dae8711ed | 174 | { |
| WarwickRacing | 0:158dae8711ed | 175 | |
| WarwickRacing | 0:158dae8711ed | 176 | } |
| WarwickRacing | 0:158dae8711ed | 177 | |
| WarwickRacing | 0:158dae8711ed | 178 | |
| WarwickRacing | 0:158dae8711ed | 179 | |
| WarwickRacing | 0:158dae8711ed | 180 | //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, |