Sie sind auf Seite 1von 3

10/24/2020 Rak_David_Encoder_Homing.

html

//David Rak ME460 Design Project 2


//Closed Loop DC Encoder Homing

#define Dir 12 // LOW=CCW HIGH=CW(towards button)


#define Voltage 3 //LOW=0 HIGH=255
#define Brake 9 //LOW=OFF HIGH=ON
#define A 7
#define B 2
#define Button 10

//SETUP
void setup() {
//Motor is is controled by a direction and speed input
pinMode(12,OUTPUT); //Direction
pinMode(3,OUTPUT); //Voltage
pinMode(9,OUTPUT); //Brake
pinMode(7,INPUT); //already a pulldown switch
//USE Pin #2 to count the interrupt.everytime there is a rising interrupt fin the function count
attachInterrupt(digitalPinToInterrupt(2),count,RISING); //RISING, FALLING, or CHANGE

pinMode(10,INPUT_PULLUP); //Pushbutton is a pullup switch so pushing the button returns 0

digitalWrite(Voltage,LOW); //Motor OFF


digitalWrite(Brake,LOW); //Brake OFF
digitalWrite(Dir,HIGH); //Direction CW=Towards button

while(digitalRead(Button)){}; //Push button to begin script


Serial.begin(9600);
delay(1000);

//Define Variables
int v=0, current_mmps=0;
float vel_curr=0, err=0, tStart=0;
volatile long int pulses=0, ref_pulses=0; //helps it run quickly
volatile int initial_pulses=0;

//Set Proportional Control Constants


float K1 = 0.35 , K2 = 0.15 , K3=0.5, K4 = 0.50;

//Set Final Stop constant


float CF=0.97;

//---------------------MAIN PORTION OF CODE FOR HOMING PROCESS--------------------------


void loop() {

//Get Initial time


tStart=millis();

//STEP #1
//Retract stage to the homing switch at 35 mm/s
digitalWrite(Dir,HIGH); //HIGH = CW = Retracting
v=100; //Set initial voltage
analogWrite(Voltage,v); //Inital Speed Input
while(digitalRead(Button)){ //UNTIL it hits button
vel_curr= getSpeed(20000); //Get current speed
err=(35+vel_curr); //Calculate error between actual and goal; want (+) value
v += K1*err; //Change Voltage input
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
ref_pulses = pulses; //Establishes 0 point for distance
stop1(); //stop for 1 second
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Stop 1");

//STEP #2
//Advance 10 mm from the switch at 20 mm/s
digitalWrite(Dir,LOW); //LOW = CCW = Advance
v=50; //Set initial voltage
analogWrite(Voltage,v); //Inital Speed

while(pulses-ref_pulses< 10*374/8){ //move 10 mm = 468


vel_curr=getSpeed(20000); //Get current speed
err=(20-vel_curr); //Calculate error
v += K2*err; //Change Voltage input
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
stop1();
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Stop 2");

file:///C:/Users/david/Documents/Señor/ME460- Senior Design/Homeworks/Design HW02- Project 02 - Closed Loop/Rak_David_Encoder_Homing.html 1/3


10/24/2020 Rak_David_Encoder_Homing.html

//STEP #3
//Retract to homing switch at lowest possible speed
digitalWrite(Dir,HIGH); //HIGH = CW = Retract
v=50; //Minimum initial speed
analogWrite(Voltage,v); //Slowest possible speed
while(digitalRead(Button)){ //UNTIL it hits button
vel_curr=getSpeed(50000); //Get current speed
err=(6+vel_curr); //Calculate error - SET AT LOWEST POSSIBLE SPEED FROM TESTS
v += K3*err; //Change Voltage input - DECREASE TO GO SLOWER
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
ref_pulses=pulses; //Re-estableshes 0 point immediately when switch is hit
stop1(); //Stops motion for 1 second
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Stop 3");

//STEP #4
//Advance 30 mm from the switch at 10 mm/s
digitalWrite(Dir,LOW); //LOW = CCW = Advance
v=50; //Set initial voltage
analogWrite(Voltage,v); //Inital Speed
while(pulses-ref_pulses < CF*(30*374/8)){ //move 30 mm =1402 pulses
vel_curr=getSpeed(20000); //Get current speed
err=(10-vel_curr); //Calculate error
v += K4*err; //Change Voltage input
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
stop1();
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Final Stop");

//Turn off
shutdown();
while(true); //sinkhole
}

//--------------------END MAIN HOMING FUNCTION-----------------------------

//Count function that runs when there is a rising interrupt


//detect events quickly, needs to be short loop!
void count(){
if(digitalRead(A)==HIGH) //Change which direcion counts as a step.
pulses++; //increase by 1
else
pulses--; //decrease by 1
}

double getSpeed(long int us){


long int t0=0, initialPulses=0, delta_pulses=0, delta_t;
double mmps=0;
initialPulses=pulses; //set initial pulse count
t0=micros(); //Set initial time in microseconds, us
while(micros()-t0<us){} //allow sampling time (us) to elapse
delta_t=us; //duration of the sample, in microseconds(passed as argument)
delta_pulses=pulses-initialPulses; //pulse change in sample period

mmps=double(delta_pulses)/double(delta_t)*1E+6 /374.0 * 8; //convert to mmps


// pulses / time in full sec /pulses/rev * mm/rev

return(mmps);
}

//emergency shutdown function


void shutdown() {
//apply LOW V to speed control and turn on break
digitalWrite(Voltage,LOW);
digitalWrite(Brake,LOW);
}

//STOP for 1 Second


void stop1() {
analogWrite(Voltage,0); //Power off
analogWrite(Brake,HIGH); //Brake ON
delay(1000); //Wait 1 Second
analogWrite(Brake,LOW); //Brake OFF

file:///C:/Users/david/Documents/Señor/ME460- Senior Design/Homeworks/Design HW02- Project 02 - Closed Loop/Rak_David_Encoder_Homing.html 2/3


10/24/2020 Rak_David_Encoder_Homing.html
}

file:///C:/Users/david/Documents/Señor/ME460- Senior Design/Homeworks/Design HW02- Project 02 - Closed Loop/Rak_David_Encoder_Homing.html 3/3

Das könnte Ihnen auch gefallen