Skip to content

Commit e985f7a

Browse files
committed
Add a car demo for electron
1 parent b74162e commit e985f7a

File tree

12 files changed

+1029
-5
lines changed

12 files changed

+1029
-5
lines changed

electron_demo/car/README.md

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
# ROS2 Car Control Demo with Electron and rclnodejs
2+
3+
This demo showcases how to use **rclnodejs** with **Electron** to create an interactive car control application. The demo features a virtual joystick that publishes ROS2 velocity commands and a car visualization that responds to those commands in real-time.
4+
5+
![Demo Screenshot](demo-screenshot.png)
6+
7+
## 🚗 Features
8+
9+
### Joystick Control Panel
10+
11+
- **Directional Controls**: Up/Down/Left/Right buttons for car movement
12+
- **Stop Button**: Emergency stop functionality
13+
- **Keyboard Support**: WASD and arrow keys for control
14+
- **Real-time Status**: Display current command and velocity values
15+
16+
### Car Visualization
17+
18+
- **Real-time Movement**: Car moves and rotates based on received commands
19+
- **Position Tracking**: Shows current position (North/South/East/West/Center)
20+
- **Visual Feedback**: Color-coded movement indicators
21+
- **Command Counter**: Tracks total number of commands received
22+
23+
### ROS2 Integration
24+
25+
- **Topic**: `cmd_vel` (geometry_msgs/Twist)
26+
- **Publisher**: Sends velocity commands
27+
- **Subscriber**: Receives and displays velocity commands
28+
- **Node Name**: `car_control_node`
29+
30+
## 🔧 Prerequisites
31+
32+
Before running this demo, ensure you have:
33+
34+
1. **ROS2 installed** (Humble, Jazzy, Kilted, or Rolling)
35+
2. **Node.js** (version 16 or higher)
36+
3. **rclnodejs built** and working
37+
38+
### ROS2 Setup
39+
40+
```bash
41+
# Source your ROS2 installation
42+
source /opt/ros/<your-ros-distro>/setup.bash
43+
44+
# Verify ROS2 is working
45+
ros2 topic list
46+
```
47+
48+
### Build rclnodejs
49+
50+
From the main rclnodejs directory:
51+
52+
```bash
53+
npm install
54+
npm run build
55+
```
56+
57+
## 🚀 Installation & Running
58+
59+
1. **Navigate to the demo directory:**
60+
61+
```bash
62+
cd electron_car_demo
63+
```
64+
65+
2. **Install dependencies:**
66+
67+
```bash
68+
npm install
69+
```
70+
71+
3. **Rebuild native modules for Electron:**
72+
73+
```bash
74+
npm run rebuild
75+
```
76+
77+
4. **Start the demo:**
78+
```bash
79+
# Make sure ROS2 is sourced first
80+
source /opt/ros/<your-ros-distro>/setup.bash
81+
npm start
82+
```
83+
84+
![demo screenshot](./car-control-electron.gif)
85+
86+
## 🎮 How to Use
87+
88+
### Control Methods
89+
90+
#### Mouse Controls
91+
92+
- Click the directional buttons (↑↓←→) on the joystick
93+
- Click the red **STOP** button to halt movement
94+
95+
#### Keyboard Controls
96+
97+
- **W** or ****: Move forward
98+
- **S** or ****: Move backward
99+
- **A** or ****: Turn left
100+
- **D** or ****: Turn right
101+
- **Space** or **Esc**: Stop
102+
103+
### Understanding the Interface
104+
105+
#### Left Panel - Joystick Control
106+
107+
- **Command**: Shows the current command being sent
108+
- **Linear X**: Forward/backward velocity (m/s)
109+
- **Angular Z**: Rotation velocity (rad/s)
110+
- **Topic**: ROS2 topic name (`cmd_vel`)
111+
112+
#### Right Panel - Car Visualization
113+
114+
- **Received Commands**: Total count of commands received
115+
- **Last Command**: Most recent command processed
116+
- **Car Position**: Current position in the visualization area
117+
118+
## 🔍 Technical Details
119+
120+
### ROS2 Message Format
121+
122+
The demo uses `geometry_msgs/Twist` messages with the following structure:
123+
124+
```javascript
125+
{
126+
linear: {
127+
x: 1.0, // Forward/backward velocity (m/s)
128+
y: 0.0, // Left/right velocity (typically 0 for cars)
129+
z: 0.0 // Up/down velocity (typically 0 for ground vehicles)
130+
},
131+
angular: {
132+
x: 0.0, // Roll rate (typically 0 for cars)
133+
y: 0.0, // Pitch rate (typically 0 for cars)
134+
z: 1.0 // Yaw rate (turning left/right in rad/s)
135+
}
136+
}
137+
```
138+
139+
### Command Mapping
140+
141+
| Command | Linear X | Angular Z | Description |
142+
| -------- | -------- | --------- | ---------------------- |
143+
| Forward | +1.0 | 0.0 | Move forward at 1 m/s |
144+
| Backward | -1.0 | 0.0 | Move backward at 1 m/s |
145+
| Left | 0.0 | +1.0 | Turn left at 1 rad/s |
146+
| Right | 0.0 | -1.0 | Turn right at 1 rad/s |
147+
| Stop | 0.0 | 0.0 | Stop all movement |
148+
149+
### Architecture
150+
151+
```
152+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
153+
│ Renderer │ │ Main Process │ │ ROS2 Network │
154+
│ (UI/Browser) │ │ (Node.js) │ │ │
155+
├─────────────────┤ ├──────────────────┤ ├─────────────────┤
156+
│ • Joystick UI │───▶│ • rclnodejs Node │───▶│ • cmd_vel Topic │
157+
│ • Car Display │◀───│ • Publisher │ │ • Other Nodes │
158+
│ • Status Panel │ │ • Subscriber │◀───│ • Robot/Sim │
159+
└─────────────────┘ └──────────────────┘ └─────────────────┘
160+
```
161+
162+
## 🧪 Testing with ROS2 Tools
163+
164+
You can test the demo using standard ROS2 command-line tools:
165+
166+
### Listen to Published Commands
167+
168+
```bash
169+
# In a new terminal (with ROS2 sourced)
170+
ros2 topic echo /cmd_vel
171+
```
172+
173+
### Send Commands from Command Line
174+
175+
```bash
176+
# Send a forward command
177+
ros2 topic pub /cmd_vel geometry_msgs/Twist "linear: {x: 1.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}"
178+
179+
# Send a turn left command
180+
ros2 topic pub /cmd_vel geometry_msgs/Twist "linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.0}"
181+
182+
# Stop command
183+
ros2 topic pub /cmd_vel geometry_msgs/Twist "linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}"
184+
```
185+
186+
### Check Active Topics
187+
188+
```bash
189+
ros2 topic list
190+
ros2 topic info /cmd_vel
191+
ros2 topic hz /cmd_vel
192+
```
193+
194+
## 🤖 Integration with Real Robots
195+
196+
This demo can be easily connected to real robots or simulators:
197+
198+
### TurtleBot3 (Example)
199+
200+
```bash
201+
# Launch TurtleBot3 simulation
202+
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py
203+
204+
# The demo will automatically control the robot via cmd_vel topic
205+
```
206+
207+
### Custom Robot
208+
209+
Ensure your robot subscribes to the `/cmd_vel` topic with `geometry_msgs/Twist` messages.
210+
211+
## 🛠️ Customization
212+
213+
### Modify Velocity Values
214+
215+
Edit the `speed` and `turnSpeed` constants in `main.js`:
216+
217+
```javascript
218+
const speed = 1.0; // Linear velocity (m/s)
219+
const turnSpeed = 1.0; // Angular velocity (rad/s)
220+
```
221+
222+
### Change Topic Name
223+
224+
Modify the topic name in `main.js`:
225+
226+
```javascript
227+
// Change 'cmd_vel' to your desired topic name
228+
carControlPublisher = carControlNode.createPublisher(
229+
'geometry_msgs/msg/Twist',
230+
'your_topic_name'
231+
);
232+
```
233+
234+
### Add More Commands
235+
236+
Extend the joystick commands by modifying the switch statement in `main.js` and adding corresponding UI elements.
237+
238+
## 🐛 Troubleshooting
239+
240+
### Common Issues
241+
242+
1. **"Failed to initialize ROS2" Error**
243+
244+
- Ensure ROS2 is properly sourced before running npm start
245+
- Check that rclnodejs is built correctly
246+
247+
2. **Native Module Rebuild Issues**
248+
249+
```bash
250+
npm run rebuild
251+
# or manually:
252+
./node_modules/.bin/electron-rebuild
253+
```
254+
255+
3. **Topic Not Appearing**
256+
257+
- Verify ROS2 daemon is running: `ros2 daemon status`
258+
- Check topic list: `ros2 topic list`
259+
260+
4. **Car Not Moving in UI**
261+
- Check browser console for JavaScript errors
262+
- Verify IPC communication between main and renderer processes
263+
264+
### Debug Mode
265+
266+
Add debug logging by modifying `main.js`:
267+
268+
```javascript
269+
// Enable debug logging
270+
console.log('Publishing command:', command, twist);
271+
```
272+
273+
## 📚 Learning Resources
274+
275+
- [rclnodejs Documentation](https://github.com/RobotWebTools/rclnodejs)
276+
- [ROS2 Tutorials](https://docs.ros.org/en/rolling/Tutorials.html)
277+
- [Electron Documentation](https://www.electronjs.org/docs)
278+
- [geometry_msgs/Twist Documentation](https://docs.ros.org/en/rolling/p/geometry_msgs/interfaces/msg/Twist.html)
279+
280+
## 📄 License
281+
282+
This demo is licensed under the Apache License 2.0, same as the main rclnodejs project.
283+
284+
## 🤝 Contributing
285+
286+
Feel free to submit issues and enhancement requests! This demo serves as both a functional example and a starting point for more complex ROS2 Electron applications.
29 MB
Loading

0 commit comments

Comments
 (0)