Technology Sharing

UAV positioning of ground moving targets-obtaining the moving direction and speed of the target

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Table of contents

1. Introduction

We use a monocular drone to locate a moving target on the ground by taking photos at equal time intervals. Currently, we have obtained the three-dimensional coordinates of the target in each photo, and know the time interval between the photos taken by the drone during flight. Then we can obtain the target's movement direction and speed through certain calculations.

2. Code Explanation

1. The imported data is a txt file consisting of the photo name and the three-dimensional data of the target point, so we need to create a string segmentation function to obtain the data in the txt file.

2. Define the value of pi

3. Define direction calculation function

4. Extract required information from txt file

5. Calculate the target’s direction change angle between adjacent photos

6. Calculate the moving distance and speed of the target between adjacent photos

3. Complete code display

4. Results

All codes in this article are provided by CSDN user CV-X.WANG. Any individual or group may not conduct commercial or teaching activities. Any citation or partial citation requires authorization.


1. Introduction

        We use a monocular drone to locate a moving target on the ground by taking photos at equal time intervals. Currently, we have obtained the three-dimensional coordinates of the target in each photo, and know the time interval between the photos taken by the drone during flight. Then we can obtain the target's movement direction and speed through certain calculations.

2. Code Explanation

1. The imported data is a txt file consisting of the photo name and the three-dimensional data of the target point, so we need to create a string segmentation function to obtain the data in the txt file.

  1. //字符串分割
  2. vector<string> split(const string &s, char delimiter) {
  3. vector<string> tokens;
  4. string token;
  5. istringstream tokenStream(s);
  6. while (getline(tokenStream, token, delimiter)) {
  7. tokens.push_back(token);
  8. }
  9. return tokens;
  10. }

2. Define the value of pi

#define M_PI       3.14159265358979323846   // pi

3. Define direction calculation function

In order to obtain the moving direction of the target in the plane, this paper adopts the 360° direction method commonly used in the military field. That is, the north is 0°, and the clockwise direction is 0-360°. For example, the east direction is 90° in our direction system.

some of,

 double lon1_rad = lon1 * M_PI / 180.0;
 double lat1_rad = lat1 * M_PI / 180.0;
 double lon2_rad = lon2 * M_PI / 180.0;
 double lat2_rad = lat2 * M_PI / 180.0;

Measured in radians.

  1. //方向函数
  2. double calculateDirectionAngle(double lon1, double lat1, double lon2, double lat2) {
  3. // Convert degrees to radians
  4. double lon1_rad = lon1 * M_PI / 180.0;
  5. double lat1_rad = lat1 * M_PI / 180.0;
  6. double lon2_rad = lon2 * M_PI / 180.0;
  7. double lat2_rad = lat2 * M_PI / 180.0;
  8. // Calculate delta longitude and convert to radians
  9. double delta_lon_rad = (lon2 - lon1) * M_PI / 180.0;
  10. // Calculate y and x components
  11. double y = sin(delta_lon_rad) * cos(lat2_rad);
  12. double x = cos(lat1_rad) * sin(lat2_rad) - sin(lat1_rad) * cos(lat2_rad) * cos(delta_lon_rad);
  13. // Calculate direction angle in radians
  14. double direction_rad = atan2(y, x);
  15. // Convert direction angle to degrees
  16. double direction_deg = direction_rad * 180.0 / M_PI;
  17. // Ensure direction angle is within [0, 360) degrees
  18. if (direction_deg < 0) {
  19. direction_deg += 360.0;
  20. }
  21. return direction_deg;
  22. }

4. Extract required information from txt file

  1. ifstream file("LBH.txt");
  2. if (!file.is_open()) {
  3. cerr << "Could not open the file!" << endl;
  4. return 1;
  5. }
  6. string line;
  7. // Skip the header line
  8. getline(file, line);
  9. vector<vector<string>> extractedData;
  10. // Read each line from the file
  11. while (getline(file, line)) {
  12. vector<string> columns = split(line, 't');
  13. if (columns.size() < 16) {
  14. cerr << "Invalid line format" << endl;
  15. continue;
  16. }
  17. // Extract the required columns: 0, 13, 14, 15
  18. vector<string> extractedColumns;
  19. extractedColumns.push_back(columns[0]); // Image Name
  20. extractedColumns.push_back(columns[13]); // Longitude
  21. extractedColumns.push_back(columns[14]); // Latitude
  22. extractedColumns.push_back(columns[15]); // Altitude
  23. extractedData.push_back(extractedColumns);
  24. }
  25. file.close();

