BotNavSim  v0.4.3
Mobile Robot Simulation
Robot Class Reference

Robot class bridges the gap between sensors and INavigation. The navigation direction is cached in Robot.navigationCommand. This class does not implement any physics simulation. Robot locomotion models are implemented in other classes that require this component for communications and data. More...

Inheritance diagram for Robot:
Collaboration diagram for Robot:

Public Member Functions

delegate void SensorData (ProximityData data)
 Sensor callback delegate type More...
 
void NavigateToDestination ()
 Enable sensors, start moving toward destination using INavigation More...
 
void Reset ()
 Reset the robot, disable sensors More...
 
void ReceiveSensorData (ProximityData data)
 Sensor data callback passes proximity data to INavigation More...
 

Public Attributes

bool manualControl
 If true, the robot navigation is controlled using the keyboard. More...
 
bool moveEnabled
 A flag indicating whether the robot should try moving. More...
 
float stopDistance
 The maximum distance from the destination before setting atDestination to true. More...
 
Transform destination
 Reference to the destination GameObject More...
 
Vector3 centerOfMassOffset
 RigidBody center of mass offset vector in local coordinates. More...
 

Properties

INavigation navigation [get, set]
 Interface to the navigation assembly. More...
 
Vector3 navigationCommand [get, private set]
 The cached navigation bearing from INavigation. More...
 
bool pathFound [get]
 Gets a value indicating whether navigation found a path to destination. More...
 
Vector3 position [get, set]
 Gets or sets the transform position. More...
 
Transform cameraMount [get, private set]
 The first-person perspective camera position. More...
 
Bounds bounds [get]
 Gets the bounds of the robot for IObservable More...
 
bool atDestination [get]
 
float distanceToDestination [get]
 
bool isStuck [get]
 
int stuckpc [get]
 
- Properties inherited from IObservable
string name [get]
 Gets the name of the observable object More...
 
Bounds bounds [get]
 Gets the bounds of the observable object (size and location) More...
 

Private Member Functions

void Awake ()
 
void Update ()
 
void OnDrawGizmos ()
 
IEnumerator StuckDetector ()
 
IEnumerator RecordPath ()
 

Private Attributes

INavigation _navigation
 
Sensor[] _sensors
 
Vector3 _move
 
int _stuckCounter
 
Vector3[] _positions
 
BotPath _path
 
Vector3 _size
 

Detailed Description

Robot class bridges the gap between sensors and INavigation. The navigation direction is cached in Robot.navigationCommand. This class does not implement any physics simulation. Robot locomotion models are implemented in other classes that require this component for communications and data.

Definition at line 11 of file Robot.cs.

Member Function Documentation

void Robot.Awake ( )
private

Definition at line 210 of file Robot.cs.

210  {
211  _sensors = GetComponentsInChildren<Sensor>();
212  StartCoroutine(StuckDetector());
213  cameraMount = transform.Find("CameraMount");
214  if (!cameraMount) {
215  cameraMount = transform;
216  Debug.LogWarning("CameraMount object not found on Robot.");
217  }
218  // calculate size bounding box for renderers
219  Bounds b = new Bounds(Vector3.zero, Vector3.zero);
220  foreach(Renderer r in GetComponentsInChildren<Renderer>())
221  b.Encapsulate(r.bounds);
222  _size = b.size;
223  // add center of mass offset
224  rigidbody.centerOfMass += centerOfMassOffset;
225  // initialise bath plotter
226  _path = new BotPath();
227  StartCoroutine(RecordPath());
228  }
BotPath displays information about a recorded path taken or calculated.
Definition: BotPath.cs:9
Sensor[] _sensors
Definition: Robot.cs:50
Transform cameraMount
The first-person perspective camera position.
Definition: Robot.cs:114
Vector3 _size
Definition: Robot.cs:55
IEnumerator StuckDetector()
Definition: Robot.cs:302
IEnumerator RecordPath()
Definition: Robot.cs:323
Vector3 centerOfMassOffset
RigidBody center of mass offset vector in local coordinates.
Definition: Robot.cs:44
BotPath _path
Definition: Robot.cs:54

