edgework88
Posts: 7
Joined: Fri Nov 27, 2015 10:35 pm

Simple servo question - please help

Fri Nov 27, 2015 10:54 pm

I thought I knew a little about the Raspberry Pi having done some projects - XBMC, file server, webcam - but the current one has me stumped and on the verge of binning the raspi.
I would like a fairly simple set of instructions to setup the pi to control two servos in a pan and tilt mechanism. I know how to do the hardware stuff - wiring, breadboard, varous pins on the Pi - but it is the software side that baffles me and getting the Pi to make the things move. I have couple of model 'A's. I have looked at loads of stuff on the net and to be honest it doesnt take long before rocket science kicks in especially some of the stuff on youtube which have a habit of taking the 'leap of faith moment' when they jump through a number of steps on the basis that 'you know that'. I know how to flash an SD card and run python but dont - yet - have any mediocre coding knowledge. I dont want to do too much complex with the servos - just perhaps insert some one line commands in a shell that makes one or both servos move in either direction for a set distance - no need for browser based stuff - too complicated.

So can anyone point me at such a set of instructions? Very grateful if poss before I lose my marbles trying to figure it out.

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Simple servo question - please help

Sat Nov 28, 2015 4:13 am

To control a servo you need to generate a PWM signal:
https://en.wikipedia.org/wiki/Pulse-width_modulation
Doing this in software can be problematic, you never know when the scheduler is going to have a senior moment and not schedule your task for a bit, resulting in the servo twitching or even hitting the stops.
Many microcontrollers have hardware support for PWM, not sure if the Pi does.

The easiest way to do this is to use a separate micro (like an arduino) to generate the PWM signals.
Plug it into a USB port and it appears as a serial port.
The IDE comes with examples of PWM and serial control, just stitch them together to get what you need.
Then all you have to do is send commands to it to set the state of the servos.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Simple servo question - please help

Sat Nov 28, 2015 5:01 am

sprinkmeier wrote:The easiest way to do this is to use a separate micro (like an arduino) to generate the PWM signals.
No, the easiest way to do it is with 'servoblaster' or 'pigpio'. No additional hardware necessary.

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

Re: Simple servo question - please help

Sat Nov 28, 2015 9:39 am

http://abyz.co.uk/videos/pan_tilt_2.mp4 is a poor short video of an old Pi driving a pan tilt head with a couple of servos.

As ame says either servoblaster or my pigpio will drive servos.

From the command line you could use

for ((pulse=1000;pulse<2000;pulse+=10)); do pigs servo 4 $pulse; sleep 0.1; done

to move a servo connected to GPIO 4.

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Simple servo question - please help

Sat Nov 28, 2015 10:46 am

ame wrote:
sprinkmeier wrote:The easiest way to do this is to use a separate micro (like an arduino) to generate the PWM signals.
No, the easiest way to do it is with 'servoblaster' or 'pigpio'. No additional hardware necessary.
The easiest way I knew of to do this ...
No I know a better way :-)

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

Re: Simple servo question - please help

Sat Nov 28, 2015 10:57 am

sprinkmeier wrote: ...
No I know a better way :-)
How have you compared the different methods?

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Simple servo question - please help

Sat Nov 28, 2015 11:04 am

joan wrote:
sprinkmeier wrote: ...
No I know a better way :-)
How have you compared the different methods?
My psychic powers tell me that sprikmeier meant to say "now I know a better way", admitting that pigpio or servoblaster is better.
Doug.
Building Management Systems Engineer.

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

Re: Simple servo question - please help

Sat Nov 28, 2015 11:11 am

BMS Doug wrote:
joan wrote:
sprinkmeier wrote: ...
No I know a better way :-)
How have you compared the different methods?
My psychic powers tell me that sprikmeier meant to say "now I know a better way", admitting that pigpio or servoblaster is better.
My fifth mistake of the day and conting,

edgework88
Posts: 7
Joined: Fri Nov 27, 2015 10:35 pm

Re: Simple servo question - please help

Sat Nov 28, 2015 12:27 pm

