dpi74187 2013-11-13 14:58
浏览 23
已采纳

Javascript ajax等到文件被更改

I'm programming an website to control my Raspberry Pi robot. I'm driving two stepper motor using .py script I call it:

sudo ./GPS.py forward 100 30

Fist argument is way to go, second is how many steps to do, and the last is delay between steps.

The script open location.txt file (it looks like "100/50/18") and take coordinations x=100, y=50 and Alpha=18 degress. Then make a move, calculate new coordination and write it into this file.

Read part at the top of script:

fo = open("location.txt", "r")
data = fo.read()
fo.close()
coordinates= data.split("/")
temp1 = coordinates[0]
temp2 = coordinates[1]
temp3 = coordinates[2]
Alpha= float(temp3)
X = float(temp1)
Y = float(temp2)

Then it make all requested moves and calculations, and then at the end save new X,Y,Alpha back to file:

 fo =open("location.txt", "w")
 fo.write(str(X)+"/"+str(Y)+"/"+str(Alpha))
 fo.close

Allright, this works perfect in Putty, but now I wanted to drive my robot through website, so I've made website to control it.

But now I have a problem. Now I have site like this:

HTTP --> Javascript --> PHP --> .PY script to move robot.

This works, but I have no idea how refresh X,Y,Alpha coordinates from location.txt on my website. I have an idea:

Javascript run .PY and wait it finishes, then JS open .txt and get data and finally set new coordinates to my webpage. But I don't know how to do it. This waiting to .PY finishes is killing me.

Thanks for your help! Yacked2

PS.

I have apache installed on my Raspberry Pi, and I can donwload my .py script though webpage and I can open .txt file.

  • 写回答

1条回答 默认 最新

  • donglian3055 2013-11-13 15:23
    关注

    The classic web way of doing this would be to poll from the client until you are told of a change.

    E.g.

    • Tweak your file so that it contains a date+time updated.
    • Implement a PHP script to open the file and serve the contents as a JSON object (with the date updated, X, Y and Alpha as properties)
    • On load of the page, load the location and store all 4 components.
    • When you send a move instruction to the server, start to poll for a change - periodically reload the JSON object until you have one with a changed date updated. You can then stop polling.
    • This updated location should then be stored and used to update your page.
    • Set a maximum number of times to poll and abort with error if you reach the maximum.

    Let's say your main page contains

    <div id="locationInfo" />
    

    And you have implemented the PHP script getLocationInfo.php that returns a JSON object like this:

    { date_updated: "13-11-2013 15:45:98",
      x_position: 105,
      y_position: 120,
      alpha: 123 }
    

    In the main page you can have a script using jQuery that will (for example, something along the lines of - but more complex than)

    $.get( "getLocationInfo.php", function( data ) {
      var html = 'Location: ' + data.x_position + ', ' + data.y_position + ' @' + data.alpha
      $( "#locationInfo" ).html( html );
    });
    

    All that's really missing from the above is the bit that repeatedly polls and aborts when date_updated has changed.

    There is a simple example of polling described here by @johnny-craig: jQuery, simple polling example

    In those examples you just need an exit condition for once you have the data you need (recognised by a change in date_updated)

    It'll probably work, be pretty simple to implement, but suffers from the amount of duff requests being made from the web page. Though bear in mind the web has worked for a LONG time doing this kind of thing.

    Alternatively, you can get all HTML5 about it and read up on websockets. Using websockets you can instigate the update from the server side, rather than the client side. There's less polling required and the response time on the client should be better.

    Here's something that'll give you the basics:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况