View Full Version : Javascript Robots!
weaversam8
07-04-2015, 20:52
I develop full stack Node.JS professionally, and I've seen the power in Javascript. Would anyone be open to building a collaborative framework for the RoboRIO to code in Javascript?
Seems rather promising to me...
g_sawchuk
07-04-2015, 20:57
And I thought this was going to be #teamjavascript
Oh thank you Kamen.
nickbrickmaster
07-04-2015, 21:25
It wouldn't be that hard if you built it on top of Java's Nashorn interpreter. Summer project?
virtuald
08-04-2015, 00:15
I develop full stack Node.JS professionally, and I've seen the power in Javascript. Would anyone be open to building a collaborative framework for the RoboRIO to code in Javascript?
Seems rather promising to me...
I've definitely considered doing it, and would love to see someone do it.
RobotPy (the python port) is pure python this year, only interfacing to the robot at the HAL layer. If you're going to do this, I would highly recommend going that route instead of trying to write bindings for WPILib. Expect to spend 2 months of effort translating WPILib into Javascript if you want to do a full port. We did C++-based bindings for a number of years, but having everything be pure python (or js) means you can run the actual robot code on your laptop, which is great for testing + simulation.
There's a lot you can learn by looking at how RobotPy is implemented -- and if you implement HAL the same way we did, then you can use our HTML/JS simulator interface (not quite ready, but almost there)!
If you want to write a robot dashboard in HTML/JS, we've got that covered (https://github.com/robotpy/pynetworktables2js).
Jacob4564
08-04-2015, 07:37
It's not entirely implausible, we built our own version of java script to use in autonomous coding.
weaversam8
08-04-2015, 07:53
Interesting. Ill definitely look into the HAL interfacing, since Node is extremely capable.
connor.worley
08-04-2015, 13:54
By all means, go for it, but I wouldn't use it.
From: http://nodeguide.com/convincing_the_boss.html
But please be careful here, since JavaScript is a dynamic / garbage collected language, your response times may sometimes vary depending on how often and long the garbage collection kicks in (at which point your program is stopped). So don't try to build hard realtime systems in node, that require consistent response times. Erlang is probably a better choice for these kinds of applications.
virtuald
08-04-2015, 14:23
By all means, go for it, but I wouldn't use it.
From: http://nodeguide.com/convincing_the_boss.html
Python has similar issues, and it works just fine for what most FRC teams create. Java has potentially similar GC issues.
AlexanderTheOK
08-04-2015, 14:57
Python has similar issues, and it works just fine for what most FRC teams create. Java has potentially similar GC issues.
That explains the 250ms lag spikes I've been seeing every once in a while. Any standard way to mitigate this?
Jonathan Norris
08-04-2015, 15:38
By all means, go for it, but I wouldn't use it.
From: http://nodeguide.com/convincing_the_boss.html
Java has the same garbage collection issues, and it seems to me that most teams use it... If that's a concern teams should be using C++.
I'd be willing to help out, I write Node/JS every day (in addition to Java/Objective-C), the event'ed/streams Javascript model could be really useful for robot programing.
weaversam8
08-04-2015, 16:19
Java has the same garbage collection issues, and it seems to me that most teams use it... If that's a concern teams should be using C++.
I'd be willing to help out, I write Node/JS every day (in addition to Java/Objective-C), the event'ed/streams Javascript model could be really useful for robot programing.
Yeah, I'm not concerned with Garbage Collection. It actually really helped with Recycle Rush this year (sorry :) .)
Excellent John, I'd probably set this up as a collaboration-able repo, so I'll definitely contact you if this gets started.
EDIT: Nobody caught the pun? :D
virtuald
08-04-2015, 16:39
Yeah, I'm not concerned with Garbage Collection. It actually really helped with Recycle Rush this year (sorry :) .)
Excellent John, I'd probably set this up as a collaboration-able repo, so I'll definitely contact you if this gets started.
Setup a github organization. Then you can expand to lots of little projects, instead of just a single repo. :)
Bryan Herbst
08-04-2015, 17:04
That explains the 250ms lag spikes I've been seeing every once in a while. Any standard way to mitigate this?
Memory management is complicated, so there isn't really a silver bullet you can apply to make it better. To minimize garbage collection, you need to understand why things get garbage collected and why things don't get garbage collected.
Garbage collection generally occurs when Java decides it needs more memory for new allocations. Objects that are no longer referenced are eligible to be collected.
If you feel as though the garbage collector is running too often, it is likely because you are allocating too many objects too frequently. For example, if you have a loop that creates a new instance of an object in every iteration, those objects can pile up quickly.
SoftwareBug2.0
09-04-2015, 02:12
That explains the 250ms lag spikes I've been seeing every once in a while. Any standard way to mitigate this?
1) Use a language implementation whose runtime doesn't do garbage collection (this may or may not exist for your language of choice)
2) Use a specially-tuned "low latency" garbage collector implementation (you probably need to test that it actually works properly for your application's usage pattern though)
3) Avoid heap allocation (to the extent that you can - just running the required libraries may produce a considerable amount of garbage, you'd need to check this)
You might also consider manually triggering a collection frequently so that less work is done each time it runs. For example, you could have it so that every time the robot got a packet from the driver station it would run an iteration of the control code and then do a GC. You would have to see how long the pause is to see if that would be acceptable.
I AM GAME! I would love to run a Node.js application on the roboRIO and use our DriverStation.js (https://github.com/gluxon/DriverStation.js) (Node-Webkit) DS with it. (once we get the 2015 protocol unearthed...).
I'm down as well (I was actually planning to do JavaScript this season but the teams member count was really low so I had no time).
Stuff that would need to be done
1) WPILib for JavaScript
2) JavaScript interpreter for the roborio (Java has on built in, there are some good cpp ones available)
3) sample projects
4) code development tool, preferably cross platform (recommended javafx or Qt)
5) rio tools (flasher If necessary, code dashboard, JavaScript rio interface)
6) code deployment tool
7) debugging tools
8) (optional) Js native interface tools (for running hard vision processing loops, sensor feedback loops, etc)
I'd be interested in learning Qt if there were enough other people to support the rest of the project (else I'd do something simple like a ftp server attached to sublime)
michaelwm
15-04-2015, 10:00
This was a small side project for me this year, and i'm finishing it during this offseason. Some useful things we learned that anyone interested in using javascript for their robot:
Don't use Java 8 / Nashorn (their built in JS interpreter):
The initial build of Java 8 installed on the roboRIO does not include Nashorn (or any JS interpreter for that matter). See here. (http://www.chiefdelphi.com/forums/showthread.php?t=133219)
It grows exponentially slow. Just printing "hello world!" executes fine. The more code we added, the slower it's execution time got. Any function (releasing a cylinder, driving a motor) took upwards of 5 seconds to execute.
Have an intuitive software design
We based our JS design off of the DOM webdesign convention. Driving a motor in JS is as simple as robot.talon.port3.set(1); (Subject to change as we refine our programming model)
Aside from being interpreted in real time, JS is the most common language taught in high school programming courses. If you ever want new programmers, an intuitive design based on what students already know (DOM) will attract new programmers like crazy.
Our team is using the Google v8 JS engine (the same engine powering node), and we have had major success. We will be releasing the source soon, but I personally would love some competition!
Any function (releasing a cylinder, driving a motor) took upwards of 5 seconds to execute
O.O
How exactly are you doing this...? Assuming you're just throwing the talons inside of a SimpleBinding, 5 seconds is absolutely monstrous. Are you doing something wierd like interfacing with the HAL layer directly from JavaScript?
michaelwm
15-04-2015, 19:31
O.O
How exactly are you doing this...? Assuming you're just throwing the talons inside of a SimpleBinding, 5 seconds is absolutely monstrous. Are you doing something wierd like interfacing with the HAL layer directly from JavaScript?
Not at all actually, we were passing the Talon, Solenoid, and other classes directly to the scripts as variables, and the scripts simply called their methods (i.e. Talon.set(n);). The problem is with the amount of scripts / the length of scripts. If your program had just a single script that said talon.set(1), it would execute fine (but is entirely useless). The more scripts and the longer the scripts are, the longer it took for the interpreter to parse them, and call their methods, thus the 5 second execution time.
We're switching to v8 because it utilizes JIT compilation. We are using the roboRIO's file system to store scripts, and the program compiles them at startup (during queue, for example), and never touches them again, unless our development mode is active. In this case, an external file watcher on the development machine watches for changes to javascript files, and when it detects one, sends the changes to the roboRIO, which stores the new file on the File System, and recompiles it. This minimizes execution time to instantaneous during competition (as no compilation is done during a match because no development is active), and much shorter times than the current java/c++ compilation times for WPI/eclipse.
Have an intuitive software design
We based our JS design off of the DOM webdesign convention. Driving a motor in JS is as simple as robot.talon.port3.set(1); (Subject to change as we refine our programming model)
Aside from being interpreted in real time, JS is the most common language taught in high school programming courses. If you ever want new programmers, an intuitive design based on what students already know (DOM) will attract new programmers like crazy.
If you're talking about intuitive design, Talon's don't have 3 ports - PWMs do. It'd get particularly sticky if you had to do Talon SR vs SRX vs SP. I'd recommend doing robot.pwm.port3.set(1), then at instantiation determine what port 3 actually is and make sure that object has a set() method.
If you're talking about intuitive design, Talon's don't have 3 ports - PWMs do. It'd get particularly sticky if you had to do Talon SR vs SRX vs SP. I'd recommend doing robot.pwm.port3.set(1), then at instantiation determine what port 3 actually is and make sure that object has a set() method.
I don't know if the roboRIO does this but I know you couldn't with the sidecar because jaguars and talons/victors took different PWM wavelengths. The most intuitive would be something like this
robot.elevatorMotor = robot.pwm.port3
or, if you have to
robot.elevatorMotor = HAL.getMotor(3)
virtuald
16-04-2015, 12:00
I don't know if the roboRIO does this but I know you couldn't with the sidecar because jaguars and talons/victors took different PWM wavelengths. The most intuitive would be something like this
robot.elevatorMotor = robot.pwm.port3
or, if you have to
robot.elevatorMotor = HAL.getMotor(3)
Correct, there isn't a way for the program to determine which type of PWM device is attached to it, so if you were to use this notation then the user would need to provide some declarative configuration that indicates what device is at which port.
The robot.pwm.* or robot.port* notation is interesting, but it's probably mostly useful for retrieving objects, instead of actively using them that way. Consider 'robot.arm_motor.set()' vs 'robot.pwm.port2.set()' -- if you ever change what port your arm motor is attached to, then you'll have to change it everywhere in code if you use the second notation everywhere.
However, it could be a more useful way to create these objects. Maybe something like robot.pwm.port2.createTalon('arm_motor') automatically creates a robot.arm_motor attribute.
Hi all, I just saw this thread and got super excited.
I've been wanting a JS alternative since I started doing FRC 3 years ago.
I was thinking it'd be fairly straightforward to implement RobotJS as a node add-on, but going with the v8 engine directly may be better. I was thinking it would be nice to use the built nide modules like fs if necessary so that was my attraction to the add-on route.
As far as syntax, couldn't you do something like:
var wpilib = require('wpilib');
var robot = new wpilib.IterativeRobot();
...
robot.liftMotor=new wpilib.Talon(1);
which is a very nodejs idiom.
Thoughts?
michaelwm
17-04-2015, 20:24
Hi all, I just saw this thread and got super excited.
I've been wanting a JS alternative since I started doing FRC 3 years ago.
I was thinking it'd be fairly straightforward to implement RobotJS as a node add-on, but going with the v8 engine directly may be better. I was thinking it would be nice to use the built nide modules like fs if necessary so that was my attraction to the add-on route.
As far as syntax, couldn't you do something like:
var wpilib = require('wpilib');
var robot = new wpilib.IterativeRobot();
...
robot.liftMotor=new wpilib.Talon(1);
which is a very nodejs idiom.
Thoughts?
There are 2 way's to go about this problem, a solution similar to Node, and a solution similar to DOM. Whichever way you choose, it shouldn't matter at all performance wise. We chose DOM, because the majority of new programmers are very familiar with DOM. If your programming team is more familiar with Node / classic programming markup, then your method is definitely better.
The great thing about using v8 directly instead of Node is that you get to choose your syntax and the way your programming team scripts it's robot. Go nuts!
Resurrecting an old thread.
Did anything come of this project? I'd be interested in helping or taking a look at how it turned out.
Toast has support for the Oracle Nashorn JS engine, which integrates directly with the JVM. https://github.com/Open-RIO/ToastAPI
You can write your robot program in .js files and we provide some common hooks to things like motors, logging and other stuff
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.