Here is the call graph for this function:

void Robot.NavigateToDestination ( )

Enable sensors, start moving toward destination using INavigation

Definition at line 164 of file Robot.cs.

164  {
165  if (_navigation == null) return;
166  StartCoroutine( _navigation.SearchForPath(rigidbody.worldCenterOfMass, destination.position) );
167  Debug.Log("Path search due to NavigationToDestination() call.");
168  foreach(Sensor s in _sensors) {
170  }
171  }
Abstract Sensor class for all sensor implementations to inherit from.
Definition: Sensor.cs:7
Sensor[] _sensors
Definition: Robot.cs:50
IEnumerator SearchForPath()
Start the search algorithm to find a path from origin to destination.
abstract void Enable(Robot.SensorData callback)
Enable this instance and pass the reference to the Robot.SensorData callback function to be called wh...
void ReceiveSensorData(ProximityData data)
Sensor data callback passes proximity data to INavigation
Definition: Robot.cs:188
INavigation _navigation
Definition: Robot.cs:49
Transform destination
Reference to the destination GameObject
Definition: Robot.cs:39

Here is the call graph for this function:

Here is the caller graph for this function:

void Robot.OnDrawGizmos ( )
private

Definition at line 286 of file Robot.cs.

286  {
287  if (_navigation != null) {
289  }
290 
291  // draw center of mass
292  Gizmos.color = Color.Lerp(Color.yellow, Color.clear, 0.25f);
293  if (Application.isPlaying) {
294  Gizmos.DrawSphere(rigidbody.worldCenterOfMass, 0.05f);
295  } else {
296  Gizmos.DrawSphere(rigidbody.worldCenterOfMass + centerOfMassOffset, 0.05f);
297  }
298 
299  }
void DrawGizmos()
Called by Unity OnDrawGizmos() event to draw 3D world space debug information.
INavigation _navigation
Definition: Robot.cs:49
Vector3 centerOfMassOffset
RigidBody center of mass offset vector in local coordinates.
Definition: Robot.cs:44

Here is the call graph for this function:

void Robot.ReceiveSensorData ( ProximityData  data)

Sensor data callback passes proximity data to INavigation

Parameters
dataData.

Definition at line 188 of file Robot.cs.

188  {
189  if (_navigation == null) return;
190  // data is recieved here by any enabled sensor in world space
191  // transformed into robot local space if necessary
192  // and transmitted to INavigation
193  if (_navigation.spaceRelativeTo == Space.Self) {
195  Vector3.zero,
196  transform.InverseTransformDirection(data.direction),
197  data.obstructed);
198  } else {
200  transform.position,
201  transform.position + data.direction,
202  data.obstructed);
203  }
204  }
bool obstructed
Indicates whether or not the location at the end of the direction is obstructed.
Space spaceRelativeTo
Indicates the frame of reference World space is data relative to (0,0,0) Self space is data relative ...
Definition: INavigation.cs:73
void Proximity(Vector3 from, Vector3 to, bool obstructed)
Proximity sensor data.
INavigation _navigation
Definition: Robot.cs:49
Vector3 direction
The direction of the proximity

Here is the call graph for this function:

Here is the caller graph for this function:

IEnumerator Robot.RecordPath ( )
private

Definition at line 323 of file Robot.cs.

323  {
324  Vector3 prev = rigidbody.worldCenterOfMass;;
325  _path.AddNode(prev, Simulation.time);
326  while (true) {
327  // only record a line when bot has moved far enough
328  if (Vector3.Distance(prev, rigidbody.worldCenterOfMass) > 0.1f) {
329  prev = rigidbody.worldCenterOfMass;
330  _path.AddNode(rigidbody.worldCenterOfMass, Simulation.time);
331  }
332  yield return new WaitForSeconds(Log.timeStep);
333  }
334  }
static float timeStep
The time between log entries in seconds.
Definition: Log.cs:47
void AddNode(Vector3 node, float time)
Adds a node to the path.
Definition: BotPath.cs:81
This is a manager class used to overlook the running of a simulation.
Definition: Simulation.cs:8
static float time
Time (in seconds) since robot started searching for destination.
Definition: Simulation.cs:366
Provides data logging capabilities. Data is logged in CSV format.
Definition: Log.cs:10
BotPath _path
Definition: Robot.cs:54

