add motor control

master
Zen 5 years ago
parent 5d2c5d1740
commit 754a14085a
  1. 21
      README.md
  2. 55
      server.py
  3. 8
      www/css/main.css
  4. 90
      www/index.html

@ -108,7 +108,26 @@ sudo nano /etc/default/motion
``` ```
N. Пишем на пыхе 6. Разбираемся с Питоном.
```bash
sudo apt install python3-pip
pip3 install twisted autobahn pyserial
```
```bash
git clone https://gitlab.com/zenls/rpi_remote_cam
sudo cp -r rpi_remote_cam/www/* /var/www/html/
sudo mv /var/www/html/index.php /var/www/html/index1.php
#работа с скрвером
scp -r * pi@10.253.35.143:~/rpi_remote_cam
sudo cp -r www/* /var/www/html/
python3 server.py
```

@ -1,4 +1,7 @@
import sys import sys
import serial
import sys
import time
from twisted.internet import reactor from twisted.internet import reactor
from twisted.python import log from twisted.python import log
@ -11,12 +14,34 @@ from autobahn.twisted.websocket import WebSocketServerFactory, \
from autobahn.twisted.resource import WebSocketResource from autobahn.twisted.resource import WebSocketResource
i = 0 i = 0
turn_sleeptime = 0.8
straight_sleeptime = 0.5
def setMotor(command):
if command == 'D':
vacuum_serial.write(str.encode(
"SetMotor LWheelDist 155 RWheelDist 155 Speed 150 \n"))
time.sleep(straight_sleeptime)
if command == 'L':
vacuum_serial.write(str.encode(
"SetMotor LWheelDist -190 RWheelDist 190 Speed 200 \n"))
time.sleep(turn_sleeptime)
if command == 'R':
vacuum_serial.write(str.encode(
"SetMotor LWheelDist 190 RWheelDist -190 Speed 200 \n"))
time.sleep(turn_sleeptime)
if command == 'F':
vacuum_serial.write(str.encode(
"SetMotor LWheelDist -155 RWheelDist -155 Speed 150 \n"))
time.sleep(straight_sleeptime)
def setServo(servoChannel, position): def setServo(servoChannel, position):
servoStr = str(servoChannel) + "=" + str(position) + "\n" servoStr = str(servoChannel) + "=" + str(position) + "\n"
with open("/dev/servoblaster", "wb") as f: with open("/dev/servoblaster", "wb") as f:
f.write(bytes(servoStr, 'utf-8')) f.write(bytes(servoStr, 'utf-8'))
class EchoServerProtocol(WebSocketServerProtocol): class EchoServerProtocol(WebSocketServerProtocol):
def onConnect(self, request): def onConnect(self, request):
@ -25,22 +50,32 @@ class EchoServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary): def onMessage(self, payload, isBinary):
global i global i
i = i + 1 i = i + 1
if isBinary: # if isBinary:
print("Binary message received: {0} bytes".format(len(payload))) # print("Binary message received: {0} bytes".format(len(payload)))
else: # else:
print("Text message received: {0}".format(payload.decode('utf8'))) # print("Text message received: {0}".format(payload.decode('utf8')))
string = " " + str(i) string = " " + str(i)
self.sendMessage(payload + bytes(string, 'utf-8'), isBinary) self.sendMessage(payload + bytes(string, 'utf-8'), isBinary)
string = payload.decode('ASCII') string = payload.decode('ASCII')
setServo(0, int(string.split("/")[0])) print("Str ->{0}".format(string))
if string[0] != 'R' and string[0] != 'L' and string[0] != 'F' and string[0] != 'D':
setServo(2, int(string.split("/")[0]))
setServo(1, int(string.split("/")[1])) setServo(1, int(string.split("/")[1]))
else:
setMotor(string[0])
if __name__ == '__main__': if __name__ == '__main__':
try:
vacuum_serial = serial.Serial('/dev/ttyACM0', 115200)
vacuum_serial.write(str.encode("testmode on \n"))
time.sleep(1)
log.startLogging(sys.stdout) log.startLogging(sys.stdout)
factory = WebSocketServerFactory(u"ws://10.174.136.143:45684") factory = WebSocketServerFactory(u"ws://10.253.35.143:45684")
factory.protocol = EchoServerProtocol factory.protocol = EchoServerProtocol
resource = WebSocketResource(factory) resource = WebSocketResource(factory)
@ -57,3 +92,11 @@ if __name__ == '__main__':
reactor.listenTCP(45684, site) reactor.listenTCP(45684, site)
reactor.run() reactor.run()
vacuum_serial.write(str.encode("testmode off \n"))
vacuum_serial.close()
except KeyboardInterrupt:
print("Fuuuuuuuuuuuu")
vacuum_serial.write(str.encode("testmode off \n"))
vacuum_serial.close()

