Animals
Description
Animals are considered “actors” in the Open World Workspaces. The animal library controls the animals placed in Quanser Interactive Labs.
Animal actors can be spawned anywhere in the open worlds, but to use the move_to methods to allow the people to self-navigate around the environment, they must originate and travel to a connected valid nav area.
For the best visual animation, it is recommended that you use the speed constants defining different gait styles. A character will accurately travel at any specified speed up to 6m/s, but the visual animation may give the impression of “slipping” at certain speeds outside the recommended values.
Library
Constants
- QLabsAnimal.ID_ANIMAL = 10031
- QLabsAnimal.GOAT = 0
Configuration constant.
- QLabsAnimal.SHEEP = 1
Configuration constant.
- QLabsAnimal.COW = 2
Configuration constant.
- QLabsAnimal.GOAT_STANDING = 0
Speed constant for the move_to method.
- QLabsAnimal.GOAT_WALK = 0.8
Speed constant for the move_to method.
- QLabsAnimal.GOAT_RUN = 4.0
Speed constant for the move_to method.
- QLabsAnimal.SHEEP_STANDING = 0
Speed constant for the move_to method.
- QLabsAnimal.SHEEP_WALK = 0.6
Speed constant for the move_to method.
- QLabsAnimal.SHEEP_RUN = 3.0
Speed constant for the move_to method.
- QLabsAnimal.COW_STANDING = 0
Speed constant for the move_to method.
- QLabsAnimal.COW_WALK = 1.0
Speed constant for the move_to method.
- QLabsAnimal.COW_RUN = 6.0
Speed constant for the move_to method.
Member Variables
- QLabsAnimal.actorNumber = None
The current actor number of this class to be addressed. This will be set by spawn methods and cleared by destroy methods. It will not be modified by the destroy all actors. This can be manually altered at any time to use one object to address multiple actors.
Methods
- QLabsAnimal.enable_collision(enable, waitForConfirmation=True)[source]
Allows for the animal to respond and track the number of external collisions. Note that at least one collision filter must also be added.
- Parameters
enable (boolean) – Enable or disable the collision response.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise.
- Return type
boolean
- QLabsAnimal.add_collision_filter(class_id, waitForConfirmation=True)[source]
When collisions are enabled, the animal will respond to hits from any actor of this class. This function may be called multiple times to add multiple classes.
- Parameters
class_id – The actor class number to which the person should respond to.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise.
- Return type
boolean
- QLabsAnimal.get_collision_count()[source]
Get the total number of collisions this actor has experienced. Note that collisions must be enabled and at least one collision filter set for this count to be active.
- Returns
status - True if successful or False otherwise
count - Number of hits the actor has registered
- Return type
boolean, int32
Parent Class (actor.py) Methods
- QLabsAnimal.__init__(qlabs, verbose=False)[source]
Constructor method
- Parameters
qlabs (object) – A QuanserInteractiveLabs object
verbose (boolean) – (Optional) Print error information to the console.
- QLabsAnimal.spawn(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor with the next available actor number within this class.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred. Note that if this is False, the returned actor number will be invalid.
- Returns
status - 0 if successful, 1 class not available, 3 unknown error, -1 communications error.
actorNumber - An actor number to use for future references.
- Return type
int32, int32
Tip
The origin of the animal is in the center of the body so by default, it will be spawned 1m above the surface of the target. An additional vertical offset may be required if the surface is sloped to prevent the actor from falling through the world ground surface.
Tip
If you would like to use the move_to method, the actor must be spawned in a valid nav area.
- QLabsAnimal.spawn_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor with the next available actor number within this class.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred. Note that if this is False, the returned actor number will be invalid.
- Returns
status - 0 if successful, 1 class not available, 3 unknown error, -1 communications error.
actorNumber - An actor number to use for future references.
- Return type
int32, int32
Tip
The origin of the animal is in the center of the body so by default, it will be spawned 1m above the surface of the target. An additional vertical offset may be required if the surface is sloped to prevent the actor from falling through the world ground surface.
Tip
If you would like to use the move_to method, the actor must be spawned in a valid nav area.
- QLabsAnimal.spawn_id(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 unknown error, -1 communications error
- Return type
int32
Tip
The origin of the animal is in the center of the body so by default, it will be spawned 1m above the surface of the target. An additional vertical offset may be required if the surface is sloped to prevent the actor from falling through the world ground surface.
Tip
If you would like to use the move_to method, the actor must be spawned in a valid nav area.
- QLabsAnimal.spawn_id_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 unknown error, -1 communications error
- Return type
int32
Tip
The origin of the animal is in the center of the body so by default, it will be spawned 1m above the surface of the target. An additional vertical offset may be required if the surface is sloped to prevent the actor from falling through the world ground surface.
Tip
If you would like to use the move_to method, the actor must be spawned in a valid nav area.
- QLabsAnimal.move_to(location, speed, waitForConfirmation=True)
Commands an actor to move from the present location to a new target location by using AI path navigation.
- Parameters
location (float array[3]) – A target destination as an array of floats for x, y and z coordinates in full-scale units.
speed (float) – The speed at which the person should walk to the destination (refer to the constants for recommended speeds)
waitForConfirmation (boolean) – (Optional) Wait for confirmation before proceeding. This makes the method a blocking operation, but only until the command is received. The time for the actor to traverse to the destination is always non-blocking.
- Returns
status - True if successful, False otherwise
- Return type
boolean
Tip
Ensure the start and end locations are in valid navigation areas so the actor can find a path to the destination.
- QLabsAnimal.destroy()
Find and destroy a specific actor. This is a blocking operation.
- Returns
numActorsDestroyed - The number of actors destroyed. -1 if failed.
- Return type
int32
- QLabsAnimal.destroy_all_actors_of_class()
Find and destroy all actors of this class. This is a blocking operation.
- Returns
numActorsDestroyed - The number of actors destroyed. -1 if failed.
- Return type
int32
- QLabsAnimal.ping()
Checks if the actor is still present in the environment. Note that if you did not spawn the actor with one of the spawn functions, you may need to manually set the actorNumber member variable.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsAnimal.get_world_transform()
Get the location, rotation, and scale in world coordinates of the actor.
- Returns
status - True if successful, False otherwise
location
rotation
scale
- Return type
boolean, float array[3], float array[3], float array[3]
- QLabsAnimal.get_world_transform_degrees()
Get the location, rotation, and scale in world coordinates of the actor.
- Returns
status - True if successful, False otherwise
location
rotation
scale
- Return type
boolean, float array[3], float array[3], float array[3]
Configurations
There are 3 configurations (0-2) for a animal generated in QLabs.
Connection Points
There are no connection points for the animal actor.
Animals Tutorial
Python Tutorial
Raw to download this tutorial: Animals Tutorial (.py).
1"""
2Animal Example
3---------------
4
5.. note::
6
7 Make sure you have Quanser Interactive Labs open before running this
8 example. This example is designed to run in the QCar Cityscape.
9
10"""
11
12from qvl import qlabs
13from qvl.animal import QLabsAnimal
14from qvl.qlabs import QuanserInteractiveLabs
15from qvl.free_camera import QLabsFreeCamera
16from qvl.environment_outdoors import QLabsEnvironmentOutdoors
17from qvl.system import QLabsSystem
18
19import time
20import os
21import math
22
23
24# initialize our variables
25# note that you can use the ..Coordinate Helper to pick locations for your actor.
26LOCATION_START_P1 = [-6.85, 40.396, 0.005]
27ROTATION_P1P2 = [0,0,math.pi/2]
28SCALE = [1,1,1]
29
30LOCATION_START_P2 = [-8.53, 40.7, 0.005]
31LOCATION_START_P3 = [-11.884, 40.292, 0.005]
32ROTATION_P3 = [0,0,90]
33
34LOCATION_END_P1 = [-7.637, 51, 0.005]
35LOCATION_END_P2 = [-11.834, 51, 0.005]
36LOCATION_END_P3 = [-23.71, 43.245, 0.005]
37
38
39def main():
40
41 print("\n\n------------- Communications -------------------\n")
42
43 # Creates a server connection with Quanser Interactive Labs and manages
44 # the communications
45 qlabs = QuanserInteractiveLabs()
46
47 # Ensure that QLabs is running on your local machine
48 print("Connecting to QLabs...")
49 if (not qlabs.open("localhost")):
50 print("Unable to connect to QLabs")
51 return
52
53 print("Connected")
54
55 # Use hSystem to set the tutorial title on the qlabs display screen
56 hSystem = QLabsSystem(qlabs)
57 hSystem.set_title_string('Animals Tutorial')
58
59 num_destroyed = qlabs.destroy_all_spawned_actors()
60
61 # create a camera in this qlabs instance
62 camera = QLabsFreeCamera(qlabs)
63
64 #place the custom camera at a specified location and rotation using radians
65 camera.spawn([-0., 43.807, 8.82], [-0, 0.606, 3.127])
66
67 # to switch our view from our current camera to the new camera we just initialized to
68 # be able to view where our animals will spawn
69 camera.possess()
70
71 # Animal configurations
72 #
73 # Goat = 0
74 # Sheep = 1
75 # Cow = 2
76
77 # creates an instance of the Animal
78 goat = QLabsAnimal(qlabs)
79
80 # place the Animal at a specified location and rotation using radians
81 # spawn_id allows us to specify the internal number for the actor
82 goat.spawn_id(0, LOCATION_START_P1, ROTATION_P1P2, SCALE, 0, True)
83
84 # creates a second instance of a animal
85 sheep = QLabsAnimal(qlabs)
86
87 # place the animal at a specified location and rotation using radians
88 # spawn creates the internal number for the actor automatically using
89 # the next available actor number
90 sheep.spawn(LOCATION_START_P2, ROTATION_P1P2, SCALE, 1, True)
91
92 # creates a third instance of a animal
93 cow = QLabsAnimal(qlabs, True)
94
95 # place the animal at a specified location and rotation using degrees
96 # spawn_degrees creates the internal number for the actor automatically using the next
97 # available number and takes the inputted rotation as degrees
98 cow.spawn_degrees(LOCATION_START_P3, ROTATION_P3, SCALE, 2, True)
99
100 # move the 3 people created to a new location
101 goat.move_to(LOCATION_END_P1, goat.GOAT_WALK, True)
102 sheep.move_to(LOCATION_END_P2, sheep.SHEEP_RUN, True)
103 cow.move_to(LOCATION_END_P3, cow.COW_RUN, True)
104
105 # time.sleep to change camera angle
106 time.sleep(7)
107
108 # destroy each animal one by one
109 goat.destroy()
110 time.sleep(1)
111
112 sheep.destroy()
113 time.sleep(1)
114
115 cow.destroy()
116 time.sleep(1)
117
118 # re position camera
119 hCameraAnimals = QLabsFreeCamera(qlabs)
120 x = hCameraAnimals.spawn([25.243, 46.069, 1.628], [-0, 0.188, 1.098])
121 hCameraAnimals.possess()
122
123 # Spawn a Goat and make it run to a specific location
124 hGoat = QLabsAnimal(qlabs)
125 hGoat.spawn([26.206, 57, 1], [0,0,0], [1,1,1], hGoat.GOAT, True)
126 hGoat.move_to([27.214, 49.286, 0], hGoat.GOAT_RUN, True)
127 time.sleep(3)
128
129 # Move the Goat at walking speed to your desired location
130 hGoat.move_to([28.338, 47.826, 0], hGoat.GOAT_WALK, True)
131 time.sleep(4)
132 hGoat.destroy()
133
134 # Spawn a Sheep and make it run to a specific location
135 hSheep = QLabsAnimal(qlabs)
136 hSheep.spawn([26.206, 57, 1], [0,0,0], [1,1,1], hSheep.SHEEP, True)
137 hSheep.move_to([27.214, 49.286, 0], hSheep.SHEEP_RUN, True)
138 time.sleep(3)
139
140 # Move the Sheep at walking speed to your desired location
141 hSheep.move_to([28.338, 47.826, 0], hSheep.SHEEP_WALK, True)
142 time.sleep(4)
143 hSheep.destroy()
144
145 # Spawn a Cow and make it run to a specific location
146 hCow = QLabsAnimal(qlabs)
147 hCow.spawn([26.206, 57, 1], [0,0,0], [1,1,1], hCow.COW, True)
148 hCow.move_to([27.214, 49.286, 0], hCow.COW_RUN, True)
149 time.sleep(3)
150
151 # Move the Cow at walking speed to your desired location
152 hCow.move_to([28.338, 47.826, 0], hCow.COW_WALK, True)
153 time.sleep(6)
154
155 # Close qlabs
156 qlabs.close()
157 print('Done!')
158
159if __name__ == "__main__":
160 main()
Matlab Tutorial
Raw to download this tutorial: Animals Tutorial (.m).
1% Animal Example
2% -------------------------
3%
4% .. note::
5%
6% Make sure you have Quanser Interactive Labs open before running this
7% example. This example is designed to best be run in QCar Cityscape.
8
9close all;
10clear all;
11clc;
12
13% --------------------------------------------------------------
14% Setting MATLAB Path for the libraries
15% Always keep at the start, it will make sure it finds the correct references
16newPathEntry = fullfile(getenv('QAL_DIR'), '0_libraries', 'matlab', 'qvl');
17pathCell = regexp(path, pathsep, 'split');
18if ispc % Windows is not case-sensitive
19 onPath = any(strcmpi(newPathEntry, pathCell));
20else
21 onPath = any(strcmp(newPathEntry, pathCell));
22end
23
24if onPath == 0
25 path(path, newPathEntry)
26 savepath
27end
28% --------------------------------------------------------------
29
30fprintf('\n\n----------------- Communications -------------------\n\n');
31
32qlabs = QuanserInteractiveLabs();
33connection_established = qlabs.open('localhost');
34
35if connection_established == false
36 disp("Failed to open connection.")
37 return
38end
39
40disp('Connected')
41
42num_destroyed = qlabs.destroy_all_spawned_actors();
43
44fprintf('%d actors destroyed', num_destroyed);
45
46% Use hSystem to set the tutorial title on the qlabs display screen
47hSystem = QLabsSystem(qlabs);
48hSystem.set_title_string('Animals Tutorial')
49
50% initialize our variables
51% Note that you can use the
52% Coordinate Helper to pick locations for your actor.
53
54LOCATION_START_P1 = [-6.85, 40.396, 0.005];
55ROTATION_P1P2 = [0,0,pi/2];
56SCALE = [1,1,1];
57
58LOCATION_START_P2 = [-8.53, 40.7, 0.005];
59LOCATION_START_P3 = [-11.884, 40.292, 0.005];
60ROTATION_P3 = [0,0,90];
61
62LOCATION_END_P1 = [-7.637, 51, 0.005];
63LOCATION_END_P2 = [-11.834, 51, 0.005];
64LOCATION_END_P3 = [-23.71, 43.245, 0.005];
65
66% create a camera in this qlabs instance
67camera = QLabsFreeCamera(qlabs);
68
69% place the custom camera at a specified location and rotation using radians
70camera.spawn([-0., 43.807, 8.82], [-0, 0.606, 3.127]);
71
72% to switch our view from our current camera
73% to the new camera we just initialized
74% to be able to view where our animals will spawn
75camera.possess();
76
77% creates an instance of the Animal
78goat = QLabsAnimal(qlabs);
79
80% Animal configurations
81%
82% Goat = 0
83% Sheep = 1
84% Cow = 2
85
86% place the animal at a specified location and rotation using radians
87% spawn_id allows us to specify the internal number for the actor
88goat.spawn_id(0, LOCATION_START_P1, ROTATION_P1P2, SCALE, 0, 1);
89
90% creates a second instance of a animal
91sheep = QLabsAnimal(qlabs);
92
93% place the animal at a specified location and rotation using radians
94% spawn creates the internal number for the actor automatically using
95% the next available actor number
96sheep.spawn(LOCATION_START_P2, ROTATION_P1P2, SCALE, 1, 1)
97
98% creates a third instance of a animal
99cow = QLabsAnimal(qlabs, 1);
100
101% place the animal at a specified location and rotation using degrees
102% spawn_degrees creates the internal actor number
103% automatically using the next available number
104% this function also takes the input rotation as degrees
105cow.spawn_degrees(LOCATION_START_P3, ROTATION_P3, SCALE, 2, 1);
106
107% move the 3 animals created to a new location
108goat.move_to(LOCATION_END_P1, goat.GOAT_WALK, 1);
109sheep.move_to(LOCATION_END_P2, sheep.SHEEP_RUN, 1);
110cow.move_to(LOCATION_END_P3, cow.COW_RUN, 1);
111
112% pause to change camera
113pause(7);
114
115% destroy each animal one by one
116
117goat.destroy();
118pause(1);
119
120sheep.destroy();
121pause(1);
122
123cow.destroy();
124pause(1);
125
126% re position camera
127hCameraAnimals = QLabsFreeCamera(qlabs);
128x = hCameraAnimals.spawn([25.243, 46.069, 1.628], [-0, 0.188, 1.098]);
129hCameraAnimals.possess();
130
131% Spawn a Goat and make it run to a specific location
132hGoat = QLabsAnimal(qlabs);
133hGoat.spawn([26.206, 57, 1], [0,0,0], [1,1,1], hGoat.GOAT, true);
134hGoat.move_to([27.214, 49.286, 0], hGoat.GOAT_RUN, true);
135pause(3);
136
137% Move the Goat at walking speed to your desired location
138hGoat.move_to([28.338, 47.826, 0], hGoat.GOAT_WALK, true);
139pause(4);
140hGoat.destroy();
141
142% Spawn a Sheep and make it run to a specific location
143hSheep = QLabsAnimal(qlabs);
144hSheep.spawn([26.206, 57, 1], [0,0,0], [1,1,1], hSheep.SHEEP, true);
145hSheep.move_to([27.214, 49.286, 0], hSheep.SHEEP_RUN, true);
146pause(3);
147
148% Move the Sheep at walking speed to your desired location
149hSheep.move_to([28.338, 47.826, 0], hSheep.SHEEP_WALK, true);
150pause(4);
151hSheep.destroy();
152
153% Spawn a Cow and make it run to a specific location
154hCow = QLabsAnimal(qlabs);
155hCow.spawn([26.206, 57, 1], [0,0,0], [1,1,1], hCow.COW, true);
156hCow.move_to([27.214, 49.286, 0], hCow.COW_RUN, true);
157pause(3);
158
159% Move the Cow at walking speed to your desired location
160hCow.move_to([28.338, 47.826, 0], hCow.COW_WALK, true);
161pause(6);
162
163% close qlabs
164qlabs.close();
165disp('Done !')