BotNavSim  v0.4.3
Mobile Robot Simulation
UltrasonicSensor Class Reference

Ultrasonic sensor model. More...

Inheritance diagram for UltrasonicSensor:
Collaboration diagram for UltrasonicSensor:

Public Member Functions

override void Enable (Robot.SensorData callback)
 Enable this instance and pass the reference to the Robot.SensorData callback function to be called when the sensor has new information to share. More...
 
override void Disable ()
 Disable this instance. More...
 

Public Attributes

float updateDt
 The sensor update delta time in seconds. More...
 
float Fov
 The field of view angle in degrees. More...
 
float maxRange
 The max range of the sensor in metres. More...
 
LayerMask raycastLayer
 The raycast collision layer. More...
 

Properties

bool scanning [get, private set]
 Gets a value indicating whether this UltrasonicSensor is enabled and scanning. More...
 

Private Member Functions

IEnumerator Scan ()
 Scan for obstructions in a cone More...
 
float Cast (Vector3 direction)
 Cast a ray and return detected obstruction if the hit angle is less than a threshold More...
 
void Awake ()
 Awake this instance. More...
 
void OnDrawGizmos ()
 Raises the draw gizmos event. More...
 

Private Attributes

Robot.SensorData _callback
 The callback function to execute when the sensor has new data to share. More...
 
ProximityData _data
 Cached proximity data from the last scan More...
 
Color _recieveColor
 
Color _hitColor
 
Color _missColor
 

Detailed Description

Ultrasonic sensor model.

Definition at line 7 of file UltrasonicSensor.cs.

Member Function Documentation

void UltrasonicSensor.Awake ( )
private

Awake this instance.

Definition at line 144 of file UltrasonicSensor.cs.

144  {
145  _recieveColor = Color.red;
146  _recieveColor.a = 0.5f;
147  _hitColor = Color.green;
148  _hitColor.a = 0.25f;
149  _missColor = Color.grey;
150  _missColor.a = 0.5f;
151  }
float UltrasonicSensor.Cast ( Vector3  direction)
private

Cast a ray and return detected obstruction if the hit angle is less than a threshold

Parameters
directionDirection.

Definition at line 103 of file UltrasonicSensor.cs.

103  {
104  Ray ray = new Ray(transform.position, direction);
105  RaycastHit hit;
106  float proximity = maxRange;
107  if (Physics.Raycast(ray, out hit, maxRange, raycastLayer)) {
108  // if the hit angle is small enough, then assume the sound was reflected and recieved
109  if (Vector3.Angle(-hit.normal, direction) < 30f) {
110  proximity = hit.distance;
112  transform.position,
113  hit.point,
114  _recieveColor);
116  hit.point,
117  hit.point+hit.normal*0.1f,
118  Color.magenta);
119  }
120  else {
122  transform.position,
123  hit.point,
124  _hitColor);
126  hit.point,
127  hit.point+hit.normal*0.1f,
128  Color.cyan);
129  }
130  }
131  else {
133  transform.position,
134  transform.position + direction * maxRange,
135  _missColor);
136  }
137 
138  return proximity;
139  }
LayerMask raycastLayer
The raycast collision layer.
Draws stuff, useful for displaying debug data in the simulation. Note that GL implementations require...
Definition: Draw.cs:10
float maxRange
The max range of the sensor in metres.
void Line(Vector3 start, Vector3 end, Color color, Space relativeTo=Space.World)
Draw a line between two positions, start and end, in a specified color.
Definition: Draw.cs:76
static Draw Instance
Reference to the MonoBehaviour instance.
Definition: Draw.cs:39

Here is the call graph for this function:

Here is the caller graph for this function:

override void UltrasonicSensor.Disable ( )
virtual

Disable this instance.

Implements Sensor.

Definition at line 58 of file UltrasonicSensor.cs.

58  {
59  scanning = false;
60  }
bool scanning
Gets a value indicating whether this UltrasonicSensor is enabled and scanning.
override void UltrasonicSensor.Enable ( Robot.SensorData  callback)
virtual

Enable this instance and pass the reference to the Robot.SensorData callback function to be called when the sensor has new information to share.

Parameters
callbackCallback.

Implements Sensor.

