QCar

Description
QCars are considered “actors” in Quanser Interactive Labs. The QCar library can be used to acquire sensor data from the virtual environment and controls the motion of the vehicles.
See the QCar Tutorial to get a better understanding of using QCars in Quanser Interactive Labs.
Library
Constants
- QLabsQCar.ID_QCAR = 160
Class ID
Note
CSI camera resolution is 820x410.
- QLabsQCar.CAMERA_CSI_RIGHT = 0
- QLabsQCar.CAMERA_CSI_BACK = 1
- QLabsQCar.CAMERA_CSI_LEFT = 2
- QLabsQCar.CAMERA_CSI_FRONT = 3
Note
RGB and depth resolution is 640x480.
- QLabsQCar.CAMERA_RGB = 4
- QLabsQCar.CAMERA_DEPTH = 5
Note
Overhead and trailing cameras support user zoom using the mouse wheel.
- QLabsQCar.CAMERA_OVERHEAD = 6
- QLabsQCar.CAMERA_TRAILING = 7
Member Variables
- QLabsQCar.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
- QLabsQCar.set_transform_and_request_state(location, rotation, enableDynamics, headlights, leftTurnSignal, rightTurnSignal, brakeSignal, reverseSignal, waitForConfirmation=True)[source]
Sets the location, rotation, and other car properties. Note that setting the location ignores collisions so ensure that the location is free of obstacles that may trap the actor if it is subsequently used in a dynamic mode. This transform can also be used to “playback” previously recorded position data without the need for a full dynamic model.
- Parameters
location (float array[3]) – An array of floats for x, y and z coordinates in full-scale units. Multiply physical QCar locations by 10 to get full scale locations.
rotation (float array[3]) – An array of floats for the roll, pitch, and yaw in radians
enableDynamics (boolean) – (default True) Enables or disables gravity for set transform requests.
headlights (boolean) – Enable the headlights
leftTurnSignal (boolean) – Enable the left turn signal
rightTurnSignal (boolean) – Enable the right turn signal
brakeSignal (boolean) – Enable the brake lights (does not affect the motion of the vehicle)
reverseSignal (boolean) – Play a honking sound
waitForConfirmation (boolean) – (Optional) Wait for confirmation before proceeding. This makes the method a blocking operation. NOTE: Return data will only be valid if waitForConfirmation is True.
- Returns
status - True if successful or False otherwise
location - in full scale
rotation - in radians
forward vector - unit scale vector
up vector - unit scale vector
front bumper hit - True if in contact with a collision object, False otherwise
rear bumper hit - True if in contact with a collision object, False otherwise
- Return type
boolean, float array[3], float array[3], float array[3], float array[3], boolean, boolean
- QLabsQCar.set_transform_and_request_state_degrees(location, rotation, enableDynamics, headlights, leftTurnSignal, rightTurnSignal, brakeSignal, reverseSignal, waitForConfirmation=True)[source]
Sets the location, rotation, and other car properties. Note that setting the location ignores collisions so ensure that the location is free of obstacles that may trap the actor if it is subsequently used in a dynamic mode. This transform can also be used to “playback” previously recorded position data without the need for a full dynamic model.
- Parameters
location (float array[3]) – An array of floats for x, y and z coordinates in full-scale units. Multiply physical QCar locations by 10 to get full scale locations.
rotation (float array[3]) – An array of floats for the roll, pitch, and yaw in degrees.
enableDynamics (boolean) – (default True) Enables or disables gravity for set transform requests.
headlights (boolean) – Enable the headlights.
leftTurnSignal (boolean) – Enable the left turn signal.
rightTurnSignal (boolean) – Enable the right turn signal.
brakeSignal (boolean) – Enable the brake lights (does not affect the motion of the vehicle).
reverseSignal (boolean) – Enable the reverse lights.
waitForConfirmation (boolean) – (Optional) Wait for confirmation before proceeding. This makes the method a blocking operation. NOTE: Return data will only be valid if waitForConfirmation is True.
- Returns
status - True if successful or False otherwise
location - in full scale
rotation - in radians
forward vector - unit scale vector
up vector - unit scale vector
front bumper hit - True if in contact with a collision object, False otherwise
rear bumper hit - True if in contact with a collision object, False otherwise
- Return type
boolean, float array[3], float array[3], float array[3], float array[3], boolean, boolean
- QLabsQCar.set_velocity_and_request_state(forward, turn, headlights, leftTurnSignal, rightTurnSignal, brakeSignal, reverseSignal)[source]
Sets the velocity, turn angle in radians, and other car properties.
- Parameters
forward (float) – Speed in m/s of a full-scale car. Multiply physical QCar speeds by 10 to get full scale speeds.
turn (float) – Turn angle in radians. Positive values turn right.
headlights (boolean) – Enable the headlights.
leftTurnSignal (boolean) – Enable the left turn signal.
rightTurnSignal (boolean) – Enable the right turn signal.
brakeSignal (boolean) – Enable the brake lights (does not affect the motion of the vehicle).
reverseSignal (boolean) – Enable the reverse lights.
- Returns
status - True if successful, False otherwise
location
rotation - in radians
front bumper hit - True if in contact with a collision object, False otherwise
rear bumper hit - True if in contact with a collision object, False otherwise
- Return type
boolean, float array[3], float array[3], boolean, boolean
- QLabsQCar.set_velocity_and_request_state_degrees(forward, turn, headlights, leftTurnSignal, rightTurnSignal, brakeSignal, reverseSignal)[source]
Sets the velocity, turn angle in degrees, and other car properties.
- Parameters
forward (float) – Speed in m/s of a full-scale car. Multiply physical QCar speeds by 10 to get full scale speeds.
turn (float) – Turn angle in degrees. Positive values turn right.
headlights (boolean) – Enable the headlights.
leftTurnSignal (boolean) – Enable the left turn signal.
rightTurnSignal (boolean) – Enable the right turn signal.
brakeSignal (boolean) – Enable the brake lights (does not affect the motion of the vehicle).
reverseSignal (boolean) – Enable the reverse lights.
- Returns
status - True if successful, False otherwise
location
rotation - in radians
front bumper hit - True if in contact with a collision object, False otherwise
rear bumper hit - True if in contact with a collision object, False otherwise
- Return type
boolean, float array[3], float array[3], boolean, boolean
- QLabsQCar.possess(camera=CAMERA_TRAILING)[source]
Possess (take control of) a QCar in QLabs with the selected camera.
- Parameters
camera (uint32) – Pre-defined camera constant. See CAMERA constants for available options. Default is the trailing camera.
- Returns
status - True if possessing the camera was successful, False otherwise
- Return type
boolean
- QLabsQCar.ghost_mode(enable=True, color=[0, 1, 0])[source]
Ghost mode changes the selected QCar actor into a transparent colored version. This can be useful as a reference actor or indicating a change in state.
- Parameters
enable (boolean) – Set the QCar to the defined transparent color, otherwise revert to the solid color scheme.
color (float array[3]) – Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsQCar.get_image(camera)[source]
Request a JPG image from one of the QCar cameras.
- Parameters
camera (uint32) – Pre-defined camera constant. See CAMERA constants for available options. Trailing and Overhead cameras cannot be selected.
- Returns
status - True and image data if successful, False and empty otherwise
imageData - Image in a JPG format
- Return type
boolean, byte array with jpg data
- QLabsQCar.get_lidar(samplePoints=400)[source]
Request LIDAR data from a QCar.
- Parameters
samplePoints (uint32) – (Optional) Change the number of points per revolution of the LIDAR.
- Returns
True, angles in radians, and distances in m if successful, False, none, and none otherwise
- Return type
boolean, float array, float array
Parent Class (actor.py) Methods
- QLabsQCar.__init__(qlabs, verbose=False)[source]
Constructor Method
- Parameters
qlabs (object) – A QuanserInteractiveLabs object
verbose (boolean) – (Optional) Print error information to the console.
- QLabsQCar.spawn(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)[source]
Spawns a new QCar 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 and only uniform scaling is recommended. Sensor scaling will be based on scale[0].
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
- QLabsQCar.spawn_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)[source]
Spawns a new QCar 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 and only uniform scaling is recommended. Sensor scaling will be based on scale[0].
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
- QLabsQCar.spawn_id(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)[source]
Spawns a new QCar 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 and only uniform scaling is recommended. Sensor scaling will be based on scale[0].
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
- QLabsQCar.spawn_id_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)[source]
Spawns a new QCar 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 and only uniform scaling is recommended. Sensor scaling will be based on scale[0].
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
- QLabsQCar.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
- QLabsQCar.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
- QLabsQCar.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
- QLabsQCar.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]
- QLabsQCar.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 is only one configuration of the QCar actor.
Connection Points

Reference Frame Number |
Parent Frame |
Relative Transform to Parent (Location, Rotation) |
Description |
---|---|---|---|
0 |
The base frame is located at ground level, centered between the two rear wheels. This represents the location of the car with no filtering, suspension, or dynamics. Collision detection is connected to this reference frame. |
||
1 |
0 |
[0,0,0] [0,0,0] |
The filtered frame is co-located with connection point 0, but it is a filtered position to simulated the suspension and dynamic effects. All the visual elements and sensors of the QCar are connected to this frame. |
Component Extrinsics
“Extrinsics” refer to the external relationship of an object with respect to a specific frame of reference (in this case the body center of the QCar). Sometimes it’s important to know specific distances and orientation of extrinsic components, for instance, this can be use for obstacle detection and camera calibration. You will find a list of the important extrinsics below.
Distances From Body Center
The body frame is located between the front and rear axles on the ground plane. Distances of the QCar in its virtual environment are 10 times larger than on the physical system so a QCar spawned at a scale of 1 is equivalent size to a full-scale automobile. A QCar spawned at a scale of 0.1 will be equivalent to the size of a physical QCar.

Component |
x (m) |
y (m) |
z (m) |
---|---|---|---|
CG |
0.248 |
-0.074 |
0.709 |
Front axle |
1.300 |
0 |
0.310 |
Rear axle |
-1.300 |
0 |
0.310 |
CSI front |
1.930 |
0 |
0.953 |
CSI left |
0.140 |
0.438 |
0.953 |
CSI rear |
-1.650 |
0 |
0.953 |
CSI right |
0.140 |
-0.674 |
0.953 |
IMU |
1.278 |
0.223 |
0.895 |
RealSense |
0.822 |
0.003 |
1.582 |
RPLIDAR |
-0.108 |
-0.001 |
1.799 |
Transformation Matrices
All transformation matrices are built off of the body frame and camera frames for the QCar. To read more about this check out our documentation here by clicking on Research Resources link and looking inside the zip folder for src/user_manuals/qcar/user_manual_system_hardware.pdf

QCar Tutorial
Python Tutorial
Raw to download this tutorial: QCar Tutorial (.py)
.
1"""
2QCar Library Example
3----------------------
4This example will show you how to spawn cars, and use the qvl library commands
5to control the car and its related functions.
6
7.. note::
8
9 Make sure you have Quanser Interactive Labs open before running this
10 example. This example is designed to best be run in QCar Cityscape.
11
12"""
13
14from qvl.qlabs import QuanserInteractiveLabs
15from qvl.free_camera import QLabsFreeCamera
16from qvl.basic_shape import QLabsBasicShape
17from qvl.qcar import QLabsQCar
18from qvl.environment_outdoors import QLabsEnvironmentOutdoors
19
20import time
21import math
22import numpy as np
23import cv2
24import os
25
26import pyqtgraph as pg
27from pyqtgraph.Qt import QtWidgets
28
29from qvl.system import QLabsSystem
30
31
32
33def main():
34 os.system('cls')
35
36 #Communications with qlabs
37
38 qlabs = QuanserInteractiveLabs()
39 cv2.startWindowThread()
40
41 print("Connecting to QLabs...")
42 if (not qlabs.open("localhost")):
43 print("Unable to connect to QLabs")
44 return
45
46 print("Connected")
47
48 qlabs.destroy_all_spawned_actors()
49
50 # Use hSystem to set the tutorial title in the upper left of the qlabs window
51 hSystem = QLabsSystem(qlabs)
52 hSystem.set_title_string('QCar Tutorial')
53
54 ### QCar
55
56 hCameraQCars = QLabsFreeCamera(qlabs)
57 hCameraQCars.spawn_id(actorNumber=1, location=[-15.075, 26.703, 6.074], rotation=[0, 0.564, -1.586])
58 hCameraQCars.possess()
59
60 print("\n\n---QCar---")
61
62 #spawning the QCar with radians
63 hQCar0 = QLabsQCar(qlabs)
64 hQCar0.spawn_id(actorNumber=0, location=[-8.700, 14.643, 0.005], rotation=[0,0,math.pi/2], waitForConfirmation=True)
65
66 #Spawn and destroy the existing QCar
67 hQCar1 = QLabsQCar(qlabs)
68 hQCar1.spawn_id(actorNumber=1, location=[-15.075, 26.703, 6.074], rotation=[0,0,math.pi/2], waitForConfirmation=True)
69 hQCar1.destroy()
70
71 #spawn a QCar with degrees
72 hQCar2 = QLabsQCar(qlabs)
73 x = hQCar2.spawn_id_degrees(actorNumber=2, location=[-11.048, 14.643, 0.005], rotation=[0,0,90], waitForConfirmation=True)
74
75 #pinging the QCar
76 hQCar2.ping()
77
78 time.sleep(0.5)
79
80 # Set the velocity and direction of the QCar in radians while also turning on the headlights and right turn signal
81 hQCar2.set_velocity_and_request_state(forward=1, turn = -math.pi/6, headlights=True, leftTurnSignal=False, rightTurnSignal=True, brakeSignal=False, reverseSignal=False)
82 time.sleep(1)
83 # Set the velocity to 0 and direction of the QCar in radians while keeping the headlights on and right turn signal on
84 hQCar2.set_velocity_and_request_state(forward=0.0, turn = -math.pi/6, headlights=True, leftTurnSignal=False, rightTurnSignal=True, brakeSignal=False, reverseSignal=False)
85 # Set the velocity to 1 and direction of the QCar in degrees while keeping the headlights on and turning on the left turn signal
86 hQCar2.set_velocity_and_request_state_degrees(forward=1, turn = 30, headlights=True, leftTurnSignal=True, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
87 time.sleep(1)
88 # Set the velocity to 0 and direction of the QCar in degrees while keeping the headlights on and left turn signal on
89 success, location, rotation, frontHit, rearHit = hQCar2.set_velocity_and_request_state_degrees(forward=0.0, turn = 30, headlights=True, leftTurnSignal=True, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
90
91 # Possess another QCar
92 x = hQCar2.possess()
93
94 time.sleep(0.1)
95 # Set the velocity to 1 of the QCar in radians while keeping the headlights, brakeSignal and reverseSignal on
96 hQCar2.set_velocity_and_request_state(forward=1, turn = 0, headlights=True, leftTurnSignal=True, rightTurnSignal=True, brakeSignal=True, reverseSignal=True)
97 time.sleep(1)
98 # Set the velocity to 0 while keeping the headlights, brakeSignal and reverseSignal on and turning on the left turn signal and right turn signal
99 hQCar2.set_velocity_and_request_state(forward=0.0, turn = 0, headlights=True, leftTurnSignal=True, rightTurnSignal=True, brakeSignal=True, reverseSignal=True)
100
101 # Turn all the lights off
102 hQCar2.set_velocity_and_request_state(forward=0, turn = 0, headlights=False, leftTurnSignal=False, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
103
104 # Car bumper test
105 hCameraQCars.possess()
106 # Change the camera view to see the bumper test
107 hCameraQCars.set_transform(location=[-17.045, 32.589, 6.042], rotation=[0, 0.594, -1.568])
108
109 # Spawn some shapes for our bumper test
110 hCubeQCarBlocks = QLabsBasicShape(qlabs)
111 hCubeQCarBlocks .spawn_id(100, [-11.919, 26.289, 0.5], [0,0,0], [1,1,1], configuration=hCubeQCarBlocks.SHAPE_CUBE, waitForConfirmation=True)
112 hCubeQCarBlocks .spawn_id(101, [-19.919, 26.289, 0.5], [0,0,0], [1,1,1], configuration=hCubeQCarBlocks.SHAPE_CUBE, waitForConfirmation=True)
113
114 # Create another QCar
115 hQCar3 = QLabsQCar(qlabs)
116 hQCar3.spawn_id(actorNumber=3, location=[-13.424, 26.299, 0.005], rotation=[0,0,math.pi])
117
118 # Have the QCar drive forward to hit the front block
119 for count in range(10):
120 x, location, rotation, frontHit, rearHit = hQCar3.set_velocity_and_request_state(forward=2, turn = 0, headlights=False, leftTurnSignal=False, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
121 time.sleep(0.25)
122
123 # Put the QCar in ghost mode
124 hQCar3.ghost_mode()
125
126 # Have the QCar drive backwards to hit the back bumper
127 for count in range(10):
128 x, location, rotation, frontHit, rearHit = hQCar3.set_velocity_and_request_state(forward=-2, turn = 0, headlights=False, leftTurnSignal=False, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
129 time.sleep(0.25)
130
131 # Change the color of ghost mode to red
132 hQCar3.ghost_mode(enable=True, color=[1,0,0])
133 time.sleep(0.50)
134
135 # Set the velocity to 0 and turn all lights off
136 hQCar3.set_velocity_and_request_state(forward=0, turn = 0, headlights=False, leftTurnSignal=False, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
137
138 # Set the location of the QCar and request the state of the car. If x== True and frontHit==True then the front bumper hit the block correctly.
139 x, location, rotation, forward_vector, up_vector, frontHit, rearHit = hQCar3.set_transform_and_request_state(location=[-16.1, 26.299, 0.005], rotation=[0,0,math.pi-0.01], enableDynamics=True, headlights=False, leftTurnSignal=False, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
140 time.sleep(0.50)
141
142 # Getting and saving the world transform of the QCar in Quanser Interactive Labs
143 x, loc, rot, scale = hQCar3.get_world_transform()
144
145 # Set the location of the QCar and request the state of the car. If x== True and rearHit==True then the back bumper hit the block correctly.
146 x, location, rotation, forward_vector, up_vector, frontHit, rearHit = hQCar3.set_transform_and_request_state_degrees(location=[-13.1, 26.299, 0.005], rotation=[0,0,179], enableDynamics=True, headlights=False, leftTurnSignal=False, rightTurnSignal=False, brakeSignal=False, reverseSignal=False)
147 time.sleep(0.50)
148
149 #Turning off ghost mode for the QCar
150 hQCar3.ghost_mode(enable=False, color=[1,0,0])
151
152 # Possessing the overhead camera on the QCar
153 hQCar2.possess(hQCar2.CAMERA_OVERHEAD)
154 time.sleep(0.5)
155
156 # Possessing the trailing camera on the QCar
157 hQCar2.possess(hQCar2.CAMERA_TRAILING)
158 time.sleep(0.5)
159
160 #Possessing the front CSI camera on the QCar
161 hQCar2.possess(hQCar2.CAMERA_CSI_FRONT)
162 time.sleep(0.5)
163
164 # Possessing the right CSI camera on the QCar
165 hQCar2.possess(hQCar2.CAMERA_CSI_RIGHT)
166 time.sleep(0.5)
167
168 # Possessing the back CSI camera on the QCar
169 hQCar2.possess(hQCar2.CAMERA_CSI_BACK)
170 time.sleep(0.5)
171
172 # Possessing the left CSI camera on the QCar
173 hQCar2.possess(hQCar2.CAMERA_CSI_LEFT)
174 time.sleep(0.5)
175
176 # Possessing the front RealSense RGB camera on the QCar
177 hQCar2.possess(hQCar2.CAMERA_RGB)
178 time.sleep(0.5)
179
180 # Possessing the RealSense depth camera on the QCar
181 hQCar2.possess(hQCar2.CAMERA_DEPTH)
182 time.sleep(0.5)
183
184 # Getting images from the different cameras
185 x, camera_image = hQCar2.get_image(camera=hQCar2.CAMERA_CSI_FRONT)
186 x, camera_image = hQCar2.get_image(camera=hQCar2.CAMERA_CSI_RIGHT)
187 x, camera_image = hQCar2.get_image(camera=hQCar2.CAMERA_CSI_BACK)
188 x, camera_image = hQCar2.get_image(camera=hQCar2.CAMERA_CSI_LEFT)
189 x, camera_image = hQCar2.get_image(camera=hQCar2.CAMERA_RGB)
190 x, camera_image = hQCar2.get_image(camera=hQCar2.CAMERA_DEPTH)
191
192
193 #LIDAR
194
195 hQCar3.possess(hQCar3.CAMERA_OVERHEAD)
196
197 # Creating a plot to plot the LIDAR data
198 lidarPlot = pg.plot(title="LIDAR")
199 squareSize = 100
200 lidarPlot.setXRange(-squareSize, squareSize)
201 lidarPlot.setYRange(-squareSize, squareSize)
202 lidarData = lidarPlot.plot([], [], pen=None, symbol='o', symbolBrush='r', symbolPen=None, symbolSize=2)
203
204 time.sleep(1)
205
206 print("Reading from LIDAR... if QLabs crashes or output isn't great, make sure FPS > 100")
207
208 # Have the QCar drive forward to hit the front block to show the live lidar.
209 # Speed can be changed by increasing or decreasing the value in the first
210 # parameter "forward"
211 hQCar3.set_velocity_and_request_state(1, 0, False, False, False, False, False)
212 lidar_rate = 0.05
213
214 # Obtaining and plotting lidar data for 0.2s
215 for count in range(25):
216
217 success, angle, distance = hQCar3.get_lidar(samplePoints=400)
218
219 x = np.sin(angle)*distance
220 y = np.cos(angle)*distance
221
222 lidarData.setData(x,y)
223 QtWidgets.QApplication.instance().processEvents()
224 time.sleep(lidar_rate) #lidar_rate is set at the top of this example
225
226
227 time.sleep(5)
228
229 # Closing qlabs
230 qlabs.close()
231 print("Done!")
232
233
234main()
Matlab Tutorial
Raw to download this tutorial: QCar Tutorial (.m)
.
1% QCar Library Example
2% -------------------------
3% This example will show you how to spawn cars, and use the qvl library commands
4% to control the car and its related functions.
5%
6% .. note::
7%
8% Make sure you have Quanser Interactive Labs open before running this
9% example. This example is designed to best be run in QCar Cityscape.
10
11
12close all;
13clear all;
14clc;
15
16% --------------------------------------------------------------
17% Setting MATLAB Path for the libraries
18% Always keep at the start, it will make sure it finds the correct references
19newPathEntry = fullfile(getenv('QAL_DIR'), 'libraries', 'matlab', 'qvl');
20pathCell = regexp(path, pathsep, 'split');
21if ispc % Windows is not case-sensitive
22 onPath = any(strcmpi(newPathEntry, pathCell));
23else
24 onPath = any(strcmp(newPathEntry, pathCell));
25end
26
27if onPath == 0
28 path(path, newPathEntry)
29 savepath
30end
31% --------------------------------------------------------------
32
33fprintf('\n\n----------------- Communications -------------------\n\n');
34
35qlabs = QuanserInteractiveLabs();
36connection_established = qlabs.open('localhost');
37
38if connection_established == false
39 disp("Failed to open connection.")
40 return
41end
42
43
44disp('Connected')
45
46num_destroyed = qlabs.destroy_all_spawned_actors();
47
48fprintf('%d actors destroyed', num_destroyed);
49
50% Use hSystem to set the tutorial title in the upper left of the qlabs window
51hSystem = QLabsSystem(qlabs);
52hSystem.set_title_string('QCar Tutorial')
53
54% Initialize QLabs
55hCameraQCars = QLabsFreeCamera(qlabs);
56hCameraQCars.spawn_id(1, [-15.075, 26.703, 6.074], [0, 0.564, -1.586]);
57hCameraQCars.possess();
58
59disp('---QCar---');
60
61% Spawning the QCar with radians
62hQCar0 = QLabsQCar(qlabs);
63hQCar0.spawn_id(0, [-8.700, 14.643, 0.005], [0, 0, pi/2], 1);
64
65% Spawn and destroy the existing QCar
66hQCar1 = QLabsQCar(qlabs);
67hQCar1.spawn_id(1, [-15.075, 26.703, 6.074], [0, 0, pi/2], 1);
68hQCar1.destroy();
69
70% Spawn a QCar with degrees
71hQCar2 = QLabsQCar(qlabs);
72x = hQCar2.spawn_id_degrees(2, [-11.048, 14.643, 0.005], [0, 0, 90], 1);
73
74% Pinging the QCar
75hQCar2.ping();
76
77
78
79% Set the velocity and direction of the QCar in radians while also turning on the headlights and right turn signal
80hQCar2.set_velocity_and_request_state(1, -pi/6, true, false, true, false, false);
81pause(1);
82
83% Set the velocity to 0 and direction of the QCar in radians while keeping the headlights on and right turn signal on
84hQCar2.set_velocity_and_request_state(0.0, -pi/6, true, false, true, false, false);
85
86% Set the velocity to 1 and direction of the QCar in degrees while keeping the headlights on and turning on the left turn signal
87hQCar2.set_velocity_and_request_state_degrees(1, 30, true, true, false, false, false);
88pause(1);
89
90% Set the velocity to 0 and direction of the QCar in degrees while keeping the headlights on and left turn signal on
91[success, location, rotation, frontHit, rearHit] = hQCar2.set_velocity_and_request_state_degrees(0.0, 30, true, true, false, false, false);
92
93% Possess another QCar
94x = hQCar2.possess();
95
96pause(0.1);
97
98% Set the velocity to 1 of the QCar in radians while keeping the headlights, brakeSignal and reverseSignal on
99hQCar2.set_velocity_and_request_state(1, 0, true, true, true, true, true);
100pause(1);
101
102% Set the velocity to 0 while keeping the headlights, brakeSignal and reverseSignal on and turning on the left turn signal and right turn signal
103hQCar2.set_velocity_and_request_state(0.0, 0, true, true, true, true, true);
104
105% Turn all the lights off
106hQCar2.set_velocity_and_request_state(0, 0, false, false, false, false, false);
107
108% Car bumper test
109hCameraQCars.possess();
110
111% Change the camera view to see the bumper test
112hCameraQCars.set_transform([-17.045, 32.589, 6.042], [0, 0.594, -1.568]);
113
114% Spawn some shapes for our bumper test
115hCubeQCarBlocks = QLabsBasicShape(qlabs);
116hCubeQCarBlocks.spawn_id(100, [-11.919, 26.289, 0.5], [0, 0, 0], [1, 1, 1], hCubeQCarBlocks.SHAPE_CUBE, true);
117hCubeQCarBlocks.spawn_id(101, [-19.919, 26.289, 0.5], [0, 0, 0], [1, 1, 1], hCubeQCarBlocks.SHAPE_CUBE, true);
118
119% Create another QCar
120hQCar3 = QLabsQCar(qlabs);
121hQCar3.spawn_id(3, [-13.424, 26.299, 0.005], [0, 0, pi]);
122
123% Have the QCar drive forward to hit the front block
124for count = 1:10
125 [x, location, rotation, frontHit, rearHit] = hQCar3.set_velocity_and_request_state(2, 0, false, false, false, false, false);
126 pause(0.25);
127end
128
129% Put the QCar in ghost mode
130hQCar3.ghost_mode();
131
132% Have the QCar drive backwards to hit the back bumper
133for count = 1:10
134 [x, location, rotation, frontHit, rearHit] = hQCar3.set_velocity_and_request_state(-2, 0, false, false, false, false, false);
135 pause(0.25);
136end
137
138% Change the color of ghost mode to red
139hQCar3.ghost_mode(true, [1, 0, 0]);
140pause(0.5);
141
142
143% Set the velocity to 0 and turn all lights off
144hQCar3.set_velocity_and_request_state(0, 0, false, false, false, false, false);
145
146% Set the location of the QCar and request the state of the car.
147% If x== True and frontHit==True then the front bumper hit the block correctly.
148[x, location, rotation, forward_vector, up_vector, frontHit, rearHit] = ...
149 hQCar3.set_transform_and_request_state([-16.1, 26.299, 0.005], [0, 0, pi-0.01], ...
150 true, false, false, false, false, false);
151pause(0.5);
152
153% Getting and saving the world transform of the QCar in Quanser Interactive Labs
154[x, loc, rot, scale] = hQCar3.get_world_transform();
155
156% Set the location of the QCar and request the state of the car.
157% If x== True and rearHit==True then the back bumper hit the block correctly.
158[x, location, rotation, forward_vector, up_vector, frontHit, rearHit] = ...
159 hQCar3.set_transform_and_request_state_degrees([-13.1, 26.299, 0.005], ...
160 [0, 0, 179], true, false, false, false, false, false);
161pause(0.5);
162
163% Turning off ghost mode for the QCar
164hQCar3.ghost_mode(false, [1, 0, 0]);
165
166
167% Possessing the overhead camera on the QCar
168hQCar2.possess(hQCar2.CAMERA_OVERHEAD);
169pause(0.5);
170
171% Possessing the trailing camera on the QCar
172hQCar2.possess(hQCar2.CAMERA_TRAILING);
173pause(0.5);
174
175% Possessing the front CSI camera on the QCar
176hQCar2.possess(hQCar2.CAMERA_CSI_FRONT);
177pause(0.5);
178
179% Possessing the right CSI camera on the QCar
180hQCar2.possess(hQCar2.CAMERA_CSI_RIGHT);
181pause(0.5);
182
183% Possessing the back CSI camera on the QCar
184hQCar2.possess(hQCar2.CAMERA_CSI_BACK);
185pause(0.5);
186
187% Possessing the left CSI camera on the QCar
188hQCar2.possess(hQCar2.CAMERA_CSI_LEFT);
189pause(0.5);
190
191% Possessing the front RealSense RGB camera on the QCar
192hQCar2.possess(hQCar2.CAMERA_RGB);
193pause(0.5);
194
195% Possessing the RealSense depth camera on the QCar
196
197hQCar2.possess(hQCar2.CAMERA_DEPTH);
198pause(0.5);
199
200
201% % Getting images from the different cameras
202% [x, camera_image] = hQCar2.get_image(hQCar2.CAMERA_CSI_FRONT);
203% [x, camera_image] = hQCar2.get_image(hQCar2.CAMERA_CSI_RIGHT);
204% [x, camera_image] = hQCar2.get_image(hQCar2.CAMERA_CSI_BACK);
205% [x, camera_image] = hQCar2.get_image(hQCar2.CAMERA_CSI_LEFT);
206% [x, camera_image] = hQCar2.get_image(hQCar2.CAMERA_RGB);
207% [x, camera_image] = hQCar2.get_image(hQCar2.CAMERA_DEPTH);
208
209
210disp('LIDAR')
211
212hQCar3.possess(hQCar3.CAMERA_OVERHEAD);
213
214hFigure = figure();
215
216disp('Reading from LIDAR... if QLabs crashes or output isn''t great, make sure FPS > 100')
217
218% Have the QCar drive forward to hit the front block to show the live lidar.
219% Speed can be changed by increasing or decreasing the value in the first
220% parameter "forward"
221hQCar3.set_velocity_and_request_state(1, 0, false, false, false, false, false);
222lidar_rate = 0.05;
223
224for count = 0:25
225
226 [success, angle, distance] = hQCar3.get_lidar(400);
227
228 x = sin(angle).*distance;
229 y = cos(angle).*distance;
230
231 plot(x,y, '.');
232 axis([-60 60 -60 60]);
233 drawnow;
234 pause(lidar_rate);
235
236end
237
238pause(5);
239
240% Closing qlabs
241qlabs.close();
242disp('Done!');
243close(hFigure);