Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimerThree.h>
- #define PWM_OUT_PIN 14 //teensy++ 2.0 pin C4 YELLOW WIRE
- #define LED_PIN PIN_D6 //teensy++ 2.0 pin D6 (build-in LED)
- #define MAX_PWM 1024
- #define PWM_MEASURE_PIN 0 //teensy++ 2.0 pin D0 GREEN WIRE
- #define CYCLE_TIME_MILLIS 50
- #define SAMPLES 20
- static double dutyCycleOffset = 0.1;
- static double minDutyCycle = 0.9; //10% RPM
- static unsigned long lastmillis = 0;
- static byte readDutyCycleCounter = 0;
- static double readDuties[SAMPLES];
- static double readDutyAvg;
- static double readDuty;
- static double freq;
- static long highTime = 0;
- static long lowTime = 0;
- static long tempPulse;
- static double targetDuty;
- static boolean errorState = false;
- void setup()
- {
- pinMode(PWM_OUT_PIN, OUTPUT);
- pinMode(LED_PIN, OUTPUT);
- Timer3.initialize(40); // 40ยตs period -> 25kHz
- Timer3.pwm(PWM_OUT_PIN, 0); // setup pwm
- pinMode(PWM_MEASURE_PIN,INPUT_PULLUP);
- Serial.begin(115200);
- }
- void loop()
- {
- if (millis() - lastmillis >= (CYCLE_TIME_MILLIS)){
- errorState = false;
- // read PWM
- readPWM();
- //Serial.print(readDutyCycleCounter);
- //Serial.print("\t");
- //Serial.println(readDuty);
- readDuties[readDutyCycleCounter] = readDuty;
- if (readDutyCycleCounter == 0) {
- readDutyAvg = 0;
- for (byte i=0; i<SAMPLES; i++) {
- readDutyAvg += readDuties[i];
- }
- readDutyAvg = readDutyAvg / SAMPLES;
- // set PWM
- // REGION ERR: < 3% RPM OR PWM FREQ < 20kHz
- if (readDutyAvg < 0.1 || readDutyAvg >= 0.97 || freq < 20000) {
- targetDuty = 0.0;
- errorState = true;
- Serial.println("\terrorState");
- }
- // REGION 0: <= 10% RPM
- else if (readDutyAvg >= 0.9) {
- Serial.print("REGION 0: ");
- Serial.println(targetDuty);
- targetDuty = minDutyCycle;
- }
- // REGION 1: 10% < PWM <= 20%
- //else if (readDutyAvg >= 0.8) {
- // targetDuty = readDutyAvg;
- // Serial.print("REGION I: ");
- // Serial.println(targetDuty);
- //}
- // REGION 2: 20% < PWM <= 35%
- else if (readDutyAvg >= 0.65) {
- targetDuty = readDutyAvg + dutyCycleOffset; //adding offset -> reducing RPM
- Serial.print("REGION II: ");
- Serial.print(readDutyAvg);
- Serial.print(" + ");
- Serial.print(dutyCycleOffset);
- Serial.print(" = ");
- Serial.println(targetDuty);
- }
- // REGION 3: >= 35% RPM
- else {
- targetDuty = readDutyAvg;
- Serial.print("REGION III: ");
- Serial.println(targetDuty);
- }
- digitalWrite(LED_PIN, errorState);
- Timer3.setPwmDuty(PWM_OUT_PIN, min(minDutyCycle, targetDuty) * MAX_PWM);
- //send debug info to serial
- Serial.print("freq = ");
- Serial.print(freq);
- Serial.print("\treadDutyAvg = ");
- Serial.print(readDutyAvg);
- Serial.print("\ttargetDuty = ");
- Serial.print(min(minDutyCycle, targetDuty));
- Serial.print("\terrorState = ");
- Serial.print(errorState);
- Serial.println("");
- }
- readDutyCycleCounter++; // increment counter
- readDutyCycleCounter = readDutyCycleCounter % SAMPLES;
- lastmillis = millis(); // Uptade lasmillis
- }
- }
- //Takes in reading pins and outputs pwm frequency and readDuty cycle.
- void readPWM(){
- highTime = 0;
- lowTime = 0;
- tempPulse = pulseIn(PWM_MEASURE_PIN, HIGH, 400);
- if (tempPulse > highTime) {
- highTime = tempPulse;
- }
- tempPulse = pulseIn(PWM_MEASURE_PIN, LOW, 400);
- if (tempPulse > lowTime){
- lowTime = tempPulse;
- }
- freq = ((double) 1000000)/(double (lowTime+highTime));
- readDuty = (/*100**/(highTime/(double (lowTime+highTime))));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement