Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tests supported:
- enum testType { LEFT_ONLY, SIMPLE_FLAP, DUTY_SWEEP, DEAD_ZONE_SWEEP };
- enum dir {RIGHT, LEFT};
- class TestSuite {
- public:
- // Member Variables:
- testType ActiveTest; // which pattern is running
- // Joint pin settings
- int LeftPin;
- int RightPin;
- int LED = 13;
- bool isPwm = false; // Toggle bang-bang and pwm.
- // Flapping settings
- dir currDir = RIGHT;
- int currFlapNum;
- // Duty settings
- int MinDuty;
- int MaxDuty;
- int currDuty;
- int DutyInterval;
- // Dead Zone settings
- int MinDeadZone;
- int MaxDeadZone;
- int currDeadZone; // dead zone is percent of period.
- int DeadZoneInterval;
- long Period; // milliseconds. Period of whole sine wave.
- long lastUpdate; // last update of position.
- int FlapsPerSetting; // num flaps before setting update.
- // Constructor - calls base-class constructor to initialize strip
- TestSuite(int leftPin, int rightPin, float frequency = 0.5, int duty = 50, int deadZone = 0, bool isPwmTest = false)
- {
- // pinMode(leftPin, OUTPUT);
- // pinMode(rightPin, OUTPUT);
- pinMode(LED, OUTPUT);
- SimpleFlapSet(duty, deadZone, frequency, true);
- }
- void SimpleFlapSet(int duty, int deadZone, float frequency, bool isPwmTest) {
- currDuty = duty;
- currDeadZone = deadZone;
- isPwm = isPwmTest;
- currFlapNum = 1;
- ActiveTest = SIMPLE_FLAP;
- Serial.println("Set Simple Flap."); // When set, may not print correclt as the Serial isn't initialized until the main sketch setup.
- SetPeriod(frequency);
- }
- void Update() {
- // Update parameters depending on test.
- switch (ActiveTest) {
- case LEFT_ONLY:
- LeftOnlyUpdate();
- default:
- break;
- }
- Flap();
- }
- void Flap() {
- UpdateFlapDir();
- UpdatePins();
- }
- void UpdateFlapDir() {
- if (millis() - lastUpdate >= Period / 2) {
- // currDir = 1 - CurrDir; // Switch direction
- if (currDir == RIGHT) {
- currDir = LEFT;
- } else {
- currDir = RIGHT;
- }
- lastUpdate = millis();
- currFlapNum++; // Another flap has been completed.
- Serial.println("Flap.");
- }
- }
- void UpdatePins() {
- // Check if not in dead zone.
- if (millis() - lastUpdate <= Period / 2 * (100 - currDeadZone) / 100) {
- if (isPwm) { // Use pwm to the pins.
- int pwm = (int)(255 * currDuty / 100);
- if (currDir == RIGHT) {
- Serial.println("RIGHT PWM");
- analogWrite(RightPin, pwm);
- analogWrite(LeftPin, 0);
- digitalWrite(LED, HIGH);
- } else {
- Serial.println("LEFT PM");
- analogWrite(RightPin, 0);
- analogWrite(LeftPin, pwm);
- digitalWrite(LED, LOW);
- }
- } else { // Bang-bang control
- if (currDir == RIGHT) {
- Serial.println("RIGHT");
- digitalWrite(RightPin, HIGH);
- digitalWrite(LeftPin, LOW);
- digitalWrite(LED, HIGH);
- } else {
- Serial.println("LEFT");
- analogWrite(RightPin, LOW);
- analogWrite(LeftPin, HIGH);
- digitalWrite(LED, LOW);
- }
- }
- } else {
- Serial.println("ZERO PWM!!!");
- digitalWrite(RightPin, LOW);
- digitalWrite(LeftPin, LOW);
- }
- }
- void LeftOnlySet(int duty, int deadZone, bool isPwmTest) {
- currDuty = duty;
- currDeadZone = deadZone;
- isPwm = isPwmTest;
- currFlapNum = 1;
- currDir = LEFT;
- ActiveTest = LEFT_ONLY;
- SetPeriod();
- Serial.println("Set Left Side Only."); // When set, may not print correcty as the Serial isn't initialized until the main sketch setup.
- }
- void LeftOnlyUpdate() {
- lastUpdate = millis(); // To prevent flipping of the joint, trick it into thinking it's switched sides already.
- }
- void SetPeriod(float frequency = 0.5) {
- Period = (int)(1 / frequency * 1000); // convert to ms.
- Serial.print("Period set to: ");
- Serial.print(Period);
- Serial.print("ms.\n");
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement