Advertisement
Guest User

Untitled

a guest
May 25th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.50 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <ArduinoOTA.h>
  4. #include <SoftwareSerial.h>
  5. #include <Wire.h>
  6. #include <LSM303.h>
  7.  
  8. //Wektory
  9. struct Vector2
  10. {
  11.     double x;
  12.     double y;
  13. };
  14.  
  15. //Sieć połączeniowa
  16. const char* SSID = "Robonomik";
  17. const char* PASSWORD = "WojewodaA1";
  18.  
  19. //Połączenie GPS z ESP RX TX
  20. SoftwareSerial gps_uart (0, 16);
  21.  
  22. SoftwareSerial bt_serial(D5,D6); // RX, TX
  23.  
  24. //Zmienne dla GPS
  25. double robotLatitude = 0;
  26. double robotLongitude = 0;
  27. double robotHeight = 0;
  28. String GPS_data = "";
  29. String GP_GGA = "";
  30.  
  31. //Pozycje robota, smartfona - x y
  32. struct Vector2 robotPositionGlobal;
  33. struct Vector2 robotPositionLocal;
  34. struct Vector2 smartfonPositionGlobal;
  35. struct Vector2 smartfonPositionLocal;
  36. struct Vector2 virtualNorth;
  37.  
  38. //Właściwości wektorów
  39. double vectorRSMagnitude = 0;
  40. double vectorNorthMagnitude = 0;
  41. double vectorRSAngle = 0;
  42. double robotAngle = 0;
  43.  
  44. //Protokół I2C
  45. int ESC1Address = 6;
  46. int ESC2Address = 7;
  47. unsigned long I2CsendRate = 50;    //Czas w milisekundach pomiędzy wysyłaniem
  48. unsigned long I2Cmotor1 = 0;
  49. unsigned long I2Cmotor2 = 0;
  50.  
  51. //Kompas
  52. LSM303 compass;
  53.  
  54. //Smartfon data - sterowanie
  55. String WIFI_data = "1,9479323.47395,9275956.53963,0,1,243,232,";
  56. bool manualControl = true;
  57. bool robotDirectionMotor1 = true;
  58. bool robotDirectionMotor2 = true;
  59. bool robotAlarm = false;
  60. int robotMotor1Val = 0;
  61. int robotMotor2Val = 0;
  62.  
  63. //Debug
  64. unsigned long currentMillis = 0;
  65. unsigned long prevMillis = 0;
  66.  
  67. double deg2rad(double deg)
  68. {
  69.     //Zmiana stopni na radiany
  70.     double rad = deg * PI / 180;
  71.     return rad;
  72. }
  73. double rad2deg(double rad)
  74. {
  75.     //Zmiana radiany na stopnie
  76.     double deg = rad * 180 / PI;
  77.     return deg;
  78. }
  79. void WiFi_settings()
  80. {
  81.     //Ustawienia WiFi
  82.     WiFi.mode(WIFI_STA);
  83.     WiFi.begin(SSID, PASSWORD);
  84.  
  85.     //Czekanie na połączenie
  86.     if (WiFi.waitForConnectResult() != WL_CONNECTED)
  87.     {
  88.         Serial.println("Nieudane połączenie do sieci! Restartowanie...");
  89.         ESP.restart();
  90.     }
  91.     else if (WiFi.waitForConnectResult() == WL_CONNECTED)
  92.     {
  93.         Serial.print("Połączono do sieci: ");
  94.         Serial.println(WiFi.SSID());
  95.         Serial.print("Adres IP: ");
  96.         Serial.println(WiFi.localIP());
  97.     }
  98. }
  99. void OTA_updater()
  100. {
  101.     //Ustawianie nazwy ESP8266 w sieci
  102.     ArduinoOTA.setHostname("ESP8266-Przewodnik");
  103.  
  104.     //Rebootowanie ESP po aktualizacji
  105.     ArduinoOTA.setRebootOnSuccess(true);
  106.  
  107.     //Ustawienia ESP na start aktualizacji
  108.     ArduinoOTA.onStart([](){
  109.         Serial.println("Wgrywanie...");
  110.         // Tutaj ustawić wszystko nieruchomo żeby łazik podczas aktualizacji nie zaczął jechać bez kontroli
  111.     });
  112.  
  113.     //Ustawienia ESP po zakończeniu aktualizacji
  114.     ArduinoOTA.onEnd([](){
  115.         Serial.println("Wgrano program!");
  116.     });
  117.  
  118.     //Ustawienia ESP gdy nastapi błąd podczas aktualizacji
  119.     ArduinoOTA.onError([](ota_error_t error){
  120.         Serial.print("Podczas aktualizacji wystąpił błąd: ");
  121.         if(error == OTA_AUTH_ERROR)
  122.         {
  123.             Serial.println("AUTH_ERROR");
  124.         }
  125.         else if (error == OTA_BEGIN_ERROR)
  126.         {
  127.             Serial.println("BEGIN_FAILED");
  128.         }
  129.         else if (error == OTA_CONNECT_ERROR)
  130.         {
  131.             Serial.println("CONNECT_ERROR");
  132.         }
  133.         else if (error == OTA_RECEIVE_ERROR)
  134.         {
  135.             Serial.println("RECEIVE_ERROR");
  136.         }
  137.         else if (error == OTA_END_ERROR)
  138.         {
  139.             Serial.println("END_ERROR");
  140.         }
  141.     });
  142.  
  143.     //Inicjalizacja OTA
  144.     ArduinoOTA.begin();
  145.     Serial.println("Gotowy!");
  146. }
  147. void geo_to_cartesian(double lati, double longi, double hght, double *x, double *y)
  148. {
  149.   //Średnie promienie ziemi a i b
  150.   double equatorialRadius = 6378137.0;
  151.   double polarRadius = 6356752.314245;
  152.  
  153.   //Zmiana latitude i longitude z stopni na radiany
  154.   lati = deg2rad(lati);
  155.   longi = deg2rad(longi);
  156.  
  157.   //Przeliczanie z geo na XYZ
  158.   double eToSquare = 1 - (pow(polarRadius, 2) / pow(equatorialRadius, 2));
  159.   double Nlati = equatorialRadius / sqrt(1 - eToSquare * pow(sin(lati), 2));
  160.  
  161.   //Przypisywanie wartości
  162.   *x = (Nlati + hght) * cos(lati) * cos(longi);
  163.   *y = (Nlati + hght) * cos(lati) * sin(longi);
  164.   //Opcjonalne Z
  165.   //double z = ((pow(polarRadius, 2) / pow(equatorialRadius, 2)) * Nlati + hght) * sin(lati);
  166. }
  167. void GPS_get_postion(double *setLati, double *setLongi, double *setHght)
  168. {
  169.     if(gps_uart.available())
  170.     {
  171.         GPS_data = gps_uart.readStringUntil('\n');
  172.  
  173.         if(GPS_data.indexOf("$GPGGA") > -1)
  174.         {
  175.             //Wyznaczanie końca lini
  176.             GP_GGA = GPS_data.substring(GPS_data.indexOf("$GPGGA"), GPS_data.indexOf("*") + 3);
  177.  
  178.             int colon_array[15];
  179.             for (int i = 1; i < 15; i++)
  180.             {
  181.                 colon_array[i] = GPS_data.indexOf(',', colon_array[i - 1] + 1);
  182.             }
  183.  
  184.             //Ustawienia latitude
  185.             if (GP_GGA.substring(colon_array[2], colon_array[3]) != ",")
  186.             {
  187.                 *setLati = (GP_GGA.substring(colon_array[2] + 1, colon_array[3])).toDouble() / 100;
  188.             }
  189.             else
  190.             {
  191.                 *setLati = 0;
  192.             }
  193.            
  194.             //Ustawianie longitude
  195.             if (GP_GGA.substring(colon_array[4], colon_array[5]) != ",")
  196.             {
  197.                 *setLongi = (GP_GGA.substring(colon_array[4] + 1, colon_array[5])).toDouble() / 100;
  198.             }
  199.             else
  200.             {
  201.                 *setLongi = 0;
  202.             }
  203.            
  204.             //Ustawianie Height
  205.             if (GP_GGA.substring(colon_array[9], colon_array[10]) != ",")
  206.             {
  207.                 *setHght = (GP_GGA.substring(colon_array[9] + 1, colon_array[10])).toDouble();
  208.             }
  209.             else
  210.             {
  211.                 *setHght = 0;
  212.             }
  213.         }
  214.         // Serial.print("Latitude: ");
  215.         // Serial.println(*setLati, 7);
  216.         // Serial.print("Longitude: ");
  217.         // Serial.println(*setLongi, 7);
  218.         // Serial.print("Height: ");
  219.         // Serial.println(*setHght, 7);
  220.         // Serial.println("");
  221.     }
  222. }
  223. void get_local_postion()
  224. {
  225.     robotPositionLocal.x = 0;
  226.     robotPositionLocal.y = 0;
  227.     smartfonPositionLocal.x = robotPositionGlobal.x - smartfonPositionGlobal.x;  
  228.     smartfonPositionLocal.y = robotPositionGlobal.y - smartfonPositionGlobal.y;  
  229. }
  230. void get_vectorRS()
  231. {  
  232.     //Ustalanie pozycji wirtualnej północy
  233.     virtualNorth.x = 0;
  234.     virtualNorth.y = 3;
  235.  
  236.     //Wyznaczanie długości wektora
  237.     vectorRSMagnitude = sqrt(pow((smartfonPositionLocal.x - robotPositionLocal.x), 2) + pow((smartfonPositionLocal.y - robotPositionLocal.y), 2));
  238.     vectorNorthMagnitude = sqrt(pow((virtualNorth.x - robotPositionLocal.x), 2) + pow((virtualNorth.y - robotPositionLocal.y), 2));
  239.  
  240.     //Obliczanie kątu wektora RS z wektorem North
  241.     double vectorRSdot = (smartfonPositionLocal.x * virtualNorth.x) + (smartfonPositionLocal.y * virtualNorth.y);
  242.     if(smartfonPositionLocal.x >= 0)
  243.     {
  244.         vectorRSAngle = rad2deg(acos(vectorRSdot / (vectorRSMagnitude * vectorNorthMagnitude)));
  245.     }
  246.     else
  247.     {
  248.         vectorRSAngle = -rad2deg(acos(vectorRSdot / (vectorRSMagnitude * vectorNorthMagnitude)));
  249.     }
  250. }
  251. double read_robot_angle()
  252. {
  253.     compass.read();
  254.     double angle = compass.heading();
  255.     return angle;
  256. }
  257. void get_interface_info(String interface_data, bool *manualControl, double *smartfonX, double *smartfonY, bool *moto1Dir, bool *motor2Dir, int *motor1Value, int *motor2Value, bool *alarm)
  258. {
  259.     //GPS czy sterowanie ręczne
  260.     if(interface_data.substring(0, 1) == "1")
  261.     {
  262.         *manualControl = true;
  263.     }
  264.     else if(interface_data.substring(0, 1) == "0")
  265.     {
  266.         *manualControl = false;
  267.     }
  268.     else
  269.     {
  270.         *alarm = true;
  271.     }
  272.     //Siatka karezjańska
  273.     //X
  274.     *smartfonX = interface_data.substring(2, 15).toDouble();
  275.     //Y
  276.     *smartfonY = interface_data.substring(16, 29).toDouble();
  277.    
  278.     //Kierunek motor1 1 - przod, 0 - tył
  279.     if(interface_data.substring(30, 31) == "1")
  280.     {
  281.         *moto1Dir = true;
  282.     }
  283.     else if(interface_data.substring(30, 31) == "0")
  284.     {
  285.         *moto1Dir = false;
  286.     }
  287.     else
  288.     {
  289.         *alarm = true;
  290.     }
  291.     //Kierunek motor2 1 - przod, 0 - tył
  292.     if(interface_data.substring(32, 33) == "1")
  293.     {
  294.         *motor2Dir = true;
  295.     }
  296.     else if(interface_data.substring(32, 33) == "0")
  297.     {
  298.         *motor2Dir = false;
  299.     }
  300.     else
  301.     {
  302.         *alarm = true;
  303.     }
  304.  
  305.     //Motor1 Power
  306.     if(interface_data.substring(34, 37).toInt() >= 0 && interface_data.substring(34, 37).toInt() <= 255)
  307.     {
  308.         *motor1Value = interface_data.substring(34, 37).toInt();
  309.     }
  310.     else
  311.     {
  312.         *alarm = true;
  313.     }
  314.  
  315.     //Motor2 Power
  316.     if(interface_data.substring(38, 41).toInt() >= 0 && interface_data.substring(38, 41).toInt() <= 255)
  317.     {
  318.         *motor2Value = interface_data.substring(38, 51).toInt();
  319.     }
  320.     else
  321.     {
  322.         *alarm = true;
  323.     }
  324. }
  325. void I2C_send(int address, int value)
  326. {
  327.     //Wysyłanie wiadomości
  328.     Wire.beginTransmission(address);
  329.     Wire.write(value);
  330.     Wire.endTransmission(address);
  331. }
  332. void motor_send(int motorAdr, int value, bool direction, unsigned long *prevTime)
  333. {
  334.     if(currentMillis - *prevTime >= I2CsendRate)
  335.     {
  336.         I2C_send(motorAdr, direction);
  337.         I2C_send(motorAdr, value);
  338.         *prevTime = currentMillis;
  339.     }
  340. }
  341.  
  342. String read_bt(){
  343.     String bt_message = "";
  344.     if (bt_serial.available())
  345.     {
  346.         bt_message = bt_serial.readStringUntil('\n');
  347.     }
  348.  
  349.     if (bt_message.length() > 0)
  350.     {
  351.         return bt_message;
  352.     }
  353.  
  354. }
  355. void setup()
  356. {
  357.     //Ustawienia USB-UART
  358.     Serial.begin(115200);
  359.     Serial.println("Inicjalizacja ESP8266!");
  360.  
  361.     //Połączenie GPS z ESP
  362.     gps_uart.begin(9600);
  363.  
  364.     bt_serial.begin(9600);
  365.  
  366.     //I2C Protokół
  367.     Wire.begin();
  368.  
  369.     //Kompas
  370.     compass.init();
  371.     compass.enableDefault();
  372.     compass.m_min = (LSM303::vector<int16_t>){-2453, -2735, -2865};
  373.     compass.m_max = (LSM303::vector<int16_t>){+3635, +3145, +3089};
  374.  
  375.     WiFi_settings();
  376.     OTA_updater();
  377. }
  378. void loop()
  379. {
  380.     currentMillis = millis();
  381.     ArduinoOTA.handle();
  382.     get_interface_info(WIFI_data, &manualControl, &smartfonPositionGlobal.x, &smartfonPositionGlobal.y, &robotDirectionMotor1, &robotDirectionMotor2, &robotMotor1Val, &robotMotor2Val, &robotAlarm);
  383.     if(currentMillis - prevMillis >= 500)
  384.     {
  385.         Serial.print("Manual Control: ");
  386.         Serial.println(manualControl);
  387.         Serial.print("Smartfon X global: ");
  388.         Serial.println(smartfonPositionGlobal.x, 6);
  389.         Serial.print("Smartfon Y global: ");
  390.         Serial.println(smartfonPositionGlobal.y, 6);
  391.         Serial.print("Front/Back-ward motor1: ");
  392.         Serial.println(robotDirectionMotor1);
  393.         Serial.print("Front/Back-ward motor2: ");
  394.         Serial.println(robotDirectionMotor2);
  395.         Serial.print("Motor1: ");
  396.         Serial.println(robotMotor1Val);
  397.         Serial.print("Motor2: ");
  398.         Serial.println(robotMotor2Val);
  399.         Serial.println("");
  400.         prevMillis = currentMillis;
  401.     }
  402.  
  403.     if(manualControl == true)
  404.     {
  405.         motor_send(ESC1Address, robotMotor1Val, robotDirectionMotor1, &I2Cmotor1);
  406.         motor_send(ESC2Address, robotMotor2Val, robotDirectionMotor2, &I2Cmotor2);
  407.     }
  408.     if(robotAlarm)
  409.     {
  410.         I2C_send(ESC1Address, 0);
  411.         I2C_send(ESC2Address, 0);
  412.     }    
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement