]> defiant.homedns.org Git - ros_wild_thumper.git/blobdiff - avr/motor_ctrl/main.c
fix angle velocity
[ros_wild_thumper.git] / avr / motor_ctrl / main.c
index d25271fe945993606b49214d756134f4f01f0049..576d57c1c7d75d22b286f462f6c3618c57f61b3c 100644 (file)
 #define TWI_RESET      TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
 #define TWI_NAK                TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
 
-#define KP 0.045
-#define KI 2.298
-#define KD 0.0004
+#define KP 0.039
+#define KI 0.08
+#define KD 0.0
 #define PID_T 0.01
 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34, real wheel diameter: 0.12454m
 #define STEP_PER_M_AVG 4171.4
@@ -172,7 +172,7 @@ ISR(TWI_vect)
        static ufloat_t tmp_angle;
 
        switch (TWSR & 0xF8)
-       {  
+       {
                case 0x60: // start write
                        TWI_ACK;
                        ireg = 0;
@@ -790,7 +790,6 @@ static void update_pos(void) {
        int16_t pos4_diff;
        float diff_left_m, diff_right_m, angle_diff, translation;
        float pos_x_new, pos_y_new, angle_new;
-       int16_t speed_l, speed_r;
        float tmp_speed_lin, tmp_speed_rot;
        int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
        int16_t new_speed1, new_speed2, new_speed3, new_speed4;
@@ -825,10 +824,8 @@ static void update_pos(void) {
        pos_x_new = pos_x.f + cos(angle_new)*translation;
        pos_y_new = pos_y.f + sin(angle_new)*translation;
 
-       speed_l = (new_speed1+new_speed2)/2;
-       speed_r = (new_speed3+new_speed4)/2;
-       tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M_AVG);
-       tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M_AVG);
+       tmp_speed_lin = translation/PID_T;
+       tmp_speed_rot = angle_diff/PID_T;
 
        // copy from tmp
        cli();
@@ -871,7 +868,9 @@ static void update_pid(void) {
                        motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
                        eold1 = e;
 
-                        if (motor1 > 255) motor1 = 255;
+                       if (motor1 > 0 && speed1_wish < 0) motor1=0;
+                       else if (motor1 < 0 && speed1_wish > 0) motor1=0;
+                       else if (motor1 > 255) motor1 = 255;
                        else if (motor1 < -255) motor1 = -255;
                }
        }
@@ -886,7 +885,9 @@ static void update_pid(void) {
                        motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
                        eold2 = e;
 
-                        if (motor2 > 255) motor2 = 255;
+                       if (motor2 > 0 && speed2_wish < 0) motor2=0;
+                       else if (motor2 < 0 && speed2_wish > 0) motor2=0;
+                       else if (motor2 > 255) motor2 = 255;
                        else if (motor2 < -255) motor2 = -255;
                }
        }
@@ -901,7 +902,9 @@ static void update_pid(void) {
                        motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
                        eold3 = e;
 
-                        if (motor3 > 255) motor3 = 255;
+                       if (motor3 > 0 && speed3_wish < 0) motor3=0;
+                       else if (motor3 < 0 && speed3_wish > 0) motor3=0;
+                       else if (motor3 > 255) motor3 = 255;
                        else if (motor3 < -255) motor3 = -255;
                }
        }
@@ -916,7 +919,9 @@ static void update_pid(void) {
                        motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
                        eold4 = e;
 
-                        if (motor4 > 255) motor4 = 255;
+                       if (motor4 > 0 && speed4_wish < 0) motor4=0;
+                       else if (motor4 < 0 && speed4_wish > 0) motor4=0;
+                       else if (motor4 > 255) motor4 = 255;
                        else if (motor4 < -255) motor4 = -255;
                }
        }
@@ -1000,7 +1005,7 @@ int main(void) {
                        cmd_vel.bUpdate = 0;
                        sei();
 
-                       speed_wish_right = angle*M_PI*WHEEL_DIST/2 + speed;
+                       speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
                        speed_wish_left = speed*2-speed_wish_right;
 
                        speed_wish_left*=STEP_PER_M_LEFT;