![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Controller futhers (PI)
Diff: main.cpp
- Revision:
- 7:6006a473ea0b
- Parent:
- 6:8ab07cce3098
- Child:
- 8:998f4575089b
--- a/main.cpp Wed Oct 14 13:26:48 2015 +0000 +++ b/main.cpp Thu Oct 15 13:39:59 2015 +0000 @@ -7,14 +7,22 @@ // Define pin for motor control DigitalOut directionPin(D4); PwmOut PWM(D5); +DigitalIn buttonccw(PTA4); +DigitalIn buttoncw(PTC6); // define ticker Ticker aansturen; Ticker Printen; -// define rotation direction +// define rotation direction and begin possition const int cw = 1; const int ccw = 0; +double setpoint = 0; //setpoint is in pulses + +// saying buttons are not pressed +const int Buttoncw_pressed = 0; +const int Buttonccw_pressed = 0; + // Controller gain proportional and intergrator const double motor1_Kp = 5; // more or les random number. @@ -23,10 +31,12 @@ double motor1_error_integraal = 0; // initial value of integral error -// calculating pulses to rotations in degree. +// Defining pulses per revolution (calculating pulses to rotations in degree.) const double pulses_per_revolution = 4200 ;//8400 counts is aangegeven op de motor for x4. 10 - 30 counts oveshoot. for moter 1(tape)! Motor 2 almost the same(nice) +/* double Rotation = -2; // rotation in degree double movement = Rotation * pulses_per_revolution; // times 360 to make Rotations degree. +*/ // defining flags volatile bool flag_motor = false; @@ -43,6 +53,17 @@ flag_pcprint = true; } +// To make a new setpoint +double making_setpoint(double direction){ + if ( cw == direction){ + setpoint = setpoint + (pulses_per_revolution/4); + } + if ( ccw == direction){ + setpoint = setpoint - (pulses_per_revolution/4); + } + return(setpoint); + } + // Reusable P controller double PI(double error, const double Kp, const double Ki, double Ts, double &e_int) { @@ -55,7 +76,7 @@ // Next task, measure the error and apply the output to the plant void motor1_Controller() { - double reference = movement; // movement is in pulses + double reference = setpoint; // setpoint is in pulses double position = wheel.getPulses(); double error_pulses = (reference - position); // calculate the error in pulses double error_rotation = error_pulses / pulses_per_revolution; //calculate how much the rotaions the motor has to turn @@ -82,7 +103,7 @@ { double kijken = wheel.getPulses() / pulses_per_revolution; - pc.printf("ref %.0f rounds%.2f \n",Rotation,kijken); + pc.printf("ref %.0f rounds%.2f \n",setpoint/pulses_per_revolution,kijken); } @@ -91,7 +112,7 @@ aansturen.attach( &Go_flag_motor, 0.01f ); // 100 Hz // timer 0.00001f motor keeps spinning Printen.attach(&Go_flag_pcprint,0.1f); // 10 Hz // printstatement here because printing cost to much time. the motor void wouldn't be completed while( 1 ) { - +// to make the motor move if(flag_motor) { flag_motor = false; motor1_Controller(); @@ -102,6 +123,15 @@ counts_showing(); } +// Pussing buttons to get input signal + + if(buttoncw.read() == Buttoncw_pressed){ + setpoint = making_setpoint(cw); + } +if(buttonccw.read() == Buttonccw_pressed) { + setpoint = making_setpoint(ccw); + } + } }