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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
---
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
<!-- Example camera sensor in a URDF/SDF -->
<sensor name="camera" type="camera">
  <camera name="head">
    <horizontal_fov>1.089</horizontal_fov>
    <image>
      <width>640</width>
      <height>480</height>
      <format>R8G8B8</format>
    </image>
    <clip>
      <near>0.1</near>
      <far>100</far>
    </clip>
  </camera>
  <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
    <frame_name>camera_frame</frame_name>
    <topic_name>image_raw</topic_name>
  </plugin>
</sensor>
```

### LIDAR Sensors

LIDAR (Light Detection and Ranging) sensors simulate laser range finders:

```xml
<sensor name="lidar" type="ray">
  <ray>
    <scan>
      <horizontal>
        <samples>720</samples>
        <resolution>1</resolution>
        <min_angle>-1.570796</min_angle>
        <max_angle>1.570796</max_angle>
      </horizontal>
    </scan>
    <range>
      <min>0.1</min>
      <max>30.0</max>
      <resolution>0.01</resolution>
    </range>
  </ray>
  <plugin name="lidar_controller" filename="libgazebo_ros_laser.so">
    <topic_name>scan</topic_name>
    <frame_name>lidar_frame</frame_name>
  </plugin>
</sensor>
```

### IMU Sensors

IMU (Inertial Measurement Unit) sensors provide orientation and acceleration data:

```xml
<sensor name="imu_sensor" type="imu">
  <always_on>true</always_on>
  <update_rate>100</update_rate>
  <imu>
    <angular_velocity>
      <x>
        <noise type="gaussian">
          <mean>0.0</mean>
          <stddev>2e-4</stddev>
        </noise>
      </x>
      <y>
        <noise type="gaussian">
          <mean>0.0</mean>
          <stddev>2e-4</stddev>
        </noise>
      </y>
      <z>
        <noise type="gaussian">
          <mean>0.0</mean>
          <stddev>2e-4</stddev>
        </noise>
      </z>
    </angular_velocity>
    <linear_acceleration>
      <x>
        <noise type="gaussian">
          <mean>0.0</mean>
          <stddev>1.7e-2</stddev>
        </noise>
      </x>
      <y>
        <noise type="gaussian">
          <mean>0.0</mean>
          <stddev>1.7e-2</stddev>
        </noise>
      </y>
      <z>
        <noise type="gaussian">
          <mean>0.0</mean>
          <stddev>1.7e-2</stddev>
        </noise>
      </z>
    </linear_acceleration>
  </imu>
  <plugin name="imu_plugin" filename="libgazebo_ros_imu.so">
    <topic_name>imu</topic_name>
    <body_name>imu_link</body_name>
    <frame_name>imu_link</frame_name>
  </plugin>
</sensor>
```

## 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
<?xml version="1.0" ?>
<sdf version="1.6">
  <world name="small_room">
    <!-- Include the sun -->
    <include>
      <uri>model://sun</uri>
    </include>
    
    <!-- Include the ground plane -->
    <include>
      <uri>model://ground_plane</uri>
    </include>
    
    <!-- Add furniture -->
    <model name="table">
      <pose>-1 0 0 0 0 0</pose>
      <include>
        <uri>model://table</uri>
      </include>
    </model>
    
    <model name="chair">
      <pose>-1.5 0.5 0 0 0 1.57</pose>
      <include>
        <uri>model://chair</uri>
      </include>
    </model>
    
    <!-- Add objects for manipulation -->
    <model name="box">
      <pose>-0.8 0.3 0.5 0 0 0</pose>
      <link name="box_link">
        <collision name="collision">
          <geometry>
            <box>
              <size>0.1 0.1 0.1</size>
            </box>
          </geometry>
        </collision>
        <visual name="visual">
          <geometry>
            <box>
              <size>0.1 0.1 0.1</size>
            </box>
          </geometry>
          <material>
            <ambient>1 0 0 1</ambient>
            <diffuse>1 0 0 1</diffuse>
          </material>
        </visual>
        <inertial>
          <mass>0.1</mass>
          <inertia>
            <ixx>0.0001</ixx>
            <iyy>0.0001</iyy>
            <izz>0.0001</izz>
          </inertia>
        </inertial>
      </link>
    </model>
  </world>
</sdf>
```

### 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
<!-- Example link with multiple sensors -->
<link name="head">
  <visual>
    <geometry>
      <sphere radius="0.1"/>
    </geometry>
  </visual>
  <collision>
    <geometry>
      <sphere radius="0.1"/>
    </geometry>
  </collision>
  <inertial>
    <mass value="2.0"/>
    <inertia ixx="0.0083" ixy="0" ixz="0" iyy="0.0083" iyz="0" izz="0.0083"/>
  </inertial>
  
  <!-- Head camera -->
  <sensor name="head_camera" type="camera">
    <pose>0.05 0 0 0 0 0</pose>
    <camera name="head">
      <horizontal_fov>1.089</horizontal_fov>
      <image>
        <width>640</width>
        <height>480</height>
        <format>R8G8B8</format>
      </image>
      <clip>
        <near>0.1</near>
        <far>10</far>
      </clip>
    </camera>
    <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
      <frame_name>head_camera_frame</frame_name>
      <topic_name>head_camera/image_raw</topic_name>
    </plugin>
  </sensor>
  
  <!-- IMU in head -->
  <sensor name="head_imu" type="imu">
    <pose>0 0 0 0 0 0</pose>
    <always_on>true</always_on>
    <update_rate>100</update_rate>
    <plugin name="imu_plugin" filename="libgazebo_ros_imu.so">
      <topic_name>imu/head</topic_name>
      <frame_name>head_imu_frame</frame_name>
    </plugin>
  </sensor>
</link>
```

## 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.