---
sidebar_position: 3
---
# Chapter 2: Sensors and Environment Building
## Learning Objectives
- Understand how to simulate various robot sensors in Gazebo
- Build realistic environments for robot testing
- Configure sensor models with appropriate parameters
- Integrate simulated sensors with ROS 2 systems
- Validate sensor data for humanoid robot applications
## Sensor Simulation in Robotics
Sensor simulation is a critical component of robot development, allowing for safe and cost-effective testing of perception algorithms. In humanoid robotics, accurate sensor simulation is particularly important due to the complex interaction between the robot and its environment.
### Types of Sensors in Humanoid Robots
Humanoid robots typically use these sensor types:
1. **Proprioceptive Sensors**: Measure internal robot state
- Joint encoders: Position, velocity of joints
- IMUs: Orientation, angular velocity, acceleration
- Force/Torque sensors: Forces at joints and end effectors
2. **Exteroceptive Sensors**: Measure environment
- Cameras: Visual perception
- LIDAR: Distance measurements for navigation
- Sonar: Additional distance sensing
- Tactile sensors: Contact detection
## Gazebo Sensor Plugins
Gazebo provides plugins for simulating various sensors. These plugins publish data to ROS topics that can be processed by robot algorithms.
### Camera Sensors
Camera sensors in Gazebo simulate RGB cameras and publish images to ROS topics:
```xml
1.089640480R8G8B80.1100camera_frameimage_raw
```
### LIDAR Sensors
LIDAR (Light Detection and Ranging) sensors simulate laser range finders:
```xml
7201-1.5707961.5707960.130.00.01scanlidar_frame
```
### IMU Sensors
IMU (Inertial Measurement Unit) sensors provide orientation and acceleration data:
```xml
true1000.02e-40.02e-40.02e-40.01.7e-20.01.7e-20.01.7e-2imuimu_linkimu_link
```
## Environment Building in Gazebo
Creating realistic environments is crucial for meaningful robot testing. Gazebo provides several methods to build environments:
### World Files
World files define the complete simulation environment:
```xml
model://sunmodel://ground_plane-1 0 0 0 0 0model://table-1.5 0.5 0 0 0 1.57model://chair-0.8 0.3 0.5 0 0 00.1 0.1 0.10.1 0.1 0.11 0 0 11 0 0 10.10.00010.00010.0001
```
### Building Complex Environments
For humanoid robots, environments should include:
- **Navigation areas**: Open spaces for walking, pathways
- **Obstacles**: Furniture, walls, other objects to navigate around
- **Interaction objects**: Items for manipulation tasks
- **Markers/landmarks**: Objects for localization and mapping
- **Varied terrain**: Different floor materials, slight inclines, stairs (for advanced robots)
## Sensor Integration with ROS 2
Once sensors are configured in Gazebo, they need to be integrated with ROS 2 systems:
### Camera Data Processing Node
```python
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
class CameraProcessor(Node):
def __init__(self):
super().__init__('camera_processor')
self.subscription = self.create_subscription(
Image,
'/camera/image_raw',
self.image_callback,
10)
self.subscription # prevent unused variable warning
self.bridge = CvBridge()
def image_callback(self, msg):
# Convert ROS Image message to OpenCV image
cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8")
# Process the image (example: detect edges)
gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# Display the result
cv2.imshow("Camera View", cv_image)
cv2.imshow("Edges", edges)
cv2.waitKey(1)
def main(args=None):
rclpy.init(args=args)
camera_processor = CameraProcessor()
rclpy.spin(camera_processor)
cv2.destroyAllWindows()
camera_processor.destroy_node()
rclpy.shutdown()
```
### LIDAR Processing
```python
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
import numpy as np
class LidarProcessor(Node):
def __init__(self):
super().__init__('lidar_processor')
self.subscription = self.create_subscription(
LaserScan,
'/scan',
self.scan_callback,
10)
self.subscription # prevent unused variable warning
def scan_callback(self, msg):
# Process LIDAR data
# Convert to numpy array for easier processing
ranges = np.array(msg.ranges)
# Find minimum distance (closest obstacle)
valid_ranges = ranges[np.isfinite(ranges)] # Remove invalid (inf) values
if len(valid_ranges) > 0:
min_distance = np.min(valid_ranges)
self.get_logger().info(f'Closest obstacle: {min_distance:.2f}m')
# Simple obstacle detection
threshold = 1.0 # meters
obstacles = valid_ranges < threshold
obstacle_count = np.sum(obstacles)
if obstacle_count > 0:
self.get_logger().info(f'Found {obstacle_count} obstacles within {threshold}m')
def main(args=None):
rclpy.init(args=args)
lidar_processor = LidarProcessor()
rclpy.spin(lidar_processor)
lidar_processor.destroy_node()
rclpy.shutdown()
```
## Humanoid Robot Specific Sensors
Humanoid robots have unique sensor requirements:
### Balance Sensors
- **ZMP (Zero Moment Point) sensors**: Critical for bipedal stability
- **Force plates**: Measure ground reaction forces
- **Foot contact sensors**: Detect when feet make contact with ground
### Manipulation Sensors
- **Tactile sensors**: On fingertips for object manipulation
- **Force/Torque sensors**: In wrists to measure interaction forces
- **Stereo cameras**: For depth perception during manipulation
## Sensor Validation
Validating simulated sensors is crucial:
1. **Compare to real sensors**: When possible, compare simulated sensor data to real hardware
2. **Physics consistency**: Ensure sensor readings make sense given the simulated physics
3. **Timing accuracy**: Verify sensors publish at the correct rate
4. **Noise characteristics**: Ensure realistic noise models
## Example: Complete Sensor Setup for Humanoid Robot
```xml
0.05 0 0 0 0 01.089640480R8G8B80.110head_camera_framehead_camera/image_raw0 0 0 0 0 0true100imu/headhead_imu_frame
```
## Troubleshooting Common Issues
### Sensor Not Publishing
- Check if the plugin is loaded correctly
- Verify topic names and namespaces
- Ensure the sensor has power/connections in the model
### Incorrect Sensor Data
- Verify sensor placement in the model
- Check coordinate frame transformations
- Validate sensor parameters (FOV, range, etc.)
### Performance Issues
- Reduce sensor update rates if not needed
- Lower image resolution for cameras
- Use fewer LIDAR rays if precision allows
## Summary
Sensor simulation is vital for developing and testing humanoid robots safely and efficiently. Proper configuration of sensor models, integration with ROS 2 systems, and validation of sensor data are crucial steps in creating realistic simulations. The environments you create should match the complexity of the real-world scenarios your humanoid robot will encounter.
## Exercises
1. Add a camera sensor to your simulated robot and visualize the output
2. Create a simple environment with obstacles for navigation testing
3. Implement a basic LIDAR obstacle detection node
## Next Steps
In the next chapter, we'll explore high-fidelity rendering and human-robot interaction using Unity, providing a different perspective on robot simulation and visualization.