Author Topic: PWM noise on Raspberry Pi  (Read 8680 times)

0 Members and 1 Guest are viewing this topic.

Offline sugarfree90Topic starter

  • Newbie
  • Posts: 3
  • Country: pl
PWM noise on Raspberry Pi
« on: July 29, 2015, 08:23:14 am »
Hello!

I think this is PWM noise...

I am building a robot, i have two tracks which are controlled with two H bridges (or i think so - off the shelf solution), everything is working beautifully, but recently i have added a cheap chinese servomechanism (SG90 Tower Pro) to pivot the camera. The pivot works reasonably well (Raspberry Pi + PWM =  :-BROKE) but when i am sending any command to move and engage track motors, the servo is going crazy.

To power this thing i am using a 2A switching voltage regulator (and 7.2V Li-Ion battery)

My command to move servo looks like that (python):

pwm = GPIO.PWM(18, 200) <-initializing servo on pin 18 with 200Hz PWM modulation
pwm.start(25) <- setting in% value of PWM signal
time.sleep(0.3) <-time for servo to move
pwm.stop() <-switching off the signal and leaving the servo in previously defined position

I am initializing servo for each swing and deactivating it after that in order to prevent the shaking which occurs due to poor Raspberry Pi PWM capabilities
And this works as a charm (well... almost...)
I am powering this servo directly from the Raspberry Pi pins and thourgh that the voltage drops at RPi are making this interference so i have installed 10V 1000uF cap on servo power wires - servo is still crazy...

My solution will be to controll power of the servo with a transistor, or add an arduino...

But for now, any ideas what to or why is this happening?
Kind Regards,
Sugarfree90
GNSS, air navigation aids, avionics, UAV avionics "expert"
 

Offline piranha32

  • Supporter
  • ****
  • Posts: 151
  • Country: us
    • random ramblings of an engineer
Re: PWM noise on Raspberry Pi
« Reply #1 on: July 29, 2015, 06:03:25 pm »
I don't know Raspi, but my guess is that the PWM signal is generated by software. Since the OS is not real time, and the CPU is rather lousy, there will be many random delays in toggling the I/O pin, which will affect the timings. If you have more tasks which require hard-RT timings, using a dedicated micro for handling them will be a good idea, however, if you only need to drive servos, take a look at one of the PWM LED driver chips. E.g. PCA9685 will give you 16 servo channels, controlled via I2C bus.
Another issue is the repetition frequency. Most modern servos should handle 200Hz quite well, but it may be too much for some. Try to lower it to 20-25Hz and see what happens.

Offline Chris C

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: PWM noise on Raspberry Pi
« Reply #2 on: July 30, 2015, 09:19:59 pm »
A small servo can easily use 100mA when moving just itself and its horn.  When moving weight (like that of your camera), it can consume many times more.

I looked up the maximum current a Raspberry Pi GPIO pin can source.  Saw a few different figures, from 16mA to 33mA.  Not enough to power a servo, even in a best-case scenario.

Attaching a capacitor and allowing it to charge up before movement is requested, will allow for a brief burst of motion.  But you'd need a very large cap.  1,000uF is only enough for perhaps a few tens of milliseconds at best, probably much less.  Your servo is "rebooting" over and over.

You *might* be able to get this working without any additional parts by using several GPIO pins in parallel to supply power, with each supplying a portion of the needed current.  But that's iffy, and on a robot, you'll probably need those pins for other things.

So personally, I wouldn't bother with that, and would use a transistor instead.  Or use an alternate method to generate higher-quality PWM, so that switching the power off to the servo when it's not supposed to be moving isn't necessary; like an Arduino or the PCA9685 [piranha32] mentioned.
 

Offline piranha32

  • Supporter
  • ****
  • Posts: 151
  • Country: us
    • random ramblings of an engineer
Re: PWM noise on Raspberry Pi
« Reply #3 on: July 30, 2015, 09:32:28 pm »
A small servo can easily use 100mA when moving just itself and its horn.  When moving weight (like that of your camera), it can consume many times more.
I looked up the maximum current a Raspberry Pi GPIO pin can source.  Saw a few different figures, from 16mA to 33mA.  Not enough to power a servo, even in a best-case scenario.

