0

也许这很容易解决,也许我搞砸了。我搞不清楚了。提前感谢您能给我的任何帮助!

所以,我有一个必须安装在漫游车(RC 爬虫)上的树莓派 2 b+。我决定使用 WebIOPi,但找不到任何有用的东西。

我的项目应该是这样工作的:HTML/JS -> PYTHON -> SERIAL TX -> ARDUINO 我已经拥有了我需要的一切,而且树莓派的 PWM 屏蔽在这里非常昂贵。

我设法在我的 HTML 上显示了两个滑块,并且 arduino 已准备好接收命令字符串。但介于两者之间的某个地方不起作用。

完整的代码在底部,这里只是一些片段:

<div class="slidecontainer">
<input type="range" min="1" max="179" value="90" class="slider" id="myRange" width="500"></div>

在这里,我有两个滑块之一。

<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("steering");
var slider2 = document.getElementById("myRange2");
var output2 = document.getElementById("throttle");
var vs = slider.value;
var vt = slider2.value;
output.innerHTML = slider.value;
output2.innerHTML = slider2.value;
slider.oninput = function() {
    steering.innerHTML = slider.value - 90;
    webiopi().callMacro("new_steering",vs);
}
slider2.oninput = function() {
    throttle.innerHTML = slider2.value - 90;
    webiopi().callMacro("new_throttle",vt);
}
</script>

在这里,我运行一个脚本,该脚本将值写入 HTML 页面,格式为 -89 到 89 范围。我调用了一个名为 new_steering 的 WebIOPi 宏,传递值 vs(vs 应该是滑块的值,从 1 到 179)。

@webiopi.macro
def new_steering(nsv):
    f = open("slog.txt", "a+")
    f.write(nsv)
    news_string = "S{}".format(nsv)
    f.write(news_string)
    serial.writeString(news_string)
    f.close()

在 python 代码中,我有读取传入值的宏,将其写入文本文档(不工作也不需要,只是为了检查它是否接收到变量),它创建一个以 S 作为前导字符的字符串并尝试发送它槽串口

出了点问题。我不知道什么和有多糟糕。请帮忙!我的小脑袋正在沸腾。

我已经像这样设置了 WebIOPi:

[GPIO]

[~GPIO]

[SCRIPTS]

botler1 = /home/pi/botler1/python/interface.py

[HTTP]

enabled = true
port = 8786

passwd-file = /etc/webiopi/passwd

prompt = "BOTLER1_hello"

doc-root = /home/pi/botler1

welcome-file = index.html

#------------------------------------------------------------------------#

[COAP]
enabled = true
port = 5683
multicast = true

[DEVICES]

usb1 = Serial device:ttyACM0 baudrate:9600

[REST]

gpio-export = 17
gpio-post-value = true
gpio-post-function = false

[ROUTES] 

这是 HTML-Web 界面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebIOPi | Light Control</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
    // Create a "Light" labeled button for GPIO 17
    var button = webiopi().createGPIOButton(17, "Light");

    // Append button to HTML element with ID="controls" using jQuery
    $("#controls").append(button);

    // Refresh GPIO buttons
    // pass true to refresh repeatedly of false to refresh once
            webiopi().refreshGPIO(true);
});

</script>
<style type="text/css">
    button {
        display: block;
        margin: 5px 5px 5px 5px;
        width: 160px;
        height: 45px;
        font-size: 24pt;
        font-weight: bold;
        color: white;
    }

    #gpio17.LOW {
        background-color: Black;
    }

    #gpio17.HIGH {
        background-color: Blue;
    }
    .slidecontainer {
width: 100%; /* Width of the outside container */
}

/* The slider itself */
.slider {
-webkit-appearance: none;  /* Override default CSS styles */
appearance: none;
width: 100%; /* Full-width */
height: 25px; /* Specified height */
background: #d3d3d3; /* Grey background */
outline: none; /* Remove outline */
opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
-webkit-transition: .2s; /* 0.2 seconds transition on hover */
transition: opacity .2s;
}

</style>
</head>
<body>

<div class="slidecontainer">
<input type="range" min="1" max="179" value="90" class="slider" id="myRange" width="500"></div>
<div align="center"><p>Steering: <span id="steering"></span></p></div>
<div class="slidecontainer">
<input type="range" min="1" max="179" value="90" class="slider" 
id="myRange2" width="500"></div>
<div align="center"><p>Throttle: <span id="throttle"></span></p></div>

</div>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("steering");
var slider2 = document.getElementById("myRange2");
var output2 = document.getElementById("throttle");
var vs = slider.value;
var vt = slider2.value;
output.innerHTML = slider.value;
output2.innerHTML = slider2.value;
slider.oninput = function() {
    steering.innerHTML = slider.value - 90;
    webiopi().callMacro("new_steering",vs);
}
slider2.oninput = function() {
    throttle.innerHTML = slider2.value - 90;
    webiopi().callMacro("new_throttle",vt);
}
</script>

</body>
</html>

这是python文件

import webiopi
import datetime

GPIO = webiopi.GPIO

# setup function is automatically called at WebIOPi startup
def setup():
    nosense = 1

# loop function is repeatedly called by WebIOPi 
def loop():
    # retrieve device named "serial" in the config
    serial = webiopi.deviceInstance("usb1") 
    # write a string
    #serial.writeString("some text")

    webiopi.sleep(1)

@webiopi.macro
def new_steering(nsv):
    f = open("slog.txt", "a+")
    f.write(nsv)
    news_string = "S{}".format(nsv)
    f.write(news_string)
    serial.writeString(news_string)
    f.close()

@webiopi.macro
def new_throttle(ntv):
    d = open("tlog.txt", "a+")
    d.write(ntv)
    newt_string = "T{}".format(ntv)
    d.write(newt_string)
    serial.writeString(newt_string)
    d.close

def destroy():
    nosense = 1
4

1 回答 1

-1

您是否曾经尝试过独立于 webiopi 的串行通信?我读过串行通信不像以前那样传统,所以,首先尝试一下。

于 2018-12-28T19:39:12.847 回答