DCNick3 / trik-typescript-template

Template for creating typescript ES6+ project for TRIK platform using npm.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Motivation

This project was written so that TRIK programming could be less of a pain. It does so by providing JS api's and syntactic features that are missing in QtScript using core-js and babel along with static typing using TypeScript. This allows to use not-so-ugly classes, iterators, generators and so on.

Also this project can be used for programming of TRIKs without trik-studio. This is achieved by using remote-run.py script that allows to upload, run and get output of your program. But no simulator integration is implemented yet.

Description of build system

In order to accomplish this goal, a compilation process must be organized. rollup is used for this. Simply speaking, it takes a bunch of .ts files and generates one .js bundle that has all the functionality combined.

This setup is more-or-less generic rollup-typescript-babel setup (like this one), with some notable additions:

TRIK modules and typedefs

Conventional TRIK scripting uses various global variables to interact with APIs. To support type definitions they are provided as modules instead. brick corresponds to trik_brick, script corresponds to trik_script, mailbox corresponds to trik_mailbox and Threading corresponds to trik_threading.

All of those modules are have (partial) type definitions, located in src/lib_defs/trik.d.ts. They are based on trikRuntime source code (brickInterface.h, ScriptExecutionControl.h, mailbox.h and threading.h)

Entry points

Entry point should be located in src/entry_points or any subdirectory. Each entry point will result in generation of separate, fully-independent bundle that can be executed on TRIK.

Entry points are divided into two types: those that execute on real robot and those that are designed for simulator. This is shown by putting either real or sim to the file name.

Resulting bundles will be placed into bin/real and bin/sim.

Polyfills

Polyfills used on TRIK are located in src/polyfills directory. index.js imports all other files (it is implicitly added as an import to every entry point compiled). global.js defines variable global (QtScript does not have it). unsystem.js removes unneccessary variables added by TRIK's system.js from global namespace. trik_shim.js imports various core-js modules that provide runtime support for missing APIs. Some of the APIs are not imported, but implemented there using TRIK's non-standard APIs.

remote-run.py and End-Of-Script marker

remote-run.py implements the protocol used by trik studio for uploading and running of programs. But the problem is that it has no means of knowing when your script is done executing (without errors). This is achieved by adding end-of-script markers to each real robot entry point. It's a piece of code that prints a pre-defined value (__eb3caide9Oojaiku__stop_marker__) to the console. When remote-run.py encounters it, it assumes that script is done executing and exits.

Placement of such a marker is achieved by a (small) rollup plugin: rollup-plugin-real-transform.js.

Also remote-run.py has ability to work with several robots. This is done by introducing a ADDRESSES constant, defining all the addresses that are to be used. An index in that array could later be provided to the script as a second (optional) parameter.

Usage

Quick start guide would be:

  1. Install npm
  2. Clone the repo (git clone https://github.com/DCNick3/trik-typescript-template.git)
  3. Install required npm packages (npm install, executed in the repo directory)
  4. Build the code (npm run build)

To upload some code to the robot run npm run run

Target file and robot index can be changed in package.json

Code examples

This repo contains sample code that demonstrates some of the features of the build system. See src/implementation/index.ts for actual code and src/entry_points for entry points.

About

Template for creating typescript ES6+ project for TRIK platform using npm.


Languages

Language:JavaScript 51.2%Language:Python 28.7%Language:TypeScript 20.1%