Page 1 of 1

common code problem

Posted: Fri Aug 02, 2024 6:28 am
by arjun005
i am using esp32 wroom module and when i am trying to upload this programme after i facing this error. How to resolved it ?

ERROR:-
fatal error: avr/eeprom.h: No such file or directory
1 | #include <avr/eeprom.h>


CODE:-
  1. #include <Wire.h>
  2. #include <WiFi.h>
  3. #include <WiFiUdp.h>
  4. #include <GPS.h>
  5. WiFiUDP UDP;
  6. char packet[4];
  7. //IPAddress local_IP(192, 168, 203, 158);
  8. //IPAddress gateway(192, 168, 1, 158);
  9. //IPAddress subnet(255, 255, 0, 0);
  10. //_________________________________________//
  11. int ESCout_1, ESCout_2, ESCout_3, ESCout_4;
  12. int input_PITCH = 50;
  13. int input_ROLL = 50;
  14. int input_YAW = 50;
  15. volatile int input_THROTTLE = 0;
  16. int Mode = 0;
  17. boolean wall_car_init = false;
  18. boolean set_motor_const_speed = false;
  19. int8_t target_axis = 0;
  20. int8_t target_dirr = 0;
  21. boolean wheal_state = false;
  22. uint8_t pwm_stops;
  23. int arr[] = {20, 10, 20, 10};
  24. volatile int order[] = {0, 0, 0, 0}; //volatile key
  25. int temp_arr[] = {0, 0, 0, 0};
  26. int pulldown_time_temp[] = {0, 0, 0, 0, 0};
  27. int pulldown_time[] = {0, 0, 0, 0, 0};
  28. volatile int pulldown_time_temp_loop[] = {0, 0, 0, 0, 0}; //volatile key
  29. uint8_t pin[] = {14, 12, 13, 15};
  30. int i, j, temp_i, temp;
  31. boolean orderState1, orderState2, orderState3, orderState4, Timer_Init;
  32. int16_t gyro_x, gyro_y, gyro_z, acc_x, acc_y, acc_z, temperature, acc_total_vector;
  33. float angle_pitch, angle_roll, angle_yaw, prev_roll, prev_pitch, prev_yaw;
  34. boolean set_gyro_angles;
  35. float angle_roll_acc, angle_pitch_acc;
  36. float angle_pitch_output, angle_roll_output, angle_yaw_output;
  37. long Time, timePrev;
  38. float elapsedTime, P_factor;
  39. float acceleration_x, acceleration_y, acceleration_z;
  40. long gyro_x_cal, gyro_y_cal, gyro_z_cal;
  41. float pitch_PID, roll_PID, yaw_PID;
  42. float roll_error, roll_previous_error, pitch_error, pitch_previous_error, yaw_error, yaw_previous_error;
  43. float roll_pid_p, roll_pid_d, roll_pid_i, pitch_pid_p, pitch_pid_i, pitch_pid_d, yaw_pid_p, yaw_pid_i, yaw_pid_d;
  44. float roll_desired_angle, pitch_desired_angle, yaw_desired_angle;
  45. double twoX_kp = 5; //5
  46. double twoX_ki = 0.003; //0.003
  47. double twoX_kd = 1.4; //1.4
  48. double yaw_kp = 8; //5
  49. double yaw_ki = 0; //0.005
  50. double yaw_kd = 4; //2.8
  51. void ICACHE_RAM_ATTR PWM_callback() {
  52.   switch (pwm_stops) {
  53.     case 0:
  54.       pulldown_time_temp[0] = pulldown_time_temp_loop[0];
  55.       pulldown_time_temp[1] = pulldown_time_temp_loop[1];
  56.       pulldown_time_temp[2] = pulldown_time_temp_loop[2];
  57.       pulldown_time_temp[3] = pulldown_time_temp_loop[3];
  58.       pulldown_time_temp[4] = pulldown_time_temp_loop[4];
  59.       pwm_stops = 1;
  60.       if (input_THROTTLE != 0) {
  61.         GPOS = (1  <<  14);
  62.         GPOS = (1  <<  12);
  63.         GPOS = (1  <<  15);
  64.         GPOS = (1  <<  13);
  65.       }
  66.       timer1_write(80 * pulldown_time_temp[0]);
  67.       break;
  68.     case 1:
  69.       pwm_stops = 2;
  70.       GPOC = (1  <<  pin[order[0]]);
  71.       timer1_write(80 * pulldown_time_temp[1]);
  72.       break;
  73.     case 2:
  74.       pwm_stops = 3;
  75.       GPOC = (1  <<  pin[order[1]]);
  76.       timer1_write(80 * pulldown_time_temp[2]);
  77.       break;
  78.     case 3:
  79.       pwm_stops = 4;
  80.       GPOC = (1  <<  pin[order[2]]);
  81.       timer1_write(80 * pulldown_time_temp[3]);
  82.       break;
  83.     case 4:
  84.       pwm_stops = 0;
  85.       GPOC = (1  <<  pin[order[3]]);
  86.       timer1_write(80 * pulldown_time_temp[4]);
  87.       break;
  88.   }
  89. }
  90.  
  91. void setup() {
  92.   pinMode(D5, OUTPUT); pinMode(D6, OUTPUT); pinMode(D7, OUTPUT); pinMode(D8, OUTPUT); pinMode(D0, OUTPUT);
  93.   GPOC = (1  <<  14); GPOC = (1  <<  12); GPOC = (1  <<  13); GPOC = (1  <<  15);
  94.   digitalWrite(D0, LOW);
  95.   Serial.begin(115200);
  96.   WiFi.mode(WIFI_STA);
  97.   WiFi.begin("Redmi_R", "deywifi3210");
  98.   while (WiFi.status() != WL_CONNECTED) {
  99.     delay(500);
  100.   }
  101.   Serial.println(WiFi.localIP());
  102.   UDP.begin(9999);
  103.   delay(6000);
  104.   //____________________________________________________________________//
  105.   Wire.begin();
  106.   Wire.setClock(400000);
  107.   Wire.beginTransmission(0x68);
  108.   Wire.write(0x6B);
  109.   Wire.write(0x00);
  110.   Wire.endTransmission();
  111.   Wire.beginTransmission(0x68);
  112.   Wire.write(0x1C); //accel
  113.   Wire.write(0x08); //+-4g
  114.   Wire.endTransmission();
  115.   Wire.beginTransmission(0x68);
  116.   Wire.write(0x1B); //gyro
  117.   Wire.write(0x18); //2000dps
  118.   Wire.endTransmission();
  119.   Wire.beginTransmission(0x68);
  120.   Wire.write(0x1A);
  121.   Wire.write(0x03);
  122.   Wire.endTransmission();
  123.   //____________________________________________________________________//
  124.   for (int cal_int = 0; cal_int < 4000 ; cal_int ++) {
  125.     if (cal_int % 125 == 0)Serial.print(".");
  126.     Wire.beginTransmission(0x68);
  127.     Wire.write(0x3B);
  128.     Wire.endTransmission();
  129.     Wire.requestFrom(0x68, 14);
  130.     while (Wire.available() < 14);
  131.     acc_y = Wire.read() << 8 | Wire.read();
  132.     acc_x = Wire.read() << 8 | Wire.read();
  133.     acc_z = Wire.read() << 8 | Wire.read();
  134.     temperature = Wire.read() << 8 | Wire.read();
  135.     gyro_y = Wire.read() << 8 | Wire.read();
  136.     gyro_x = Wire.read() << 8 | Wire.read();
  137.     gyro_z = Wire.read() << 8 | Wire.read();
  138.     gyro_x_cal += gyro_x;
  139.     gyro_y_cal += gyro_y;
  140.     gyro_z_cal += gyro_z;
  141.     delayMicroseconds(100);
  142.   }
  143.   gyro_x_cal /= 4000;
  144.   gyro_y_cal /= 4000;
  145.   gyro_z_cal /= 4000;
  146.   timer1_attachInterrupt(PWM_callback);
  147.   timer1_enable(TIM_DIV1, TIM_EDGE, TIM_SINGLE);
  148. }
  149.  
  150. void loop() {
  151.   //--------------------------------MPU6050----------------------------//
  152.   timePrev = Time;
  153.   Time = micros();
  154.   elapsedTime = (float)(Time - timePrev) / (float)1000000;
  155.   Wire.beginTransmission(0x68);
  156.   Wire.write(0x3B);
  157.   Wire.endTransmission();
  158.   Wire.requestFrom(0x68, 14);
  159.   while (Wire.available() < 14);
  160.   acc_y = Wire.read() << 8 | Wire.read();
  161.   acc_x = Wire.read() << 8 | Wire.read();
  162.   acc_z = Wire.read() << 8 | Wire.read();
  163.   temperature = Wire.read() << 8 | Wire.read();
  164.   gyro_y = Wire.read() << 8 | Wire.read();
  165.   gyro_x = Wire.read() << 8 | Wire.read();
  166.   gyro_z = Wire.read() << 8 | Wire.read();
  167.   gyro_x -= gyro_x_cal;
  168.   gyro_y -= gyro_y_cal;
  169.   gyro_z -= gyro_z_cal;
  170.   acceleration_x = gyro_x * (-0.0610687023);
  171.   acceleration_y = gyro_y * (-0.0610687023);
  172.   acceleration_z = gyro_z * (-0.0610687023);
  173.   angle_pitch += acceleration_x * elapsedTime;
  174.   angle_roll += acceleration_y * elapsedTime;
  175.   angle_yaw += acceleration_z * elapsedTime;
  176.   if (angle_yaw  > = 180.00) {
  177.     angle_yaw -= 360;
  178.   }
  179.   else if (angle_yaw < -180.00) {
  180.     angle_yaw += 360;
  181.   }
  182.   angle_roll_acc = atan(acc_x / sqrt(acc_y * acc_y + acc_z * acc_z)) * (-57.296);
  183.   angle_pitch_acc = atan(acc_y / sqrt(acc_x * acc_x + acc_z * acc_z)) * 57.296;
  184.   angle_pitch_acc -= 4;
  185.   angle_roll_acc += 9;
  186.   if (set_gyro_angles) {
  187.     angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
  188.     angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;
  189.   }
  190.   else {
  191.     angle_pitch = angle_pitch_acc;
  192.     angle_roll = angle_roll_acc;
  193.     set_gyro_angles = true;
  194.   }
  195.   angle_pitch_output = angle_pitch_output * 0.9 + angle_pitch * 0.1;
  196.   angle_roll_output = angle_roll_output * 0.9 + angle_roll * 0.1;
  197.   angle_yaw_output = angle_yaw_output * 0.9 + angle_yaw * 0.1;
  198.  
  199.   //--------------------------PID_Calculation--------------------------//
  200.   if (wall_car_init == false) {
  201.     roll_desired_angle = 3 * (input_ROLL - 50) / 10.0;
  202.     pitch_desired_angle = 3 * (input_PITCH - 50) / 10.0;
  203.   }
  204.   P_factor = 0.001286376 * input_THROTTLE + 0.616932;
  205.  
  206.   roll_error = angle_roll_output - roll_desired_angle;
  207.   pitch_error = angle_pitch_output - pitch_desired_angle;
  208.   yaw_error = angle_yaw_output;
  209.  
  210.   roll_pid_p = P_factor * twoX_kp * roll_error;
  211.   pitch_pid_p = P_factor * twoX_kp * pitch_error;
  212.   yaw_pid_p = yaw_kp * yaw_error;
  213.  
  214.   roll_pid_i += twoX_ki * roll_error;
  215.   pitch_pid_i += twoX_ki * pitch_error;
  216.   yaw_pid_i += yaw_ki * yaw_error;
  217.  
  218.   roll_pid_d = twoX_kd * acceleration_y;
  219.   pitch_pid_d = twoX_kd * acceleration_x;
  220.   yaw_pid_d = yaw_kd * acceleration_z;
  221.  
  222.   if (roll_pid_i > 0 && roll_error < 0) {
  223.     roll_pid_i = 0;
  224.   }
  225.   else if (roll_pid_i < 0 && roll_error > 0) {
  226.     roll_pid_i = 0;
  227.   }
  228.   if (pitch_pid_i > 0 && pitch_error < 0) {
  229.     pitch_pid_i = 0;
  230.   }
  231.   else if (pitch_pid_i < 0 && pitch_error > 0) {
  232.     pitch_pid_i = 0;
  233.   }
  234.   if (yaw_pid_i > 0 && yaw_error < 0) {
  235.     yaw_pid_i = 0;
  236.   }
  237.   else if (yaw_pid_i < 0 && yaw_error > 0) {
  238.     yaw_pid_i = 0;
  239.   }
  240.  
  241.   roll_PID = roll_pid_p + roll_pid_i + roll_pid_d;
  242.   pitch_PID = pitch_pid_p + pitch_pid_i + pitch_pid_d;
  243.   yaw_PID = yaw_pid_p + yaw_pid_i + yaw_pid_d;
  244.  
  245.   ESCout_1 = input_THROTTLE + pitch_PID - roll_PID + yaw_PID;
  246.   ESCout_2 = input_THROTTLE + pitch_PID + roll_PID - yaw_PID;
  247.   ESCout_3 = input_THROTTLE - pitch_PID + roll_PID + yaw_PID;
  248.   ESCout_4 = input_THROTTLE - pitch_PID - roll_PID - yaw_PID;
  249.  
  250.  
  251.  
  252.   //----------------------------- Sorting -------------------------------//
  253.   arr[0] = ESCout_1; arr[1] = ESCout_2; arr[2] = ESCout_3; arr[3] = ESCout_4;
  254.   temp_arr[0] = arr[0]; temp_arr[1] = arr[1]; temp_arr[2] = arr[2]; temp_arr[3] = arr[3];
  255.   for (i = 0; i < 3; i++) {
  256.     temp_i = i;
  257.     for (j = i + 1; j < 4; j++)
  258.       if (temp_arr[j] < temp_arr[temp_i])
  259.         temp_i = j;
  260.     temp = temp_arr[temp_i];
  261.     temp_arr[temp_i] = temp_arr[i];
  262.     temp_arr[i] = temp;
  263.   }
  264.   pulldown_time[0] = temp_arr[0];
  265.   pulldown_time[1] = temp_arr[1] - temp_arr[0];
  266.   pulldown_time[2] = temp_arr[2] - temp_arr[1];
  267.   pulldown_time[3] = temp_arr[3] - temp_arr[2];
  268.   pulldown_time[4] = 1200 - temp_arr[3];
  269.   if (pulldown_time[1] == 0) {
  270.     pulldown_time[1] = 1;
  271.   }
  272.   if (pulldown_time[2] == 0) {
  273.     pulldown_time[2] = 1;
  274.   }
  275.   if (pulldown_time[3] == 0) {
  276.     pulldown_time[3] = 1;
  277.   }
  278.   if (pulldown_time[4] == 0) {
  279.     pulldown_time[4] = 1;
  280.   }
  281.   pulldown_time_temp_loop[0] = pulldown_time[0];
  282.   pulldown_time_temp_loop[1] = pulldown_time[1];
  283.   pulldown_time_temp_loop[2] = pulldown_time[2];
  284.   pulldown_time_temp_loop[3] = pulldown_time[3];
  285.   pulldown_time_temp_loop[4] = pulldown_time[4];
  286.   orderState1 = false; orderState2 = false; orderState3 = false; orderState4 = false;
  287.   for (int k = 0; k  < 4; k++) {
  288.     if (temp_arr[0] == arr[k] && orderState1 == false) {
  289.       order[0] = k;
  290.       orderState1 = true;
  291.     }
  292.     else if (temp_arr[1] == arr[k] && orderState2 == false) {
  293.       order[1] = k;
  294.       orderState2 = true;
  295.     }
  296.     else if (temp_arr[2] == arr[k] && orderState3 == false) {
  297.       order[2] = k;
  298.       orderState3 = true;
  299.     }
  300.     else if (temp_arr[3] == arr[k] && orderState4 == false) {
  301.       order[3] = k;
  302.       orderState4 = true;
  303.     }
  304.   }
  305.  
  306.   //----------------------------- WiFi ----------------------------------//
  307.   int packetSize = UDP.parsePacket();
  308.   if (packetSize) {
  309.     int len = UDP.read(packet, 4);
  310.     if (len > 0) {
  311.       packet[len] = '\0';
  312.     }
  313.     input_ROLL = int(packet[0]);
  314.     input_PITCH = int(packet[1]);
  315.     input_THROTTLE = int(packet[2]) * 24;
  316.     Mode = int(packet[3]);
  317.   }
  318.   if (input_THROTTLE == 0) {
  319.     angle_yaw_output = 0; angle_yaw = 0; yaw_PID = 0;
  320.     yaw_pid_p = 0; yaw_pid_i = 0; yaw_pid_d = 0; twoX_ki = 0;
  321.   }
  322.   //-------------------------------------------------------------------//
  323.  
  324.   //Serial.print(input_ROLL);Serial.print(" ");
  325.   //Serial.print(input_PITCH);Serial.print(" ");
  326.   //Serial.print(input_THROTTLE);Serial.print(" ");
  327.   //Serial.print(angle_roll_output,0);Serial.print(" ");
  328.   //Serial.print(angle_pitch_output,0);//Serial.print(" ");
  329.   //Serial.println(input_THROTTLE);
  330.  
  331.   if (wheal_state == false) {
  332.     digitalWrite(D0, LOW);
  333.   }
  334.  
  335.   if (Timer_Init == false) {
  336.     timer1_write(80);
  337.     Timer_Init = true;
  338.  
  339.   }
  340.   wheal_state = !wheal_state;
  341.   while (Time - timePrev  < 1200);
  342. }

Re: common code problem

Posted: Sat Aug 03, 2024 1:08 am
by ESP_Sprite
You seem to have some stuff that is specific to the AVR series of microcontrollers in your code or libraries. Unsurprisingly, those don't work as the ESP32 is an entire different microcontroller. You may need to hunt down libraries that are more platform-independent.