Operations with different servos
Posted: Mon Oct 01, 2018 6:24 pm
I have a project that requires three different servos: two continuous rotation (CR) and one linear. The two CR servos are represent radars, while the linear one controls a security gate.
The gate is currently has a time.sleep() command to cause the arm to move 90 degrees open/close every 10 seconds. I have all servos monitoring pin X5 for a control signal: if a voltage is seen on X5, then the servos run. If that voltage goes away, then the servos should stop.
Eventually, I would like to have a separate control signal for each servo, as well as having the gate respond to user input. But, for the current simulation, having the gate cycle open and closed simply demonstrates the capability of the model, as well as justifying the purchase of more pyboards.
The main problem is the sleep command. Because it sleeps the entire program, the servos continue in their current state until the sleep command finishes. Thus, if X5 loses voltage, it can be up to 10 seconds before the servos start/stop.
I looked at threading the gate function but have had no luck, probably because I don't know how to properly implement the _thread module. In lieu of rewiring the model, is there a way to make the gate cycling not affect the other servos?
Current code is below:
[code]
# main.py -- put your code here!
from pyb import Pin, Servo, Timer
import _thread
import time
control_pin = Pin("X5", Pin.IN)
weather = Servo(1)
atc = Servo(2)
gate = Servo(3)
def radar():
if control_pin.value() == 1:
weather.speed(5)
atc.speed(5)
else:
weather.speed(-1)
atc.speed(-1)
def gate_cycle():
if gate.angle() <= 5:
gate.angle(90, 1000)
time.sleep(10)
else:
gate.angle(5, 1000)
time.sleep(10)
while True:
radar()
gate_cycle()
[/code]
The gate is currently has a time.sleep() command to cause the arm to move 90 degrees open/close every 10 seconds. I have all servos monitoring pin X5 for a control signal: if a voltage is seen on X5, then the servos run. If that voltage goes away, then the servos should stop.
Eventually, I would like to have a separate control signal for each servo, as well as having the gate respond to user input. But, for the current simulation, having the gate cycle open and closed simply demonstrates the capability of the model, as well as justifying the purchase of more pyboards.
The main problem is the sleep command. Because it sleeps the entire program, the servos continue in their current state until the sleep command finishes. Thus, if X5 loses voltage, it can be up to 10 seconds before the servos start/stop.
I looked at threading the gate function but have had no luck, probably because I don't know how to properly implement the _thread module. In lieu of rewiring the model, is there a way to make the gate cycling not affect the other servos?
Current code is below:
[code]
# main.py -- put your code here!
from pyb import Pin, Servo, Timer
import _thread
import time
control_pin = Pin("X5", Pin.IN)
weather = Servo(1)
atc = Servo(2)
gate = Servo(3)
def radar():
if control_pin.value() == 1:
weather.speed(5)
atc.speed(5)
else:
weather.speed(-1)
atc.speed(-1)
def gate_cycle():
if gate.angle() <= 5:
gate.angle(90, 1000)
time.sleep(10)
else:
gate.angle(5, 1000)
time.sleep(10)
while True:
radar()
gate_cycle()
[/code]