Here is the call graph for this function:

Here is the caller graph for this function:

void Robot.Reset ( )

Reset the robot, disable sensors

Definition at line 176 of file Robot.cs.

176  {
177  _path = new BotPath();
178  _positions = new Vector3[30];
179  foreach(Sensor s in _sensors) {
180  s.Disable();
181  }
182  }
BotPath displays information about a recorded path taken or calculated.
Definition: BotPath.cs:9
Abstract Sensor class for all sensor implementations to inherit from.
Definition: Sensor.cs:7
Vector3[] _positions
Definition: Robot.cs:53
Sensor[] _sensors
Definition: Robot.cs:50
BotPath _path
Definition: Robot.cs:54
abstract void Disable()
Disable this instance.

Here is the call graph for this function:

Here is the caller graph for this function:

delegate void Robot.SensorData ( ProximityData  data)

Sensor callback delegate type

IEnumerator Robot.StuckDetector ( )
private

Definition at line 302 of file Robot.cs.

302  {
303  _positions = new Vector3[30];
304  int i = 0;
305  while(true) {
306  _positions[i] = transform.position;
307  if (++i >= _positions.Length) i = 0;
308  Vector3 avg = Vector3.zero;
309  foreach(Vector3 v in _positions) {
310  avg += v;
311  }
312  avg /= (float)_positions.Length;
313  if (Vector3.Distance(avg, transform.position) < 1f && Simulation.isRunning) {
314  _stuckCounter++;
315  }
316  else {
317  _stuckCounter = 0;
318  }
319  yield return new WaitForSeconds(0.3f);
320  }
321  }
int _stuckCounter
Definition: Robot.cs:52
Vector3[] _positions
Definition: Robot.cs:53
static bool isRunning
Gets a value indicating whether this Simulation is running.
Definition: Simulation.cs:325
This is a manager class used to overlook the running of a simulation.
Definition: Simulation.cs:8

Here is the caller graph for this function:

void Robot.Update ( )
private

Definition at line 231 of file Robot.cs.

231  {
232 
233 
234 
235  // manual control for testing robots
236  if (manualControl) {
237  float x = Input.GetAxis("X");
238  float y = Input.GetAxis("Y");
239  float z = Input.GetAxis("Z");
240 
241  navigationCommand = new Vector3(x, y, z);
242 
243  }
244  else if (Simulation.isRunning){
245 
246  // draw path
247  _path.DrawPath();
248 
249  if (_navigation == null) return;
250  // Update INavigation.destination if it has changed.
251  if (destination.hasChanged) {
252  if (Vector3.Distance(_navigation.destination,destination.position)>0.5f) {
253  if (_navigation.spaceRelativeTo == Space.Self) {
254  Vector3 dest = transform.InverseTransformPoint(destination.position);
255  StartCoroutine( _navigation.SearchForPath(Vector3.zero, dest) );
256  }
257  else {
258  StartCoroutine( _navigation.SearchForPath(rigidbody.worldCenterOfMass, destination.position) );
259  }
260  Debug.Log("Path search due to destination change.");
261  }
262 
263  destination.hasChanged = false;
264  }
265 
266  // Read move direction if a path has been found.
267  if (_navigation.pathFound) {
268  if (navigation.spaceRelativeTo == Space.Self) {
270  navigationCommand = transform.TransformDirection(navigationCommand);
271  }
272  else {
273  navigationCommand = _navigation.PathDirection(transform.position);
274  }
275  Debug.DrawRay(transform.position, navigationCommand * stopDistance, Color.green);
276  }
277  else {
278  navigationCommand = Vector3.zero;
279  }
280  }
281  }
float stopDistance
The maximum distance from the destination before setting atDestination to true.
Definition: Robot.cs:34
Vector3 navigationCommand
The cached navigation bearing from INavigation.
Definition: Robot.cs:89
Vector3 PathDirection(Vector3 myLocation)
Gives a direction to follow the path given your location.
Space spaceRelativeTo
Indicates the frame of reference World space is data relative to (0,0,0) Self space is data relative ...
Definition: INavigation.cs:73
IEnumerator SearchForPath()
Start the search algorithm to find a path from origin to destination.
static bool isRunning
Gets a value indicating whether this Simulation is running.
Definition: Simulation.cs:325
INavigation navigation
Interface to the navigation assembly.
Definition: Robot.cs:67
bool pathFound
Gets a value indicating whether this INavigation found a path between origin and destination.
Definition: INavigation.cs:49
INavigation _navigation
Definition: Robot.cs:49
This is a manager class used to overlook the running of a simulation.
Definition: Simulation.cs:8
Transform destination
Reference to the destination GameObject
Definition: Robot.cs:39
Vector3 destination
Gets or sets the destination.
Definition: INavigation.cs:29
BotPath _path
Definition: Robot.cs:54
bool manualControl
If true, the robot navigation is controlled using the keyboard.
Definition: Robot.cs:24
void DrawPath()
Draws the path on screen using Draw class
Definition: BotPath.cs:101

