Created
June 21, 2018 09:59
-
-
Save nkenna/d3c1f8d3bbf3945328256c0064310587 to your computer and use it in GitHub Desktop.
controlling and tracking Servo position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from RPIO import PWM | |
CServo = PWM.Servo() | |
# move servo to your desired default position and give it time to arrive there | |
CServo.set_servo(pinCServo, 1500) | |
sleep(1) | |
fDoorServo = PWM.Servo() | |
garageDoorServo = PWM.Servo() | |
# move servo to your desired default position and give it time to arrive there | |
garageDoorServo.set_servo(pingarageDoorServo, 800) | |
sleep(1) | |
garageDoorServo.stop_servo(pingarageDoorServo) | |
# move servo to your desired default position and give it time to arrive there | |
fDoorServo.set_servo(pinFrontDoorServo, 800) | |
sleep(1) | |
fDoorServo.stop_servo(pinFrontDoorServo) | |
........................... | |
# if payload is a command | |
elif payload == camera_Right: | |
#tracking servo position | |
saveServoPosition.setPositionRight() | |
CServo.set_servo(pinCServo, saveServoPosition.getPosition() ) | |
sleep(1) | |
CServo.stop_servo(pinCServo) | |
elif payload == camera_Left: | |
#tracking servo position | |
saveServoPosition.setPositionLeft() | |
CServo.set_servo(pinCServo, saveServoPosition.getPosition() ) | |
sleep(1) | |
CServo.stop_servo(pinCServo) | |
#pubnub.publish().channel(FEEDBACKS).message(camera_Left).async(my_publish_callback) | |
elif payload == garageDoor_OPEN: | |
garageDoorServo.set_servo(pingarageDoorServo, 1600) | |
sleep(1) | |
garageDoorServo.stop_servo(pingarageDoorServo) | |
#pubnub.publish().channel(FEEDBACKS).message(garageDoor_OPEN).async(my_publish_callback) | |
elif payload == garageDoor_CLOSE: | |
garageDoorServo.set_servo(pingarageDoorServo, 800) | |
sleep(1) | |
garageDoorServo.stop_servo(pingarageDoorServo) | |
#pubnub.publish().channel(FEEDBACKS).message(garageDoor_CLOSE).async(my_publish_callback) | |
elif payload == frontDoor_OPEN: | |
fDoorServo.set_servo(pinFrontDoorServo, 2000) | |
sleep(1) | |
fDoorServo.stop_servo(pinFrontDoorServo) | |
#pubnub.publish().channel(FEEDBACKS).message(frontDoor_OPEN).async(my_publish_callback) | |
elif payload == frontDoor_CLOSE: | |
fDoorServo.set_servo(pinFrontDoorServo, 800) | |
sleep(1) | |
fDoorServo.stop_servo(pinFrontDoorServo) | |
# this is how to track servo position and avoid it from reaching position where it will start jittering | |
class SaveServoPosition: | |
defaultPosition = 1700 | |
def setPositionRight(self): | |
# dont allow it to move beyond 2400 | |
if self.defaultPosition == 2300: | |
self.defaultPosition = 2300 | |
else: | |
self.defaultPosition += 100 | |
def setPositionLeft(self): | |
# dont allow it to move beyond 600 | |
if self.defaultPosition == 600: | |
self.defaultPosition = 600 | |
else: | |
self.defaultPosition -= 100 | |
def getPosition(self): | |
return self.defaultPosition | |
saveServoPosition = SaveServoPosition() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment