Traffic Cone

Description
Traffic cones are considered an “actor” in Quanser Interactive Labs Open Worlds. Traffic cones can be spawned anywhere in the Open Worlds and they only exist as dynamic actors.
See the Traffic Cones Tutorial to get a better understanding of using traffic cones in Quanser Interactive Labs.
Library
Constants
- QLabsTrafficCone.ID_TRAFFIC_CONE = 10000
Class ID
Member Variables
- QLabsTrafficCone.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
- QLabsTrafficCone.set_material_properties(materialSlot=0, color=[0, 0, 0], roughness=0.4, metallic=False, waitForConfirmation=True)[source]
Sets the visual surface properties of the cone. The default colors are orange for material slot 0, and black for slot 1.
- Parameters
materialSlot – Material index to be modified. Setting an index for an unsupported slot will be ignored.
color (float array[3]) – Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
roughness (float) – A value between 0.0 (completely smooth and reflective) to 1.0 (completely rough and diffuse). Note that reflections are rendered using screen space reflections. Only objects visible in the camera view will be rendered in the reflection of the object.
metallic (boolean) – Metallic (True) or non-metallic (False)
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise
- Return type
boolean
Parent Class (actor.py) Methods
- QLabsTrafficCone.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
- QLabsTrafficCone.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
- QLabsTrafficCone.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
- QLabsTrafficCone.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
- QLabsTrafficCone.spawn_id_and_parent_with_relative_transform(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Spawns a new actor relative to an existing actor and creates a kinematic relationship.
- 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.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame 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 cannot find the parent actor, 4 unknown error, -1 communications error
- Return type
int32
- QLabsTrafficCone.spawn_id_and_parent_with_relative_transform_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Spawns a new actor relative to an existing actor and creates a kinematic relationship.
- 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 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.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame 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 cannot find the parent actor, 4 unknown error, -1 communications error
- Return type
int32
- QLabsTrafficCone.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
- QLabsTrafficCone.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
- QLabsTrafficCone.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
- QLabsTrafficCone.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]
- QLabsTrafficCone.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]
- QLabsTrafficCone.parent_with_relative_transform(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship.
- 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.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsTrafficCone.parent_with_relative_transform_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship.
- 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.
parentClassID (uint32) – (Optional) See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – (Optional) User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – (Optional) 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsTrafficCone.parent_with_current_world_transform(parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship while preserving the current world transform of the child actor.
- Parameters
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsTrafficCone.parent_break(waitForConfirmation=True)
Breaks any relationship with a parent actor (if it exists) and preserves the current world transform
- Parameters
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, -1 communications error
- Return type
int32
Configurations
There are four configurations (0-3) for the traffic cone actor class generated
in QLabs. For the purpose of the set_material_properties
method, configuration 0
has a single material slot (0), and configurations 1-3 have two material slots (0, 1).

Connection Points
There are no connection points for this actor class.
Traffic Cones Tutorial
Python Tutorial
Raw to download this tutorial: Traffic Cones Tutorial (.py)
.
1"""
2Road Signage Library Traffic Cones 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 best be run in QCar Cityscape
9 or Cityscape Lite.
10
11"""
12
13# imports to important libraries
14import sys
15import math
16import time
17
18from qvl.qlabs import QuanserInteractiveLabs
19from qvl.free_camera import QLabsFreeCamera
20from qvl.system import QLabsSystem
21from qvl.traffic_cone import QLabsTrafficCone
22
23
24def main():
25
26 print("\n\n------------------------------ Communications --------------------------------\n")
27
28 # Creates a server connection with Quanser Interactive Labs and manages
29 # the communications
30 qlabs = QuanserInteractiveLabs()
31
32 # Ensure that QLabs is running on your local machine
33 print("Connecting to QLabs...")
34 if (not qlabs.open("localhost")):
35 print("Unable to connect to QLabs")
36 return
37
38 print("Connected")
39
40 # Use hSystem to set the tutorial title in the upper left of the qlabs window
41 hSystem = QLabsSystem(qlabs)
42 hSystem.set_title_string('Traffic Cones Tutorial')
43
44 num_destroyed = qlabs.destroy_all_spawned_actors()
45
46 # Switch the camera angle to see where we will be spawning the cones
47 camera0 = QLabsFreeCamera(qlabs)
48 camera0.spawn([-20.355, 27.374, 2.055], [-0, 0.308, -0.001])
49 camera0.possess()
50
51 # creates 3 cones in this qlabs instance
52 cone = QLabsTrafficCone(qlabs)
53 cone1 = QLabsTrafficCone(qlabs)
54 cone2 = QLabsTrafficCone(qlabs)
55 cone3 = QLabsTrafficCone(qlabs)
56 cone4 = QLabsTrafficCone(qlabs)
57
58 # spawns a small traffic cone we just initialized using radians
59 cone.spawn(location=[-17, 28, 1.0], rotation=[0,0,math.pi], scale=[1,1,1], configuration=0, waitForConfirmation=True)
60 # waits so we can see the output
61 time.sleep (1)
62 # destroy the cone we just made
63 cone.destroy()
64 # waits so we can see the output
65 time.sleep(1)
66 # spawns another small traffic cone we just initialized using radians in the same place
67 cone1.spawn_id(actorNumber=1, location=[-17, 28, 1.0], rotation=[0,0,math.pi], scale=[1,1,1], configuration=0, waitForConfirmation=True)
68 # spawns a construction pylon using the cone we just initialized using degrees and generating
69 # the actorNumber internally
70 cone2.spawn_degrees(location=[-15, 28, 1.0], rotation=[0,0,180], scale=[1,1,1], configuration=1, waitForConfirmation=True)
71 # spawns a small cone with one color stripe initialized using actor number and a position in degrees
72 cone3.spawn_id_degrees(actorNumber=3, location=[-15, 26.5, 1.0], rotation=[0,0,90], scale=[1,1,1], configuration=2, waitForConfirmation=True)
73 # spawns a bigger cone with two color stripes using radians and generating the actorNumber internally
74 cone4.spawn(location=[-17, 26.5, 1.0], rotation=[0,0,math.pi/4], scale=[1,1,1], configuration=3, waitForConfirmation=True)
75
76 # waits so we can see the output
77 time.sleep(1.5)
78
79
80 # change the color of the cones (materialSlot 0 is the base color, and 1 is the stripes)
81 cone1.set_material_properties(materialSlot=0, color=[0,0,1],metallic=True) # Blue
82 # waits so we can see the output
83 time.sleep(.5)
84
85 cone2.set_material_properties(materialSlot=0, color=[1,0,0],roughness=1,metallic=False) # Red
86 cone2.set_material_properties(materialSlot=1, color=[1,.5,0])
87 time.sleep(.5)
88
89 cone3.set_material_properties(materialSlot=0, color=[0,1,1]) # Cyan
90 cone3.set_material_properties(materialSlot=1, color=[0,.3,1],roughness=1,metallic=True)
91 time.sleep(.5)
92
93 cone4.set_material_properties(materialSlot=0, color=[1,0,1],roughness=0,metallic=False) # Magenta
94 cone4.set_material_properties(materialSlot=1, color=[.3,0,1])
95 time.sleep(.5)
96
97 cone1.set_material_properties(materialSlot=0, color=[0,1,0],roughness=0.5,metallic=False) # Green
98 cone1.set_material_properties(materialSlot=1, color=[1,.5,0])
99 time.sleep(.5)
100
101 cone2.set_material_properties(materialSlot=0, color=[1,1,0],roughness=1,metallic=True) # Yellow
102 cone2.set_material_properties(materialSlot=1, color=[0,0,0])
103 time.sleep(.5)
104
105 cone3.set_material_properties(materialSlot=0, color=[0.5,0.5,0.5]) # Grey
106 cone3.set_material_properties(materialSlot=1, color=[0.6,0.2,0.6],roughness=1,metallic=True)
107 time.sleep(.5)
108
109 cone4.set_material_properties(materialSlot=0, color=[0.0,0.5,0.5],roughness=0,metallic=False) # Magenta
110 cone4.set_material_properties(materialSlot=1, color=[0.5,0.5,0.0])
111 time.sleep(.5)
112
113 cone1.destroy()
114 time.sleep(0.5)
115
116 cone2.destroy()
117 time.sleep(0.5)
118
119 cone3.destroy()
120 time.sleep(0.5)
121
122 cone4.destroy()
123 time.sleep(0.5)
124
125 # Closing qlabs
126 qlabs.close()
127 print('Done!')
128
129if __name__ == "__main__":
130 main()
Complete Road Signage Python Tutorial
Raw to download this tutorial: Complete Road Signage Tutorial (.py)
.
1"""
2Road Signage Library 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 best be run in QCar Cityscape
9 or Cityscape Lite. This is an example of a typical setup script
10 to populate the city with signage for subsequent use in a vehicle
11 simulation.
12
13"""
14
15# imports to important libraries
16import time
17import math
18
19from qvl.qlabs import QuanserInteractiveLabs
20from qvl.free_camera import QLabsFreeCamera
21from qvl.crosswalk import QLabsCrosswalk
22from qvl.roundabout_sign import QLabsRoundaboutSign
23from qvl.system import QLabsSystem
24from qvl.yield_sign import QLabsYieldSign
25from qvl.stop_sign import QLabsStopSign
26from qvl.traffic_cone import QLabsTrafficCone
27from qvl.traffic_light import QLabsTrafficLight
28
29
30# specify if you want the signage on the left or right side of the road.
31right_hand_driving = True
32
33def main(right_hand_driving):
34
35 # creates a server connection with Quanser Interactive Labs and manages the communications
36 qlabs = QuanserInteractiveLabs()
37
38 # trying to connect to QLabs and open the instance we have created - program will end if this fails
39 print("Connecting to QLabs...")
40 if (not qlabs.open("localhost")):
41 print("Unable to connect to QLabs")
42 return
43
44 print("Connected")
45
46 # destroying any spawned actors in our QLabs that currently exist
47 qlabs.destroy_all_spawned_actors()
48
49 # Use hSystem to set the tutorial title on the qlabs display screen
50 hSystem = QLabsSystem(qlabs)
51 hSystem.set_title_string('Complete Road Signage Tutorial')
52
53 spawn_crosswalk(qlabs)
54 spawn_signs(qlabs, right_hand_driving)
55 spawn_traffic_lights(qlabs, right_hand_driving)
56 spawn_cones(qlabs)
57
58 fly_through_animation(qlabs)
59
60 # Closing qlabs
61 qlabs.close()
62 print('Done!')
63
64
65
66
67def spawn_crosswalk(qlabs):
68 # Create a crosswalk in this qlabs instance. Since we don't need
69 # to access the actors again after creating them, we can use a single
70 # class object to spawn all the varieties. We also don't need to use
71 # the waitForConfirmation because we don't need to store the actor ID
72 # for future reference.
73
74 crosswalk = QLabsCrosswalk(qlabs)
75
76 # spawn crosswalk with degrees in config 0
77 crosswalk.spawn_degrees(location=[-12.992, -7.407, 0.005], rotation=[0,0,48], scale=[1,1,1], configuration=0, waitForConfirmation=False)
78
79 # spawn crosswalk with degrees in config 1
80 crosswalk.spawn_degrees(location=[-6.788, 45, 0.00], rotation=[0,0,90], scale=[1,1,1], configuration=1, waitForConfirmation=False)
81
82 # spawn crosswalk with degrees in config 2
83 crosswalk.spawn_degrees(location=[21.733, 3.347, 0.005], rotation=[0,0,0], scale=[1,1,1], configuration=2, waitForConfirmation=False)
84
85 # spawn the last crosswalk with waitForConfirmation=True to confirm everything is flushed from the send buffers
86 crosswalk.spawn_degrees(location=[21.733, 16, 0.005], rotation=[0,0,0], scale=[1,1,1], configuration=2, waitForConfirmation=True)
87
88
89def spawn_signs(qlabs, right_hand_driving):
90 # Like the crosswalks, we don't need to access the actors again after
91 # creating them.
92
93 roundabout_sign = QLabsRoundaboutSign(qlabs)
94 yield_sign = QLabsYieldSign(qlabs)
95 stop_sign = QLabsStopSign(qlabs)
96
97 if (right_hand_driving):
98 stop_sign.spawn_degrees([17.561, 17.677, 0.215], [0,0,90])
99 stop_sign.spawn_degrees([24.3, 1.772, 0.2], [0,0,-90])
100 stop_sign.spawn_degrees([14.746, 6.445, 0.215], [0,0,180])
101
102 roundabout_sign.spawn_degrees([3.551, 40.353, 0.215], [0,0,180])
103 roundabout_sign.spawn_degrees([10.938, 28.824, 0.215], [0,0,-135])
104 roundabout_sign.spawn_degrees([24.289, 32.591, 0.192], [0,0,-90])
105
106 yield_sign.spawn_degrees([-2.169, -12.594, 0.2], [0,0,180])
107 else:
108 stop_sign.spawn_degrees([24.333, 17.677, 0.215], [0,0,90])
109 stop_sign.spawn_degrees([18.03, 1.772, 0.2], [0,0,-90])
110 stop_sign.spawn_degrees([14.746, 13.01, 0.215], [0,0,180])
111
112 roundabout_sign.spawn_degrees([16.647, 28.404, 0.215], [0,0,-45])
113 roundabout_sign.spawn_degrees([6.987, 34.293, 0.215], [0,0,-130])
114 roundabout_sign.spawn_degrees([9.96, 46.79, 0.2], [0,0,-180])
115
116 yield_sign.spawn_degrees([-21.716, 7.596, 0.2], [0,0,-90])
117
118
119def spawn_traffic_lights(qlabs, right_hand_driving):
120 # In this case, we want to track each traffic light individually so we
121 # can subsequently set the color state. By using spawning with an ID,
122 # we'll know exactly which one is which and this will allow us to also
123 # reference them in separate programs, and we can also spawn without
124 # waiting for confirmation because the object already knows its own ID.
125
126
127 # initialize four traffic light instances in qlabs
128 trafficLight1 = QLabsTrafficLight(qlabs)
129 trafficLight2 = QLabsTrafficLight(qlabs)
130 trafficLight3 = QLabsTrafficLight(qlabs)
131 trafficLight4 = QLabsTrafficLight(qlabs)
132
133 if (right_hand_driving):
134
135 trafficLight1.spawn_id_degrees(actorNumber=0, location=[5.889, 16.048, 0.215], rotation=[0,0,0], configuration=0, waitForConfirmation=False)
136 trafficLight2.spawn_id_degrees(actorNumber=1, location=[-2.852, 1.65, 0], rotation=[0,0,180], configuration=0, waitForConfirmation=False)
137 trafficLight1.set_color(color=trafficLight1.COLOR_GREEN, waitForConfirmation=False)
138 trafficLight2.set_color(color=trafficLight2.COLOR_GREEN, waitForConfirmation=False)
139
140 trafficLight3.spawn_id_degrees(actorNumber=3, location=[8.443, 5.378, 0], rotation=[0,0,-90], configuration=0, waitForConfirmation=False)
141 trafficLight4.spawn_id_degrees(actorNumber=4, location=[-4.202, 13.984, 0.186], rotation=[0,0,90], configuration=0, waitForConfirmation=False)
142 trafficLight3.set_color(color=trafficLight3.COLOR_RED, waitForConfirmation=False)
143 trafficLight4.set_color(color=trafficLight4.COLOR_RED, waitForConfirmation=False)
144
145 else:
146 trafficLight1.spawn_id_degrees(actorNumber=0, location=[-2.831, 16.643, 0.186], rotation=[0,0,180], configuration=1, waitForConfirmation=False)
147 trafficLight2.spawn_id_degrees(actorNumber=1, location=[5.653, 1.879, 0], rotation=[0,0,0], configuration=1, waitForConfirmation=False)
148 trafficLight1.set_color(color=trafficLight1.COLOR_GREEN, waitForConfirmation=False)
149 trafficLight2.set_color(color=trafficLight2.COLOR_GREEN, waitForConfirmation=False)
150
151 trafficLight3.spawn_id_degrees(actorNumber=3, location=[8.779, 13.7, 0.215], rotation=[0,0,90], configuration=1, waitForConfirmation=False)
152 trafficLight4.spawn_id_degrees(actorNumber=4, location=[-4.714, 4.745, 0], rotation=[0,0,-90], configuration=1, waitForConfirmation=False)
153 trafficLight3.set_color(color=trafficLight3.COLOR_RED, waitForConfirmation=False)
154 trafficLight4.set_color(color=trafficLight4.COLOR_RED, waitForConfirmation=False)
155
156
157
158def spawn_cones(qlabs):
159
160 # We'll assume the cones don't need to be referenced after they're spawned so a
161 # single class object will suffice for spawning.
162
163 cone = QLabsTrafficCone(qlabs)
164
165 for count in range(10):
166 # Since we're going to set the color, we need to wait for QLabs to assign
167 # an actor number. This can be executed more quickly if you spawn by ID
168 # instead and manually assign the numbers.
169 #
170 # Also note that since this are physics objects, it's a good idea to
171 # spawn the actors slight above the surface so they can fall into place.
172 # If you spawn exactly at ground level, they may "pop" up from the surface.
173
174 cone.spawn(location=[-15.313, 35.374+count*-1.3, 0.25], configuration=1, waitForConfirmation=True)
175 cone.set_material_properties(materialSlot=0, color=[0,0,0],roughness=1,metallic=False)
176 cone.set_material_properties(materialSlot=1, color=HSVtoRGB([count/10, 1, 1]))
177
178
179def HSVtoRGB(hsv):
180
181 H = hsv[0]
182 S = hsv[1]
183 V = hsv[2]
184
185 kr = (5+H*6) % 6
186 kg = (3+H*6) % 6
187 kb = (1+H*6) % 6
188
189 r = 1 - max(min(min(kr, 4-kr), 1), 0)
190 g = 1 - max(min(min(kg, 4-kg), 1), 0)
191 b = 1 - max(min(min(kb, 4-kb), 1), 0)
192
193 return [r, g, b]
194
195
196def fly_through_animation(qlabs):
197 # Linearly interpolate through a series of points to fly the camera
198 # around the map. For each source and destination, calculate the distance
199 # so the step size is an even multiple that is approximately equal to the
200 # desired velocity.
201
202 # Translation/rotation point pairs
203 points = [[[1.5, -12.558, 1.708], [-0, 0.023, 1.405]],
204 [[0.721, -0.922, 1.721], [-0, 0.027, 1.255]],
205 [[6.082, 7.208, 1.566], [-0, 0.027, -0.309]],
206 [[20.732, 3.179, 1.997], [0, 0.049, 1.452]],
207 [[26.083, 30.157, 2.459], [0, 0.153, 2.491]],
208 [[17.211, 46.775, 11.61], [-0, 0.348, -2.189+2*math.pi]],
209 [[-17.739, 38.866, 0.956], [0, 0.142, -1.385+2*math.pi]],
210 [[-16.068, 24.53, 0.628], [-0, -0.043, -1.484+2*math.pi]],
211 [[-20.302, 1.82, 1.815], [-0, 0.03, -0.872+2*math.pi]],
212 [[-3.261, -14.664, 1.597], [-0, -0.038, 0.894+2*math.pi]]]
213
214 speed = 0.3
215 filter_translation_weight = 0.1
216 filter_rotation_weight = 0.1
217
218
219 camera = QLabsFreeCamera(qlabs)
220 camera.spawn_id(0,points[0][0], points[0][1])
221 camera.possess()
222
223 fx = points[0][0][0]
224 fy = points[0][0][1]
225 fz = points[0][0][2]
226
227 froll = points[0][1][0]
228 fpitch = points[0][1][1]
229 fyaw = points[0][1][2]
230
231 for index in range(len(points) - 1):
232 # Calculate the integer number of steps by dividing the distance by speed
233 translation_distance = dist(points[index][0], points[index+1][0])
234 total_steps = int(round(translation_distance/speed,0))
235
236
237 for step in range(total_steps):
238 # Linearly interpolate between each of the target points
239 x = interp(points[index][0][0], points[index+1][0][0], step, total_steps)
240 y = interp(points[index][0][1], points[index+1][0][1], step, total_steps)
241 z = interp(points[index][0][2], points[index+1][0][2], step, total_steps)
242
243 roll = interp(points[index][1][0], points[index+1][1][0], step, total_steps)
244 pitch = interp(points[index][1][1], points[index+1][1][1], step, total_steps)
245 yaw = interp(points[index][1][2], points[index+1][1][2], step, total_steps)
246
247 # Filter the calcuated values to smooth out the camera motion
248 fx = fx*(1-filter_translation_weight) + x*filter_translation_weight
249 fy = fy*(1-filter_translation_weight) + y*filter_translation_weight
250 fz = fz*(1-filter_translation_weight) + z*filter_translation_weight
251
252 froll = froll*(1-filter_rotation_weight) + roll*filter_rotation_weight
253 fpitch = fpitch*(1-filter_rotation_weight) + pitch*filter_rotation_weight
254 fyaw = fyaw*(1-filter_rotation_weight) + yaw*filter_rotation_weight
255
256
257 # To try to make the animation as consistent as possible across different
258 # hardware, measure the elapsed time and delay a variable amount to try
259 # to maintain 33 fps.
260
261 start_time = time.time()
262 camera.set_transform(location=[fx, fy, fz], rotation=[froll, fpitch, fyaw])
263 end_time = time.time()
264 while (end_time - start_time < 0.03):
265 end_time = time.time()
266
267def dist(v1, v2):
268 return pow( pow(v1[0]-v2[0], 2) + pow(v1[1]-v2[1], 2) + pow(v1[2]-v2[2], 2), 0.5 )
269
270def interp(start, finish, step, total_steps):
271 return (finish-start)/total_steps*step + start
272
273if __name__ == "__main__":
274 main(right_hand_driving)
Matlab Tutorial
Raw to download this tutorial: Traffic Cones Tutorial (.m)
.
1% Road Signage Library Traffic Cones 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% or Cityscape Lite
9
10close all;
11clear all;
12clc;
13
14% --------------------------------------------------------------
15% Setting MATLAB Path for the libraries
16% Always keep at the start, it will make sure it finds the correct references
17newPathEntry = fullfile(getenv('QAL_DIR'), 'libraries', 'matlab', 'qvl');
18pathCell = regexp(path, pathsep, 'split');
19if ispc % Windows is not case-sensitive
20 onPath = any(strcmpi(newPathEntry, pathCell));
21else
22 onPath = any(strcmp(newPathEntry, pathCell));
23end
24
25if onPath == 0
26 path(path, newPathEntry)
27 savepath
28end
29% --------------------------------------------------------------
30
31fprintf('\n\n----------------- Communications -------------------\n\n');
32
33qlabs = QuanserInteractiveLabs();
34connection_established = qlabs.open('localhost');
35
36if connection_established == false
37 disp("Failed to open connection.")
38 return
39end
40
41
42disp('Connected')
43
44num_destroyed = qlabs.destroy_all_spawned_actors();
45
46fprintf('%d actors destroyed', num_destroyed);
47
48% Use hSystem to set the tutorial title in the upper left of the qlabs window
49hSystem = QLabsSystem(qlabs);
50hSystem.set_title_string('Traffic Cones Tutorial')
51
52% Switch the camera angle to see where we will be spawning the cones
53camera0 = QLabsFreeCamera(qlabs);
54camera0.spawn([-20.355, 27.374, 2.055], [-0, 0.308, -0.001]);
55camera0.possess();
56
57cone = QLabsTrafficCone(qlabs);
58cone1 = QLabsTrafficCone(qlabs);
59cone2 = QLabsTrafficCone(qlabs);
60cone3 = QLabsTrafficCone(qlabs);
61cone4 = QLabsTrafficCone(qlabs);
62
63% Spawn a small traffic cone using radians
64cone.spawn([-17, 28, 1.0], [0, 0, pi], [1, 1, 1], 0, 1);
65% Wait to see the output
66pause(1);
67
68% Destroy the cone
69cone.destroy();
70% Wait to see the output
71pause(1);
72
73% Spawn another small traffic cone using radians in the same place
74cone1.spawn_id(1, [-17, 28, 1.0], [0, 0, pi], [1, 1, 1], 0, 1);
75% Spawn a construction pylon using degrees and generating the actorNumber internally
76cone2.spawn_degrees([-15, 28, 1.0], [0, 0, 180], [1, 1, 1], 1, 1);
77% Spawn a small cone with one color stripe initialized using actor number and a position in degrees
78cone3.spawn_id_degrees(3, [-15, 26.5, 1.0], [0, 0, 90], [1, 1, 1], 2, 1);
79% Spawn a bigger cone with two color stripes using radians and generating the actorNumber internally
80cone4.spawn([-17, 26.5, 1.0], [0, 0, pi/4], [1, 1, 1], 3, 1);
81
82%Wait to see the output
83pause(1.5);
84
85% Change the color of the cones (materialSlot 0 is the base color, and 1 is the stripes)
86
87% set_material_properties(materialSlot, color, roughness, metallic, waitForConfirmation)
88cone1.set_material_properties(0, [0, 0, 1], 1); % Blue
89% Wait to see the output
90pause(0.5);
91
92cone2.set_material_properties(0, [1, 0, 0], 1, 0); % Red
93cone2.set_material_properties(1, [1, 0.5, 0]);
94pause(0.5);
95
96cone3.set_material_properties(0, [0, 1, 1]); % Cyan
97cone3.set_material_properties(1, [0, 0.3, 1], 1, 1);
98pause(0.5);
99
100cone4.set_material_properties(0, [1, 0, 1], 0, 0); % Magenta
101cone4.set_material_properties(1, [0.3, 0, 1]);
102pause(0.5);
103
104cone1.set_material_properties(0, [0,1,0],0.5,0) % Green
105cone1.set_material_properties(1, [1,.5,0],0)
106pause(0.5)
107
108cone2.set_material_properties(0, [1,1,0],1,1) % Yellow
109cone2.set_material_properties(1, [0,0,0],0)
110pause(0.5)
111
112cone3.set_material_properties(0, [0.5,0.5,0.5],1,1) % Grey
113cone3.set_material_properties(1, [0.6,0.2,0.6],1,1)
114pause(0.5)
115
116cone4.set_material_properties(0, [0.5,0.0,0.7],0,0) % Magenta
117cone4.set_material_properties(1, [0.5,0.2,0.0],1,1)
118
119pause(3);
120
121cone1.destroy();
122pause(1);
123
124cone2.destroy();
125pause(1);
126
127cone3.destroy();
128pause(1);
129
130cone4.destroy();
131pause(1);
132
133% Closing qlabs
134qlabs.close();
135disp('Done!');
Complete Road Signage Matlab Tutorial
Raw to download this tutorial: Complete Road Signage Tutorial (.m)
.
1% Road signage Library 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% or Cityscape Lite
9
10close all;
11clear all;
12clc;
13
14% Select if you would like to spawn the signs for right or left hand
15% orientation.
16right_hand_driving = true;
17
18% --------------------------------------------------------------
19% Setting MATLAB Path for the libraries
20% Always keep at the start, it will make sure it finds the correct references
21newPathEntry = fullfile(getenv('QAL_DIR'), 'libraries', 'matlab', 'qvl');
22pathCell = regexp(path, pathsep, 'split');
23if ispc % Windows is not case-sensitive
24 onPath = any(strcmpi(newPathEntry, pathCell));
25else
26 onPath = any(strcmp(newPathEntry, pathCell));
27end
28
29if onPath == 0
30 path(path, newPathEntry)
31 savepath
32end
33% --------------------------------------------------------------
34
35
36main(right_hand_driving);
37
38function main(right_hand_driving)
39
40 % creates a server connection with Quanser Interactive Labs and manages the communications
41 qlabs = QuanserInteractiveLabs();
42 connection_established = qlabs.open('localhost');
43
44 % trying to connect to QLabs and open the instance we have created - program will end if this fails
45 if connection_established == false
46 disp("Failed to open connection.")
47 return
48 end
49
50 disp('Connected')
51
52 % destroying any spawned actors in our QLabs that currently exist
53 num_destroyed = qlabs.destroy_all_spawned_actors();
54 fprintf('%d actors destroyed\n', num_destroyed);
55
56 % Use hSystem to set the tutorial title on the qlabs display screen
57 hSystem = QLabsSystem(qlabs);
58 hSystem.set_title_string('Complete Road Signage Tutorial');
59
60 spawn_crosswalk(qlabs);
61 spawn_signs(qlabs, right_hand_driving);
62 spawn_traffic_lights(qlabs, right_hand_driving);
63 spawn_cones(qlabs);
64
65 fly_through_animation(qlabs);
66
67 % Closing qlabs
68 qlabs.close();
69 disp('Done!');
70
71end
72
73function spawn_crosswalk(qlabs)
74 % Create a crosswalk in this qlabs instance. Since we don't need
75 % to access the actors again after creating them, we can use a single
76 % class object to spawn all the varieties. We also don't need to use
77 % the waitForConfirmation because we don't need to store the actor ID
78 % for future reference.
79
80 crosswalk = QLabsCrosswalk(qlabs);
81
82 % spawn crosswalk with degrees in config 0
83 crosswalk.spawn_degrees([-12.992, -7.407, 0.005], [0,0,48], [1,1,1], 0, false);
84
85 % spawn crosswalk with degrees in config 1
86 crosswalk.spawn_degrees([-6.788, 45, 0.00], [0,0,90], [1,1,1], 1, false);
87
88 % spawn crosswalk with degrees in config 2
89 crosswalk.spawn_degrees([21.733, 3.347, 0.005], [0,0,0], [1,1,1], 2, false);
90
91 % spawn the last crosswalk with waitForConfirmation=True to confirm everything is flushed from the send buffers
92 crosswalk.spawn_degrees([21.733, 16, 0.005], [0,0,0], [1,1,1], 2, true);
93end
94
95function spawn_signs(qlabs, right_hand_driving)
96 % Like the crosswalks, we don't need to access the actors again after
97 % creating them.
98
99 roundabout_sign = QLabsRoundaboutSign(qlabs);
100 yield_sign = QLabsYieldSign(qlabs);
101 stop_sign = QLabsStopSign(qlabs);
102
103 if (right_hand_driving)
104 stop_sign.spawn_degrees([17.561, 17.677, 0.215], [0,0,90]);
105 stop_sign.spawn_degrees([24.3, 1.772, 0.2], [0,0,-90]);
106 stop_sign.spawn_degrees([14.746, 6.445, 0.215], [0,0,180]);
107
108 roundabout_sign.spawn_degrees([3.551, 40.353, 0.215], [0,0,180]);
109 roundabout_sign.spawn_degrees([10.938, 28.824, 0.215], [0,0,-135]);
110 roundabout_sign.spawn_degrees([24.289, 32.591, 0.192], [0,0,-90]);
111
112 yield_sign.spawn_degrees([-2.169, -12.594, 0.2], [0,0,180]);
113 else
114 stop_sign.spawn_degrees([24.333, 17.677, 0.215], [0,0,90]);
115 stop_sign.spawn_degrees([18.03, 1.772, 0.2], [0,0,-90]);
116 stop_sign.spawn_degrees([14.746, 13.01, 0.215], [0,0,180]);
117
118 roundabout_sign.spawn_degrees([16.647, 28.404, 0.215], [0,0,-45]);
119 roundabout_sign.spawn_degrees([6.987, 34.293, 0.215], [0,0,-130]);
120 roundabout_sign.spawn_degrees([9.96, 46.79, 0.2], [0,0,-180]);
121
122 yield_sign.spawn_degrees([-21.716, 7.596, 0.2], [0,0,-90]);
123 end
124end
125
126function spawn_traffic_lights(qlabs, right_hand_driving)
127 % In this case, we want to track each traffic light individually so we
128 % can subsequently set the color state. By using spawning with an ID,
129 % we'll know exactly which one is which and this will allow us to also
130 % reference them in separate programs, and we can also spawn without
131 % waiting for confirmation because the object already knows its own ID.
132
133
134 % initialize four traffic light instances in qlabs
135 trafficLight1 = QLabsTrafficLight(qlabs);
136 trafficLight2 = QLabsTrafficLight(qlabs);
137 trafficLight3 = QLabsTrafficLight(qlabs);
138 trafficLight4 = QLabsTrafficLight(qlabs);
139
140 if (right_hand_driving)
141
142 trafficLight1.spawn_id_degrees(0, [5.889, 16.048, 0.215], [0,0,0], [1,1,1], 0, false);
143 trafficLight2.spawn_id_degrees(1, [-2.852, 1.65, 0], [0,0,180], [1,1,1], 0, false);
144 trafficLight1.set_color(trafficLight1.COLOR_GREEN, false);
145 trafficLight2.set_color(trafficLight2.COLOR_GREEN, false);
146
147 trafficLight3.spawn_id_degrees(3, [8.443, 5.378, 0], [0,0,-90], [1,1,1], 0, false);
148 trafficLight4.spawn_id_degrees(4, [-4.202, 13.984, 0.186], [0,0,90], [1,1,1], 0, false);
149 trafficLight3.set_color(trafficLight3.COLOR_RED, false);
150 trafficLight4.set_color(trafficLight4.COLOR_RED, false);
151
152 else
153 trafficLight1.spawn_id_degrees(0, [-2.831, 16.643, 0.186], [0,0,180], [1,1,1], 1, false);
154 trafficLight2.spawn_id_degrees(1, [5.653, 1.879, 0], [0,0,0], [1,1,1], 1, false);
155 trafficLight1.set_color(trafficLight1.COLOR_GREEN, false);
156 trafficLight2.set_color(trafficLight2.COLOR_GREEN, false);
157
158 trafficLight3.spawn_id_degrees(3, [8.779, 13.7, 0.215], [0,0,90], [1,1,1], 1, false);
159 trafficLight4.spawn_id_degrees(4, [-4.714, 4.745, 0], [0,0,-90], [1,1,1], 1, false);
160 trafficLight3.set_color(trafficLight3.COLOR_RED, false);
161 trafficLight4.set_color(trafficLight4.COLOR_RED, false);
162 end
163end
164
165
166function spawn_cones(qlabs)
167
168 % We'll assume the cones don't need to be referenced after they're spawned so a
169 % single class object will suffice for spawning.
170
171 cone = QLabsTrafficCone(qlabs, true);
172
173 for count = 1:10
174 % Since we're going to set the color, we need to wait for QLabs to assign
175 % an actor number. This can be executed more quickly if you spawn by ID
176 % instead and manually assign the numbers.
177 %
178 % Also note that since this are physics objects, it's a good idea to
179 % spawn the actors slight above the surface so they can fall into place.
180 % If you spawn exactly at ground level, they may "pop" up from the surface.
181
182 cone.spawn([-15.313, 35.374+(count-1)*-1.3, 0.25], [0,0,0], [1,1,1], 1, true);
183 cone.set_material_properties(0, [0,0,0], 1, true);
184 cone.set_material_properties(1, HSVtoRGB([(count-1)/10, 1, 1]));
185 end
186end
187
188
189function color = HSVtoRGB(hsv)
190
191 H = hsv(1);
192 S = hsv(2);
193 V = hsv(3);
194
195 kr = mod((5+H*6), 6);
196 kg = mod((3+H*6), 6);
197 kb = mod((1+H*6), 6);
198
199 r = 1 - max(min(min(kr, 4-kr), 1), 0);
200 g = 1 - max(min(min(kg, 4-kg), 1), 0);
201 b = 1 - max(min(min(kb, 4-kb), 1), 0);
202
203 color = [r, g, b];
204end
205
206function fly_through_animation(qlabs)
207 % Linearly interpolate through a series of points to fly the camera
208 % around the map. For each source and destination, calculate the distance
209 % so the step size is an even multiple that is approximately equal to the
210 % desired velocity.
211
212 % Translation/rotation point pairs
213 points = [[1.5, -12.558, 1.708], [-0, 0.023, 1.405]
214 [0.721, -0.922, 1.721], [-0, 0.027, 1.255]
215 [6.082, 7.208, 1.566], [-0, 0.027, -0.309]
216 [20.732, 3.179, 1.997], [0, 0.049, 1.452]
217 [26.083, 30.157, 2.459], [0, 0.153, 2.491]
218 [17.211, 46.775, 11.61], [-0, 0.348, -2.189+2*pi]
219 [-17.739, 38.866, 0.956], [0, 0.142, -1.385+2*pi]
220 [-16.068, 24.53, 0.628], [-0, -0.043, -1.484+2*pi]
221 [-20.302, 1.82, 1.815], [-0, 0.03, -0.872+2*pi]
222 [-3.261, -14.664, 1.597], [-0, -0.038, 0.894+2*pi]];
223
224 speed = 0.3;
225 filter_translation_weight = 0.1;
226 filter_rotation_weight = 0.1;
227
228
229 camera = QLabsFreeCamera(qlabs);
230 camera.spawn_id(0,points(1,1:3), points(1, 4:6));
231 camera.possess();
232
233 fx = points(1,1);
234 fy = points(1,2);
235 fz = points(1,3);
236
237 froll = points(1,4);
238 fpitch = points(1,5);
239 fyaw = points(1,6);
240
241 for index = 1:(size(points,1)-1)
242 % Calculate the integer number of steps by dividing the distance by speed
243 translation_distance = dist(points(index, 1:3), points(index+1, 1:3));
244 total_steps = round(translation_distance/speed);
245
246
247 for step = 1:total_steps
248 % Linearly interpolate between each of the target points
249 x = interp(points(index,1), points(index+1,1), step, total_steps);
250 y = interp(points(index,2), points(index+1,2), step, total_steps);
251 z = interp(points(index,3), points(index+1,3), step, total_steps);
252
253 roll = interp(points(index,4), points(index+1,4), step, total_steps);
254 pitch = interp(points(index,5), points(index+1,5), step, total_steps);
255 yaw = interp(points(index,6), points(index+1,6), step, total_steps);
256
257 % Filter the calcuated values to smooth out the camera motion
258 fx = fx*(1-filter_translation_weight) + x*filter_translation_weight;
259 fy = fy*(1-filter_translation_weight) + y*filter_translation_weight;
260 fz = fz*(1-filter_translation_weight) + z*filter_translation_weight;
261
262 froll = froll*(1-filter_rotation_weight) + roll*filter_rotation_weight;
263 fpitch = fpitch*(1-filter_rotation_weight) + pitch*filter_rotation_weight;
264 fyaw = fyaw*(1-filter_rotation_weight) + yaw*filter_rotation_weight;
265
266
267 % To try to make the animation as consistent as possible across different
268 % hardware, measure the elapsed time and delay a variable amount to try
269 % to maintain 33 fps. If QLabs can't run at 33fps, it will just
270 % run as fast as possible.
271
272 tic
273 camera.set_transform([fx, fy, fz], [froll, fpitch, fyaw]);
274 while (toc < 0.03)
275 pause(0.001);
276 end
277 end
278 end
279end
280
281function value = dist(v1, v2)
282 value = ((v1(1)-v2(1))^2 + (v1(2)-v2(2))^2 + (v1(3)-v2(3))^2)^0.5;
283end
284
285function value = interp(start, finish, step, total_steps)
286 value = (finish-start)/total_steps*step + start;
287end