-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.cpp
More file actions
167 lines (143 loc) · 5.51 KB
/
Copy pathindex.cpp
File metadata and controls
167 lines (143 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <chrono>
#include <thread>
#include <wiringPi.h>
#include <softPwm.h>
#include <unistd.h>
#include <cstdlib>
#include <string>
// Constants for GPIO pins
#define TRIG 23
#define ECHO 24
#define PIR 4
#define SERVO_PIN 18
// Function prototypes
double measureDistance();
void setAngle(int angle);
void radarScan();
void saveDataForPlotting(const std::vector<int>& angles, const std::vector<double>& distances);
int main() {
// Initialize wiringPi
if (wiringPiSetupGpio() == -1) {
std::cerr << "Failed to initialize wiringPi." << std::endl;
return 1;
}
// Setup GPIO pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(PIR, INPUT);
// Initialize servo using softPwm
softPwmCreate(SERVO_PIN, 0, 200);
std::cout << "Starting radar system..." << std::endl;
radarScan();
return 0;
}
// Measure distance using ultrasonic sensor
double measureDistance() {
// Send trigger pulse
digitalWrite(TRIG, HIGH);
usleep(10); // 10 microseconds
digitalWrite(TRIG, LOW);
// Wait for echo to start
auto startTime = std::chrono::high_resolution_clock::now();
auto endTime = startTime;
// Wait for echo pin to go HIGH (pulse start)
while (digitalRead(ECHO) == LOW) {
startTime = std::chrono::high_resolution_clock::now();
// Add timeout to prevent infinite loop
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - endTime).count();
if (duration > 100) return -1; // Timeout after 100ms
}
// Wait for echo pin to go LOW (pulse end)
while (digitalRead(ECHO) == HIGH) {
endTime = std::chrono::high_resolution_clock::now();
// Add timeout to prevent infinite loop
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - startTime).count();
if (duration > 100) return -1; // Timeout after 100ms
}
// Calculate duration in seconds
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
endTime - startTime).count() / 1000000.0;
// Calculate distance (speed of sound = 34300 cm/s, divide by 2 for two-way trip)
return duration * 34300.0 / 2.0;
}
// Set servo angle
void setAngle(int angle) {
// Convert angle to pulse width
// Map 0-180 degrees to 5-25 (0.5ms to 2.5ms pulse width)
int pulseWidth = 5 + (angle * 20) / 180;
softPwmWrite(SERVO_PIN, pulseWidth);
// Give servo time to move
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
// Main radar scanning function
void radarScan() {
std::vector<int> angles;
std::vector<double> distances;
try {
while (true) {
angles.clear();
distances.clear();
// Scan from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle += 5) {
setAngle(angle);
double distance = measureDistance();
bool pirSignal = digitalRead(PIR);
if (pirSignal) {
std::cout << "Live object! Angle: " << angle
<< "°, Distance: " << distance << " cm" << std::endl;
angles.push_back(angle);
distances.push_back(distance);
}
}
// Save data for external plotting
saveDataForPlotting(angles, distances);
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
// Cleanup
std::cout << "Shutting down radar..." << std::endl;
softPwmStop(SERVO_PIN);
}
// Save radar data to file for external plotting
void saveDataForPlotting(const std::vector<int>& angles, const std::vector<double>& distances) {
std::ofstream dataFile("radar_data.txt");
if (dataFile.is_open()) {
dataFile << "Angle,Distance\n";
for (size_t i = 0; i < angles.size(); ++i) {
dataFile << angles[i] << "," << distances[i] << "\n";
}
dataFile.close();
std::cout << "Data saved for plotting" << std::endl;
// Generate plotting script
std::ofstream scriptFile("plot_radar.py");
if (scriptFile.is_open()) {
scriptFile << "import numpy as np\n";
scriptFile << "import matplotlib.pyplot as plt\n";
scriptFile << "import pandas as pd\n\n";
scriptFile << "data = pd.read_csv('radar_data.txt')\n";
scriptFile << "angles = np.radians(data['Angle'])\n";
scriptFile << "distances = data['Distance']\n\n";
scriptFile << "fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})\n";
scriptFile << "ax.set_theta_zero_location('N')\n";
scriptFile << "ax.set_theta_direction(-1)\n";
scriptFile << "ax.set_thetamin(0)\n";
scriptFile << "ax.set_thetamax(180)\n";
scriptFile << "ax.scatter(angles, distances, color='red', s=30)\n";
scriptFile << "ax.set_facecolor('#004545')\n";
scriptFile << "plt.savefig('radar_plot.png')\n";
scriptFile << "plt.show()\n";
scriptFile.close();
// Attempt to run the plot script
system("python3 plot_radar.py &");
}
} else {
std::cerr << "Unable to open file for data saving" << std::endl;
}
}