5. Calculate the target’s direction change angle between adjacent photos

  1. cout << "Direction angles between adjacent image centers:" << endl;
  2. for (size_t i = 1; i < extractedData.size(); ++i) {
  3. //三角函数计算用弧度制
  4. double lon1 = (stod(extractedData[i - 1][1]))* M_PI/180; // Longitude
  5. double lat1 = (stod(extractedData[i - 1][2]))* M_PI / 180; // Latitude
  6. double lon2 = (stod(extractedData[i][1]))* M_PI / 180; // Longitude
  7. double lat2 = (stod(extractedData[i][2]))* M_PI / 180; // Latitude
  8. //计算方向变化角也要用弧度制
  9. double direction_angle = calculateDirectionAngle(lon1, lat1, lon2, lat2);
  10. cout << "lon1=" << lon1 << endl << "lat1=" << lat1 << endl << "lon2=" << lon2 << endl << "lat2=" << lat2 << endl;
  11. // Output Direction
  12. cout << "Direction from " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << direction_angle << " degrees" << endl;

6. Calculate the moving distance and speed of the target between adjacent photos

Please note: Here we get the distance calculation formula as:

This is just the simplest demonstration. In actual situations, we need to consider a series of conditions such as the coordinate system and the location of the survey area to obtain a more accurate distance.

  1. double lon2_1 = lon2 - lon1;
  2. double lat2_1 = lat2 - lat1;
  3. double lon_ = lon2_1 / 2;//1/2的Δlon
  4. double lat_ = lat2_1 / 2; //1 / 2的Δlat
  5. double sin2lon_ = sin(lon_)*sin(lon_);//sin²(1/2Δlon)
  6. double sin2lat_ = sin(lat_)*sin(lat_); //sin²(1 / 2Δlat)
  7. double cos_lat1 = cos(lat1);
  8. double cos_lat2 = cos(lat2);
  9. double sqrtA = sqrt(sin2lat_+ cos_lat1* cos_lat2*sin2lon_);
  10. //cout << "Direction from " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << "sqrtA =" << sqrtA << endl;
  11. double asinA = asin(sqrtA);
  12. //长半轴 短半轴 单位是m
  13. int a_r = 6378137.0;
  14. int b_r = 6356752;
  15. double Earth_R = (2 * a_r + b_r) / 3;
  16. double Distance = 2 * Earth_R*asinA;
  17. cout << "Distance From " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << "=" << Distance <<" meter"<< endl;
  18. int time = 3;//拍照间隔 s
  19. double speed = Distance / time;
  20. cout << "Speed From " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << "=" << speed << " meter per second" << endl;
  21. }

