User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Servo HELP!

Sat Apr 13, 2013 5:12 pm

Hello all,

I have just bought 2 servos (listing here: http://www.ebay.co.uk/itm/MG90S-Metal-G ... 2c6d47153e)...

And I want to get both of them working with the Pi! Originally I thought that this was not possible as the Pi only has one PWM pin, but apparently not! I think that I heard that you can control multiple servos with certain software... Servoblaster?

Whilst I am aware of the software, I have absolutely no idea how to use it! There is no tutorials online either!

Can someone help me? Show me how to use my 2 servos? Ideally through Python, but I'm not too fussy!

Thanks

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo HELP!

Sat Apr 13, 2013 5:34 pm

For a C solution.

https://www.youtube.com/watch?v=2vq0Q4dD6KE

Example code.

Code: Select all

#include <stdio.h>
#include "pigpio.h"

/*

P1  Name  gpio    used for

11  ---   17      SERVO 1
12  ---   18      SERVO 2
*/

#define SERVO1        17
#define SERVO2        18

/* forward prototypes */

void servoTick  (void);

int main(int argc, char *argv[])
{
   int version;
   char str[256];

   /* before using the library you must call gpioInitialise */

   version = gpioInitialise();

   if (version >= 0)
   {
      /* initialise pins, only gpio numbers are supported */

      gpioSetMode(SERVO1,        PI_OUTPUT);
      gpioSetMode(SERVO2,        PI_OUTPUT);

      gpioSetTimerFunc(0, 1000, servoTick);

      while (1)
      {
         sleep(1); /* do nothing */
      }
   }   
}

void servoTick(void)
{
   static int wid1=1500, wid2=1500;
   static int inc1=50,   inc2=75;

   gpioServo(SERVO1, wid1);
   gpioServo(SERVO2, wid2);

   wid1+=inc1; if ((wid1<1000) || (wid1>2000)) {inc1 = -inc1; wid1+=inc1;}
   wid2+=inc2; if ((wid2<1000) || (wid2>2000)) {inc2 = -inc2; wid2+=inc2;}
}
To build

cc -o servo servo.c -lpigpio -lrt -lpthread
Last edited by joan on Fri Dec 20, 2013 9:33 am, edited 1 time in total.

mikerr
Posts: 2686
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Servo HELP!

Sat Apr 13, 2013 5:44 pm

There's a step by step install for servoblaster in the thread here:

http://www.raspberrypi.org/phpBB3/viewt ... 00#p164500

admittedly it not the first post in the thread.

I've just posted some python code at the end of that thread too:

http://www.raspberrypi.org/phpBB3/viewt ... 54#p330654

Move 2 servos side to side in python

Code: Select all

from subprocess import call
from time import sleep

while True:
        call (["echo 1=1 > /dev/servoblaster"], shell=True)
        call (["echo 2=1 > /dev/servoblaster"], shell=True)
        sleep (2)
        call (["echo 1=200 > /dev/servoblaster"], shell=True)
        call (["echo 2=200 > /dev/servoblaster"], shell=True)
        sleep (2)
Last edited by mikerr on Sun Apr 14, 2013 8:39 pm, edited 2 times in total.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 6:12 pm

mikerr wrote:There's a step by step install for servoblaster in the thread here:

http://www.raspberrypi.org/phpBB3/viewt ... 00#p164500

admittedly it not the first post in the thread.

I've just posted some python code at the end of that thread too:

http://www.raspberrypi.org/phpBB3/viewt ... 54#p330654

Move 2 servos side to side in python

Code: Select all

from subprocess import call
from time import sleep

while True:
        call (["echo 1=0 > /dev/servoblaster"], shell=True)
        call (["echo 2=0 > /dev/servoblaster"], shell=True)
        sleep (2)
        call (["echo 1=200 > /dev/servoblaster"], shell=True)
        call (["echo 2=200 > /dev/servoblaster"], shell=True)
Thanks for the help! The code you have above... What Pins do I need to plug into my Pi?

Thanks

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

mikerr
Posts: 2686
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Servo HELP!

Sat Apr 13, 2013 6:43 pm

Basically looking at this diagram, the dark green pins:

Image
http://elinux.org/Rpi_Low-level_peripherals


Servo 0 = GPIO 04
1= GPIO 17
2= GPIO 18
3= GPIO 21
4= GPIO 22
5= GPIO 23
6= GPIO 24
7= GPIO 25

I.e. I connected the white [middle] servo leads to GPIO 17 and GPIO 18 (1 & 2)
Last edited by mikerr on Sat Apr 13, 2013 9:05 pm, edited 1 time in total.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 8:06 pm

Thanks for your help!

Basically I have installed Servoblaster as per that guys instructions, nothing wrong here as far as I can tell... I then copied your program into a Python 2.7.3 window, and wired up my 2 servos. I wired one to pin 17 and the other to 18... I wired the middle of each of the 3 pins to the 17, 18 pins... You know what I mean? I then connected the 2 furtherest servo pins to 6V and Gnd (On my battery pack)...

I click run....... Nothing happens! What am I doing wrong here?

Thanks

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...


User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 8:21 pm

Sorry, don't quite understand why you posted that Joan?

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

mikerr
Posts: 2686
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Servo HELP!

Sat Apr 13, 2013 8:23 pm

You also need to have a common ground between the pi and the battery pack - a wire from GND on the battery pack to GND on the Pi.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo HELP!

Sat Apr 13, 2013 8:28 pm

You need to connect the control wire of the servo to the gpio.

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 8:46 pm

mikerr wrote:You also need to have a common ground between the pi and the battery pack - a wire from GND on the battery pack to GND on the Pi.
Ah... OK, I will do that!
joan wrote:You need to connect the control wire of the servo to the gpio.
Already did that!

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo HELP!

Sat Apr 13, 2013 8:51 pm

Bigcat123 wrote:...
joan wrote:You need to connect the control wire of the servo to the gpio.
Already did that!

Bigcat123
Are you sure? You said "...I wired the middle of each of the 3 pins to the 17, 18 pins... You know what I mean? I then connected the 2 furtherest servo pins to 6V and Gnd..."

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 8:56 pm

joan wrote:
Bigcat123 wrote:...
joan wrote:You need to connect the control wire of the servo to the gpio.
Already did that!

Bigcat123
Are you sure? You said "...I wired the middle of each of the 3 pins to the 17, 18 pins... You know what I mean? I then connected the 2 furtherest servo pins to 6V and Gnd..."
Hmm... I am not sure which of the servo pins IS the control one (There is 3 so I am guessing the middle one!?)... I have unplugged everything and I am wiring it up again, carefully!

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo HELP!

Sat Apr 13, 2013 8:59 pm

The link you posted shows servos with brown, red, orange leads (to my eyes).

I'd connect brown to ground, red to +5V, orange to gpio.

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 9:03 pm

Wired everything up again... Exactly the same result! Not a twitch... Maybe it is a code problem? Or Servoblaster? When I run the program I don't get any errors... But... I don't know!

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sat Apr 13, 2013 9:26 pm

They are being run by one battery pack, is that a problem? One servo is hot, the other cold...
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sun Apr 14, 2013 7:05 am

Well, I tried what you said... The servo is definitely wired up correctly! When I connected to power there was a twitch...

However... Running the programming nothing happens, not a thing!

So I wired it up to my Arduino... It worked!

What's wrong with the program? I really need it fixed!

Thanks
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Servo HELP!

Sun Apr 14, 2013 7:59 am

Forget programs, do it from the command line.

Code: Select all

echo 1=100 > /dev/servoblaster
echo 1=150 > /dev/servoblaster
echo 1=200 > /dev/servoblaster
echo 1=250 > /dev/servoblaster
My servos are only sane with numbers between 150 and 250 IIRC. Yours may vary.

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sun Apr 14, 2013 8:34 am

Thanks for the help!

I tried what you said but it comes up with 'permission denied'... Do you know how to fix that?

Ideally I would like to be able to control the servos from Python...

Thanks again

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo HELP!

Sun Apr 14, 2013 8:48 am

Put sudo at the start of the command.

If it works from the command line it will also work from Python.

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sun Apr 14, 2013 8:59 am

Already tried that... Nothing happens the same 'permission denied' message... Any other suggestions?! What is going wrong?!

Thanks
Just a beginner sharing his experiences on his way to geek nirvana...

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Servo HELP!

Sun Apr 14, 2013 9:04 am

Is there a file called "servoblaster in the /dev directory? If not then you don't have servoblaster installed.

If the file is there, try "sudo -i" and then the echo commands without sudo.

(Edited after joan replied.)

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Servo HELP!

Sun Apr 14, 2013 9:05 am

Does /dev/servoblaster exist?

Try

ls -l /dev/servoblaster

mikerr
Posts: 2686
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Servo HELP!

Sun Apr 14, 2013 9:11 am

Try the user version:

Code: Select all

cd ~/PiBits/ServoBlaster/
make
sudo ./servod
then the same commands to run it:

Code: Select all

sudo echo 1=200 > /dev/servoblaster
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: Servo HELP!

Sun Apr 14, 2013 8:11 pm

Yes! Success! It works, turns out I hadn't installed ServoBlaster properly!

I can now make the servo move between 0 and 249 in the command line... However when I tried the original program that you mentioned Mikerr, weird things happened! The servo seemingly moved to random places and twitched and stopped...

Ideally I really need this to work in Python, and with Mikerr's program not working, I have no idea how to do so! Could someone help me? Show me how to do it?

Thanks for the help! You have been great!

Bigcat123
Just a beginner sharing his experiences on his way to geek nirvana...

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 6 guests