Community discussions

MikroTik App
 
optio
Long time Member
Long time Member
Topic Author
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Visual Studio Code task for running ROS scripts remotely

Wed Oct 04, 2023 12:32 am

Hi all,

I'm using for my personal use Visual Studio Code IDE task for running ROS scripts remotely on device for some time. Now I polished it as far as ROS features allowed me and decided to share it:
tasks.zip
.

Prerequisites:
  • Device with ROS 7.x. - tested on ROS 7.11.2 but probably compatible with other 7.x versions
  • bash shell - in VSCode running on macOS or Linux should work as is (tested on macOS with bash from MacPorts), it could work on Windows if bash shell (git/msys bash, WSL...) is avaiable from path or set in task configuration options.shell.executable
  • command line OpenSSH client (ssh/scp)
  • configured non-interactive (PKI) SSH authentication to ROS device for user that will be used for running script

Usage:
  • edit that file and replace ROS_SSH_DST env. variable with SSH connection string to ROS device (user@host)
  • in VSCode open scripts root folder (File->Open Folder...) or Workspace and in Explorer panel select ROS script file
  • run task with name "Run ROS script" using Tasks: Run Task from Command Palette... or from GUI panel provided by some extension like Task Explorer

Example script run:
vscode_rsc_ok.png
Example script run with errors and displaying line error in editor:
vscode_rsc_err.png

Potential issues:
  • if other scripts are running at same time on same device, VSCode terminal output will show logs from other scripts - afaik there is no way to relate exact script with script log topic
  • false script error may occur for same reason as above, because script errors are also retrieved from log, proposed feature request how it will be possible to handle this
  • if SSH connection to ROS device takes longer, adjustment of LOG_SEEK_OFFSET env. variable in task configuration is needed to proper interval for better finding all script logs
  • stopping task in VSCode will not stop script job execution on ROS - if there are issues in script like infinite loops or long execution that needs to be terminated, script job needs to be removed from /system/script/job.

Share any suggestions for improvements and I will try to implement it when I find some time.
You do not have the required permissions to view the files attached to this post.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Visual Studio Code task for running ROS scripts remotely

Wed Oct 04, 2023 2:54 am

Nice work. I use VSCode. Parsing the script then checking log likely works, just not ideal...

Another approach, also not perfect, be to have the VSCode extension use a fetch() to REST API with /console/inspect request=highlight input=":pute \"it should be put\"".
USER=admin ROUTER=xxx; curl -l -u $USER -X POST https://$ROUTER/rest/console/inspect --json '{ "request":"highlight", "input":":pute \"it should be :put\"" }'
[{
"highlight":"dir,obj-inactive,obj-inactive,obj-inactive,obj-inactive,none,none,none,none,none,none,none,none,none,none,none,none,none,none,none,none,none,none,none,none",
"type":"highlight"
}] 
The "inactive-obj" which mean syntax error AFAIK. But this seem to be what the actual RouterOS uses to color the command line, but in theory you'd know where the errors were.

What be a lot of work, but nifty is a Language Server Protocol ("LSP") implementation for RouterOS: https://code.visualstudio.com/api/langu ... sion-guide & what's nice is there editor also use beyond VSCode (like vim), see: https://langserver.org
The LSP could get its data from the additional operations in RouterOS's /console/inspect like request=completion or request=syntax large map to the LSP's primitive like definition and completion. MS has an example here https://github.com/microsoft/vscode-ext ... lsp-sample, but still be a lot of work...
 
optio
Long time Member
Long time Member
Topic Author
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Re: Visual Studio Code task for running ROS scripts remotely

Wed Oct 04, 2023 8:11 pm

Yeah, parsing log just to detect if script is ened is far from ideal, but I did not find other way since you cannot find exact job in /system/script/job to watch for uploaded autorun script. If script job's property contains script file name, same as when running script from ROS, it would be possible to watch that instead parsing log, but unfortunately it's empty for uploaded autorun scripts - this can be feature request for improvement.

Regarding introspecting code with LSP is out of scope for task. :) It would be nice to have such ROS script extension for sure.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Visual Studio Code task for running ROS scripts remotely

Wed Oct 04, 2023 10:19 pm

I think your "trick" is best you can do.

Yeah that was my thought re LSP... While Mikhail Korzhov's extension does an excellent job at syntax, it not great at the valid options for a command. I had thought it be nice to see script errors, looked at what it would take to build an LSP while back – but similar conclusion too much work & I generally know the commands ;).

I use ":import x.rsc verbose=yes" from VSCode's terminal to "debug" a script — which get you errors by line — but the :import verbose=yes just outputs to the terminal...so not exactly very usable from JS to make into a VSCode "task".

Who is online

Users browsing this forum: Ami123waston and 7 guests