@ -20,4 +20,10 @@ img {
max-width: 100%; max-width: 100%;
height: auto; height: auto;
} }
p {
margin-right: 5%;
margin-left: 5%;
font-size: 18px;
}

@ -18,17 +18,19 @@
<script type="text/javascript"> <script type="text/javascript">
var sock = null; var sock = null;
var ellog = null; var ellog = null;
var video_sock = "http://" + window.location.hostname + ":8081/";
var servo_sock = "ws://" + window.location.hostname + ":45684/ws?a=23&foo=bar";
window.onload = function() { window.onload = function () {
ellog = document.getElementById('log'); ellog = document.getElementById('log');
var wsuri; var wsuri = servo_sock;
if (window.location.protocol === "file:") { //if (window.location.protocol === "file:") {
wsuri = "ws://10.174.136.143:45684/ws?a=23&foo=bar"; // wsuri = "ws://10.174.136.143:45684/ws?a=23&foo=bar";
} else { //} else {
wsuri = "ws://" + window.location.hostname + ":45684/ws?a=23&foo=bar"; // wsuri = "ws://" + window.location.hostname + ":45684/ws?a=23&foo=bar";
} //}
if ("WebSocket" in window) { if ("WebSocket" in window) {
sock = new WebSocket(wsuri); sock = new WebSocket(wsuri);
@ -39,16 +41,16 @@
} }
if (sock) { if (sock) {
sock.onopen = function() { sock.onopen = function () {
console.log("Connected to " + wsuri); console.log("Connected to " + wsuri);
} }
sock.onclose = function(e) { sock.onclose = function (e) {
console.log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')"); console.log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
sock = null; sock = null;
} }
sock.onmessage = function(e) { sock.onmessage = function (e) {
console.log("Got echo: " + e.data); console.log("Got echo: " + e.data);
} }
} }
@ -56,7 +58,31 @@
function send() { function send() {
if (sock) { if (sock) {
sock.send(document.getElementById("x-pos").value + "/" + document.getElementById("y-pos").value); var msg = document.getElementById("x-pos").value + "/" + document.getElementById("y-pos").value;
sock.send(msg);
console.log("Sent " + msg);
} else {
console.log("Not connected.");
}
};
function send_button(ch) {
if (sock) {
switch (ch) {
case 'F':
var msg = 'F';
break;
case 'D':
var msg = 'D';
break;
case 'R':
var msg = 'R';
break;
case 'L':
var msg = 'L';
break;
}
sock.send(msg);
console.log("Sent " + msg); console.log("Sent " + msg);
} else { } else {
console.log("Not connected."); console.log("Not connected.");
@ -70,7 +96,8 @@
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
@ -103,20 +130,51 @@
</div> </div>
<div class="text-center"> <div class="text-center">
<br> <br>
<img style="-webkit-user-select: none;" src="http://10.174.136.143:8081/"> <img style="-webkit-user-select: none;" src="http://10.253.35.143:8081/">
<br> <br>
<br> <br>
<table width="100%">
<tr>
<th width="20%">
<p align="center"><input type="button" style="width:120px;height:35px" id="forword-butt" value=&#8657 accesskey="w"
onclick="send_button('F')">
</p>
<p align="center"><input type="button" style="width:80px;height:35px" id="left-butt" value=&#8656 accesskey="a"
onclick="send_button('L')">
<input type="button" style="width:80px;height:35px" id="right-butt" value=&#8658 accesskey="d"
onclick="send_button('R')">
</p>
<p align="center"><input type="button" style="width:120px;height:35px;" id="down-butt" value=&#8659 accesskey="s"
onclick="send_button('D')">
</p>
</th>
<th width="80%">
<form class="range-field"> <form class="range-field">
<p>Вертикаль:<input type="range" min="50" max="250" id="y-pos" oninput="send();" value="140" /></p> <p>Вертикаль:<input type="range" min="50" max="250" id="y-pos" oninput="send();"
<p>Горизонталь:<input type="range" min="50" max="250" id="x-pos" oninput="send();" value="140" /></p> value="140" />
</p>
<p>Горизонталь:<input type="range" min="50" max="250" id="x-pos"
oninput="send();" value="140" /></p>
</form> </form>
</th>
</tr>
</table>
</div> </div>
</div> </div>
</div> </div>
<footer class="pull-left footer"> <footer class="pull-left footer">
<p class="col-md-12"> <p class="col-md-12">
<hr class="divider">Проект создан в образовательных целях, при поддержке <a href="https://t.me/baumanrobotics">Bauman <hr class="divider">Проект создан в образовательных целях, при поддержке <a
href="https://t.me/baumanrobotics">Bauman
Robotics</a>.</a> Robotics</a>.</a>
</p> </p>
</footer> </footer>

Loading…
Cancel
Save