Definition at line 52 of file UltrasonicSensor.cs.

52  {
53  scanning = true;
54  _callback = callback;
55  StartCoroutine(Scan());
56  }
Robot.SensorData _callback
The callback function to execute when the sensor has new data to share.
IEnumerator Scan()
Scan for obstructions in a cone
bool scanning
Gets a value indicating whether this UltrasonicSensor is enabled and scanning.

Here is the call graph for this function:

void UltrasonicSensor.OnDrawGizmos ( )
private

Raises the draw gizmos event.

Definition at line 157 of file UltrasonicSensor.cs.

157  {
158  Gizmos.color = Color.grey;
159  // roll
160  for (float r = 0; r <= 180f; r += 30f) {
161  // yaw
162  for (float y = -Fov; y <= Fov; y += 10f){
163  Quaternion rotation = Quaternion.AngleAxis(y, transform.up);
164  Vector3 direction = rotation * transform.forward;
165  rotation = Quaternion.AngleAxis(r, transform.forward);
166  direction = rotation * direction;
167  Gizmos.DrawRay(transform.position, direction * maxRange);
168  }
169  }
170  }
float Fov
The field of view angle in degrees.
float maxRange
The max range of the sensor in metres.
IEnumerator UltrasonicSensor.Scan ( )
private

Scan for obstructions in a cone

Definition at line 73 of file UltrasonicSensor.cs.

73  {
74 
75  while(scanning) {
76  float proximity = maxRange;
77  // roll
78  for (float r = 0; r <= 180f; r += 10f) {
79  // yaw
80  for (float y = -Fov; y < Fov; y += 6f){
81  Quaternion rotation;
82  Vector3 direction;
83  rotation = Quaternion.AngleAxis(y, transform.up);
84  direction = rotation * transform.forward;
85  rotation = Quaternion.AngleAxis(r, transform.forward);
86  direction = rotation * direction;
87  float cast = Cast(direction);
88  // if this cast is shorter than returnData
89  if (cast < proximity) proximity = cast;
90  }
91  }
92  _data = new ProximityData(transform.forward * proximity, proximity < maxRange);
94  yield return new WaitForSeconds(updateDt);
95  }
96 
97  }
Robot.SensorData _callback
The callback function to execute when the sensor has new data to share.
ProximityData _data
Cached proximity data from the last scan
float updateDt
The sensor update delta time in seconds.
Proximity data structure.
Definition: ProximityData.cs:7
float Fov
The field of view angle in degrees.
float Cast(Vector3 direction)
Cast a ray and return detected obstruction if the hit angle is less than a threshold ...
float maxRange
The max range of the sensor in metres.
bool scanning
Gets a value indicating whether this UltrasonicSensor is enabled and scanning.

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Robot.SensorData UltrasonicSensor._callback
private

The callback function to execute when the sensor has new data to share.

Definition at line 39 of file UltrasonicSensor.cs.

ProximityData UltrasonicSensor._data
private

Cached proximity data from the last scan

Definition at line 44 of file UltrasonicSensor.cs.

Color UltrasonicSensor._hitColor
private

Definition at line 48 of file UltrasonicSensor.cs.

Color UltrasonicSensor._missColor
private

Definition at line 49 of file UltrasonicSensor.cs.

Color UltrasonicSensor._recieveColor
private

Definition at line 47 of file UltrasonicSensor.cs.

float UltrasonicSensor.Fov

The field of view angle in degrees.

Definition at line 20 of file UltrasonicSensor.cs.

float UltrasonicSensor.maxRange

The max range of the sensor in metres.

Definition at line 25 of file UltrasonicSensor.cs.

LayerMask UltrasonicSensor.raycastLayer

The raycast collision layer.

Definition at line 30 of file UltrasonicSensor.cs.

float UltrasonicSensor.updateDt

The sensor update delta time in seconds.

Definition at line 15 of file UltrasonicSensor.cs.

Property Documentation

bool UltrasonicSensor.scanning
getprivate set

Gets a value indicating whether this UltrasonicSensor is enabled and scanning.

true if scanning; otherwise, false.

Definition at line 66 of file UltrasonicSensor.cs.


The documentation for this class was generated from the following file: