Last active
May 10, 2017 03:12
-
-
Save hovissimo/884ff86252eb9046497bd2e632c7c1e9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# This code hasn't been compiled or tested. You may encounter errors. | |
#define ISR_PIN 3 | |
#define OTHER_PIN 8 | |
#defin LED_PIN 13 | |
volatile int last_other_value = 0; | |
volatile int other_value = 0; | |
void setup() | |
{ | |
Serial.begin(250000); | |
pinMode(OTHER_PIN, INPUT_PULLUP); | |
pinMode(LED_PIN, OUTPUT); | |
attachInterrupt(digitalPinToInterrupt(ISR_PIN), isr, RISING); | |
} | |
void loop() | |
{ | |
Serial.print(last_other_value); | |
Serial.print(" "); | |
Serial.print(other_value); | |
Serial.println(); | |
digitalWrite(LED_PIN, other_value != last_other_value); | |
} | |
void isr() { | |
last_other_value = other_value; | |
other_value = digitalRead(OTHER_PIN); | |
} |
This file contains hidden or 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
#define ENCODER_A 3 | |
#define ENCODER_B 4 | |
#define PWM_OUTPUT 11 | |
#define SENSOR 0 // potentiometer wiper | |
#define LED_PIN 9 | |
#define PID_INTERVAL 19 | |
#define ERROR_SIZE 16 | |
#define Kp -1 | |
double control = 0; | |
double motor_drive_value = 0; | |
volatile int encoder_position = 0; | |
int last_encoder_position = 0; | |
int last_millis = 0; | |
unsigned long last_pid = 0; | |
double error = 0; | |
double current_rpm = 0; | |
unsigned long ms; | |
int delta_encoder = 0; | |
double set_point; | |
#define INPUT_INSTRUCTION_BYTES 1 | |
#define INPUT_DATA_BYTES 4 | |
#define INPUT_SIZE INPUT_INSTRUCTION_BYTES + INPUT_DATA_BYTES | |
char input[INPUT_INSTRUCTION_BYTES + INPUT_DATA_BYTES]; | |
void setup() | |
{ | |
Serial.begin(250000); | |
pinMode(PWM_OUTPUT, OUTPUT); | |
pinMode(LED_PIN, OUTPUT); | |
pinMode(8, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(ENCODER_A), encoder_isr, CHANGE); | |
} | |
void loop() | |
{ | |
ms = millis(); | |
if (ms - last_pid > PID_INTERVAL) { | |
// SET POINT INPUT | |
int sensor_value = analogRead(SENSOR); | |
set_point = (double)map(sensor_value, 2, 1021, 0, 10000)/100.0; | |
// GET SPEED | |
int delta_encoder = encoder_position - last_encoder_position; | |
int delta_ms = ms - last_millis; | |
current_rpm = delta_encoder * 37.5 / delta_ms; | |
// PID SHIT | |
error = current_rpm - set_point; | |
control = constrain(Kp*error, -5, 5); | |
motor_drive_value = motor_drive_value + control; | |
// OUT | |
analogWrite(PWM_OUTPUT, motor_drive_value); | |
if (digitalRead(8) == LOW) { | |
analogWrite(PWM_OUTPUT, 0); | |
} | |
Serial.print(set_point); | |
Serial.print(" "); | |
Serial.print(error); | |
Serial.print(" "); | |
Serial.print(current_rpm); | |
Serial.print(" "); | |
Serial.print(control); | |
Serial.print(" "); | |
Serial.println(); | |
// UPDATE LAST | |
last_encoder_position = encoder_position; | |
last_millis = ms; | |
last_pid = ms; | |
} | |
} | |
void encoder_isr() { | |
bool dir = digitalRead(ENCODER_B) != digitalRead(ENCODER_A); | |
if (dir) { | |
encoder_position ++; | |
} else { | |
encoder_position --; | |
} | |
digitalWrite(LED_PIN, dir); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment