0
\$\begingroup\$

I have a continuous rotation SG90. I am trying to implement software PWM (because the orangepi sbc I am using doesn't support hardware PWM yet). I got a bunch of electronics from a friend so I don't exactly know what everything is, but based on the fact that it rotates continuously I believe it has to be a continuous rotation SG90 (SG90 is written on the sticker). I couldn't find a clear datasheet for a continuous rotation SG90. That is why I have based the values of the period, stop, min and max values on what I found from the 180-degree SG90 datasheet. This is the code I am using to do the rotation -

#!/usr/bin/env python3
import time
import sys
import gpiod
from gpiod.line import Direction, Value
PIN = 267
PERIOD = 0.02
STOP = 0.0015
MIN = 0.001
MAX = 0.002
def gpio_set(pin,state) # function that sets a pin to a state, validated to work correctly elsewhere
def speed2pulse(speed):
 # speed = -1 to 1
 pulse = STOP + (MIN - MAX) * float(speed) / 2.0
 return pulse
 
def set_speed(pin,speed):
 width = speed2pulse(speed)
 high = width
 low = PERIOD - high
 gpio_set(pin,1)
 time.sleep(high)
 gpio_set(pin,0)
 time.sleep(low)
def main():
 while True:
 set_speed(PIN,sys.argv[1])
 
 
if __name__ == '__main__':
 main()

It does not work reliably. Sometimes it rotates for a bit in one direction then starts rotating in a different direction. Most of the time it just rotates in one direction. Even without the while-loop, it does the same thing.

Maybe it is a processing time issue? When I move my mouse scroll wheel it affects the speed of rotation. I don't know how to check for sure/fix that. This is running on Debian 11. The servo is connected to a 5V DC supply.

I tried running a bunch of validation tests but it just keeps rotating in the same direction no matter what speed I set it to. I did some validation tests on the formula for speed to pulse width conversion which I got by solving the straight-line equation, but even that seems to be fine.

JRE
75k10 gold badges115 silver badges197 bronze badges
asked May 20, 2024 at 23:14
\$\endgroup\$
5
  • \$\begingroup\$ I would not make the software PWM that way, though. I would make it event based instead of blocking sleep. With blocking sleep, you wont be able to do anything else. \$\endgroup\$ Commented May 21, 2024 at 9:02
  • \$\begingroup\$ @MrGerber thank you for the suggestion. Can you point me to a resource where I can learn about this. I don't even know what event based means. Is it like asynchronous requests in Javascript? How does one implement it in this context? \$\endgroup\$ Commented May 21, 2024 at 17:46
  • \$\begingroup\$ I'm not intimate with python in that regard, there are most likely bucketloads of libraries that fix that for you, but I'd probably just do something simple on my own using the threading library - or poll a system timer in the main loop. IDK. This might be relevant tho: stackoverflow.com/questions/22180915/… \$\endgroup\$ Commented May 21, 2024 at 17:51
  • \$\begingroup\$ Looks like the raspberry pi did a mix - they did the blocking sleep() function, but they spun that off in a separate thread. \$\endgroup\$ Commented May 21, 2024 at 17:56
  • \$\begingroup\$ Thank you! THe python solution seems simple enough \$\endgroup\$ Commented May 21, 2024 at 22:11

1 Answer 1

2
\$\begingroup\$

RC servos like yours don't work with simple PWM such as you are using. They use a 1 to 2 millisecond wide pulse that repeats every 20 milliseconds.

enter image description here

From the Wikipedia RC servo page.

The diagram is for a servo that does positioning, but the signal structure is the same for your continuous rotation servo.

  • 1 millisecond is full speed in one direction.
  • 2 milliseconds is full speed in the other direction.
  • 1.5 milliseconds is stop.

You'll need to modify your code to produce the required pulse signal.

answered May 21, 2024 at 5:41
\$\endgroup\$
1
  • \$\begingroup\$ Does having the single pulse function in a while loop as I have done in my code not achieve that? \$\endgroup\$ Commented May 21, 2024 at 17:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.