Joan - thanks for the pointer to http://abyz.co.uk/videos/pan_tilt_2.mp4. Unfortunately my PC couldn't play the file as it says it is corrupt. Thank you to all for their pointers. I had heard of servoblaster and I guess my original question was a little bit of a 'back to the basics' question of whether there is a step by step guide to setting up the code / configuring the pi to do it. As I said earlier I have cracked the SD card thing and getting the pi to do some simple stuff but can't seem to get it confgured to control a servo. Maybe installing and running stuff like servoblaster is beyond me..... :cry:

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Simple servo question - please help

Sat Nov 28, 2015 1:32 pm

joan wrote: My fifth mistake of the day and conting,
That's six, right?

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Simple servo question - please help

Sat Nov 28, 2015 1:38 pm

edgework88 wrote:Maybe installing and running stuff like servoblaster is beyond me..... :cry:
Nope. You can do it.

Once the Pi is booting and you can log in somehow you can start setting up the software.

Find a tutorial for servoblaster or pigpio and follow it. If you get stuck, report back here and ask for help.

In terms of hardware, if you just have a small RC model servo you can connect it to the Pi 5V and GND for power, then connect the signal line to a GPIO pin. That's it.

Configure the software to drive that GPIO pin and the servo will move.

After that, it's just a matter of software. :)

When you start putting a load on the servo you'll probably need a separate power supply for it. In this case, don't forget to connect servo ground to Pi ground.

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

Re: Simple servo question - please help

Sat Nov 28, 2015 1:40 pm

edgework88 wrote:Joan - thanks for the pointer to http://abyz.co.uk/videos/pan_tilt_2.mp4. Unfortunately my PC couldn't play the file as it says it is corrupt. Thank you to all for their pointers. I had heard of servoblaster and I guess my original question was a little bit of a 'back to the basics' question of whether there is a step by step guide to setting up the code / configuring the pi to do it. As I said earlier I have cracked the SD card thing and getting the pi to do some simple stuff but can't seem to get it confgured to control a servo. Maybe installing and running stuff like servoblaster is beyond me..... :cry:
I have problems playing that video, it doesn't work on Iceweasel, I have to use web or chromium. Don't bother trying to view.

Five minutes at worst to download and install. Similar for servoblaster.

http://abyz.co.uk/rpi/pigpio/download.html

edgework88
Posts: 7
Joined: Fri Nov 27, 2015 10:35 pm

Re: Simple servo question - please help

Sat Nov 28, 2015 7:21 pm

'Five minutes at worst to download and install. Similar for servoblaster.'. As I say that seems to be the bit that defeats me, what are the steps to install and run? I have tried it but no response....

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

Re: Simple servo question - please help

Sat Nov 28, 2015 7:56 pm

edgework88 wrote:'Five minutes at worst to download and install. Similar for servoblaster.'. As I say that seems to be the bit that defeats me, what are the steps to install and run? I have tried it but no response....
What did you try and what didn't give a response?

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Simple servo question - please help

Sat Nov 28, 2015 8:11 pm

