1

我正在尝试从 Web 界面控制伺服电机。我正在使用SpringBoot 2.0、ServoBlasterpi4j

为了启动我以 root 身份运行的应用程序./gradlew bootrun --no-daemon。它必须是 root 才能处理 GPIO,我对设备没有任何安全担忧。

在简化的(只有main函数的类)Java/Kotlin 中,我实现了通过以下任何一种方式控制伺服:

  1. RPIServoBlasterProvider

    val servoProvider = RPIServoBlasterProvider() val servo0 = servoProvider.getServoDriver(servoProvider.definedServoPins[5]) println("Go to 150") //middle servo0.servoPulseWidth = 150 println("Went to ${servo0.servoPulseWidth}") Thread.sleep(1550)

  2. 写给/dev/servoblaster

    val out = PrintWriter(FileOutputStream("/dev/servoblaster"), true) println("Go to 65 again") out.println("5=65") out.flush() out.close

  3. 调用写入到的辅助脚本/dev/servoblaster

    val servoId = 5 val script = "/home/pi/ServoHardwareSteering.sh" val cmdMinPosition = "$script $servoId 65" val cmdMidPosition = "$script $servoId 150" val cmdMaxPosition = "$script $servoId 235" val runtime = Runtime.getRuntime() println(cmdMidPosition) runtime.exec(cmdMidPosition)//.waitFor() Thread.sleep(1550)

  4. 将值写入文件并进行次要执行读取此文件并将此值应用于伺服

我已经尝试了以上所有方法,Springboot但没有成功。

所以问题是,有人能告诉我我怎么能:

  1. 使用RPIServoBlasterProvider来自的类Springboot或者
  2. 写信给/dev/servoblaster或者
  3. 执行任何终端脚本?或者
  4. 在哪里保存脚本以便能够调用它
  5. 写入一个简单的文件(例如 afile.txt)? 或者
  6. 以一种我还没有考虑过的更好的方式解决这个问题。

上述任何问题的解决方案都可以帮助我解决我的问题。

PS:stackoverflow中源代码的blockquote有什么问题吗?我无法将其格式化为一个块,我使用了行代码格式化!

4

2 回答 2

0
use the RPIServoBlasterProvider class from Springboot? OR

您之前所做的相同 - 它的 Java(基于 kotlin) - 或者只是将 Kotlin 与 Spring https://spring.io/guides/tutorials/spring-boot-kotlin/一起使用

write to /dev/servoblaster? OR

就像你做的一样 - 一切都是linux中的文件,因此它写入一个普通文件

execute any terminal script? OR

Runtime.getRuntime.exec- 任何变体或ProcessBuilder

where to save the script in order to be able to call it OR

任何地方

write to a simple file (ex. afile.txt)? OR

第2点。

solve the issue in a better way that I did not think about already.

没有使用伺服冲击波的经验。

于 2019-12-16T07:32:11.407 回答
0

整个问题servod出在ServoBlaster. 它被意外杀死了,我不得不再次运行它!我遵循了5 号解决方案。

于 2019-12-17T15:30:09.103 回答