问题
This is the first time I program a Raspberry PI and the first time I'm using Python, JavaScript or Node-Red.
I'm trying to lift a gate by operating a servomotor in Node-RED. Basically, what I'm doing is take a picture of a license plate with my pi camera, process it, send it to OpenALPR, and get the license plate back as a msg.payload.
Now, what I want to do is activate the servomotor if the msg.paylaod.results[0].plate matches the plate numbers I need. I'm trying to create a global variable to use in the subflow that activates the servomotor. This is the code:
if(msg.payload.results[0].plate=="SB06KBE" ||
msg.payload.results[0].plate=="AR18AUG")
{
console.log(msg.payload.results[0].plate);
context.global.cond=1;
}
else console.log("not recognized");
This node enters the motor subflow:
variable node:
var mycond=context.global.cond;
return mycond;
motor pwm node:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
p=GPIO.PWM(7,50)
p.start(7)
if mycond==1:
try:
while True:
p.ChangeDutyCycle(7)
time.sleep(1)
p.ChangeDutyCycle(3)
time.sleep(10)
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
For some reason, the motor won't start running.
I tried using the switch node and removed the variable node in the subflow and the if condition in the motor node but the switch would not go to any output. This is the switch node I tried using:
Also, if I remove the variable node in the subflow the motor would run even if the license plate is not recognized.
LE: I also tried to connect the first two outputs of the switch node to two JavaScript nodes that would print a message but they never run even though the conditions are met.
Thanks in advance!
来源:https://stackoverflow.com/questions/44086273/servomotor-doesnt-run-in-node-red