Created
July 20, 2016 14:16
-
-
Save gawnieg/c4d290e8b0b4f5f2bbfc39ae6087e768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PID_v1.h> //include PID library in sketch | |
#include <Wire.h> | |
#include <Servo.h> | |
#define ECU2_RESET 8 //was 3 | |
#define SERVO_PIN 9 //was 5 | |
#define OVERSPEED_PIN 10 //overspeed to master | |
#define RELAY_PIN 11 //local overspeed relay | |
#define RESET_ARDUINO 53 | |
volatile unsigned int Counter1 = 0; | |
double speed_readout = 0; //this is the variable where the sampled speed is stored. | |
/////////////////////////////SETPOINTS///////////////////////////////// | |
unsigned long over_speed_setpoint = 40000; //define an over speed setpoint (i.e 3000rpm) here in correct units = 3000rpm | |
double speed_setpoint = 545454 ; // see engine calcs overview for this calc. or onenote To Do sheet. 48000=2500rpm, 50000=2400rpm, 46154 =2600rpm, 52174=2300rpm, 2000rpm= 60000 | |
/////////////////////PID SETTINGS/////////////////////////////////////// | |
double Kp = 0.0000001; //was o.008 | |
double Ki = 0.0000001; // was 0.0022 | |
double Kd=0; | |
////////////////////SERVO SETTINGS////////////////////////////////////////////////// | |
double throttle_setpoint; // variable to store the desired throttle angle - WHAT SHOULD THIS BE INITIALISED AT?? | |
int throttle_sig; //whats set to the servo = (90-throttle_setpoint) calculated by map() function | |
Servo myservo; //initalising servo as servo object | |
byte numberpulses; | |
unsigned long t1; | |
//unsigned long t2; //t2 is instead defined in loop (?) | |
double t3=5000000; //was 500 switched to 2000 morning 04.03, intialised as longer than period so the startup is not as aggressive. | |
unsigned long pulses;//variable to store number of pulses | |
boolean engine_running = false; // this is false until the engine starter motor is engaged | |
PID myPID(&t3, &throttle_setpoint, &speed_setpoint, Kp, Ki, Kd, DIRECT); //PID setup using speed avg //REVERSE | |
void setup() { | |
pinMode(21, INPUT); // this is the pin the that the square wave signal from the MAX9924U is attached | |
digitalWrite(21,HIGH); //idk why this is set initally to high but the Maxim signal comes in on this pin | |
myservo.attach(SERVO_PIN);//attaching the servo to PIN 5. | |
myservo.write(8);// setting inital value of 85 to get it started on idle | |
////////////PID stuff////////////// | |
myPID.SetMode(AUTOMATIC); //this sets the PID algoithm to automatic, i.e. it will compute every loop | |
attachInterrupt(2, increaseCounter1, FALLING); //go to ISR when the signal is rising at pin 20 (pin 20 is interupt 2) changed 16.06.2015 | |
pinMode(OVERSPEED_PIN, OUTPUT); //pin 4 will be HIGH when engine speed is too high and shutdown is required, LOW when ok to keep running.OVERSPEED PIN | |
pinMode(RELAY_PIN,OUTPUT); //pin 7 as local overspeed pin | |
digitalWrite(OVERSPEED_PIN,LOW); //initailly set this to low to say that there is no overspeed on system intialisation | |
digitalWrite(RELAY_PIN,HIGH); //initailly set this to low to say that there is no overspeed on system intialisation | |
digitalWrite(RESET_ARDUINO,HIGH);//this must be set to high before the pin is set to output so it does not reset continuosly at setup. This connection must also be removed before programming the Arduino. | |
pinMode(RESET_ARDUINO,OUTPUT); | |
pinMode(ECU2_RESET,INPUT); //this pin resets the arduino from the master - 5v when reset needed, 0v under normal conditions | |
Serial.begin(9600);//added for debugging | |
Serial.println("Setup Finished"); | |
} //bracket to end setup////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
void increaseCounter1() { | |
Counter1++; | |
} | |
int returnCounter1() { // this function returns the number of interputs counted over a certain time period. the time period is determined from the main loop and the time that elapses between calls to this function. | |
int c; // | |
uint8_t SaveSREG = SREG; // save interrupt flag | |
noInterrupts(); // disable interrupts | |
c = Counter1; // access the shared data | |
if(c >= 20){ // this if block is new | |
Counter1 = 0; // reset Counter1 to 0 | |
} | |
SREG = SaveSREG; // restore the interrupt flag | |
return c; | |
} | |
void loop() { | |
unsigned long t2 = micros(); | |
pulses=returnCounter1(); | |
if(pulses >= 20){ | |
// Serial.println("Number of pulses reached"); | |
t3 = t2-t1; // t3 is the time taken to record 44 pulses - i.e. time for one cycle. | |
t1=t2; | |
} | |
if(t3 < 3000000 && engine_running == false){ // was t3<100,000.. the funciton of this block to to make sure ignition has got ahold during the cranking stage a | |
engine_running = true; | |
} | |
if(engine_running==true ){ //if closed loop control is need then the master controller will set pin 3 to HIGH AND the engine has started and the servo should be activated | |
myPID.Compute();//required for PID to work | |
throttle_sig=(int)map(throttle_setpoint, 0, 255, 6, 87);// remapping the PID controller output (0-255), set by the PID setoutput function in the setup to 90 to 0 i.e. 90 degrees throttle_setpoint is actually 0 degrees throttle angle ---23.05.2016 was 0,255,0,90 changed to 87 as was conking on start | |
myservo.write(throttle_sig); // this is writing the mapped PID output to the servo | |
Serial.print("engine running?: "); | |
Serial.println(engine_running); | |
Serial.print("The servo sig is: "); | |
Serial.println(throttle_sig); | |
Serial.print("The speed is is: "); | |
Serial.println(t3); | |
} | |
}//bracket for end of main loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment