You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/matlab.md
+25-28Lines changed: 25 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,10 +15,6 @@ Here is a simple MATLAB controller example:
15
15
```MATLAB
16
16
function simple_example
17
17
18
-
% uncomment the next two lines to use the MATLAB desktop
19
-
%desktop;
20
-
%keyboard;
21
-
22
18
TIME_STEP = 32;
23
19
24
20
my_led = wb_robot_get_device('my_led');
@@ -40,41 +36,42 @@ while wb_robot_step(TIME_STEP) ~= -1
40
36
end
41
37
```
42
38
43
-
### Using the MATLAB Desktop
39
+
### Debugging Using the MATLAB Desktop
44
40
45
-
In order to avoid cluttering the desktop with too many windows, Webots starts MATLAB with the *-nodesktop* option.
46
-
The *-nodesktop* option starts MATLAB without user interface and therefore it keeps the memory usage low which is useful in particular for multi-robot experiments.
47
-
If you would like to use the MATLAB desktop to interact with your controller you just need to add these two MATLAB commands somewhere at the beginning of your controller m-file:
41
+
For each controller written using MATLAB, Webots will start a new instance of MATLAB to act as an interpreter.
42
+
In order to avoid cluttering the desktop with too many windows, Webots starts each instance of MATLAB in non-interactive mode.
43
+
This means that MATLAB starts without the user interface which keeps the memory usage low; this is particularly useful in multi-robot experiments.
44
+
Any output to stdout (such as `disp` or `fprintf`) will also be redirected to the Webots console.
48
45
49
-
```MATLAB
50
-
desktop;
51
-
keyboard;
52
-
```
46
+
If you would like to use the MATLAB desktop to interact with your controller, you will need to run it in `<extern>` mode with the appropriate additional argument.
47
+
You can read more about that [here](running-extern-robot-controllers.md).
53
48
54
-
The `desktop` command brings up the MATLAB desktop.
55
-
The `keyboard` stops the execution of the controller and gives control to the keyboard (`K>>` prompt).
56
-
Then MATLAB opens your controller m-file in its editor and indicates that the execution is stopped at the `keyboard` command.
57
-
After that, the controller m-file can be debugged interactively, i.e., it is possible to continue the execution step-by-step, set break points, watch variable, etc.
58
-
While debugging, the current values of the controller variables are shown in the MATLAB workspace.
59
-
It is possible to *continue* the execution of the controller by typing `return` at the `K>>` prompt.
60
-
Finally the execution of the controller can be terminated with <kbd>ctrl</kbd>-<kbd>C</kbd> key combination.
49
+
**Note**: This is equivalent to inserting the command `keyboard` in your controller code, but this is strongly discouraged since it will cause an error during non-interactive execution of the code.
61
50
62
-
Once the controller is terminated, the connection with Webots remains active.
51
+
Running an external controller in interactive mode will automatically place a breakpoint at the first line of your controller.
52
+
Once MATLAB desktop has initialized, it will halt the execution of the controller and give control to the keyboard (`K>>` prompt).
53
+
MATLAB also opens your controller m-file in its editor and indicates that the execution is stopped at the breakpoint.
54
+
55
+
At this point, the controller m-file can be debugged interactively, i.e., it is possible to continue the execution step-by-step, set break points, watch variable, etc.
56
+
You can use the navigation buttons in the Editor Toolstrip such as Continue/Pause, Step, and Quit Debugging to control the execution.
57
+
While running, the controller will run normally until it terminates or reaches another breakpoint.
58
+
While paused, the current values of the controller variables are shown in the MATLAB workspace, and the Command Window becomes available.
59
+
You can read more about debugging MATLAB code on the [MathWorks homepage](https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html).
60
+
61
+
While paused (or after the controller has been terminated), the connection with Webots remains active.
63
62
Therefore it becomes possible to issue Webots commands directly on the MATLAB prompt, for example you can interactively issue commands to query the sensors, etc.:
64
63
65
64
```MATLAB
66
-
>> wb_robot_step(1000);
67
-
>> wb_gps_get_values(gps)
65
+
K>> wb_robot_step(1000);
66
+
K>> wb_gps_get_values(gps)
68
67
69
68
ans =
70
69
71
70
0.0001 0.0030 -0.6425
72
-
>> |
73
71
```
74
72
75
-
It is possible to use additional `keyboard` statements in various places in your ".m" controller.
76
-
So each time MATLAB will run into a `keyboard` statement, it will return control to the `K>>` prompt where you will be able to debug interactively.
77
-
78
-
At this point, it is also possible to restart the controller by calling its m-file from MATLAB prompt.
73
+
The execution of the controller can be terminated with <kbd>Ctrl</kbd>+<kbd>C</kbd> key combination, or by quitting the debugger.
74
+
However, since all controllers are functions, their Workspace (local variables) will be lost after termination.
75
+
It is possible to re-run the controller by calling `launcher` from MATLAB prompt.
79
76
Note that this will restart the controller only, not the whole simulation, so the current robot and motor positions will be preserved.
80
-
If you want to restart the whole simulation you need to use the `Reload` button as usual.
77
+
If you want to restart the whole simulation you need to use the `Reload` button in Webots as usual.
0 commit comments