Advertisement
xiia

robotiklubi kood

Jun 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include "drivers/board.h"
  3. #include "drivers/adc.h"
  4. #include "drivers/motor.h"
  5. #include "drivers/com.h"
  6. #include "drivers/gyro.h"
  7.  
  8. #define base_speed 500
  9. #define P_gain 45
  10. #define D_gain 20
  11.  
  12. int main(void)
  13. {
  14.     int i, check, Perror, kaalud[] = {3, 2, 1, -1, -2, -3}, speedL, speedR;
  15.     int square_flag, Derror, last_error = 0;
  16.     clock_init();       // Seadista süsteemi kell 32MHz peale
  17.     board_init();       // Seadista LED ja nupud
  18.     adc_init();         // Seadista ADC kanal 0
  19.     radio_init(57600);  // Seadista raadiomooduli UART
  20.     motor_init();       // Seadista mootorikontroller
  21.     while(!sw1_read());
  22.     _delay_ms(1000);
  23.  
  24.     while(1)
  25.     {
  26.         // andurid 0 kuni 5 = 6 tk
  27.        
  28.         for(i = 0, Perror = 0, check = 0; i < 6; i++)
  29.         {
  30.             if (adc_read(i) > 1700) // näeb musta(joont)
  31.             {
  32.                 Perror += kaalud[i];
  33.                 check = 1;
  34.                 if (i == 0) {
  35.                     square_flag = 0;
  36.                 }
  37.                 if (i == 5) {
  38.                     square_flag = 1;
  39.                 }
  40.             }
  41.            
  42.         }
  43.         if (check) // kui näeb joont vähemalt ühe anduriga
  44.         {
  45.             Derror = Perror - last_error;
  46.             last_error = Perror;
  47.             Perror *= P_gain;
  48.             Perror += Derror * D_gain;     
  49.             if (Perror < 0)
  50.             {
  51.                 if ((base_speed - Perror) > 1000)
  52.                 {
  53.                     speedR = 1000;
  54.                 }
  55.                 else
  56.                     speedR = (base_speed - Perror);
  57.                
  58.                 if(base_speed + Perror < 0)
  59.                 {
  60.                     speedL = 0;
  61.                 }
  62.                 else
  63.                     speedL = base_speed + Perror;
  64.             }
  65.            
  66.             else
  67.             {
  68.                 if ((base_speed + Perror) > 1000)
  69.                 {
  70.                     speedL = 1000;
  71.                 }
  72.                 else
  73.                 speedL = (base_speed + Perror);
  74.                
  75.                 if(base_speed - Perror < 0)
  76.                 {
  77.                     speedR = 0;
  78.                 }
  79.                 else
  80.                 speedR = base_speed - Perror;
  81.             }
  82.            
  83.            
  84.             motor_set(speedL, speedR);
  85.         }
  86.        
  87.         else
  88.         {
  89.             if (square_flag)
  90.             {
  91.                 motor_set(0, 1000);
  92.             }
  93.            
  94.             else
  95.             {
  96.                 motor_set(1000, 0);
  97.             }
  98.         }
  99.        
  100.        
  101.         /*
  102.         if(sw1_read())
  103.         {
  104.             rgb_set(RED);
  105.         }
  106.         else if(sw2_read())
  107.         {
  108.             rgb_set(BLUE);
  109.         }
  110.         else
  111.         {
  112.             rgb_set(GREEN);
  113.             sprintf(buff,"%4d,%4d,%5d \n\r",adc_read(0),adc_read(1),systick);
  114.             radio_puts(buff);
  115.             _delay_ms(100);
  116.         }
  117.         */
  118.  
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement