[Q] Writing a HTML/Javascript to control the car from a browser
Opened this issue · 5 comments
Hi again,
Now that I have PI-RC working, is it possible to send UDP packets to the pi_pcm server from a web browser using Javascript? If so, what contents does the UDP packet contain exactly? Can you provide an example? Is it also possible for you to provide a simple Javascript that sends the packet?
I'm asking because I've written a web page for my current project which allows me to control a robot from my Nintendo 3DS and I think it would be awesome to create a page that works with Pi-RC too.
Thanks again!
That's a good idea, I like it! I'm pretty sure it's not possible to send UDP packets from JavaScript, but you should be able to send TCP packets using WebSockets. I have some time this weekend so I'll try to add TCP support to the server and add a sample page to the repository that you can use to send messages to the server to drive the var.
I have an example of what messages that you send should look like in the README. I think for your car, you'll want to do something similar to this (I've left out the web socket setup stuff though, I'll get a full example done this weekend. Also, I haven't tested any of this code.):
var forward = [
// Synchronization burst
{
"frequency": 40.0,
"dead_frequency": 26.995,
"burst_us": 1200,
"spacing_us": 400,
"repeats": 4
},
// Command burst
{
"frequency": 40.0,
"dead_frequency": 26.995,
"burst_us": 400,
"spacing_us": 400,
"repeats": 11
}
];
var message = JSON.stringify(forward);
socket.send(message);
Cool! Thanks!
Managed to create a PHP script that does it, fixed a issue with pi_pcm crashing after a few commands, too - it'd crash after receiving a specific .json command. Here's the script
Okay, I added WebSocket support to the server. If you start it with ./pi_pcm --tcp
, then you should be able to connect to the server by opening the control.html
page with a browser (I tested Firefox and Chrome), entering in the control values or loading a JSON control file, and connecting to the server. Then just use the arrow keys.
There is a bug where occasionally the decoding of the message from the browser will fail and the server will ignore the message. I have no idea what's going on with that, but I'll try to fix it later. I'll leave this issue open in the mean time.
Oh wow! Thanks! I'll surely have fun with this nifty piece of html.