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.
main_working1.cpp.txt@0:0d0af930b49a, 2015-03-17 (annotated)
- Committer:
- JoeMiller
- Date:
- Tue Mar 17 04:29:11 2015 +0000
- Revision:
- 0:0d0af930b49a
Working using old commutation scheme
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoeMiller | 0:0d0af930b49a | 1 | // Revision 4 PWM Added ----- WORK IN PROGRESS |
JoeMiller | 0:0d0af930b49a | 2 | |
JoeMiller | 0:0d0af930b49a | 3 | #include "mbed.h" |
JoeMiller | 0:0d0af930b49a | 4 | |
JoeMiller | 0:0d0af930b49a | 5 | |
JoeMiller | 0:0d0af930b49a | 6 | #define EncRes 256 |
JoeMiller | 0:0d0af930b49a | 7 | #define DefaultGap 1 |
JoeMiller | 0:0d0af930b49a | 8 | #define Quad0 0 |
JoeMiller | 0:0d0af930b49a | 9 | #define Quad1 EncRes/4 |
JoeMiller | 0:0d0af930b49a | 10 | #define Quad2 EncRes/2 |
JoeMiller | 0:0d0af930b49a | 11 | #define Quad3 Quad1 + Quad2 |
JoeMiller | 0:0d0af930b49a | 12 | #define Quad4 EncRes |
JoeMiller | 0:0d0af930b49a | 13 | #define Quad5 EncRes + Quad1 |
JoeMiller | 0:0d0af930b49a | 14 | |
JoeMiller | 0:0d0af930b49a | 15 | |
JoeMiller | 0:0d0af930b49a | 16 | #define OPENLOOP 0 |
JoeMiller | 0:0d0af930b49a | 17 | #define CLOSEDLOOP 1 |
JoeMiller | 0:0d0af930b49a | 18 | #define MODE CLOSEDLOOP |
JoeMiller | 0:0d0af930b49a | 19 | |
JoeMiller | 0:0d0af930b49a | 20 | //Ticker DebugDisplay; |
JoeMiller | 0:0d0af930b49a | 21 | //Serial pc(PTE0, PTE1); |
JoeMiller | 0:0d0af930b49a | 22 | |
JoeMiller | 0:0d0af930b49a | 23 | DigitalOut LEDR(PTB18); |
JoeMiller | 0:0d0af930b49a | 24 | DigitalOut Cyl_1(PTD1); |
JoeMiller | 0:0d0af930b49a | 25 | DigitalOut Cyl_2(PTD3); |
JoeMiller | 0:0d0af930b49a | 26 | DigitalOut Cyl_3(PTD2); |
JoeMiller | 0:0d0af930b49a | 27 | DigitalOut Cyl_4(PTD0); |
JoeMiller | 0:0d0af930b49a | 28 | DigitalOut Cyl_5(PTD5); |
JoeMiller | 0:0d0af930b49a | 29 | DigitalOut Cyl_6(PTA13); |
JoeMiller | 0:0d0af930b49a | 30 | DigitalOut Cyl_7(PTC9); |
JoeMiller | 0:0d0af930b49a | 31 | DigitalOut Cyl_8(PTC8); |
JoeMiller | 0:0d0af930b49a | 32 | |
JoeMiller | 0:0d0af930b49a | 33 | // User Rotary Encoder definition |
JoeMiller | 0:0d0af930b49a | 34 | /* |
JoeMiller | 0:0d0af930b49a | 35 | PwmOut LEDRed(PTA5); |
JoeMiller | 0:0d0af930b49a | 36 | PwmOut LEDGreen(PTA4); |
JoeMiller | 0:0d0af930b49a | 37 | PwmOut LEDBlue(PTA12); |
JoeMiller | 0:0d0af930b49a | 38 | InterruptIn UserEnc1A(PTD4); |
JoeMiller | 0:0d0af930b49a | 39 | DigitalIn UserEnc1B(PTA12); |
JoeMiller | 0:0d0af930b49a | 40 | DigitalIn PB1(PTC3); */ |
JoeMiller | 0:0d0af930b49a | 41 | DigitalOut ANODE(PTC6); |
JoeMiller | 0:0d0af930b49a | 42 | DigitalOut COMMON(PTC5); |
JoeMiller | 0:0d0af930b49a | 43 | |
JoeMiller | 0:0d0af930b49a | 44 | //--------------------------- |
JoeMiller | 0:0d0af930b49a | 45 | |
JoeMiller | 0:0d0af930b49a | 46 | InterruptIn CrankAngleEncA(PTA1); |
JoeMiller | 0:0d0af930b49a | 47 | DigitalIn CrankAngleEncB(PTC0); |
JoeMiller | 0:0d0af930b49a | 48 | |
JoeMiller | 0:0d0af930b49a | 49 | |
JoeMiller | 0:0d0af930b49a | 50 | Timer t; |
JoeMiller | 0:0d0af930b49a | 51 | |
JoeMiller | 0:0d0af930b49a | 52 | char CylinderFired = 0; |
JoeMiller | 0:0d0af930b49a | 53 | uint8_t CrankAngle, CrankAngleChange; |
JoeMiller | 0:0d0af930b49a | 54 | uint8_t Gap = DefaultGap; |
JoeMiller | 0:0d0af930b49a | 55 | float Throttle; |
JoeMiller | 0:0d0af930b49a | 56 | // Max Speed is currently about 15RPS, 923RPM, 65ms/rev, 254uS/Encoder pulse |
JoeMiller | 0:0d0af930b49a | 57 | // 1000us interval = 3.9RPS. 0.5RPS = 7813uS encoder pulse interval |
JoeMiller | 0:0d0af930b49a | 58 | uint16_t TargetInterval = 400; |
JoeMiller | 0:0d0af930b49a | 59 | int elapseTime; |
JoeMiller | 0:0d0af930b49a | 60 | |
JoeMiller | 0:0d0af930b49a | 61 | #define PWMPeriod 1000 |
JoeMiller | 0:0d0af930b49a | 62 | #define DefaultPWMPercent 0.5 |
JoeMiller | 0:0d0af930b49a | 63 | |
JoeMiller | 0:0d0af930b49a | 64 | //Throttle = DefaultPWMPercent; |
JoeMiller | 0:0d0af930b49a | 65 | //Cyl_1.period_us = PWMPeriod ; Cyl_1 = Throttle; |
JoeMiller | 0:0d0af930b49a | 66 | //Cyl_2.period_us = PWMPeriod ; Cyl_2 = Throttle; |
JoeMiller | 0:0d0af930b49a | 67 | //Cyl_3.period_us = PWMPeriod ; Cyl_3 = Throttle; |
JoeMiller | 0:0d0af930b49a | 68 | //Cyl_4.period_us = PWMPeriod ; Cyl_4 = Throttle; |
JoeMiller | 0:0d0af930b49a | 69 | //Cyl_5.period_us = PWMPeriod ; Cyl_5 = Throttle; |
JoeMiller | 0:0d0af930b49a | 70 | //Cyl_6.period_us = PWMPeriod ; Cyl_6 = Throttle; |
JoeMiller | 0:0d0af930b49a | 71 | //Cyl_7.period_us = PWMPeriod ; Cyl_7 = Throttle; |
JoeMiller | 0:0d0af930b49a | 72 | //Cyl_8.period_us = PWMPeriod ; Cyl_8 = Throttle; |
JoeMiller | 0:0d0af930b49a | 73 | |
JoeMiller | 0:0d0af930b49a | 74 | /* |
JoeMiller | 0:0d0af930b49a | 75 | void UserEnc1_pulse(void) |
JoeMiller | 0:0d0af930b49a | 76 | { |
JoeMiller | 0:0d0af930b49a | 77 | if (UserEnc1B && (TargetInterval < 8000)) |
JoeMiller | 0:0d0af930b49a | 78 | TargetInterval += 100; |
JoeMiller | 0:0d0af930b49a | 79 | else if (TargetInterval > 200) |
JoeMiller | 0:0d0af930b49a | 80 | TargetInterval -= 100; |
JoeMiller | 0:0d0af930b49a | 81 | |
JoeMiller | 0:0d0af930b49a | 82 | } |
JoeMiller | 0:0d0af930b49a | 83 | */ |
JoeMiller | 0:0d0af930b49a | 84 | |
JoeMiller | 0:0d0af930b49a | 85 | void CrankAngleEnc_pulse(void) |
JoeMiller | 0:0d0af930b49a | 86 | { |
JoeMiller | 0:0d0af930b49a | 87 | if (CrankAngleEncB) |
JoeMiller | 0:0d0af930b49a | 88 | CrankAngle++; |
JoeMiller | 0:0d0af930b49a | 89 | else |
JoeMiller | 0:0d0af930b49a | 90 | CrankAngle--; |
JoeMiller | 0:0d0af930b49a | 91 | //elapseTime = t.read_us(); t.reset(); |
JoeMiller | 0:0d0af930b49a | 92 | CrankAngleChange = true; |
JoeMiller | 0:0d0af930b49a | 93 | } |
JoeMiller | 0:0d0af930b49a | 94 | |
JoeMiller | 0:0d0af930b49a | 95 | |
JoeMiller | 0:0d0af930b49a | 96 | //void DispUpdate(void) { |
JoeMiller | 0:0d0af930b49a | 97 | // pc.printf("%d : %d : %d\n\r",CrankAngle,CylindersFired1,CylindersFired2);} |
JoeMiller | 0:0d0af930b49a | 98 | |
JoeMiller | 0:0d0af930b49a | 99 | int main() { |
JoeMiller | 0:0d0af930b49a | 100 | ANODE = 1 ; COMMON = 0; |
JoeMiller | 0:0d0af930b49a | 101 | LEDR = 0; |
JoeMiller | 0:0d0af930b49a | 102 | CrankAngleChange = true; |
JoeMiller | 0:0d0af930b49a | 103 | // pc.baud(57600); |
JoeMiller | 0:0d0af930b49a | 104 | // pc.printf("V8-Solenoid Engine..........."); |
JoeMiller | 0:0d0af930b49a | 105 | |
JoeMiller | 0:0d0af930b49a | 106 | |
JoeMiller | 0:0d0af930b49a | 107 | Cyl_1 = 0; Cyl_2 = 0; Cyl_3 = 0; Cyl_4 = 0; |
JoeMiller | 0:0d0af930b49a | 108 | Cyl_5 = 0; Cyl_6 = 1; Cyl_7 = 0; Cyl_8 = 0; |
JoeMiller | 0:0d0af930b49a | 109 | wait_ms(500); |
JoeMiller | 0:0d0af930b49a | 110 | Cyl_2 = 1; Cyl_5 = 1;Cyl_6 = 0; |
JoeMiller | 0:0d0af930b49a | 111 | wait_ms(1000); |
JoeMiller | 0:0d0af930b49a | 112 | CrankAngle = 0; |
JoeMiller | 0:0d0af930b49a | 113 | Cyl_2 = 0; Cyl_5 = 0; |
JoeMiller | 0:0d0af930b49a | 114 | |
JoeMiller | 0:0d0af930b49a | 115 | // DebugDisplay.attach(&DispUpdate,2.0); |
JoeMiller | 0:0d0af930b49a | 116 | CrankAngleEncA.rise(&CrankAngleEnc_pulse); |
JoeMiller | 0:0d0af930b49a | 117 | // UserEnc1A.rise(&UserEnc1_pulse); |
JoeMiller | 0:0d0af930b49a | 118 | |
JoeMiller | 0:0d0af930b49a | 119 | // pc.printf("Initialized!\n"); |
JoeMiller | 0:0d0af930b49a | 120 | |
JoeMiller | 0:0d0af930b49a | 121 | while(true) { |
JoeMiller | 0:0d0af930b49a | 122 | |
JoeMiller | 0:0d0af930b49a | 123 | if(CrankAngleChange){ |
JoeMiller | 0:0d0af930b49a | 124 | |
JoeMiller | 0:0d0af930b49a | 125 | // Pair#2 270 deg to 90 deg |
JoeMiller | 0:0d0af930b49a | 126 | // if ( (CrankAngle < ((int8_t)Quad1-Gap-Taper) ) || ( (CrankAngle<(Quad5-Gap-Taper)) && (CrankAngle>(Quad3+Gap)) ) ) |
JoeMiller | 0:0d0af930b49a | 127 | if ( (CrankAngle < (Quad1-Gap)) || (CrankAngle>(Quad3+Gap)) ) { |
JoeMiller | 0:0d0af930b49a | 128 | Cyl_1 = 0; Cyl_4 = 0; |
JoeMiller | 0:0d0af930b49a | 129 | Cyl_7 = 1; Cyl_6 = 1; |
JoeMiller | 0:0d0af930b49a | 130 | } |
JoeMiller | 0:0d0af930b49a | 131 | |
JoeMiller | 0:0d0af930b49a | 132 | //Pair#4 90 deg to 270 |
JoeMiller | 0:0d0af930b49a | 133 | else if ((CrankAngle>(Quad1+Gap)) && (CrankAngle<(Quad3-Gap))) { |
JoeMiller | 0:0d0af930b49a | 134 | Cyl_7 = 0; Cyl_6 = 0; |
JoeMiller | 0:0d0af930b49a | 135 | Cyl_4 = 1; Cyl_1 = 1; |
JoeMiller | 0:0d0af930b49a | 136 | } |
JoeMiller | 0:0d0af930b49a | 137 | else { |
JoeMiller | 0:0d0af930b49a | 138 | Cyl_1 = 0; |
JoeMiller | 0:0d0af930b49a | 139 | Cyl_4 = 0; |
JoeMiller | 0:0d0af930b49a | 140 | Cyl_6 = 0; |
JoeMiller | 0:0d0af930b49a | 141 | Cyl_7 = 0; |
JoeMiller | 0:0d0af930b49a | 142 | } |
JoeMiller | 0:0d0af930b49a | 143 | |
JoeMiller | 0:0d0af930b49a | 144 | |
JoeMiller | 0:0d0af930b49a | 145 | //Pair#3 0-180 deg |
JoeMiller | 0:0d0af930b49a | 146 | if ((CrankAngle>Gap) && (CrankAngle<(Quad2-Gap))) { |
JoeMiller | 0:0d0af930b49a | 147 | Cyl_2 = 0; Cyl_5 = 0; |
JoeMiller | 0:0d0af930b49a | 148 | Cyl_8 = 1; Cyl_3 = 1; |
JoeMiller | 0:0d0af930b49a | 149 | } |
JoeMiller | 0:0d0af930b49a | 150 | // Pair#1 180-360 deg |
JoeMiller | 0:0d0af930b49a | 151 | else if ((CrankAngle>(Quad2+Gap)) && (CrankAngle<(Quad4-Gap))) { |
JoeMiller | 0:0d0af930b49a | 152 | Cyl_3 = 0; Cyl_8 = 0; |
JoeMiller | 0:0d0af930b49a | 153 | Cyl_2 = 1; Cyl_5 = 1; |
JoeMiller | 0:0d0af930b49a | 154 | } |
JoeMiller | 0:0d0af930b49a | 155 | else { |
JoeMiller | 0:0d0af930b49a | 156 | Cyl_3 = 0; |
JoeMiller | 0:0d0af930b49a | 157 | Cyl_8 = 0; |
JoeMiller | 0:0d0af930b49a | 158 | Cyl_2 = 0; |
JoeMiller | 0:0d0af930b49a | 159 | Cyl_5 = 0; |
JoeMiller | 0:0d0af930b49a | 160 | } |
JoeMiller | 0:0d0af930b49a | 161 | |
JoeMiller | 0:0d0af930b49a | 162 | CrankAngleChange = false; |
JoeMiller | 0:0d0af930b49a | 163 | |
JoeMiller | 0:0d0af930b49a | 164 | // Speed Control Section |
JoeMiller | 0:0d0af930b49a | 165 | |
JoeMiller | 0:0d0af930b49a | 166 | if (MODE == CLOSEDLOOP) { |
JoeMiller | 0:0d0af930b49a | 167 | /* |
JoeMiller | 0:0d0af930b49a | 168 | Error = TargetInterval - elapseTime; |
JoeMiller | 0:0d0af930b49a | 169 | ErrorPrime = Error - PreviousError; |
JoeMiller | 0:0d0af930b49a | 170 | CompensationSignal = pGain*Error + dGain*ErrorPrime; |
JoeMiller | 0:0d0af930b49a | 171 | PreviousError = Error; |
JoeMiller | 0:0d0af930b49a | 172 | */ |
JoeMiller | 0:0d0af930b49a | 173 | } |
JoeMiller | 0:0d0af930b49a | 174 | else { |
JoeMiller | 0:0d0af930b49a | 175 | |
JoeMiller | 0:0d0af930b49a | 176 | Throttle = TargetInterval; |
JoeMiller | 0:0d0af930b49a | 177 | |
JoeMiller | 0:0d0af930b49a | 178 | } |
JoeMiller | 0:0d0af930b49a | 179 | } //if crankAngleChange |
JoeMiller | 0:0d0af930b49a | 180 | |
JoeMiller | 0:0d0af930b49a | 181 | |
JoeMiller | 0:0d0af930b49a | 182 | }//main loop - while(true) |
JoeMiller | 0:0d0af930b49a | 183 | }//main |
JoeMiller | 0:0d0af930b49a | 184 |