Servo.py script no longer works after i edited pwm permissions

this is servo.py script :move the motor to angle x

import sys
import time
import navio.pwm
import navio.util
with navio.pwm.PWM(1) as pwm:
pwm.set_period(50)
pwm.enable()

 while (True):
     angle=float(input())
     m = 0.6 + (2.4 - 0.6) * ((angle - 0) / (180-0))
     print(m)
     pwm.set_duty_cycle(m)
     time.sleep(1)

i want to execute this script from command line without sudo so like this :
python servo.py
but am getting permissions errors from cd /sys/class/pwm/pwmchip0/pwm0/period
and the pwm0 file was not showing so after some research i found this page
https://raspberrypi.stackexchange.com/questions/66890/accessing-pwm-module-without-root-permissions

the comment suggested to add the following lines to the /etc/udev/rules.d/99-com.rules file
SUBSYSTEM==“pwm*”, PROGRAM="/bin/sh -c ’
chown -R root:gpio /sys/class/pwm && chmod -R 770 /sys/class/pwm;
chown -R root:gpio /sys/devices/platform/soc/.pwm/pwm/pwmchip && chmod -R 770 /sys/devices/platform/soc/.pwm/pwm/pwmchip
'"

,after i added them i got no errors and the script was working but the motor was not moving and when i connected it to pin 0 the motor is staying at a fixed angle and if i manually try to move i feel it trying to move back to the fixed angle so i changed this file /etc/udev/rules.d/99-com.rules back to this
SUBSYSTEM==“pwm*”, PROGRAM="/bin/sh -c ’
chown -R root:gpio /sys/class/pwm && chmod -R 770 /sys/class/pwm;
chown -R root:gpio /sys/devices/platform/soc/.pwm/pwm/pwmchip && chmo$
'"
but still am facing the same problem and i realized the pwm0,pwm1… files are showing now and am able to modify their permissions
now even if i try sudo python servo.py the file executes but the motor dose not move
not: i tried connecting the motor to another input but it is still not working.

Hi Hussein,

Welcome to our forum!

Navio2 kernel driver that generates PWM needs to be fed with data at least every 100 ms. To make PWM output work, you need to update the value set_duty_cycle every 100 ms or less.

Thank you for replying to my previous message .

somehow i managed to solve the first problem but still i want to run Servo.py without using sudo.

Servo.py:
import sys
import time

import navio.pwm
import navio.util

navio.util.check_apm()

PWM_OUTPUT = 0
SERVO_MIN = 1.250 #ms
SERVO_MAX = 1.750 #ms

with navio.pwm.PWM(PWM_OUTPUT) as pwm:
pwm.set_period(50)
pwm.enable()

while (True):
    pwm.set_duty_cycle(SERVO_MIN)
    time.sleep(1)
    pwm.set_duty_cycle(SERVO_MAX)
    time.sleep(1)

when i run Python Servo.py am getting this error message:
Traceback (most recent call last):
File “Servo.py”, line 21, in
time.sleep(1)
File “/home/pi/Navio2/Python/navio/pwm.py”, line 19, in exit
self.deinitialize()
File “/home/pi/Navio2/Python/navio/pwm.py”, line 25, in deinitialize
with open(self.SYSFS_PWM_UNEXPORT_PATH, “a”) as pwm_unexport:
IOError: [Errno 13] Permission denied: ‘/sys/class/pwm/pwmchip0/unexport’

how can i solve it?

Servo.py:
import sys
import time

import navio.pwm
import navio.util

navio.util.check_apm()

PWM_OUTPUT = 0
SERVO_MIN = 1.250 #ms
SERVO_MAX = 1.750 #ms

with navio.pwm.PWM(PWM_OUTPUT) as pwm:
pwm.set_period(50)
pwm.enable()

while (True):
    pwm.set_duty_cycle(SERVO_MIN)
    time.sleep(1)
    pwm.set_duty_cycle(SERVO_MAX)
    time.sleep(1)

when i run Python Servo.py am getting this error message:
Traceback (most recent call last):
File “Servo.py”, line 21, in
time.sleep(1)
File “/home/pi/Navio2/Python/navio/pwm.py”, line 19, in exit
self.deinitialize()
File “/home/pi/Navio2/Python/navio/pwm.py”, line 25, in deinitialize
with open(self.SYSFS_PWM_UNEXPORT_PATH, “a”) as pwm_unexport:
IOError: [Errno 13] Permission denied: ‘/sys/class/pwm/pwmchip0/unexport’

how can i solve this?

Hi Hussein,

Could you share what you are trying to achieve by configuring the Servo.py example to run without sudo? This might help me to find out a workaround for you.

l am trying to make an object tracking using a (pan tilt camera mount) and i want to move the servo motor when it is connected to the navio 2 pins ,the movement using this script is very smooth so this is why i want to use it in my project.

Hi Hussein,

Are there any restrictions in your system that don’t allow you to run the Servo.py script with sudo?

no i don’t think so ,Although i re-uploaded the os image the problem is still there .

Hi Hussein,

It might be possible to run the script without sudo by using udev rule. Please check out the details in the following thread:

However, I’ve not done it myself so I can barely guide you through the process.

thank for replying ,but i have used this solution before and you are right the script will work but the servo motor will not move , and the navio2 pin 0 will have consistent output of almost 0.17 so basically it broke my pin 0 , and after i deleted the lines of code and rebooted my navio2 the problem did not go a way so l had to re-upload the os image to solve the problem ,if there any other solution i will gladly try it on.

Hussein,

I suggest using the original script for servo control with this pin. It’s highly possible that there’s a software-related issue with it.

I can hardly share any other solutions for the issue in such case. Servo.py script is mainly intended to work as an example showing how to control servos. It might be better to write your own script from scratch to bypass possible issues with PWM access.