Skip to content

Instantly share code, notes, and snippets.

@chico
Created May 3, 2016 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chico/5bd455171de88d22a52a478c9eac5b32 to your computer and use it in GitHub Desktop.
Save chico/5bd455171de88d22a52a478c9eac5b32 to your computer and use it in GitHub Desktop.
/********************************************************
* PID Basic Example
* Reading analog input 0 to control analog PWM output 3
********************************************************/
#include <PID_v1.h>
#define PIN_INPUT 0
#define PIN_OUTPUT 3
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
// TODO Really not sure what these tuning parameters should be? :(
/*
Kp: Determines how aggressively the PID reacts to the current amount of error (Proportional) (double >=0)
Ki: Determines how aggressively the PID reacts to error over time (Integral) (double>=0)
Kd: Determines how aggressively the PID reacts to the change in error (Derivative) (double>=0)
*/
//Specify the links and initial tuning parameters
//double Kp=2, Ki=5, Kd=1;
double Kp=1, Ki=0.1, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup()
{
Serial.begin(115200);
//initialize the variables we're linked to
// Input = analogRead(PIN_INPUT);
// Needs to be set initially
// Option 1. Use current heading where boat is pointing
// Option 2. Calculate bearing between where boat is and next way point
Setpoint = 45;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
// Input = analogRead(PIN_INPUT);
// Get current boat heading
Input = 0;
myPID.Compute();
Serial.print("Output: ");
Serial.println(Output, 2);
// Use Output to set rudder position
// Will need smoothing! Look at http://forum.arduino.cc/index.php?topic=336130.msg2317322#msg2317322
// analogWrite(PIN_OUTPUT, Output);
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment