EL4121 Embedded System / Mbed OS odometry_omni_3roda_v1

Dependencies:   Motor PS_PAD TextLCD mbed-os

Fork of cobaLCDJoyMotor_Thread by EL4121 Embedded System

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "TextLCD.h"
00004 #include "PS_PAD.h"
00005 #include "Motor.h"
00006 #include "encoderKRAI.h"
00007 
00008 #include <string>
00009 using namespace std;
00010 
00011 #define PI  3.14159265359
00012 #define RAD_TO_DEG  57.2957795131
00013 
00014 #define PULSE_TO_MM 0.1177    //rev/pulse * K_lingkaran_roda
00015 #define L 144           // lengan roda dari pusat robot (mm)
00016 #define Ts 2    // Time Sampling sistem 2ms
00017 #define MAX_SPEED 0.3
00018 
00019 Thread thread1(osPriorityNormal, OS_STACK_SIZE, NULL);
00020 Thread thread2(osPriorityNormal, OS_STACK_SIZE, NULL);
00021 Thread thread3(osPriorityNormal, OS_STACK_SIZE, NULL);
00022 Thread thread4(osPriorityNormal, OS_STACK_SIZE, NULL);
00023 Thread thread5(osPriorityNormal, OS_STACK_SIZE, NULL);
00024 
00025 TextLCD lcd(PA_9, PC_3, PC_2, PA_8, PB_0, PC_15, TextLCD::LCD20x4); //rs,e,d4-d7
00026 encoderKRAI enc1 (PC_14, PC_13, 11, encoderKRAI::X4_ENCODING);
00027 encoderKRAI enc2 (PC_0, PC_1, 11, encoderKRAI::X4_ENCODING);
00028 encoderKRAI enc3 (PB_10, PB_3, 11, encoderKRAI::X4_ENCODING);
00029 PS_PAD ps2(PB_15,PB_14,PB_13, PC_4); //(mosi, miso, sck, ss)
00030 
00031 Motor motor3(PB_7, PA_14, PA_15); //motor4
00032 Motor motor2(PA_11, PA_6, PA_5); //motor2
00033 Motor motor1(PB_6, PA_7, PB_12); //motor 3
00034 //Motor motor1(PA_10, PB_5, PB_4);  //motor_griper
00035 
00036 Serial pc(USBTX,USBRX);
00037 
00038 void dataJoystick();
00039 void lcdPrint();
00040 void self_localization();
00041 void motorP();
00042 void calculate_PID();
00043 
00044 float moving_direction( float xs, float ys, float x, float y,float theta);
00045 
00046 
00047 
00048 /*------------buruk----------------*/
00049  float x = 0;
00050  float y = 0;
00051  float theta = 0;
00052  
00053  float x_prev = 0;
00054  float y_prev = 0;
00055  float theta_prev = 0;
00056  
00057  float vr = 0;
00058  float w = 0;
00059  float a = 0;
00060  
00061  //float Vx = 0;
00062  //float Vy = 0;
00063  //float W = 0;
00064 
00065 string str;
00066 /*---------------------------------------*/
00067 
00068 int main()
00069 {    
00070     pc.baud(115200);
00071     ps2.init();
00072     thread1.start(dataJoystick);
00073     thread2.start(lcdPrint);
00074     thread3.start(self_localization);
00075     thread4.start(motorP);
00076     thread5.start(calculate_PID);
00077     
00078     while (1)
00079     {      
00080         // baca input
00081 /*      
00082         ps2.poll();
00083         if(ps2.read(PS_PAD::PAD_X)==1)                   a = "silang";
00084         else if(ps2.read(PS_PAD::PAD_CIRCLE)==1)         a = "lingkaran";
00085         else if(ps2.read(PS_PAD::PAD_TRIANGLE)==1)       a = "segitiga";
00086         else if(ps2.read(PS_PAD::PAD_SQUARE)==1)         a = "kotak";
00087         else                                             a = "NULL";
00088  */   
00089 /*        
00090         //tampilkan LCD
00091         lcd.locate(0,0);
00092         lcd.printf("input joystik :");
00093         lcd.locate(0,1);
00094         lcd.printf("%s",a); 
00095     
00096         wait_ms(10);
00097         lcd.cls();
00098 */
00099     }
00100 }
00101 
00102 void dataJoystick(){
00103     while(true){
00104     ps2.poll();
00105     if(ps2.read(PS_PAD::PAD_X)==1)                   str =  "silang";
00106     else if(ps2.read(PS_PAD::PAD_CIRCLE)==1)         str = "lingkaran";
00107     else if(ps2.read(PS_PAD::PAD_TRIANGLE)==1)       str = "segitiga";
00108     else if(ps2.read(PS_PAD::PAD_SQUARE)==1)         str = "kotak";
00109     else                                             str = "NULL";    
00110     }
00111 }
00112 
00113 void lcdPrint(){
00114     while (true){
00115         lcd.cls();         
00116         lcd.locate(0,0);
00117         lcd.printf("input : %s", str);
00118         lcd.locate(0,1);
00119         lcd.printf("x = %.2f", x);
00120         lcd.locate(0,2);
00121         lcd.printf("y = %.2f", y);
00122         lcd.locate(0,3);
00123         //lcd.printf("theta = %.2f", theta*RAD_TO_DEG);
00124         lcd.printf("a = %.2f", a*RAD_TO_DEG);
00125         Thread::wait(100);
00126     }
00127 }
00128 
00129 
00130 void self_localization(){
00131     while(true){       
00132         float d1 = enc1.getPulses()*PULSE_TO_MM;
00133         float d2 = enc2.getPulses()*PULSE_TO_MM;
00134         float d3 = enc3.getPulses()*PULSE_TO_MM;
00135          
00136         x_prev = x;
00137         y_prev = y;
00138         theta_prev = theta;
00139         
00140         x = x_prev + (2*d1 - d2 - d3)/3*cos(theta_prev) - (-d2+d3)*0.5773*sin(theta_prev);
00141         y = y_prev + (2*d1 - d2 - d3)/3*sin(theta_prev) + (-d2+d3)*0.5773*cos(theta_prev);
00142         theta = theta_prev + (d1 + d2 + d3)/(3*L); //     //   0.132629 => 180 / (3. L. pi)
00143         
00144         //Vx = (x - x_prev)/0.002;
00145         //Vy = (y - y_prev)/0.002;
00146         //W = (theta - theta_prev)/0.002;
00147         
00148         enc1.reset();
00149         enc2.reset();
00150         enc3.reset();
00151         
00152         Thread::wait(Ts); //frekuensi sampling = 500 Hz
00153     }
00154 }
00155 
00156 
00157 void calculate_PID(){
00158     // konstanta PID untuk kendali Posisi (x y)
00159     float Kp_s = 0.05;
00160     float Ki_s = 0;
00161     float Kd_s = 0.5;
00162     
00163     // konstanta PID untuk kendali arah (theta)
00164     float Kp_w = 0.12;
00165     float Ki_w = 0.0;
00166     float Kd_w = 0.4;
00167     
00168     // setpoint sistem
00169     float x_set = 400;
00170     float y_set = 400;
00171     float theta_set = 90;
00172     float S_set = sqrt(x_set*x_set + y_set*y_set);
00173     
00174     // variabel tambahan
00175     float S_error_prev = 0;
00176     float theta_error_prev = 0;
00177     
00178     float sum_S_error = 0;
00179     float sum_theta_error = 0;
00180     
00181     while(true){
00182         //menghitung error posisi
00183         //float S = sqrt(x*x + y*y);
00184         //float S_error = S_set - S;
00185         float S_error = sqrt((x_set-x)*(x_set-x) + (y_set-y)*(y_set-y));
00186         //menghitung error arah
00187         float theta_error = theta_set - theta*RAD_TO_DEG;
00188         
00189         sum_S_error += S_error;
00190         sum_theta_error += theta_error;
00191          
00192         vr = Kp_s*S_error + Ki_s*Ts*sum_S_error + Kd_s*(S_error - S_error_prev)/Ts;
00193         w = Kp_w*theta_error + Ki_w*Ts*sum_theta_error + Kd_w*(theta_error - theta_error_prev)/Ts;
00194        
00195         a = moving_direction(x_set,y_set,x,y,theta);
00196         
00197         S_error_prev = S_error;
00198         theta_error_prev = theta_error;
00199                 
00200         Thread::wait(Ts);
00201     }   
00202 }
00203 
00204 
00205 float moving_direction( float xs, float ys, float x, float y,float theta){
00206     float temp = atan((ys - y)/(xs - x)) - theta;
00207     
00208     if (xs < x)    return temp + PI;
00209     else            return temp;
00210 }
00211 
00212 
00213 
00214 void motorP() {      
00215         Thread::wait(1500);
00216         
00217         while(1){
00218            motor1.speed(MAX_SPEED*(vr*cos(a) + w));
00219            motor2.speed(MAX_SPEED*(vr*(-0.5*cos(a) - 0.866*sin(a)) + w));
00220            motor3.speed(MAX_SPEED*(vr*(-0.5*cos(a) + 0.866*sin(a)) + w));
00221         } 
00222         
00223         motor1.speed(0);
00224         motor2.speed(0);
00225         motor3.speed(0);
00226 } 
00227