1
import pigpio
import math
import RPi.GPIO as GPIO
from gpiozero import Button
import time

GPIO_SERVO_PIN  = 27
limit = 17
switch = Button(limit)
GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

MIN_ANG=-180.0 #degrees
MAX_ANG=180.0  #degrees

MIN_PW=1000 # microseconds
MAX_PW=2000 # microseconds

ANG_RANGE=MAX_ANG-MIN_ANG
PW_RANGE=MAX_PW-MIN_PW
PWAR=float(PW_RANGE)/ANG_RANGE

RAD2DEG=180.0/math.pi

def estop():
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1500.0)
    time.sleep(0.1)
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1250.0)
    time.sleep(0.1)
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1500.0)


def angleToPulseWidth(angle): # stay between 90(right), 0 stop, -90(left)
   """
   angle is mapped to valid pulse widths for servo
   which are determined by experiment.
   """
   assert MIN_ANG <= angle <= MAX_ANG
   return MIN_PW + ((angle - MIN_ANG) * PWAR)

pi = pigpio.pi()
if not pi.connected:
    exit()
while True:
    switch.when_pressed = estop
    angle = float(input('Please enter a angle: '))
    pw = angleToPulseWidth(angle)
    print(str(pw))
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, pw)

I am trying make this drink tray move in and out using a MG996R servo. The servo was modded to be continuous by removing the physical gear metal block and pushout the electronic feedback loop. I am pigpio as the library for PWM to control my motor. I have a limiter switch to stop it if it moves back to far. The problem I having right now is that if I use my hands to press the switch it stop it. But when i let the tray press the switch it end not stopping the motor and just keep on running. Images are display below. My 3D printed rail system: "CAD file"

enter image description here

enter image description here

enter image description here

2
  • What pulsewidth are you sending to stop the servo? Does that pulsewidth stop the servo when sent from the command line with pigs? E.g. pigs s 27 0
    – joan
    Mar 21 '20 at 9:53
  • The plusewidth I use is 1500, It stop my servo completely. The Code is : pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1500.0) I have not tried with the command line yet, everything is control by the python script above. @joan
    – Kedd22
    Mar 22 '20 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.