3. Complete code display

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <cmath>
  6. using namespace std;
  7. #define M_PI 3.14159265358979323846 // pi
  8. // Function to split a string by a delimiter
  9. vector<string> split(const string &s, char delimiter) {
  10. vector<string> tokens;
  11. string token;
  12. istringstream tokenStream(s);
  13. while (getline(tokenStream, token, delimiter)) {
  14. tokens.push_back(token);
  15. }
  16. return tokens;
  17. }
  18. // direction angle in degrees
  19. //原理是 在平面上以正北方向为0°方向,顺时针为0-360°
  20. double calculateDirectionAngle(double lon1, double lat1, double lon2, double lat2) {
  21. // Convert degrees to radians
  22. double lon1_rad = lon1 * M_PI / 180.0;
  23. double lat1_rad = lat1 * M_PI / 180.0;
  24. double lon2_rad = lon2 * M_PI / 180.0;
  25. double lat2_rad = lat2 * M_PI / 180.0;
  26. // Calculate delta longitude and convert to radians
  27. double delta_lon_rad = (lon2 - lon1) * M_PI / 180.0;
  28. // Calculate y and x components
  29. double y = sin(delta_lon_rad) * cos(lat2_rad);
  30. double x = cos(lat1_rad) * sin(lat2_rad) - sin(lat1_rad) * cos(lat2_rad) * cos(delta_lon_rad);
  31. // Calculate direction angle in radians
  32. double direction_rad = atan2(y, x);
  33. // Convert direction angle to degrees
  34. double direction_deg = direction_rad * 180.0 / M_PI;
  35. // Ensure direction angle is within [0, 360) degrees
  36. if (direction_deg < 0) {
  37. direction_deg += 360.0;
  38. }
  39. return direction_deg;
  40. }
  41. int main() {
  42. ifstream file("LBH.txt");
  43. if (!file.is_open()) {
  44. cerr << "Could not open the file!" << endl;
  45. return 1;
  46. }
  47. string line;
  48. // Skip the header line
  49. getline(file, line);
  50. vector<vector<string>> extractedData;
  51. // Read each line from the file
  52. while (getline(file, line)) {
  53. vector<string> columns = split(line, 't');
  54. if (columns.size() < 16) {
  55. cerr << "Invalid line format" << endl;
  56. continue;
  57. }
  58. // Extract the required columns: 0, 13, 14, 15
  59. vector<string> extractedColumns;
  60. extractedColumns.push_back(columns[0]); // Image Name
  61. extractedColumns.push_back(columns[13]); // Longitude
  62. extractedColumns.push_back(columns[14]); // Latitude
  63. extractedColumns.push_back(columns[15]); // Altitude
  64. extractedData.push_back(extractedColumns);
  65. }
  66. file.close();
  67. // Calculate direction angles between adjacent image centers
  68. cout << "Direction angles between adjacent image centers:" << endl;
  69. for (size_t i = 1; i < extractedData.size(); ++i) {
  70. //三角函数计算用弧度制
  71. double lon1 = (stod(extractedData[i - 1][1]))* M_PI/180; // Longitude
  72. double lat1 = (stod(extractedData[i - 1][2]))* M_PI / 180; // Latitude
  73. double lon2 = (stod(extractedData[i][1]))* M_PI / 180; // Longitude
  74. double lat2 = (stod(extractedData[i][2]))* M_PI / 180; // Latitude
  75. //计算方向变化角也要用弧度制
  76. double direction_angle = calculateDirectionAngle(lon1, lat1, lon2, lat2);
  77. cout << "lon1=" << lon1 << endl << "lat1=" << lat1 << endl << "lon2=" << lon2 << endl << "lat2=" << lat2 << endl;
  78. // Output Direction
  79. cout << "Direction from " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << direction_angle << " degrees" << endl;
  80. double lon2_1 = lon2 - lon1;
  81. double lat2_1 = lat2 - lat1;
  82. double lon_ = lon2_1 / 2;//1/2的Δlon
  83. double lat_ = lat2_1 / 2; //1 / 2的Δlat
  84. double sin2lon_ = sin(lon_)*sin(lon_);//sin²(1/2Δlon)
  85. double sin2lat_ = sin(lat_)*sin(lat_); //sin²(1 / 2Δlat)
  86. double cos_lat1 = cos(lat1);
  87. double cos_lat2 = cos(lat2);
  88. double sqrtA = sqrt(sin2lat_+ cos_lat1* cos_lat2*sin2lon_);
  89. //cout << "Direction from " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << "sqrtA =" << sqrtA << endl;
  90. double asinA = asin(sqrtA);
  91. //长半轴 短半轴 单位是m
  92. int a_r = 6378137.0;
  93. int b_r = 6356752;
  94. double Earth_R = (2 * a_r + b_r) / 3;
  95. double Distance = 2 * Earth_R*asinA;
  96. cout << "Distance From " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << "=" << Distance <<" meter"<< endl;
  97. int time = 3;//拍照间隔 s
  98. double speed = Distance / time;
  99. cout << "Speed From " << extractedData[i - 1][0] << " to " << extractedData[i][0] << ": " << "=" << speed << " meter per second" << endl;
  100. }
  101. //cin.get();
  102. return 0;
  103. }

4. Results

All codes in this article are provided by CSDN user CV-X.WANGAny individual or group may not engage in commercial or teaching activities. Any quotation or partial quotation requires authorization.