Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PID Tests #107

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://github.com/Arduino-CI/arduino_ci/issues/159
# copied from vendor/bundle/ruby/2.6.0/gems/arduino_ci-0.3.0/misc/default.yml
platforms:
mega2560:
board: arduino:avr:mega:cpu=atmega2560
package: arduino:avr
gcc:
features:
defines:
- __AVR_ATmega2560__
# added this line
- ARDUINO_CI
warnings:
flags:

unittest:
platforms:
- mega2560
testfiles:
reject:
- "Common.cpp"

compile:
platforms:
- mega2560
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.bundle
Gemfile.lock
vendor
# C++ stuff
*.bin
*.bin.dSYM
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gem 'arduino_ci'
File renamed without changes.
File renamed without changes.
152 changes: 152 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include "Arduino.h"
#include "ArduinoUnitTests.h"
#include <PID_v1.h>

unittest(direct) {

// Define Variables we'll be connecting to
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);

// initialize the variables we're linked to
Input = 50;
Setpoint = 100;

// turn the PID on
myPID.SetMode(AUTOMATIC);
for (int i = 0; i < 1000; i++) {
delay(200);
if (myPID.Compute()) {
Input = Input + (Output - Input) / 25.6;
}
}
// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
assertEqual(Setpoint, round(Input));
}

unittest(reverse) {

// Define Variables we'll be connecting to
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);

// initialize the variables we're linked to
Input = 50;
Setpoint = 100;

// turn the PID on
myPID.SetMode(AUTOMATIC);
for (int i = 0; i < 1000; i++) {
delay(200);
if (myPID.Compute()) {
Input = Input + (Input - Output) / 25.6;
}
}
// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
assertEqual(Setpoint, round(Input));
}

unittest(mode) {

// Define Variables we'll be connecting to
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);

// initialize the variables we're linked to
Input = 50;
Setpoint = 100;

// turn the PID on
myPID.SetMode(MANUAL);
for (int i = 0; i < 1000; i++) {
delay(200);
if (myPID.Compute()) {
Input = Input + (Input - Output) / 25.6;
}
}
// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
assertEqual(50, round(Input));
}

unittest(getFunctions) {

// Define Variables we'll be connecting to
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);

// initialize the variables we're linked to
Input = 50;
Setpoint = 100;

// turn the PID on
myPID.SetMode(AUTOMATIC);

// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
assertEqual(2, myPID.GetKp());
assertEqual(5, myPID.GetKi());
assertEqual(1, myPID.GetKd());
assertEqual(REVERSE, myPID.GetDirection());
assertEqual(AUTOMATIC, myPID.GetMode());
}

unittest(sampleTimeWorks) {

// Define Variables we'll be connecting to
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);

// initialize the variables we're linked to
Input = 50;
Setpoint = 100;

bool flag = false;
// turn the PID on
myPID.SetMode(AUTOMATIC);
for (int i = 0; i < 1000; i++) {
delay(200);
if (myPID.Compute()) {
flag = true;
Input = Input + (Input - Output) / 25.6;
}
}
// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
assertTrue(flag);
}

unittest(sampleTimeNotWorks) {

// Define Variables we'll be connecting to
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);

// initialize the variables we're linked to
Input = 50;
Setpoint = 100;

bool flag = false;
// turn the PID on
myPID.SetMode(MANUAL);
for (int i = 0; i < 1000; i++) {
delay(199);
if (myPID.Compute()) {
flag = true;
Input = Input + (Input - Output) / 25.6;
}
}
// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
assertFalse(flag);
}

unittest_main()