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.
Diff: main_working3.cpp.txt
- Revision:
- 0:0d0af930b49a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main_working3.cpp.txt Tue Mar 17 04:29:11 2015 +0000 @@ -0,0 +1,249 @@ +// Revision 4 PWM Added ----- WORK IN PROGRESS + +#include "mbed.h" + + +#define EncRes 256 +#define DefaultGap 1 +#define Quad0 0 +#define Quad1 EncRes/4 +#define Quad2 EncRes/2 +#define Quad3 Quad1 + Quad2 +#define Quad4 EncRes +#define Quad5 EncRes + Quad1 + + +#define OPENLOOP 0 +#define CLOSEDLOOP 1 +#define MODE CLOSEDLOOP +#define PWMPeriod 500 +#define DefaultPWMPercent 0.25f + +#define UPDATERATE 0.100 +#define UPPER 2.0 +#define LOWER 1.4 +#define LOWER_OUT = (10^LOWER)/100 // ~.251 +#define SPAN UPPER-LOWER //0.6 +#define RAMPTIME 4 +#define STEPSIZE 0.015 // = SPAN/(RAMPTIME/UPDATERATE) = 0.03 + +Ticker Throttle_Update; +//Serial pc(PTE0, PTE1); + +DigitalOut LEDR(PTB18); +DigitalOut TestPoint(PTB8); +PwmOut Cyl_1(PTE31); +PwmOut Cyl_2(PTD3); +PwmOut Cyl_3(PTD2); +PwmOut Cyl_4(PTD0); +PwmOut Cyl_5(PTD5); +PwmOut Cyl_6(PTA13); +PwmOut Cyl_7(PTC9); +PwmOut Cyl_8(PTC8); + + +PwmOut LEDGreen(PTB19); +/* +// User Rotary Encoder definition +InterruptIn UserEnc1A(PTD4); +DigitalIn UserEnc1B(PTA12); +DigitalIn PB1(PTC3); */ +DigitalOut ANODE(PTC6); +DigitalOut COMMON(PTC5); + +//--------------------------- + +InterruptIn CrankAngleEncA(PTA1); +DigitalIn CrankAngleEncB(PTC0); + + +Timer t; + +char CylinderFired = 0; +uint8_t CrankAngle, CrankAngleChange; +uint8_t Gap = DefaultGap; +float Throttle = DefaultPWMPercent; +// Max Speed is currently about 15RPS, 923RPM, 65ms/rev, 254uS/Encoder pulse +// 1000us interval = 3.9RPS. 0.5RPS = 7813uS encoder pulse interval +uint16_t TargetInterval = 400; +int elapseTime; + +char Ramp_Dir = 0; +float ThrottlePosition; +uint8_t PreviousShaftAngle1, PreviousShaftAngle2, ShaftAngle1, ShaftAngle2; +uint8_t CycleCount; +/* +void UserEnc1_pulse(void) +{ + if (UserEnc1B && (TargetInterval < 8000)) + TargetInterval += 100; + else if (TargetInterval > 200) + TargetInterval -= 100; + +} +*/ + +void CrankAngleEnc_pulse(void) +{ + if (CrankAngleEncB) + CrankAngle++; + else + CrankAngle--; + //elapseTime = t.read_us(); t.reset(); + CrankAngleChange = true; +} + +void Update_Throttle(void) { + // FYI: 2.718-1.284=1.4343 + + if (Ramp_Dir == 1) + ThrottlePosition += STEPSIZE; + else + ThrottlePosition -= STEPSIZE; + + if ( (ThrottlePosition < LOWER+STEPSIZE) && (Ramp_Dir == 0)) + Ramp_Dir = 1; + else if ((ThrottlePosition > (UPPER-STEPSIZE))&&(Ramp_Dir == 1)) { + Ramp_Dir = 0; + ThrottlePosition = UPPER; + } + + //Throttle = log(ThrottlePosition); + Throttle = pow(ThrottlePosition,10)/100; + LEDGreen = Throttle; + TestPoint = !TestPoint; + PreviousShaftAngle1 = PreviousShaftAngle2 = 99; // forces update + CrankAngleChange = true; +// pc.printf("%d,%f\n\r",Ramp_Dir,ThrottlePosition); +} + +//void DispUpdate(void) { +// pc.printf("%d : %d : %d\n\r",CrankAngle,CylindersFired1,CylindersFired2);} + +int main() { + + ANODE = 1 ; COMMON = 0; + LEDR = 1; + CrankAngleChange = true; + ThrottlePosition = LOWER; + + Cyl_1.period_us(PWMPeriod) ; + Cyl_2.period_us(PWMPeriod) ; + Cyl_3.period_us(PWMPeriod) ; + Cyl_4.period_us(PWMPeriod) ; + Cyl_5.period_us(PWMPeriod) ; + Cyl_6.period_us(PWMPeriod) ; + Cyl_7.period_us(PWMPeriod) ; + Cyl_8.period_us(PWMPeriod) ; + +// pc.baud(57600); +// pc.printf("V8-Solenoid Engine..........."); + + + Cyl_1 = 0; Cyl_2 = 0; Cyl_3 = 0; Cyl_4 = 0; + Cyl_5 = 0; Cyl_6 = 1.0f; Cyl_7 = 0; Cyl_8 = 0; + wait_ms(500); + Cyl_2 = 1.0f; Cyl_5 = 1.0f;Cyl_6 = 0; + wait_ms(1000); + CrankAngle = 0; + Cyl_2 = 0; Cyl_5 = 0; + PreviousShaftAngle1 = PreviousShaftAngle2 = 99; + + + + Throttle_Update.attach(&Update_Throttle,UPDATERATE); + CrankAngleEncA.rise(&CrankAngleEnc_pulse); +// UserEnc1A.rise(&UserEnc1_pulse); + +// pc.printf("Initialized!\n"); + + + while(true) { + + + if(CrankAngleChange){ + + if ( (CrankAngle < (Quad1-Gap)) || (CrankAngle>(Quad3+Gap)) ) // Pair#2 270 to 90 deg + ShaftAngle1 = 1; + else if ((CrankAngle>(Quad1+Gap)) && (CrankAngle<(Quad3-Gap))) { // Pair#4 90 to 270 + ShaftAngle1 = 2; + } + else { + ShaftAngle1 = 0; + } + + + if ((CrankAngle>Gap) && (CrankAngle<(Quad2-Gap))) { // Pair#3 0 to 180 deg + ShaftAngle2 = 1; + } + else if ((CrankAngle>(Quad2+Gap)) && (CrankAngle<(Quad4-Gap))) { // Pair#1 180 to 360 deg + ShaftAngle2 = 2; + } + else { + ShaftAngle2 = 0; + } + + + + + + //Only if states change will the Cylinder assignments change + + if (PreviousShaftAngle1 != ShaftAngle1) { + if (ShaftAngle1 == 1) { + Cyl_1 = 0; Cyl_4 = 0; + Cyl_7 = Throttle; Cyl_6 = Throttle; + } + else if (ShaftAngle1 == 2) { + Cyl_7 = 0; Cyl_6 = 0; + Cyl_4 = Throttle; Cyl_1 = Throttle; + } + else { + Cyl_1 = 0; Cyl_4 = 0; Cyl_6 = 0; Cyl_7 = 0; + } + PreviousShaftAngle1 = ShaftAngle1; + } + + if (PreviousShaftAngle2 != ShaftAngle2) { + if (ShaftAngle2 == 1) { + Cyl_2 = 0; Cyl_5 = 0; + Cyl_8 = Throttle; Cyl_3 = Throttle; + } + else if (ShaftAngle2 == 2) { + Cyl_3 = 0; Cyl_8 = 0; + Cyl_2 = Throttle; Cyl_5 = Throttle; + } + else { + Cyl_3 = 0; Cyl_8 = 0; Cyl_2 = 0; Cyl_5 = 0; + } + PreviousShaftAngle2 = ShaftAngle2; + } + + + + + + // Speed Control Section +/* + if (MODE == CLOSEDLOOP) { + + Error = TargetInterval - elapseTime; + ErrorPrime = Error - PreviousError; + CompensationSignal = pGain*Error + dGain*ErrorPrime; + PreviousError = Error; + + } + else { + + Throttle = TargetInterval; + + } +*/ + + + CrankAngleChange = false; + } //if crankAngleChange + + + }//main loop - while(true) +}//main