Sql-database-cars
To integrate sensor software with your full-stack Carla-based application, you'll need to use Carla’s sensor API to collect real-time sensor data from vehicles in the simulation. You can then store this data in MySQL and display it in the React frontend.
1. Types of Sensors in Carla
Carla supports various sensors, such as:
- Camera Sensor (RGB, Depth, Semantic Segmentation)
- LIDAR Sensor
- Radar Sensor
- IMU Sensor (Inertial Measurement Unit)
- GNSS Sensor (GPS data)
- Collision Sensor
- Lane Invasion Sensor
2. Steps to Integrate Carla Sensors
Step 1: Install Required Carla Dependencies
Ensure you have Carla installed. If not, install it:
git clone --depth=1 https://github.com/carla-simulator/carla
cd carla
pip install -r PythonAPI/carla/requirements.txt
3. Backend - Node.js & Express.js (Sensor Data API)
We will write a Python script that collects data from Carla sensors and sends it to the Node.js backend, which stores it in MySQL.
Step 1: Create MySQL Table for Sensors
Run this SQL query in MySQL:
CREATE TABLE sensor_data (
id INT AUTO_INCREMENT PRIMARY KEY,
vehicle_id VARCHAR(50),
sensor_type VARCHAR(50),
sensor_value TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
4. Python Sensor Data Collection (sensor_data.py)
This script will run inside Carla and send sensor data to the Node.js backend.
import carla
import requests
import time
import json
# Carla Server Configuration
client = carla.Client("localhost", 2000)
client.set_timeout(10.0)
world = client.get_world()
# Find a vehicle to attach sensors
blueprint_library = world.get_blueprint_library()
vehicles = world.get_actors().filter("vehicle.*")
if not vehicles:
print("No vehicles found!")
exit()
vehicle = vehicles[0] # Attach sensors to the first vehicle
print(f"Attaching sensors to vehicle: {vehicle.id}")
# Define callback function to send data to backend
def send_sensor_data(sensor_type, data):
payload = {
"vehicle_id": str(vehicle.id),
"sensor_type": sensor_type,
"sensor_value": json.dumps(data),
}
try:
response = requests.post("http://localhost:5000/sensor-data", json=payload)
print(f"{sensor_type} data sent: {response.status_code}")
except Exception as e:
print(f"Error sending {sensor_type} data: {e}")
# Attach a Camera Sensor
camera_bp = blueprint_library.find("sensor.camera.rgb")
camera_transform = carla.Transform(carla.Location(x=0.8, z=1.7))
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
def camera_callback(image):
send_sensor_data("camera", {"frame": image.frame, "width": image.width, "height": image.height})
camera.listen(camera_callback)
# Attach a LIDAR Sensor
lidar_bp = blueprint_library.find("sensor.lidar.ray_cast")
lidar_transform = carla.Transform(carla.Location(x=0, z=2.5))
lidar = world.spawn_actor(lidar_bp, lidar_transform, attach_to=vehicle)
def lidar_callback(point_cloud):
send_sensor_data("lidar", {"num_points": len(point_cloud)})
lidar.listen(lidar_callback)
# Attach an IMU Sensor (Inertial Measurement Unit)
imu_bp = blueprint_library.find("sensor.other.imu")
imu = world.spawn_actor(imu_bp, carla.Transform(), attach_to=vehicle)
def imu_callback(data):
send_sensor_data("imu", {
"accelerometer": data.accelerometer,
"gyroscope": data.gyroscope,
"compass": data.compass,
})
imu.listen(imu_callback)
# Run the simulation for a while
try:
time.sleep(60) # Collect data for 60 seconds
finally:
camera.destroy()
lidar.destroy()
imu.destroy()
print("Sensors destroyed.")
5. Node.js Backend to Store Sensor Data
Add the following sensor data endpoint to server.js:
app.post("/sensor-data", (req, res) => {
const { vehicle_id, sensor_type, sensor_value } = req.body;
db.query(
"INSERT INTO sensor_data (vehicle_id, sensor_type, sensor_value) VALUES (?, ?, ?)",
[vehicle_id, sensor_type, sensor_value],
(err, result) => {
if (err) {
console.error(err);
res.status(500).send("Error storing sensor data.");
} else {
res.status(200).json({ message: "Sensor data stored successfully." });
}
}
);
});
// Fetch sensor data
app.get("/sensor-data", (req, res) => {
db.query("SELECT * FROM sensor_data", (err, results) => {
if (err) {
console.error(err);
res.status(500).send("Error retrieving sensor data.");
} else {
res.status(200).json(results);
}
});
});
6. Frontend - Display Sensor Data in React
Modify frontend/src/components/VehicleData.jsx to show sensor data:
import React, { useEffect, useState } from "react";
import axios from "axios";
const SensorData = () => {
const [sensorData, setSensorData] = useState([]);
useEffect(() => {
axios.get("http://localhost:5000/sensor-data").then((response) => {
setSensorData(response.data);
});
}, []);
return (
<div>
<h2>Sensor Data</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Vehicle ID</th>
<th>Sensor Type</th>
<th>Sensor Value</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
{sensorData.map((data) => (
<tr key={data.id}>
<td>{data.id}</td>
<td>{data.vehicle_id}</td>
<td>{data.sensor_type}</td>
<td>{JSON.stringify(data.sensor_value)}</td>
<td>{data.timestamp}</td>
</tr>
))}
</tbody>
</table>
</div>
);
};
export default SensorData;
7. Update App.js to Include Sensor Data
Modify App.js:
import React from "react";
import VehicleData from "./components/VehicleData";
import SensorData from "./components/SensorData";
function App() {
return (
<div>
<h1>Carla Full Stack App</h1>
<VehicleData />
<SensorData />
</div>
);
}
export default App;
Final Steps to Run the Application
Step 1: Start Carla Simulation
Run Carla:
cd /path/to/carla
./CarlaUE4.sh -opengl
Step 2: Start Python Sensor Script
Run the sensor script:
python sensor_data.py
Step 3: Start Node.js Backend
Inside backend/:
node server.js
Step 4: Start React Frontend
Inside frontend/:
npm start
Final Overview
✅ Carla Simulation runs and sends vehicle & sensor data.
✅ Node.js Backend receives data and stores it in MySQL.
✅ React.js Frontend fetches & displays real-time sensor and vehicle data.
Now, you have a full-stack application integrated with Carla sensors. Let me know if you need any modifications!
Comments
Post a Comment