Here is the call graph for this function:

Member Data Documentation

Vector3 Robot._move
private

Definition at line 51 of file Robot.cs.

INavigation Robot._navigation
private

Definition at line 49 of file Robot.cs.

BotPath Robot._path
private

Definition at line 54 of file Robot.cs.

Vector3 [] Robot._positions
private

Definition at line 53 of file Robot.cs.

Sensor [] Robot._sensors
private

Definition at line 50 of file Robot.cs.

Vector3 Robot._size
private

Definition at line 55 of file Robot.cs.

int Robot._stuckCounter
private

Definition at line 52 of file Robot.cs.

Vector3 Robot.centerOfMassOffset

RigidBody center of mass offset vector in local coordinates.

Definition at line 44 of file Robot.cs.

Transform Robot.destination

Reference to the destination GameObject

Definition at line 39 of file Robot.cs.

bool Robot.manualControl

If true, the robot navigation is controlled using the keyboard.

Definition at line 24 of file Robot.cs.

bool Robot.moveEnabled

A flag indicating whether the robot should try moving.

Definition at line 29 of file Robot.cs.

float Robot.stopDistance

The maximum distance from the destination before setting atDestination to true.

Definition at line 34 of file Robot.cs.

Property Documentation

bool Robot.atDestination
get

Definition at line 125 of file Robot.cs.

Bounds Robot.bounds
get

Gets the bounds of the robot for IObservable

Definition at line 119 of file Robot.cs.

Transform Robot.cameraMount
getprivate set

The first-person perspective camera position.

Definition at line 114 of file Robot.cs.

float Robot.distanceToDestination
get

Definition at line 134 of file Robot.cs.

bool Robot.isStuck
get

Definition at line 146 of file Robot.cs.

INavigation Robot.navigation
getset

Interface to the navigation assembly.

When set, the INavigation interface is initialised: INavigation.searchBounds is set, INavigation.origin is set, INavigation.destination is set.

Definition at line 67 of file Robot.cs.

Vector3 Robot.navigationCommand
getprivate set

The cached navigation bearing from INavigation.

Definition at line 89 of file Robot.cs.

bool Robot.pathFound
get

Gets a value indicating whether navigation found a path to destination.

true if path found; otherwise, false.

Definition at line 97 of file Robot.cs.

Vector3 Robot.position
getset

Gets or sets the transform position.

Definition at line 106 of file Robot.cs.

int Robot.stuckpc
get

Definition at line 152 of file Robot.cs.


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