GPIOs are not used to power the servo. Typical servo has 3 wires. Two are connected directly to power supply and provide power for the motor. The third wire is connected to the control circuit and should pose a negligible load. The reasons why I would use a buffer on the control line are: isolate the CPU output from external circuit as a form of protection (which by itself is a very good reason to add a buffer), or to provide level translation if servo requires higher voltage signal.

Offline Chris C

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: PWM noise on Raspberry Pi
« Reply #4 on: July 30, 2015, 10:09:37 pm »
GPIOs are not used to power the servo.

Given the "I am powering this servo directly from the Raspberry Pi pin...my solution will be to controll power of the servo with a transistor" part, it looked as if he might currently be attempting to power the servo from a GPIO.  If so, that's a separate problem than the irregular PWM from the Pi, which you covered well.
 

Offline sugarfree90Topic starter

  • Newbie
  • Posts: 3
  • Country: pl
Re: PWM noise on Raspberry Pi
« Reply #5 on: July 31, 2015, 10:25:27 am »
Yest the PWM is generated with software, but i am initializing the PWM only to move servo, when i am moving the tracks this problem occurs and during that, no PWM for this servo is generated, i am initializing PWM to move servo then i am cutting off the servo signal.

I am connecting the servo to the power 5V pin in raspbbery and i can get as much amperage as usb can provide so this is not an issue, besides of that the camera is on a well ballanced hinge so the torque required to pivot here is minimal

I will install arduino then... Why i2c? Why is this better than com port?
Kind Regards,
Sugarfree90
GNSS, air navigation aids, avionics, UAV avionics "expert"
 

Offline Chris C

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: PWM noise on Raspberry Pi
« Reply #6 on: July 31, 2015, 02:08:57 pm »
The Pi is restricted in the amount of current it can supply from its USB power lines, compared to a regular computer/laptop.  As little as 100mA on an unmodified Pi, depending on exact model and configuration.  I'm not a Pi user and don't recall the exact details, but this may be of help:

http://raspberrypi.stackexchange.com/questions/340/how-much-power-can-be-provided-through-usb

I2C is a two-wire interface, much like a basic com port.  But unlike a com port, it allows multiple devices to share those two wires.  Each device has an address, and when you send out a command, you prefix it with the address of the device you want to communicate with.  Only the addressed device listens, the rest ignore.  Good for reducing the amount of wires and hardware resources to control the multiple systems on a robot.
 

Offline Bob F.

  • Regular Contributor
  • *
  • Posts: 96
  • Country: england
Re: PWM noise on Raspberry Pi
« Reply #7 on: July 31, 2015, 05:35:51 pm »
 

Offline piranha32

  • Supporter
  • ****
  • Posts: 151
  • Country: us
    • random ramblings of an engineer
Re: PWM noise on Raspberry Pi
« Reply #8 on: July 31, 2015, 06:18:15 pm »
Yest the PWM is generated with software, but i am initializing the PWM only to move servo, when i am moving the tracks this problem occurs and during that, no PWM for this servo is generated, i am initializing PWM to move servo then i am cutting off the servo signal.
If you're not driving the servo, weird things can happen. Most likely it will not hold position, but can also move on its own.

I am connecting the servo to the power 5V pin in raspbbery and i can get as much amperage as usb can provide so this is not an issue, besides of that the camera is on a well ballanced hinge so the torque required to pivot here is minimal
High current is required not only to move the load, but also there is a surge when the servo starts. You can try to add a big electrolytic capacitor, but it has to be charged at some point, and the power lines may have problem to deliver enough current. Better connect the power leads directly to the power supply.

I will install arduino then... Why i2c? Why is this better than com port?

I mentioned I2C because PCA9685 uses it. For arduino serial port will be much better (and easier) choice.

Also, did you try to lower repetition rate? I had problems with position holding when the pulse frequency was too high.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf