1+ classdef autodrive_rzr < handle
2+ % AutoDRIVE RZR API
3+ % Attributes and methods to store and parse RZR data and commands
4+
5+ properties
6+ % RZR data
7+ id ;
8+ collision_count ;
9+ throttle ;
10+ steering ;
11+ brake ;
12+ handbrake ;
13+ encoder_ticks ;
14+ encoder_angles ;
15+ position ;
16+ orientation_quaternion ;
17+ orientation_euler_angles ;
18+ angular_velocity ;
19+ linear_acceleration ;
20+ % lidar_pointcloud;
21+ left_camera_image ;
22+ right_camera_image ;
23+ % RZR commands
24+ cosim_mode ;
25+ posX_command ;
26+ posY_command ;
27+ posZ_command ;
28+ rotX_command ;
29+ rotY_command ;
30+ rotZ_command ;
31+ rotW_command ;
32+ throttle_command ;
33+ steering_command ;
34+ brake_command ;
35+ handbrake_command ;
36+ headlights_command ;
37+ % Environment commands
38+ env = autodrive_environment;
39+ end
40+
41+ methods
42+ function parse_data(obj ,data ,leftcamera_ax ,rightcamera_ax ,verbose )
43+ % Collision count
44+ obj.collision_count = uint16(str2double(data .V1Collisions ));
45+ % Actuator feedbacks
46+ obj.throttle = str2double(data .V1Throttle );
47+ obj.steering = str2double(data .V1Steering );
48+ obj.brake = str2double(data .V1Brake );
49+ obj.handbrake = str2double(data .V1Handbrake );
50+ % Wheel encoders
51+ obj.encoder_ticks = cell2mat(textscan(data .V1EncoderTicks ,' %d ' ));
52+ obj.encoder_angles = cell2mat(textscan(data .V1EncoderAngles ,' %f ' ));
53+ % IPS
54+ obj.position = cell2mat(textscan(data .V1Position ,' %f ' ));
55+ % IMU
56+ obj.orientation_quaternion = cell2mat(textscan(data .V1OrientationQuaternion ,' %f ' ));
57+ obj.orientation_euler_angles = cell2mat(textscan(data .V1OrientationEulerAngles ,' %f ' ));
58+ obj.angular_velocity = cell2mat(textscan(data .V1AngularVelocity ,' %f ' ));
59+ obj.linear_acceleration = cell2mat(textscan(data .V1LinearAcceleration ,' %f ' ));
60+ % LIDAR
61+ % obj.lidar_pointcloud = matlab.net.base64decode(data.V1LIDARPointcloud);
62+ % Cameras
63+ obj.left_camera_image = matlab .net .base64decode(data .V1LeftCameraImage );
64+ obj.left_camera_image = javax .imageio .ImageIO .read(java .io .ByteArrayInputStream(obj .left_camera_image ));
65+ obj.left_camera_image = reshape(typecast(obj .left_camera_image .getData .getDataStorage , ' uint8' ), [3 ,1280 ,720 ]);
66+ obj.left_camera_image = cat(3 ,transpose(reshape(obj .left_camera_image(3 ,: ,: ), [1280 ,720 ])), ...
67+ transpose(reshape(obj .left_camera_image(2 ,: ,: ), [1280 ,720 ])), ...
68+ transpose(reshape(obj .left_camera_image(1 ,: ,: ), [1280 ,720 ])));
69+ obj.right_camera_image = matlab .net .base64decode(data .V1RightCameraImage );
70+ obj.right_camera_image = javax .imageio .ImageIO .read(java .io .ByteArrayInputStream(obj .right_camera_image ));
71+ obj.right_camera_image = reshape(typecast(obj .right_camera_image .getData .getDataStorage , ' uint8' ), [3 ,1280 ,720 ]);
72+ obj.right_camera_image = cat(3 ,transpose(reshape(obj .right_camera_image(3 ,: ,: ), [1280 ,720 ])), ...
73+ transpose(reshape(obj .right_camera_image(2 ,: ,: ), [1280 ,720 ])), ...
74+ transpose(reshape(obj .right_camera_image(1 ,: ,: ), [1280 ,720 ])));
75+ if verbose
76+ fprintf(' \n --------------------------------\n ' )
77+ fprintf(' Receive Data from RZR:\n ' )
78+ fprintf(' --------------------------------\n\n ' )
79+ % Monitor RZR data
80+ fprintf(' Collisions: %f\n ' ,obj .collision_count )
81+ fprintf(' Throttle: %f\n ' ,obj .throttle )
82+ fprintf(' Steering: %f\n ' ,obj .steering )
83+ fprintf(' Brake: %f\n ' ,obj .brake )
84+ fprintf(' Handbrake: %f\n ' ,obj .handbrake )
85+ fprintf(' Encoder Ticks: [%d %d ]\n ' ,obj .encoder_ticks(1 ),obj .encoder_ticks(2 ))
86+ fprintf(' Encoder Angles: [%d %d ]\n ' ,obj .encoder_angles(1 ),obj .encoder_angles(2 ))
87+ fprintf(' Position: [%f %f %f ]\n ' ,obj .position(1 ),obj .position(2 ),obj .position(3 ))
88+ fprintf(' Orientation [Quaternion]: [%f %f %f %f ]\n ' ,obj .orientation_quaternion(1 ),obj .orientation_quaternion(2 ),obj .orientation_quaternion(3 ),obj .orientation_quaternion(4 ))
89+ fprintf(' Orientation [Euler Angles]: [%f %f %f ]\n ' ,obj .orientation_euler_angles(1 ),obj .orientation_euler_angles(2 ),obj .orientation_euler_angles(3 ))
90+ fprintf(' Angular Velocity: [%f %f %f ]\n ' ,obj .angular_velocity(1 ),obj .angular_velocity(2 ),obj .angular_velocity(3 ))
91+ fprintf(' Linear Acceleration: [%f %f %f ]\n ' ,obj .linear_acceleration(1 ),obj .linear_acceleration(2 ),obj .linear_acceleration(3 ))
92+ % Visualize camera frames
93+ % Left camera
94+ imshow(imresize(obj .left_camera_image , 0.5 ),' Parent' ,leftcamera_ax );
95+ drawnow ;
96+ % Right camera
97+ imshow(imresize(obj .right_camera_image , 0.5 ),' Parent' ,rightcamera_ax );
98+ drawnow ;
99+ % Visualize LIDAR pointcloud
100+ % % Convert the byte array to numeric values
101+ % numericValues = typecast(uint8(obj.lidar_pointcloud), 'single');
102+ % % Reshape the numeric values into a 3-column matrix (assuming each point has x, y, and z coordinates)
103+ % numPoints = numel(numericValues) / 3;
104+ % pointCloud = reshape(numericValues, 3, numPoints)';
105+ % % Extract x, y, and z coordinates
106+ % x = pointCloud(:, 1);
107+ % y = pointCloud(:, 2);
108+ % z = pointCloud(:, 3);
109+ % % Plot pointcloud
110+ % colormap jet
111+ % scatter3(x, y, z, 1, z, 'filled','Parent',pointcloud_ax);
112+ % xlabel('X');
113+ % ylabel('Y');
114+ % zlabel('Z');
115+ % title('LIDAR Pointcloud');
116+ % colorbar;
117+ % axis equal;
118+ % drawnow;
119+ end
120+ end
121+
122+ function data = generate_commands(obj ,verbose )
123+ if verbose
124+ fprintf(' \n --------------------------------\n ' )
125+ fprintf(' Transmit Data to RZR:\n ' )
126+ fprintf(' --------------------------------\n\n ' )
127+ % Monitor RZR control commands
128+ if obj .cosim_mode == 0
129+ cosim_mode_str = ' False' ;
130+ else
131+ cosim_mode_str = ' True' ;
132+ end
133+ fprintf(' Co-Simulation Mode: %s\n ' ,cosim_mode_str )
134+ fprintf(' Position Command: [%d %d %d ]\n ' ,obj .posX_command , obj .posY_command , obj .posZ_command )
135+ fprintf(' Rotation Command: [%d %d %d %d ]\n ' ,obj .rotX_command , obj .rotY_command , obj .rotZ_command , obj .rotW_command )
136+ fprintf(' Throttle Command: %d\n ' ,obj .throttle_command )
137+ fprintf(' Steering Command: %d\n ' ,obj .steering_command )
138+ fprintf(' Brake Command: %d\n ' ,obj .brake_command )
139+ fprintf(' Handbrake Command: %d\n ' ,obj .handbrake_command )
140+ fprintf(' Headlights Command: %d\n ' ,obj .headlights_command )
141+ end
142+ data = obj .env .generate_commands(true );
143+ data = strcat(' 42["Bridge",{"V1 CoSim":"' ,num2str(obj .cosim_mode ), ...
144+ ' ","V1 PosX":"' ,num2str(obj .posX_command ), ...
145+ ' ","V1 PosY":"' ,num2str(obj .posY_command ), ...
146+ ' ","V1 PosZ":"' ,num2str(obj .posZ_command ), ...
147+ ' ","V1 RotX":"' ,num2str(obj .rotX_command ), ...
148+ ' ","V1 RotY":"' ,num2str(obj .rotY_command ), ...
149+ ' ","V1 RotZ":"' ,num2str(obj .rotZ_command ), ...
150+ ' ","V1 RotW":"' ,num2str(obj .rotW_command ), ...
151+ ' ","V1 Throttle":"' ,num2str(obj .throttle_command ), ...
152+ ' ","V1 Steering":"' ,num2str(obj .steering_command ), ...
153+ ' ","V1 Brake":"' ,num2str(obj .brake_command ), ...
154+ ' ","V1 Handbrake":"' ,num2str(obj .handbrake_command ), ...
155+ ' ","V1 Headlights":"' ,num2str(obj .headlights_command ), ...
156+ data ,' "}]' );
157+ end
158+ end
159+ end
0 commit comments