私は数年間 Web アプリケーション開発者として働いており、現在は Python とロボティクスを扱っています。
JavaScript websocket コマンドに基づいて実行するように Python Tornado をセットアップしました。これは素晴らしいです、モーターを動かし、LEDを点灯させます。問題ない。
やりたいことが2つ。
1) LED を点滅させる
2) 超音波距離センサーを使用して、FORWARD アクションを停止します IF range < X
私はそれ自体で両方を行う方法を知っています。
しかし、私が私のpythonを持っている方法は次のとおりです
WS.py
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import time
# My Over python module
import tank
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'New connection was opened'
self.write_message("Welcome to my websocket!")
tank.init()
def on_message(self, message):
print 'Incoming message:', message
tank.run(message)
self.write_message("You said: " + message)
def on_close(self):
tank.end()
print 'Connection was closed...'
def check_origin(self, origin):
return True
application = tornado.web.Application([
(r'/ws', WSHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
TANK.py import RPi.GPIO as gpio import time
def init():
#Setting up all my pins, Not going to add all
def moveForward():
#open GPIO pins to move motors forward
def stop():
# close all GPIO pins to stop motors
def run(action):
# the method called by the IOLoop
if action == 'go':
moveForward()
elif action == 'stop':
stop()
else:
print "Oops, i cocked up"
注: tank.py は要約であり、実際のコードではありません。
私のJSはマウスダウンで動作し、私のpython WSをgo、マウスアップ、コールストップで呼び出します
私が言ったように、WORKS FINE
しかし、moveForward() メソッドに while ループを追加して範囲を計算し、近い場合は停止すると、WS は結び付き、STOP をリッスンしません。
同様に、LED をオン、スリープ、オフ、スリープにするメソッドを実行すると、WS はコマンドをリッスンできなくなります。