Advertisement
timor2542

POP32i Macanum with Wireless-X14 Control

Oct 18th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.08 KB | Source Code | 0 0
  1. #include <POP32.h>
  2. #include <WCX_Soft.h>
  3.  
  4. WCX mywcx;
  5.  
  6. #define degToRad 0.0174f
  7. float thetaRad, spd1, spd2, spd3, spd4, omega;
  8. float omega_heading;
  9. uint8_t rxCnt = 0, rxBuf[8];
  10. float pvYaw;
  11. #define head_Kp 2.0f
  12. #define head_Ki 0.0f
  13. #define head_Kd 0.0f
  14. float head_error, head_pError, head_w, head_d, head_i;
  15. int dir = 90;
  16. int spd = 0;
  17.  
  18. void holonomic(float spd, float theta, float omega) {
  19.   thetaRad = theta * degToRad;
  20.   spd1 = spd * sin(thetaRad - M_PI_4) + omega;
  21.   spd2 = -spd * sin(thetaRad + M_PI_4) + omega;
  22.   spd3 = -spd * sin(thetaRad - M_PI_4) + omega;
  23.   spd4 = spd * sin(thetaRad + M_PI_4) + omega;
  24.   motor(1, spd1);
  25.   motor(2, spd2);
  26.   motor(3, spd3);
  27.   motor(4, spd4);
  28. }
  29.  
  30. void zeroYaw() {
  31.   Serial1.begin(115200);
  32.   delay(100);
  33.   // Sets data rate to 115200 bps
  34.   Serial1.write(0XA5);
  35.   Serial1.write(0X54);
  36.   delay(100);
  37.   // pitch correction roll angle
  38.   Serial1.write(0XA5);
  39.   Serial1.write(0X55);
  40.   delay(100);
  41.   // zero degree heading
  42.   Serial1.write(0XA5);
  43.   Serial1.write(0X52);
  44.   delay(100);
  45.   // automatic mode
  46. }
  47. bool getIMU() {
  48.   while (Serial1.available()) {
  49.     rxBuf[rxCnt] = Serial1.read();
  50.     if (rxCnt == 0 && rxBuf[0] != 0xAA) return false;
  51.     rxCnt++;
  52.     if (rxCnt == 8) {  // package is complete
  53.       rxCnt = 0;
  54.       if (rxBuf[0] == 0xAA && rxBuf[7] == 0x55) {  // data package is correct
  55.         pvYaw = (int16_t)(rxBuf[1] << 8 | rxBuf[2]) / 100.f;
  56.         return true;
  57.       }
  58.     }
  59.   }
  60.   return false;
  61. }
  62. void Auto_zero() {
  63.   zeroYaw();
  64.   getIMU();
  65.   int timer = millis();
  66.   oled.clear();
  67.   oled.text(1, 2, "Setting zero");
  68.   while (abs(pvYaw) > 0.02) {
  69.     if (getIMU()) {
  70.       oled.text(3, 6, "Yaw: %f  ", pvYaw);
  71.       oled.show();
  72.       beep();
  73.       if (millis() - timer > 5000) {
  74.         zeroYaw();
  75.         timer = millis();
  76.       }
  77.     }
  78.   }
  79.   oled.clear();
  80.   oled.show();
  81. }
  82. void heading(float spd, float theta, float spYaw) {
  83.   head_error = spYaw - pvYaw;
  84.   head_i = head_i + head_error;
  85.   head_i = constrain(head_i, -180, 180);
  86.   head_d = head_error - head_pError;
  87.   head_w = (head_error * head_Kp) + (head_i * head_Ki) + (head_d * head_Kd);
  88.   head_w = constrain(head_w, -100, 100);
  89.   holonomic(spd, theta, head_w);
  90.   head_pError = head_error;
  91. }
  92. void setup() {
  93.   mywcx.begin(A0);
  94.   Auto_zero();
  95.   waitSW_A_bmp();
  96. }
  97. void loop() {
  98.   if (SW_A()) {
  99.     Auto_zero();
  100.   }
  101.   if (mywcx.getButton()) {
  102.     if (mywcx.LL && mywcx.LU) {
  103.       dir = 135;
  104.       spd = 100;
  105.     } else if (mywcx.LR && mywcx.LU) {
  106.       dir = 45;
  107.       spd = 100;
  108.     } else if (mywcx.LL) {
  109.       dir = 180;
  110.       spd = 100;
  111.     } else if (mywcx.LU) {
  112.       dir = 90;
  113.       spd = 100;
  114.     } else if (mywcx.LR) {
  115.       dir = 0;
  116.       spd = 100;
  117.     } else if (mywcx.LD) {
  118.       dir = 270;
  119.       spd = 100;
  120.     } else if (mywcx.RU) {
  121.       omega_heading = 0;
  122.     } else if (mywcx.RL) {
  123.       omega_heading = 45;
  124.     } else if (mywcx.RR) {
  125.       omega_heading = -45;
  126.     } else {
  127.       dir = 90;
  128.       spd = 0;
  129.     }
  130.  
  131.   } else {
  132.     dir = 90;
  133.     spd = 0;
  134.   }
  135.   getIMU();
  136.   heading(spd, dir, omega_heading);
  137. }
Tags: pop32i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement