-
Notifications
You must be signed in to change notification settings - Fork 36
/
lidar.m
37 lines (31 loc) · 1.25 KB
/
lidar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function lidar(fileName, sensorType, varName)
%% Check if input is valid
if ~isstring(fileName) && ~ischar(fileName)
error("File name must a string or char array\n")
end
if ~strcmp(sensorType, "lidar")
error("Wrong sensor selected\n")
end
if ~isvarname(varName)
error("Invalid variable name\n")
end
%% Generate the python file
file = fopen(strcat(fileName, '.py'), 'w');
% Automatically genrates a python file containing the sensor call back
% bindings
fprintf(file, 'import numpy as np\n');
fprintf(file, '\n');
fprintf(file, 'def bindSensor(sensor):\n');
fprintf(file, ' sensor.listen(lambda _image: do_something(_image))\n');
fprintf(file, '\n');
fprintf(file, 'def do_something(_image):\n');
fprintf(file, ' global %s\n', varName);
fprintf(file, ' data = np.frombuffer(_image.raw_data, dtype="float32")\n');
fprintf(file, '\n');
fprintf(file, ' # Pair up in [x,y,z] format\n');
fprintf(file, ' data = np.reshape(data, (-1,4))\n');
fprintf(file, '\n');
fprintf(file, ' # Convert the data into MATLAB cast compatible type\n');
fprintf(file, ' %s = np.ascontiguousarray(data)\n', varName);
fclose(file);
end