edgework88 wrote:'Five minutes at worst to download and install. Similar for servoblaster.'. As I say that seems to be the bit that defeats me, what are the steps to install and run? I have tried it but no response....
Here is an install that I have just done on a Zero, took 5 mins. (note that I was running as root so didn't need to prefix any of the commands with sudo)

Code: Select all

pi@rpizero:~ $ sudo su -
root@rpizero:~# cd /tmp
root@rpizero:/tmp# wget abyz.co.uk/rpi/pigpio/pigpio.zip
--2015-11-28 19:59:47--  http://abyz.co.uk/rpi/pigpio/pigpio.zip
Resolving abyz.co.uk (abyz.co.uk)... 91.238.161.4
Connecting to abyz.co.uk (abyz.co.uk)|91.238.161.4|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 301857 (295K) [application/zip]
Saving to: ‘pigpio.zip’

pigpio.zip          100%[=====================>] 294.78K  1022KB/s   in 0.3s

2015-11-28 19:59:47 (1022 KB/s) - ‘pigpio.zip’ saved [301857/301857]

root@rpizero:/tmp# unzip pigpio.zip
Archive:  pigpio.zip
   creating: PIGPIO/
  inflating: PIGPIO/setup.py
  inflating: PIGPIO/command.h
  inflating: PIGPIO/pigpiod.c
  inflating: PIGPIO/pigpiod_if2.3
  inflating: PIGPIO/pigs.c
  inflating: PIGPIO/x_pigpiod_if.c
  inflating: PIGPIO/pigpiod_if2.c
  inflating: PIGPIO/pigpiod.1
  inflating: PIGPIO/README
  inflating: PIGPIO/command.c
  inflating: PIGPIO/pigpio.h
  inflating: PIGPIO/custom.cext
  inflating: PIGPIO/pigpiod_if.h
  inflating: PIGPIO/Makefile
  inflating: PIGPIO/pig2vcd.c
  inflating: PIGPIO/pigpio.py
  inflating: PIGPIO/pigs.1
  inflating: PIGPIO/pigpio.c
  inflating: PIGPIO/x_pipe
  inflating: PIGPIO/pigpiod_if.c
  inflating: PIGPIO/UNLICENCE
  inflating: PIGPIO/pigpio.3
  inflating: PIGPIO/pigpiod_if2.h
  inflating: PIGPIO/pigpiod_if.3
  inflating: PIGPIO/pig2vcd.1
  inflating: PIGPIO/x_pigpio.py
  inflating: PIGPIO/MakeRemote
  inflating: PIGPIO/x_pigpio.c
  inflating: PIGPIO/x_pigpiod_if2.c
  inflating: PIGPIO/x_pigs
root@rpizero:/tmp# cd PIGPIO
root@rpizero:/tmp/PIGPIO# make
gcc -O3 -Wall -pthread -fpic -c -o pigpio.o pigpio.c
gcc -O3 -Wall -pthread -fpic -c -o command.o command.c
gcc -shared -o libpigpio.so pigpio.o command.o
strip --strip-unneeded libpigpio.so
size     libpigpio.so
   text    data     bss     dec     hex filename
 214523    4772  673352  892647   d9ee7 libpigpio.so
gcc -O3 -Wall -pthread -fpic -c -o pigpiod_if.o pigpiod_if.c
gcc -shared -o libpigpiod_if.so pigpiod_if.o command.o
strip --strip-unneeded libpigpiod_if.so
size     libpigpiod_if.so
   text    data     bss     dec     hex filename
  55327    3864   49244  108435   1a793 libpigpiod_if.so
gcc -O3 -Wall -pthread -fpic -c -o pigpiod_if2.o pigpiod_if2.c
gcc -shared -o libpigpiod_if2.so pigpiod_if2.o command.o
strip --strip-unneeded libpigpiod_if2.so
size     libpigpiod_if2.so
   text    data     bss     dec     hex filename
  60328    3852    1844   66024   101e8 libpigpiod_if2.so
gcc -O3 -Wall -pthread   -c -o x_pigpio.o x_pigpio.c
gcc -o x_pigpio x_pigpio.o -L. -lpigpio -pthread -lrt
gcc -O3 -Wall -pthread   -c -o x_pigpiod_if.o x_pigpiod_if.c
gcc -o x_pigpiod_if x_pigpiod_if.o -L. -lpigpiod_if -pthread -lrt
gcc -O3 -Wall -pthread   -c -o x_pigpiod_if2.o x_pigpiod_if2.c
gcc -o x_pigpiod_if2 x_pigpiod_if2.o -L. -lpigpiod_if2 -pthread -lrt
gcc -O3 -Wall -pthread   -c -o pig2vcd.o pig2vcd.c
gcc -o pig2vcd pig2vcd.o
gcc -O3 -Wall -pthread   -c -o pigpiod.o pigpiod.c
gcc -o pigpiod pigpiod.o -L. -lpigpio -pthread -lrt
gcc -O3 -Wall -pthread   -c -o pigs.o pigs.c
gcc -o pigs pigs.o command.o
root@rpizero:/tmp/PIGPIO# make install
install -m 0755 -d                /opt/pigpio/cgi
install -m 0755 -d                /usr/local/include
install -m 0644 pigpio.h          /usr/local/include
install -m 0644 pigpiod_if.h      /usr/local/include
install -m 0644 pigpiod_if2.h     /usr/local/include
install -m 0755 -d                /usr/local/lib
install -m 0755 libpigpio.so      /usr/local/lib
install -m 0755 libpigpiod_if.so  /usr/local/lib
install -m 0755 libpigpiod_if2.so /usr/local/lib
install -m 0755 -d                /usr/local/bin
install -m 0755 -s pig2vcd        /usr/local/bin
install -m 0755 -s pigpiod        /usr/local/bin
install -m 0755 -s pigs           /usr/local/bin
if which python2; then python2 setup.py install; fi
/usr/bin/python2
running install
running build
running build_py
creating build
creating build/lib.linux-armv6l-2.7
copying pigpio.py -> build/lib.linux-armv6l-2.7
running install_lib
copying build/lib.linux-armv6l-2.7/pigpio.py -> /usr/local/lib/python2.7/dist-packages
byte-compiling /usr/local/lib/python2.7/dist-packages/pigpio.py to pigpio.pyc
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/pigpio-1.24.egg-info
if which python3; then python3 setup.py install; fi
install -m 0755 -d                /usr/local/man/man1
install -m 0644 *.1               /usr/local/man/man1
install -m 0755 -d                /usr/local/man/man3
install -m 0644 *.3               /usr/local/man/man3
ldconfig
root@rpizero:/tmp/PIGPIO# ./x_pigpio
Version.
pigpio version 41.
Hardware revision 146.
Mode/PUD/read/write tests.
TEST  1.1  PASS (set mode, get mode: 0)
TEST  1.2  PASS (set pull up down, read: 1)
TEST  1.3  PASS (set pull up down, read: 0)
TEST  1.4  PASS (write, get mode: 1)
TEST  1.5  PASS (read: 0)
TEST  1.6  PASS (write, read: 1)
PWM dutycycle/range/frequency tests.
TEST  2.1  PASS (set PWM range, set/get PWM frequency: 10)
TEST  2.2  PASS (get PWM dutycycle: 0)
TEST  2.3  PASS (set PWM dutycycle, callback: 0)
TEST  2.4  PASS (get PWM dutycycle: 128)
TEST  2.5  PASS (set PWM dutycycle, callback: 40)
TEST  2.6  PASS (set/get PWM frequency: 100)
TEST  2.7  PASS (callback: 400)
TEST  2.8  PASS (set/get PWM frequency: 1000)
TEST  2.9  PASS (callback: 4000)
TEST  2.10 PASS (get PWM range: 255)
TEST  2.11 PASS (get PWM real range: 200)
TEST  2.12 PASS (set/get PWM range: 2000)
TEST  2.13 PASS (get PWM real range: 200)
PWM/Servo pulse accuracy tests.
TEST  3.1  PASS (get servo pulsewidth: 500)
TEST  3.2  PASS (set servo pulsewidth: 40000)
TEST  3.3  PASS (get servo pulsewidth: 1500)
TEST  3.4  PASS (set servo pulsewidth: 13333)
TEST  3.5  PASS (get servo pulsewidth: 2500)
TEST  3.6  PASS (set servo pulsewidth: 8000)
TEST  3.7  PASS (set/get PWM frequency: 1000)
TEST  3.8  PASS (set PWM range: 200)
TEST  3.9  PASS (get PWM dutycycle: 20)
TEST  3.10 PASS (set PWM dutycycle: 200)
TEST  3.11 PASS (get PWM dutycycle: 40)
TEST  3.12 PASS (set PWM dutycycle: 400)
TEST  3.13 PASS (get PWM dutycycle: 60)
TEST  3.14 PASS (set PWM dutycycle: 600)
TEST  3.15 PASS (get PWM dutycycle: 80)
TEST  3.16 PASS (set PWM dutycycle: 800)
Pipe notification tests.
TEST  4.1  PASS (notify open/begin: 0)
TEST  4.2  PASS (notify pause: 0)
TEST  4.3  PASS (notify close: 0)
TEST  4.4  PASS (sequence numbers ok: 1)
TEST  4.5  PASS (gpio toggled ok: 1)
TEST  4.6  PASS (number of notifications: 80)
Waveforms & serial read/write tests.
TEST  5.1  PASS (callback, set mode, wave clear: 0)
TEST  5.2  PASS (pulse, wave add generic: 4)
TEST  5.3  PASS (wave tx repeat: 9)
TEST  5.4  PASS (callback: 50)
TEST  5.5  PASS (wave tx stop: 0)
TEST  5.6  PASS (serial read open: 0)
TEST  5.7  PASS (wave clear, wave add serial: 3405)
TEST  5.8  PASS (wave tx start: 6811)
TEST  5.9  PASS (NOT APPLICABLE: 0)
TEST  5.10 PASS (NOT APPLICABLE: 0)
TEST  5.11 PASS (wave tx busy, serial read: 0)
TEST  5.12 PASS (serial read close: 0)
TEST  5.13 PASS (wave get micros: 6158148)
TEST  5.14 PASS (wave get high micros: 6158148)
TEST  5.15 PASS (wave get max micros: 1800000000)
TEST  5.16 PASS (wave get pulses: 3405)
TEST  5.17 PASS (wave get high pulses: 3405)
TEST  5.18 PASS (wave get max pulses: 12000)
TEST  5.19 PASS (wave get cbs: 6810)
TEST  5.20 PASS (wave get high cbs: 6810)
TEST  5.21 PASS (wave get max cbs: 25016)
Trigger tests
TEST  6.1  PASS (gpio trigger count: 5)
TEST  6.2  PASS (gpio trigger pulse length: 150)
Watchdog tests.
TEST  7.1  PASS (set watchdog on count: 200)
TEST  7.2  PASS (set watchdog off count: 0)
Bank read/write tests.
TEST  8.1  PASS (read bank 1: 0)
TEST  8.2  PASS (read bank 1: 16)
TEST  8.3  PASS (clear bank 1: 0)
TEST  8.4  PASS (set bank 1: 1)
TEST  8.5  PASS (read bank 2: 60)
TEST  8.6  PASS (clear bank 2: 0)
TEST  8.7  PASS (NOT APPLICABLE: 0)
TEST  8.8  PASS (set bank 2: 0)
TEST  8.9  PASS (NOT APPLICABLE: 0)
Script store/run/status/stop/delete tests.
TEST  9.1  PASS (store/run script: 100)
TEST  9.2  PASS (run script/script status: 201)
TEST  9.3  PASS (run/stop script/script status: 110)
TEST  9.4  PASS (delete script: 0)
root@rpizero:/tmp/PIGPIO#

edgework88
Posts: 7
Joined: Fri Nov 27, 2015 10:35 pm

Re: Simple servo question - please help

Sun Nov 29, 2015 9:46 am

@ asandford: Thank you - wow that's a lot of typing! Do you have to enter all that at the command line and do you install it on a new rasp OS install? Once you've put in the last line what's next? Thanks.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Simple servo question - please help

Sun Nov 29, 2015 10:05 am

Hi there,

It's not a lot of typing. Most of the text you see is the result of typing the commands.

After running those commands, pigpio is available on your machine. You can now try any examples using pigpio.

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

Re: Simple servo question - please help

Sun Nov 29, 2015 10:29 am

edgework88 wrote:@ asandford: Thank you - wow that's a lot of typing! Do you have to enter all that at the command line and do you install it on a new rasp OS install? Once you've put in the last line what's next? Thanks.
All this assumes your Pi is connected to the www.

If you know how to cut&paste just cut&paste the following.

wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install

edgework88
Posts: 7
Joined: Fri Nov 27, 2015 10:35 pm

Re: Simple servo question - please help

Sun Nov 29, 2015 11:40 am

@ Joan: Ah I see so if I paste the following at the command line:
wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install

the rest is generated by the system?

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Simple servo question - please help

Sun Nov 29, 2015 12:20 pm

edgework88 wrote:@ Joan: Ah I see so if I paste the following at the command line:
wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install

the rest is generated by the system?
That's right, but one line at a time.

edgework88
Posts: 7
Joined: Fri Nov 27, 2015 10:35 pm

Re: Simple servo question - please help

Sun Nov 29, 2015 12:34 pm

Thank you. Then all I need to do is figure out how to use Pigio..

Rgds

E

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Simple servo question - please help

Sun Nov 29, 2015 9:58 pm

edgework88 wrote:@ asandford: Thank you - wow that's a lot of typing! Do you have to enter all that at the command line and do you install it on a new rasp OS install? Once you've put in the last line what's next? Thanks.
Just copied the text from puttty. I tried to highlight just the commands I typed in, but couldn'd get it to work.

Return to “Automation, sensing and robotics”