Advertisement
ksoltan

pwm_test_suite

Mar 8th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. // Tests supported:
  2. enum testType { LEFT_ONLY, SIMPLE_FLAP, DUTY_SWEEP, DEAD_ZONE_SWEEP };
  3. enum dir {RIGHT, LEFT};
  4.  
  5. class TestSuite {
  6.   public:
  7.     // Member Variables:
  8.     testType  ActiveTest;  // which pattern is running
  9.  
  10.     // Joint pin settings
  11.     int LeftPin;
  12.     int RightPin;
  13.     int LED = 13;
  14.     bool isPwm = false; // Toggle bang-bang and pwm.
  15.  
  16.     // Flapping settings
  17.     dir currDir = RIGHT;
  18.     int currFlapNum;
  19.  
  20.     // Duty settings
  21.     int MinDuty;
  22.     int MaxDuty;
  23.     int currDuty;
  24.     int DutyInterval;
  25.  
  26.     // Dead Zone settings
  27.     int MinDeadZone;
  28.     int MaxDeadZone;
  29.     int currDeadZone; // dead zone is percent of period.
  30.     int DeadZoneInterval;
  31.  
  32.     long Period;   // milliseconds. Period of whole sine wave.
  33.     long lastUpdate; // last update of position.
  34.     int FlapsPerSetting; // num flaps before setting update.
  35.  
  36.     // Constructor - calls base-class constructor to initialize strip
  37.     TestSuite(int leftPin, int rightPin, float frequency = 0.5, int duty = 50, int deadZone = 0, bool isPwmTest = false)
  38.     {
  39. //      pinMode(leftPin, OUTPUT);
  40. //      pinMode(rightPin, OUTPUT);
  41.       pinMode(LED, OUTPUT);
  42.       SimpleFlapSet(duty, deadZone, frequency, true);
  43.     }
  44.  
  45.     void SimpleFlapSet(int duty, int deadZone, float frequency, bool isPwmTest) {
  46.       currDuty = duty;
  47.       currDeadZone = deadZone;
  48.       isPwm = isPwmTest;
  49.       currFlapNum = 1;
  50.       ActiveTest = SIMPLE_FLAP;
  51.       Serial.println("Set Simple Flap."); // When set, may not print correclt as the Serial isn't initialized until the main sketch setup.
  52.       SetPeriod(frequency);
  53.     }
  54.  
  55.     void Update() {
  56.       // Update parameters depending on test.
  57.       switch (ActiveTest) {
  58.          case LEFT_ONLY:
  59.           LeftOnlyUpdate();
  60.         default:
  61.           break;
  62.       }
  63.       Flap();
  64.     }
  65.  
  66.     void Flap() {
  67.       UpdateFlapDir();
  68.       UpdatePins();
  69.     }
  70.  
  71.     void UpdateFlapDir() {
  72.       if (millis() - lastUpdate >= Period / 2) {
  73.         // currDir = 1 - CurrDir; // Switch direction
  74.         if (currDir == RIGHT) {
  75.           currDir = LEFT;
  76.         } else {
  77.           currDir = RIGHT;
  78.         }
  79.         lastUpdate = millis();
  80.         currFlapNum++; // Another flap has been completed.
  81.         Serial.println("Flap.");
  82.       }
  83.     }
  84.  
  85.     void UpdatePins() {
  86.       // Check if not in dead zone.
  87.      if (millis() - lastUpdate <= Period / 2 * (100 - currDeadZone) / 100) {
  88.         if (isPwm) { // Use pwm to the pins.
  89.           int pwm = (int)(255 * currDuty / 100);
  90.           if (currDir == RIGHT) {
  91.             Serial.println("RIGHT PWM");
  92.             analogWrite(RightPin, pwm);
  93.             analogWrite(LeftPin, 0);
  94.             digitalWrite(LED, HIGH);
  95.           } else {
  96.             Serial.println("LEFT PM");
  97.             analogWrite(RightPin, 0);
  98.             analogWrite(LeftPin, pwm);
  99.             digitalWrite(LED, LOW);
  100.           }
  101.         } else { // Bang-bang control
  102.           if (currDir == RIGHT) {
  103.             Serial.println("RIGHT");
  104.             digitalWrite(RightPin, HIGH);
  105.             digitalWrite(LeftPin, LOW);
  106.             digitalWrite(LED, HIGH);
  107.           } else {
  108.             Serial.println("LEFT");
  109.             analogWrite(RightPin, LOW);
  110.             analogWrite(LeftPin, HIGH);
  111.             digitalWrite(LED, LOW);
  112.           }
  113.         }
  114.       } else {
  115.         Serial.println("ZERO PWM!!!");
  116.         digitalWrite(RightPin, LOW);
  117.         digitalWrite(LeftPin, LOW);
  118.       }
  119.     }
  120.  
  121.     void LeftOnlySet(int duty, int deadZone, bool isPwmTest) {
  122.       currDuty = duty;
  123.       currDeadZone = deadZone;
  124.       isPwm = isPwmTest;
  125.       currFlapNum = 1;
  126.       currDir = LEFT;
  127.       ActiveTest = LEFT_ONLY;
  128.       SetPeriod();
  129.       Serial.println("Set Left Side Only."); // When set, may not print correcty as the Serial isn't initialized until the main sketch setup.
  130.     }
  131.  
  132.     void LeftOnlyUpdate() {
  133.       lastUpdate = millis(); // To prevent flipping of the joint, trick it into thinking it's switched sides already.
  134.     }
  135.  
  136.     void SetPeriod(float frequency = 0.5) {
  137.       Period = (int)(1 / frequency * 1000); // convert to ms.
  138.       Serial.print("Period set to: ");
  139.       Serial.print(Period);
  140.       Serial.print("ms.\n");
  141.     }
  142. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement