Javascript和Python - 最好的沟通方法

I am trying to make a html/javascript controlled raspberry pi robot.

So far, I have installed a LAMP server and I am hosting a local webpage with buttons which trigger events and sends commands to my robot via AJAX. AJAX calls a php script which calls a python script (no CGI) to control the robot.

This solution is currently working for me and I can control my raspberry pi hardware from html and simple send data back and forth.

Now what I want to do is, on my web page, have an on/off button which initializes and shuts down the robot, as well as having other events (currently measuring tilt angles from an accelerometer in an android device) to control the speed of the robot. This is where I am getting confused by the procedure of how things will work.

When I make an AJAX call to start the robot, that ajax call does not close until the python script ends (robot shuts off). So I have a python script running (start.py) which contains my robot object where the methods for changing the speed are stored. If I have seperate events in JS to change the speed (sends a call to changespeed.py), how can I change the properties of an object which is stored in a different python script (start.py) that is already running (See figure below)?

Possible solutions I have thought of thus far:

1) store the object somewhere in memory. Perhaps as a python object, or store its properties in a SQL database. This way, changespeed.py (or Javascript directly) can change the speed properties in that memory, and start.py can continuously read them.

2) Rather than having the new speed values sent to python through AJAX, let python try to retrieve the values from javascript variables. in other words, instead of javascript giving data to python, have python take the data from javascript (scraping?).

3) abandon my current method of having javascript and python communicate through AJAX and php, and set up a websocket to exchange data. I dont know how this works quite yet but it may be the solution I need.

4) I also have not done any programming with interrupts and I dont know much about it. Is it possible to have my main script running and when the speed needs to change, I can send an interrupt at any moment?

I am sorry if my question is a little vague or wordy. I have a lot going on in my head right now. Let me know if it is unclear. Thank you.

TLDR: what is the best way to have javascript execute python scripts and then feed data to that script asynchronously

Map of operations

dongnao2048
dongnao2048 我目前正在研究使用websockets。根据我的研究,这似乎是我尝试做的最好的方法。我成功地创建了一个从python到JS的websocket,但是Apache2正在阻碍它。我目前正在调查是否应该使用第三方软件使apache2兼容,使用nginx,或使用带有socketio的烧瓶框架
接近 3 年之前 回复
douren9077
douren9077 “我怎么能改变存储在已经运行的不同python脚本(start.py)中的对象的属性”。在我的脑海中,也许内存中的程序可能有一个侦听套接字连接的线程。您的ajax请求调用Web服务器上的服务器端脚本,该脚本与正在运行的进程建立套接字连接并向其发送数据。正在运行的进程接受连接并对数据执行操作。不知道你是如何用Python做的,但这是一种理论方法。
接近 3 年之前 回复
douji7399
douji7399 我知道的最好方法是让JS做任何需要做的事情,然后使用django或类似的运行将POST重新发送回服务器。
接近 3 年之前 回复

3个回答



您可以使用cgi - 通用网关接口,通过使用cgi-cgi.FieldStorage(),您可以在python中获取数据。 它支持巨大的数据传输和fast.it支持所有浏览器</ p>
</ div>

展开原文

原文

You can use cgi — Common Gateway Interface and By the use cgi-cgi.FieldStorage() you can able to get data in python .it support Huge data transmission and fast.it spport all the browser



听起来你正在寻找一个内存数据存储,例如 Redis 。 使用redis,你可以在内存中创建一个变量,可以通过任何支持Redis的编程语言访问(几乎所有这些都是)。</ p>

Redis也有Pub / Sub功能 允许您在将数据发布到特定通道时执行特定逻辑。 例如,如果您的javascript程序在redis中发生了变化,那么您的python程序可以订阅该特定变量,并且当javascript程序更改该变量的值时,知道执行它自己的逻辑。 有关pub / sub的更多信息,请查看此页面 </ p>
</ div>

展开原文

原文

It sounds to me that you are looking for an in-memory data store such as Redis. With redis you can create a variable in memory which can be accessed by any programming language that has support for Redis (pretty much all of them do).

Redis also has Pub/Sub functionality which allows you to execute specific logic when data is published to a specific channel. For example, if your javascript program changes something in redis, your python program could be subscribed to that specific variable, and know to execute it's own logic when the javascript program changes the value of that variable. For more information on pub/sub check out this page

doudu6100
doudu6100 谢谢。 这听起来像我在想MySQL数据库。 我会在第二天左右看一下,我会告诉你它是怎么回事。
接近 3 年之前 回复



我已经使用Flask-socketIO在我的网页和我的机器人之间成功设置了一个websocket。 我现在能够使用Android手机中的加速度计控制我的机器人,但机器人的反应太慢了。 有时机器人需要2秒才能响应来自手机的命令。 我相信问题的一部分是我的机器人通过家里的wifi扩展器连接到我的WLAN。 因此,我不是将数据通过wifi扩展器,路由器,通过扩展器,最后传递给机器人,而是计划使用我的第二个wifi天线作为接入点,并直接连接到机器人。</ p>

最终我计划进入Android应用程序开发(我想通过USB将我的第二部智能手机连接到机器人并可以访问加速度计,磁力计,GPS和相机)。 一旦我这样做,我将考虑通过蓝牙而不是websocket连接我的手机和机器人。</ p>

我仍然不知道这是否是最好的方法,但这是我目前的解决方案。 </ p>
</ div>

展开原文

原文

I have successfully set up a websocket between my webpage and my robot using Flask-socketIO. I am now able to control my robot using the accelerometer in my android phone, but the response from the robot is way too slow. sometimes it takes up to 2 seconds for the robot to respond to a command from my phone. I believe part of the issue is that my robot is connected to my WLAN through a wifi extender in my house. So rather than passing data through the wifi extender, to the router, back through the extender, and finally to the robot, I am planning on using my 2nd wifi antenna as an access point, and connect to the robot directly.

Eventually I plan on getting into android app development (I want to connect my 2nd smartphone to the robot via USB and have access to the accelerometer, magnetometer, GPS, and camera). Once I do, I will look into connecting my phone and robot via bluetooth rather than websocket.

I still dont know if this is the best method, but this is my solution at the moment.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