I am trying to control a servo motor from a web interface. I am using SpringBoot 2.0, ServoBlaster and pi4j.
In order to start the application I am running as root ./gradlew bootrun --no-daemon
. It has to be root in order to handle the GPIOs and I don't have any security worries about the device.
In simplified (a class with just the main
function) Java/Kotlin I achieved to control the servo by any of the following ways:
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)
Write to /dev/servoblaster
val out = PrintWriter(FileOutputStream("/dev/servoblaster"), true)
println("Go to 65 again")
out.println("5=65")
out.flush()
out.close
Call a secondary script which writes to /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)
Write the value to a file and have a secondary execute reading this file and applying this value to the servo
I have tried all of the above in Springboot
but without success.
So the question is, could somebody tell me how could I:
RPIServoBlasterProvider
class from Springboot
? OR/dev/servoblaster
? ORSolutions at any of the above questions could help me solve my problem.
PS: Is there anything wrong with the blockquote for the source code in stackoverflow? I could not format it as a block and I used the line code formatting!
The whole problem was with the servod
of the ServoBlaster
. It was accidentally killed and I had to run it once again!
I followed the No 5 solution.